Skip to content

Commit

Permalink
Rollup merge of rust-lang#116420 - bvanjoi:fix-116203, r=Nilstrieb
Browse files Browse the repository at this point in the history
discard invalid spans in external blocks

Fixes rust-lang#116203

This PR has discarded the invalid `const_span`, thereby making the format more neat.

r? `@Nilstrieb`
  • Loading branch information
compiler-errors authored Dec 7, 2023
2 parents f16c81f + 4138702 commit 95aa0b4
Show file tree
Hide file tree
Showing 4 changed files with 71 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 @@ -1733,7 +1733,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
4 changes: 3 additions & 1 deletion compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1139,9 +1139,11 @@ impl<'a> Parser<'a> {
Ok(kind) => kind,
Err(kind) => match kind {
ItemKind::Const(box ConstItem { ty, expr, .. }) => {
let const_span = Some(span.with_hi(ident.span.lo()))
.filter(|span| span.can_be_used_for_suggestions());
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
21 changes: 21 additions & 0 deletions tests/ui/extern/issue-116203.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
extern "C" {
thread_local! {
static FOO: u32 = 0;
//~^ error: extern items cannot be `const`
//~| error: incorrect `static` inside `extern` block
}
}

macro_rules! hello {
($name:ident) => {
const $name: () = ();
};
}

extern "C" {
hello! { yes }
//~^ error: extern items cannot be `const`
//~| error: incorrect `static` inside `extern` block
}

fn main() {}
46 changes: 46 additions & 0 deletions tests/ui/extern/issue-116203.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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: extern items cannot be `const`
--> $DIR/issue-116203.rs:16:14
|
LL | hello! { yes }
| ^^^
|
= 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: incorrect `static` inside `extern` block
--> $DIR/issue-116203.rs:16:14
|
LL | const $name: () = ();
| -- the invalid body
...
LL | extern "C" {
| ---------- `extern` blocks define existing foreign statics and statics inside of them cannot have a body
LL | hello! { yes }
| ^^^ cannot have a body
|
= note: for more information, visit https://doc.rust-lang.org/std/keyword.extern.html

error: aborting due to 4 previous errors

0 comments on commit 95aa0b4

Please sign in to comment.