File tree Expand file tree Collapse file tree 4 files changed +71
-2
lines changed Expand file tree Collapse file tree 4 files changed +71
-2
lines changed Original file line number Diff line number Diff line change @@ -1676,7 +1676,7 @@ pub(crate) struct ExternItemCannotBeConst {
16761676 #[ primary_span]
16771677 pub ident_span : Span ,
16781678 #[ suggestion( code = "static " , applicability = "machine-applicable" ) ]
1679- pub const_span : Span ,
1679+ pub const_span : Option < Span > ,
16801680}
16811681
16821682#[ derive( Diagnostic ) ]
Original file line number Diff line number Diff line change @@ -1143,9 +1143,11 @@ impl<'a> Parser<'a> {
11431143 Ok ( kind) => kind,
11441144 Err ( kind) => match kind {
11451145 ItemKind :: Const ( box ConstItem { ty, expr, .. } ) => {
1146+ let const_span = Some ( span. with_hi ( ident. span . lo ( ) ) )
1147+ . filter ( |span| span. can_be_used_for_suggestions ( ) ) ;
11461148 self . sess . emit_err ( errors:: ExternItemCannotBeConst {
11471149 ident_span : ident. span ,
1148- const_span : span . with_hi ( ident . span . lo ( ) ) ,
1150+ const_span,
11491151 } ) ;
11501152 ForeignItemKind :: Static ( ty, Mutability :: Not , expr)
11511153 }
Original file line number Diff line number Diff line change 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+ macro_rules! hello {
10+ ( $name: ident) => {
11+ const $name: ( ) = ( ) ;
12+ } ;
13+ }
14+
15+ extern "C" {
16+ hello ! { yes }
17+ //~^ error: extern items cannot be `const`
18+ //~| error: incorrect `static` inside `extern` block
19+ }
20+
21+ fn main ( ) { }
Original file line number Diff line number Diff line change 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: extern items cannot be `const`
10+ --> $DIR/issue-116203.rs:16:14
11+ |
12+ LL | hello! { yes }
13+ | ^^^
14+ |
15+ = note: for more information, visit https://doc.rust-lang.org/std/keyword.extern.html
16+
17+ error: incorrect `static` inside `extern` block
18+ --> $DIR/issue-116203.rs:3:14
19+ |
20+ LL | extern "C" {
21+ | ---------- `extern` blocks define existing foreign statics and statics inside of them cannot have a body
22+ LL | / thread_local! {
23+ LL | | static FOO: u32 = 0;
24+ | | ^^^ cannot have a body
25+ LL | |
26+ LL | |
27+ LL | | }
28+ | |_____- the invalid body
29+ |
30+ = note: for more information, visit https://doc.rust-lang.org/std/keyword.extern.html
31+
32+ error: incorrect `static` inside `extern` block
33+ --> $DIR/issue-116203.rs:16:14
34+ |
35+ LL | const $name: () = ();
36+ | -- the invalid body
37+ ...
38+ LL | extern "C" {
39+ | ---------- `extern` blocks define existing foreign statics and statics inside of them cannot have a body
40+ LL | hello! { yes }
41+ | ^^^ cannot have a body
42+ |
43+ = note: for more information, visit https://doc.rust-lang.org/std/keyword.extern.html
44+
45+ error: aborting due to 4 previous errors
46+
You can’t perform that action at this time.
0 commit comments