From 4138702621f32528fee8bf55eb1b8c0b99ac369b Mon Sep 17 00:00:00 2001 From: bohan Date: Wed, 15 Nov 2023 15:28:38 +0800 Subject: [PATCH] discard invalid spans in external blocks --- compiler/rustc_parse/src/errors.rs | 2 +- compiler/rustc_parse/src/parser/item.rs | 4 ++- tests/ui/extern/issue-116203.rs | 21 +++++++++++ tests/ui/extern/issue-116203.stderr | 46 +++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 tests/ui/extern/issue-116203.rs create mode 100644 tests/ui/extern/issue-116203.stderr diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index 8ab1ec298a1c7..3270490d7572e 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -1676,7 +1676,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, } #[derive(Diagnostic)] diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 801860c212367..0931facca98b5 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -1143,9 +1143,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) } diff --git a/tests/ui/extern/issue-116203.rs b/tests/ui/extern/issue-116203.rs new file mode 100644 index 0000000000000..f821284164495 --- /dev/null +++ b/tests/ui/extern/issue-116203.rs @@ -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() {} diff --git a/tests/ui/extern/issue-116203.stderr b/tests/ui/extern/issue-116203.stderr new file mode 100644 index 0000000000000..86e4cc763bd6e --- /dev/null +++ b/tests/ui/extern/issue-116203.stderr @@ -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 +