Skip to content

Commit dfa67a6

Browse files
authored
Unrolled build for rust-lang#116420
Rollup merge of rust-lang#116420 - bvanjoi:fix-116203, r=Nilstrieb 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``
2 parents 503e129 + 4138702 commit dfa67a6

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
lines changed

compiler/rustc_parse/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1733,7 +1733,7 @@ pub(crate) struct ExternItemCannotBeConst {
17331733
#[primary_span]
17341734
pub ident_span: Span,
17351735
#[suggestion(code = "static ", applicability = "machine-applicable")]
1736-
pub const_span: Span,
1736+
pub const_span: Option<Span>,
17371737
}
17381738

17391739
#[derive(Diagnostic)]

compiler/rustc_parse/src/parser/item.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1139,9 +1139,11 @@ impl<'a> Parser<'a> {
11391139
Ok(kind) => kind,
11401140
Err(kind) => match kind {
11411141
ItemKind::Const(box ConstItem { ty, expr, .. }) => {
1142+
let const_span = Some(span.with_hi(ident.span.lo()))
1143+
.filter(|span| span.can_be_used_for_suggestions());
11421144
self.sess.emit_err(errors::ExternItemCannotBeConst {
11431145
ident_span: ident.span,
1144-
const_span: span.with_hi(ident.span.lo()),
1146+
const_span,
11451147
});
11461148
ForeignItemKind::Static(ty, Mutability::Not, expr)
11471149
}

tests/ui/extern/issue-116203.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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() {}

tests/ui/extern/issue-116203.stderr

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+

0 commit comments

Comments
 (0)