Skip to content

Commit 583abc6

Browse files
committed
discard invalid spans in external blocks
1 parent 65519f5 commit 583abc6

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

compiler/rustc_parse/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1621,7 +1621,7 @@ pub(crate) struct ExternItemCannotBeConst {
16211621
#[primary_span]
16221622
pub ident_span: Span,
16231623
#[suggestion(code = "static ", applicability = "machine-applicable")]
1624-
pub const_span: Span,
1624+
pub const_span: Option<Span>,
16251625
}
16261626

16271627
#[derive(Diagnostic)]

compiler/rustc_parse/src/parser/item.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1142,9 +1142,14 @@ impl<'a> Parser<'a> {
11421142
Ok(kind) => kind,
11431143
Err(kind) => match kind {
11441144
ItemKind::Const(box ConstItem { ty, expr, .. }) => {
1145+
let const_span = self
1146+
.sess
1147+
.source_map()
1148+
.is_span_accessible(span)
1149+
.then_some(span.with_hi(ident.span.lo()));
11451150
self.sess.emit_err(errors::ExternItemCannotBeConst {
11461151
ident_span: ident.span,
1147-
const_span: span.with_hi(ident.span.lo()),
1152+
const_span,
11481153
});
11491154
ForeignItemKind::Static(ty, Mutability::Not, expr)
11501155
}

tests/ui/extern/issue-116203.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
extern "C" {
2+
thread_local! {
3+
static FOO: u32 = 0;
4+
//~^ error: extern items cannot be `const`
5+
//~| error: incorrect `static` inside `extern` block
6+
}
7+
}
8+
9+
fn main() {}

tests/ui/extern/issue-116203.stderr

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error: extern items cannot be `const`
2+
--> $DIR/issue-116203.rs:3:14
3+
|
4+
LL | static FOO: u32 = 0;
5+
| ^^^
6+
|
7+
= note: for more information, visit https://doc.rust-lang.org/std/keyword.extern.html
8+
9+
error: incorrect `static` inside `extern` block
10+
--> $DIR/issue-116203.rs:3:14
11+
|
12+
LL | extern "C" {
13+
| ---------- `extern` blocks define existing foreign statics and statics inside of them cannot have a body
14+
LL | / thread_local! {
15+
LL | | static FOO: u32 = 0;
16+
| | ^^^ cannot have a body
17+
LL | |
18+
LL | |
19+
LL | | }
20+
| |_____- the invalid body
21+
|
22+
= note: for more information, visit https://doc.rust-lang.org/std/keyword.extern.html
23+
24+
error: aborting due to 2 previous errors
25+

0 commit comments

Comments
 (0)