Skip to content

Commit d6238bd

Browse files
committed
reject assoc statics & extern consts during parsing
1 parent 5abedd8 commit d6238bd

11 files changed

+183
-95
lines changed

src/librustc_ast_passes/ast_validation.rs

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -533,20 +533,6 @@ impl<'a> AstValidator<'a> {
533533
}
534534
}
535535

536-
fn error_foreign_const(&self, ident: Ident, span: Span) {
537-
self.err_handler()
538-
.struct_span_err(ident.span, "extern items cannot be `const`")
539-
.span_suggestion(
540-
span.with_hi(ident.span.lo()),
541-
"try using a static value",
542-
"static ".to_string(),
543-
Applicability::MachineApplicable,
544-
)
545-
.span_label(self.current_extern_span(), "in this `extern` block")
546-
.note(MORE_EXTERN)
547-
.emit();
548-
}
549-
550536
/// Reject C-varadic type unless the function is foreign,
551537
/// or free and `unsafe extern "C"` semantically.
552538
fn check_c_varadic_type(&self, fk: FnKind<'a>) {
@@ -1003,10 +989,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
1003989
ForeignItemKind::Static(_, _, body) => {
1004990
self.check_foreign_kind_bodyless(fi.ident, "static", body.as_ref().map(|b| b.span));
1005991
}
1006-
ForeignItemKind::Const(..) => {
1007-
self.error_foreign_const(fi.ident, fi.span);
1008-
}
1009-
ForeignItemKind::Macro(..) => {}
992+
ForeignItemKind::Const(..) | ForeignItemKind::Macro(..) => {}
1010993
}
1011994

1012995
visit::walk_foreign_item(self, fi)
@@ -1267,13 +1250,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12671250
}
12681251
}
12691252

1270-
match item.kind {
1271-
AssocItemKind::Const(..) => self.check_item_named(item.ident, "const"),
1272-
AssocItemKind::Static(..) => self
1273-
.err_handler()
1274-
.struct_span_err(item.span, "associated `static` items are not allowed")
1275-
.emit(),
1276-
_ => {}
1253+
if let AssocItemKind::Const(..) = item.kind {
1254+
self.check_item_named(item.ident, "const");
12771255
}
12781256

12791257
self.with_in_trait_impl(false, |this| visit::walk_assoc_item(this, item, ctxt));

src/librustc_parse/parser/item.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,9 +643,16 @@ impl<'a> Parser<'a> {
643643
if !item.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) {
644644
item.tokens = Some(tokens);
645645
}
646+
self.error_on_assoc_static(&item);
646647
Ok(P(item))
647648
}
648649

650+
fn error_on_assoc_static(&self, item: &AssocItem) {
651+
if let AssocItemKind::Static(..) = item.kind {
652+
self.struct_span_err(item.span, "associated `static` items are not allowed").emit();
653+
}
654+
}
655+
649656
fn parse_assoc_item_(
650657
&mut self,
651658
at_end: &mut bool,
@@ -868,7 +875,25 @@ impl<'a> Parser<'a> {
868875
let lo = self.token.span;
869876
let vis = self.parse_visibility(FollowedByType::No)?;
870877
let (ident, kind) = self.parse_assoc_item_kind(at_end, &mut attrs, |_| true, &vis)?;
871-
Ok(P(self.mk_item(lo, ident, kind, vis, attrs)))
878+
let item = self.mk_item(lo, ident, kind, vis, attrs);
879+
self.error_on_foreign_const(&item);
880+
Ok(P(item))
881+
}
882+
883+
fn error_on_foreign_const(&self, item: &ForeignItem) {
884+
if let AssocItemKind::Const(..) = item.kind {
885+
self.struct_span_err(item.ident.span, "extern items cannot be `const`")
886+
.span_suggestion(
887+
item.span.with_hi(item.ident.span.lo()),
888+
"try using a static value",
889+
"static ".to_string(),
890+
Applicability::MachineApplicable,
891+
)
892+
.note(
893+
"for more information, visit https://doc.rust-lang.org/std/keyword.extern.html",
894+
)
895+
.emit();
896+
}
872897
}
873898

874899
fn is_static_global(&mut self) -> bool {

src/test/ui/extern/extern-const.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
error: extern items cannot be `const`
22
--> $DIR/extern-const.rs:16:11
33
|
4-
LL | extern "C" {
5-
| ---------- in this `extern` block
64
LL | const rust_dbg_static_mut: libc::c_int;
75
| ------^^^^^^^^^^^^^^^^^^^
86
| |

src/test/ui/parser/assoc-static-semantic-fail.stderr

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,12 @@ error: associated `static` items are not allowed
3434
LL | static TB: u8;
3535
| ^^^^^^^^^^^^^^
3636

37-
error: `default` is only allowed on items in `impl` definitions
38-
--> $DIR/assoc-static-semantic-fail.rs:24:5
39-
|
40-
LL | default static TC: u8 = 0;
41-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
42-
4337
error: associated `static` items are not allowed
4438
--> $DIR/assoc-static-semantic-fail.rs:24:5
4539
|
4640
LL | default static TC: u8 = 0;
4741
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
4842

49-
error: `default` is only allowed on items in `impl` definitions
50-
--> $DIR/assoc-static-semantic-fail.rs:27:5
51-
|
52-
LL | pub(crate) default static TD: u8;
53-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
54-
55-
error[E0449]: unnecessary visibility qualifier
56-
--> $DIR/assoc-static-semantic-fail.rs:27:5
57-
|
58-
LL | pub(crate) default static TD: u8;
59-
| ^^^^^^^^^^
60-
6143
error: associated `static` items are not allowed
6244
--> $DIR/assoc-static-semantic-fail.rs:27:5
6345
|
@@ -82,17 +64,35 @@ error: associated `static` items are not allowed
8264
LL | default static TC: u8 = 0;
8365
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
8466

85-
error[E0449]: unnecessary visibility qualifier
67+
error: associated `static` items are not allowed
8668
--> $DIR/assoc-static-semantic-fail.rs:40:5
8769
|
8870
LL | pub default static TD: u8;
89-
| ^^^ `pub` not permitted here because it's implied
71+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
9072

91-
error: associated `static` items are not allowed
73+
error: `default` is only allowed on items in `impl` definitions
74+
--> $DIR/assoc-static-semantic-fail.rs:24:5
75+
|
76+
LL | default static TC: u8 = 0;
77+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
78+
79+
error: `default` is only allowed on items in `impl` definitions
80+
--> $DIR/assoc-static-semantic-fail.rs:27:5
81+
|
82+
LL | pub(crate) default static TD: u8;
83+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
84+
85+
error[E0449]: unnecessary visibility qualifier
86+
--> $DIR/assoc-static-semantic-fail.rs:27:5
87+
|
88+
LL | pub(crate) default static TD: u8;
89+
| ^^^^^^^^^^
90+
91+
error[E0449]: unnecessary visibility qualifier
9292
--> $DIR/assoc-static-semantic-fail.rs:40:5
9393
|
9494
LL | pub default static TD: u8;
95-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
95+
| ^^^ `pub` not permitted here because it's implied
9696

9797
error: aborting due to 16 previous errors
9898

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Syntactically, we do allow e.g., `static X: u8 = 0;` as an associated item.
2+
3+
fn main() {}
4+
5+
#[cfg(FALSE)]
6+
impl S {
7+
static IA: u8 = 0; //~ ERROR associated `static` items are not allowed
8+
static IB: u8; //~ ERROR associated `static` items are not allowed
9+
default static IC: u8 = 0; //~ ERROR associated `static` items are not allowed
10+
pub(crate) default static ID: u8; //~ ERROR associated `static` items are not allowed
11+
}
12+
13+
#[cfg(FALSE)]
14+
trait T {
15+
static TA: u8 = 0; //~ ERROR associated `static` items are not allowed
16+
static TB: u8; //~ ERROR associated `static` items are not allowed
17+
default static TC: u8 = 0; //~ ERROR associated `static` items are not allowed
18+
pub(crate) default static TD: u8; //~ ERROR associated `static` items are not allowed
19+
}
20+
21+
#[cfg(FALSE)]
22+
impl T for S {
23+
static TA: u8 = 0; //~ ERROR associated `static` items are not allowed
24+
static TB: u8; //~ ERROR associated `static` items are not allowed
25+
default static TC: u8 = 0; //~ ERROR associated `static` items are not allowed
26+
pub default static TD: u8; //~ ERROR associated `static` items are not allowed
27+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
error: associated `static` items are not allowed
2+
--> $DIR/assoc-static-syntactic-fail.rs:7:5
3+
|
4+
LL | static IA: u8 = 0;
5+
| ^^^^^^^^^^^^^^^^^^
6+
7+
error: associated `static` items are not allowed
8+
--> $DIR/assoc-static-syntactic-fail.rs:8:5
9+
|
10+
LL | static IB: u8;
11+
| ^^^^^^^^^^^^^^
12+
13+
error: associated `static` items are not allowed
14+
--> $DIR/assoc-static-syntactic-fail.rs:9:5
15+
|
16+
LL | default static IC: u8 = 0;
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
19+
error: associated `static` items are not allowed
20+
--> $DIR/assoc-static-syntactic-fail.rs:10:5
21+
|
22+
LL | pub(crate) default static ID: u8;
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
25+
error: associated `static` items are not allowed
26+
--> $DIR/assoc-static-syntactic-fail.rs:15:5
27+
|
28+
LL | static TA: u8 = 0;
29+
| ^^^^^^^^^^^^^^^^^^
30+
31+
error: associated `static` items are not allowed
32+
--> $DIR/assoc-static-syntactic-fail.rs:16:5
33+
|
34+
LL | static TB: u8;
35+
| ^^^^^^^^^^^^^^
36+
37+
error: associated `static` items are not allowed
38+
--> $DIR/assoc-static-syntactic-fail.rs:17:5
39+
|
40+
LL | default static TC: u8 = 0;
41+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
42+
43+
error: associated `static` items are not allowed
44+
--> $DIR/assoc-static-syntactic-fail.rs:18:5
45+
|
46+
LL | pub(crate) default static TD: u8;
47+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
48+
49+
error: associated `static` items are not allowed
50+
--> $DIR/assoc-static-syntactic-fail.rs:23:5
51+
|
52+
LL | static TA: u8 = 0;
53+
| ^^^^^^^^^^^^^^^^^^
54+
55+
error: associated `static` items are not allowed
56+
--> $DIR/assoc-static-syntactic-fail.rs:24:5
57+
|
58+
LL | static TB: u8;
59+
| ^^^^^^^^^^^^^^
60+
61+
error: associated `static` items are not allowed
62+
--> $DIR/assoc-static-syntactic-fail.rs:25:5
63+
|
64+
LL | default static TC: u8 = 0;
65+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
66+
67+
error: associated `static` items are not allowed
68+
--> $DIR/assoc-static-syntactic-fail.rs:26:5
69+
|
70+
LL | pub default static TD: u8;
71+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
72+
73+
error: aborting due to 12 previous errors
74+

src/test/ui/parser/assoc-static-syntactic-pass.rs

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/test/ui/parser/foreign-const-semantic-fail.stderr

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
error: extern items cannot be `const`
22
--> $DIR/foreign-const-semantic-fail.rs:4:11
33
|
4-
LL | extern {
5-
| ------ in this `extern` block
64
LL | const A: isize;
75
| ------^
86
| |
@@ -13,9 +11,6 @@ LL | const A: isize;
1311
error: extern items cannot be `const`
1412
--> $DIR/foreign-const-semantic-fail.rs:6:11
1513
|
16-
LL | extern {
17-
| ------ in this `extern` block
18-
...
1914
LL | const B: isize = 42;
2015
| ------^
2116
| |
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Syntactically, a `const` item inside an `extern { ... }` block is not allowed.
2+
3+
fn main() {}
4+
5+
#[cfg(FALSE)]
6+
extern {
7+
const A: isize; //~ ERROR extern items cannot be `const`
8+
const B: isize = 42; //~ ERROR extern items cannot be `const`
9+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: extern items cannot be `const`
2+
--> $DIR/foreign-const-syntactic-fail.rs:7:11
3+
|
4+
LL | const A: isize;
5+
| ------^
6+
| |
7+
| help: try using a static value: `static`
8+
|
9+
= note: for more information, visit https://doc.rust-lang.org/std/keyword.extern.html
10+
11+
error: extern items cannot be `const`
12+
--> $DIR/foreign-const-syntactic-fail.rs:8:11
13+
|
14+
LL | const B: isize = 42;
15+
| ------^
16+
| |
17+
| help: try using a static value: `static`
18+
|
19+
= note: for more information, visit https://doc.rust-lang.org/std/keyword.extern.html
20+
21+
error: aborting due to 2 previous errors
22+

0 commit comments

Comments
 (0)