reject assoc statics & extern consts during parsing
This commit is contained in:
parent
5abedd81e0
commit
d6238bd8d4
11 changed files with 185 additions and 97 deletions
|
|
@ -533,20 +533,6 @@ impl<'a> AstValidator<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn error_foreign_const(&self, ident: Ident, span: Span) {
|
||||
self.err_handler()
|
||||
.struct_span_err(ident.span, "extern items cannot be `const`")
|
||||
.span_suggestion(
|
||||
span.with_hi(ident.span.lo()),
|
||||
"try using a static value",
|
||||
"static ".to_string(),
|
||||
Applicability::MachineApplicable,
|
||||
)
|
||||
.span_label(self.current_extern_span(), "in this `extern` block")
|
||||
.note(MORE_EXTERN)
|
||||
.emit();
|
||||
}
|
||||
|
||||
/// Reject C-varadic type unless the function is foreign,
|
||||
/// or free and `unsafe extern "C"` semantically.
|
||||
fn check_c_varadic_type(&self, fk: FnKind<'a>) {
|
||||
|
|
@ -1003,10 +989,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
|
|||
ForeignItemKind::Static(_, _, body) => {
|
||||
self.check_foreign_kind_bodyless(fi.ident, "static", body.as_ref().map(|b| b.span));
|
||||
}
|
||||
ForeignItemKind::Const(..) => {
|
||||
self.error_foreign_const(fi.ident, fi.span);
|
||||
}
|
||||
ForeignItemKind::Macro(..) => {}
|
||||
ForeignItemKind::Const(..) | ForeignItemKind::Macro(..) => {}
|
||||
}
|
||||
|
||||
visit::walk_foreign_item(self, fi)
|
||||
|
|
@ -1267,13 +1250,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
match item.kind {
|
||||
AssocItemKind::Const(..) => self.check_item_named(item.ident, "const"),
|
||||
AssocItemKind::Static(..) => self
|
||||
.err_handler()
|
||||
.struct_span_err(item.span, "associated `static` items are not allowed")
|
||||
.emit(),
|
||||
_ => {}
|
||||
if let AssocItemKind::Const(..) = item.kind {
|
||||
self.check_item_named(item.ident, "const");
|
||||
}
|
||||
|
||||
self.with_in_trait_impl(false, |this| visit::walk_assoc_item(this, item, ctxt));
|
||||
|
|
|
|||
|
|
@ -643,9 +643,16 @@ impl<'a> Parser<'a> {
|
|||
if !item.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) {
|
||||
item.tokens = Some(tokens);
|
||||
}
|
||||
self.error_on_assoc_static(&item);
|
||||
Ok(P(item))
|
||||
}
|
||||
|
||||
fn error_on_assoc_static(&self, item: &AssocItem) {
|
||||
if let AssocItemKind::Static(..) = item.kind {
|
||||
self.struct_span_err(item.span, "associated `static` items are not allowed").emit();
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_assoc_item_(
|
||||
&mut self,
|
||||
at_end: &mut bool,
|
||||
|
|
@ -868,7 +875,25 @@ impl<'a> Parser<'a> {
|
|||
let lo = self.token.span;
|
||||
let vis = self.parse_visibility(FollowedByType::No)?;
|
||||
let (ident, kind) = self.parse_assoc_item_kind(at_end, &mut attrs, |_| true, &vis)?;
|
||||
Ok(P(self.mk_item(lo, ident, kind, vis, attrs)))
|
||||
let item = self.mk_item(lo, ident, kind, vis, attrs);
|
||||
self.error_on_foreign_const(&item);
|
||||
Ok(P(item))
|
||||
}
|
||||
|
||||
fn error_on_foreign_const(&self, item: &ForeignItem) {
|
||||
if let AssocItemKind::Const(..) = item.kind {
|
||||
self.struct_span_err(item.ident.span, "extern items cannot be `const`")
|
||||
.span_suggestion(
|
||||
item.span.with_hi(item.ident.span.lo()),
|
||||
"try using a static value",
|
||||
"static ".to_string(),
|
||||
Applicability::MachineApplicable,
|
||||
)
|
||||
.note(
|
||||
"for more information, visit https://doc.rust-lang.org/std/keyword.extern.html",
|
||||
)
|
||||
.emit();
|
||||
}
|
||||
}
|
||||
|
||||
fn is_static_global(&mut self) -> bool {
|
||||
|
|
|
|||
2
src/test/ui/extern/extern-const.stderr
vendored
2
src/test/ui/extern/extern-const.stderr
vendored
|
|
@ -1,8 +1,6 @@
|
|||
error: extern items cannot be `const`
|
||||
--> $DIR/extern-const.rs:16:11
|
||||
|
|
||||
LL | extern "C" {
|
||||
| ---------- in this `extern` block
|
||||
LL | const rust_dbg_static_mut: libc::c_int;
|
||||
| ------^^^^^^^^^^^^^^^^^^^
|
||||
| |
|
||||
|
|
|
|||
|
|
@ -34,30 +34,12 @@ error: associated `static` items are not allowed
|
|||
LL | static TB: u8;
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
||||
error: `default` is only allowed on items in `impl` definitions
|
||||
--> $DIR/assoc-static-semantic-fail.rs:24:5
|
||||
|
|
||||
LL | default static TC: u8 = 0;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: associated `static` items are not allowed
|
||||
--> $DIR/assoc-static-semantic-fail.rs:24:5
|
||||
|
|
||||
LL | default static TC: u8 = 0;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: `default` is only allowed on items in `impl` definitions
|
||||
--> $DIR/assoc-static-semantic-fail.rs:27:5
|
||||
|
|
||||
LL | pub(crate) default static TD: u8;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0449]: unnecessary visibility qualifier
|
||||
--> $DIR/assoc-static-semantic-fail.rs:27:5
|
||||
|
|
||||
LL | pub(crate) default static TD: u8;
|
||||
| ^^^^^^^^^^
|
||||
|
||||
error: associated `static` items are not allowed
|
||||
--> $DIR/assoc-static-semantic-fail.rs:27:5
|
||||
|
|
||||
|
|
@ -82,18 +64,36 @@ error: associated `static` items are not allowed
|
|||
LL | default static TC: u8 = 0;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0449]: unnecessary visibility qualifier
|
||||
--> $DIR/assoc-static-semantic-fail.rs:40:5
|
||||
|
|
||||
LL | pub default static TD: u8;
|
||||
| ^^^ `pub` not permitted here because it's implied
|
||||
|
||||
error: associated `static` items are not allowed
|
||||
--> $DIR/assoc-static-semantic-fail.rs:40:5
|
||||
|
|
||||
LL | pub default static TD: u8;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: `default` is only allowed on items in `impl` definitions
|
||||
--> $DIR/assoc-static-semantic-fail.rs:24:5
|
||||
|
|
||||
LL | default static TC: u8 = 0;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: `default` is only allowed on items in `impl` definitions
|
||||
--> $DIR/assoc-static-semantic-fail.rs:27:5
|
||||
|
|
||||
LL | pub(crate) default static TD: u8;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0449]: unnecessary visibility qualifier
|
||||
--> $DIR/assoc-static-semantic-fail.rs:27:5
|
||||
|
|
||||
LL | pub(crate) default static TD: u8;
|
||||
| ^^^^^^^^^^
|
||||
|
||||
error[E0449]: unnecessary visibility qualifier
|
||||
--> $DIR/assoc-static-semantic-fail.rs:40:5
|
||||
|
|
||||
LL | pub default static TD: u8;
|
||||
| ^^^ `pub` not permitted here because it's implied
|
||||
|
||||
error: aborting due to 16 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0449`.
|
||||
|
|
|
|||
27
src/test/ui/parser/assoc-static-syntactic-fail.rs
Normal file
27
src/test/ui/parser/assoc-static-syntactic-fail.rs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
// Syntactically, we do allow e.g., `static X: u8 = 0;` as an associated item.
|
||||
|
||||
fn main() {}
|
||||
|
||||
#[cfg(FALSE)]
|
||||
impl S {
|
||||
static IA: u8 = 0; //~ ERROR associated `static` items are not allowed
|
||||
static IB: u8; //~ ERROR associated `static` items are not allowed
|
||||
default static IC: u8 = 0; //~ ERROR associated `static` items are not allowed
|
||||
pub(crate) default static ID: u8; //~ ERROR associated `static` items are not allowed
|
||||
}
|
||||
|
||||
#[cfg(FALSE)]
|
||||
trait T {
|
||||
static TA: u8 = 0; //~ ERROR associated `static` items are not allowed
|
||||
static TB: u8; //~ ERROR associated `static` items are not allowed
|
||||
default static TC: u8 = 0; //~ ERROR associated `static` items are not allowed
|
||||
pub(crate) default static TD: u8; //~ ERROR associated `static` items are not allowed
|
||||
}
|
||||
|
||||
#[cfg(FALSE)]
|
||||
impl T for S {
|
||||
static TA: u8 = 0; //~ ERROR associated `static` items are not allowed
|
||||
static TB: u8; //~ ERROR associated `static` items are not allowed
|
||||
default static TC: u8 = 0; //~ ERROR associated `static` items are not allowed
|
||||
pub default static TD: u8; //~ ERROR associated `static` items are not allowed
|
||||
}
|
||||
74
src/test/ui/parser/assoc-static-syntactic-fail.stderr
Normal file
74
src/test/ui/parser/assoc-static-syntactic-fail.stderr
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
error: associated `static` items are not allowed
|
||||
--> $DIR/assoc-static-syntactic-fail.rs:7:5
|
||||
|
|
||||
LL | static IA: u8 = 0;
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: associated `static` items are not allowed
|
||||
--> $DIR/assoc-static-syntactic-fail.rs:8:5
|
||||
|
|
||||
LL | static IB: u8;
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
||||
error: associated `static` items are not allowed
|
||||
--> $DIR/assoc-static-syntactic-fail.rs:9:5
|
||||
|
|
||||
LL | default static IC: u8 = 0;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: associated `static` items are not allowed
|
||||
--> $DIR/assoc-static-syntactic-fail.rs:10:5
|
||||
|
|
||||
LL | pub(crate) default static ID: u8;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: associated `static` items are not allowed
|
||||
--> $DIR/assoc-static-syntactic-fail.rs:15:5
|
||||
|
|
||||
LL | static TA: u8 = 0;
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: associated `static` items are not allowed
|
||||
--> $DIR/assoc-static-syntactic-fail.rs:16:5
|
||||
|
|
||||
LL | static TB: u8;
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
||||
error: associated `static` items are not allowed
|
||||
--> $DIR/assoc-static-syntactic-fail.rs:17:5
|
||||
|
|
||||
LL | default static TC: u8 = 0;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: associated `static` items are not allowed
|
||||
--> $DIR/assoc-static-syntactic-fail.rs:18:5
|
||||
|
|
||||
LL | pub(crate) default static TD: u8;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: associated `static` items are not allowed
|
||||
--> $DIR/assoc-static-syntactic-fail.rs:23:5
|
||||
|
|
||||
LL | static TA: u8 = 0;
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: associated `static` items are not allowed
|
||||
--> $DIR/assoc-static-syntactic-fail.rs:24:5
|
||||
|
|
||||
LL | static TB: u8;
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
||||
error: associated `static` items are not allowed
|
||||
--> $DIR/assoc-static-syntactic-fail.rs:25:5
|
||||
|
|
||||
LL | default static TC: u8 = 0;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: associated `static` items are not allowed
|
||||
--> $DIR/assoc-static-syntactic-fail.rs:26:5
|
||||
|
|
||||
LL | pub default static TD: u8;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 12 previous errors
|
||||
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
// Syntactically, we do allow e.g., `static X: u8 = 0;` as an associated item.
|
||||
|
||||
// check-pass
|
||||
|
||||
fn main() {}
|
||||
|
||||
#[cfg(FALSE)]
|
||||
impl S {
|
||||
static IA: u8 = 0;
|
||||
static IB: u8;
|
||||
default static IC: u8 = 0;
|
||||
pub(crate) default static ID: u8;
|
||||
}
|
||||
|
||||
#[cfg(FALSE)]
|
||||
trait T {
|
||||
static TA: u8 = 0;
|
||||
static TB: u8;
|
||||
default static TC: u8 = 0;
|
||||
pub(crate) default static TD: u8;
|
||||
}
|
||||
|
||||
#[cfg(FALSE)]
|
||||
impl T for S {
|
||||
static TA: u8 = 0;
|
||||
static TB: u8;
|
||||
default static TC: u8 = 0;
|
||||
pub default static TD: u8;
|
||||
}
|
||||
|
|
@ -1,8 +1,6 @@
|
|||
error: extern items cannot be `const`
|
||||
--> $DIR/foreign-const-semantic-fail.rs:4:11
|
||||
|
|
||||
LL | extern {
|
||||
| ------ in this `extern` block
|
||||
LL | const A: isize;
|
||||
| ------^
|
||||
| |
|
||||
|
|
@ -13,9 +11,6 @@ LL | const A: isize;
|
|||
error: extern items cannot be `const`
|
||||
--> $DIR/foreign-const-semantic-fail.rs:6:11
|
||||
|
|
||||
LL | extern {
|
||||
| ------ in this `extern` block
|
||||
...
|
||||
LL | const B: isize = 42;
|
||||
| ------^
|
||||
| |
|
||||
|
|
|
|||
9
src/test/ui/parser/foreign-const-syntactic-fail.rs
Normal file
9
src/test/ui/parser/foreign-const-syntactic-fail.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
// Syntactically, a `const` item inside an `extern { ... }` block is not allowed.
|
||||
|
||||
fn main() {}
|
||||
|
||||
#[cfg(FALSE)]
|
||||
extern {
|
||||
const A: isize; //~ ERROR extern items cannot be `const`
|
||||
const B: isize = 42; //~ ERROR extern items cannot be `const`
|
||||
}
|
||||
22
src/test/ui/parser/foreign-const-syntactic-fail.stderr
Normal file
22
src/test/ui/parser/foreign-const-syntactic-fail.stderr
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
error: extern items cannot be `const`
|
||||
--> $DIR/foreign-const-syntactic-fail.rs:7:11
|
||||
|
|
||||
LL | const A: isize;
|
||||
| ------^
|
||||
| |
|
||||
| help: try using a static value: `static`
|
||||
|
|
||||
= note: for more information, visit https://doc.rust-lang.org/std/keyword.extern.html
|
||||
|
||||
error: extern items cannot be `const`
|
||||
--> $DIR/foreign-const-syntactic-fail.rs:8:11
|
||||
|
|
||||
LL | const B: isize = 42;
|
||||
| ------^
|
||||
| |
|
||||
| help: try using a static value: `static`
|
||||
|
|
||||
= note: for more information, visit https://doc.rust-lang.org/std/keyword.extern.html
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
// Syntactically, a `const` item inside an `extern { ... }` block is allowed.
|
||||
|
||||
// check-pass
|
||||
|
||||
fn main() {}
|
||||
|
||||
#[cfg(FALSE)]
|
||||
extern {
|
||||
const A: isize;
|
||||
const B: isize = 42;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue