Skip to content

Commit

Permalink
discard invalid spans in external blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
bvanjoi committed Oct 4, 2023
1 parent 65519f5 commit 583abc6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1621,7 +1621,7 @@ pub(crate) struct ExternItemCannotBeConst {
#[primary_span]
pub ident_span: Span,
#[suggestion(code = "static ", applicability = "machine-applicable")]
pub const_span: Span,
pub const_span: Option<Span>,
}

#[derive(Diagnostic)]
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1142,9 +1142,14 @@ impl<'a> Parser<'a> {
Ok(kind) => kind,
Err(kind) => match kind {
ItemKind::Const(box ConstItem { ty, expr, .. }) => {
let const_span = self
.sess
.source_map()
.is_span_accessible(span)
.then_some(span.with_hi(ident.span.lo()));
self.sess.emit_err(errors::ExternItemCannotBeConst {
ident_span: ident.span,
const_span: span.with_hi(ident.span.lo()),
const_span,
});
ForeignItemKind::Static(ty, Mutability::Not, expr)
}
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/extern/issue-116203.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
extern "C" {
thread_local! {
static FOO: u32 = 0;
//~^ error: extern items cannot be `const`
//~| error: incorrect `static` inside `extern` block
}
}

fn main() {}
25 changes: 25 additions & 0 deletions tests/ui/extern/issue-116203.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
error: extern items cannot be `const`
--> $DIR/issue-116203.rs:3:14
|
LL | static FOO: u32 = 0;
| ^^^
|
= note: for more information, visit https://doc.rust-lang.org/std/keyword.extern.html

error: incorrect `static` inside `extern` block
--> $DIR/issue-116203.rs:3:14
|
LL | extern "C" {
| ---------- `extern` blocks define existing foreign statics and statics inside of them cannot have a body
LL | / thread_local! {
LL | | static FOO: u32 = 0;
| | ^^^ cannot have a body
LL | |
LL | |
LL | | }
| |_____- the invalid body
|
= note: for more information, visit https://doc.rust-lang.org/std/keyword.extern.html

error: aborting due to 2 previous errors

0 comments on commit 583abc6

Please sign in to comment.