Auto merge of #88680 - ehuss:more-attr-validation, r=petrochenkov

Validate builtin attributes for macro args.

This adds some validation for `path`, `crate_type`, and `recursion_limit` attributes so that they will now return an error if you attempt to pass a macro into them (such as `#[path = foo!()]`). Previously, the attribute would be completely ignored. These attributes are special because their values need to be known before/during expansion.

cc #87681
This commit is contained in:
bors 2021-09-26 05:15:11 +00:00
commit f6e6ddc09d
21 changed files with 211 additions and 76 deletions

View file

@ -30,13 +30,13 @@ error: malformed `register_attr` attribute input
--> $DIR/register-attr-tool-fail.rs:4:1
|
LL | #![register_attr]
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#[register_attr(attr1, attr2, ...)]`
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#![register_attr(attr1, attr2, ...)]`
error: malformed `register_tool` attribute input
--> $DIR/register-attr-tool-fail.rs:5:1
|
LL | #![register_tool]
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#[register_tool(tool1, tool2, ...)]`
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#![register_tool(tool1, tool2, ...)]`
error: aborting due to 6 previous errors

View file

@ -20,13 +20,13 @@ error: malformed `feature` attribute input
--> $DIR/gated-bad-feature.rs:5:1
|
LL | #![feature]
| ^^^^^^^^^^^ help: must be of the form: `#[feature(name1, name1, ...)]`
| ^^^^^^^^^^^ help: must be of the form: `#![feature(name1, name1, ...)]`
error: malformed `feature` attribute input
--> $DIR/gated-bad-feature.rs:6:1
|
LL | #![feature = "foo"]
| ^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[feature(name1, name1, ...)]`
| ^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#![feature(name1, name1, ...)]`
error: aborting due to 5 previous errors

View file

@ -0,0 +1,7 @@
#![crate_type = foo!()] //~ ERROR malformed `crate_type` attribute
macro_rules! foo {
() => {"rlib"};
}
fn main() {}

View file

@ -0,0 +1,8 @@
error: malformed `crate_type` attribute input
--> $DIR/invalid-crate-type-macro.rs:1:1
|
LL | #![crate_type = foo!()]
| ^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#![crate_type = "bin|lib|..."]`
error: aborting due to previous error

View file

@ -2,7 +2,7 @@ error: malformed `crate_type` attribute input
--> $DIR/invalid_crate_type_syntax.rs:2:1
|
LL | #![crate_type(lib)]
| ^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[crate_type = "bin|lib|..."]`
| ^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#![crate_type = "bin|lib|..."]`
error: aborting due to previous error

View file

@ -14,7 +14,7 @@ error: malformed `deny` attribute input
--> $DIR/lint-malformed.rs:1:1
|
LL | #![deny = "foo"]
| ^^^^^^^^^^^^^^^^ help: must be of the form: `#[deny(lint1, lint2, ..., /*opt*/ reason = "...")]`
| ^^^^^^^^^^^^^^^^ help: must be of the form: `#![deny(lint1, lint2, ..., /*opt*/ reason = "...")]`
error[E0452]: malformed lint attribute input
--> $DIR/lint-malformed.rs:2:10

View file

@ -2,7 +2,7 @@ error: malformed `plugin` attribute input
--> $DIR/malformed-plugin-1.rs:2:1
|
LL | #![plugin]
| ^^^^^^^^^^ help: must be of the form: `#[plugin(name)]`
| ^^^^^^^^^^ help: must be of the form: `#![plugin(name)]`
warning: use of deprecated attribute `plugin`: compiler plugins are deprecated. See https://github.com/rust-lang/rust/pull/64675
--> $DIR/malformed-plugin-1.rs:2:1

View file

@ -2,7 +2,7 @@ error: malformed `plugin` attribute input
--> $DIR/malformed-plugin-2.rs:2:1
|
LL | #![plugin="bleh"]
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#[plugin(name)]`
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#![plugin(name)]`
warning: use of deprecated attribute `plugin`: compiler plugins are deprecated. See https://github.com/rust-lang/rust/pull/64675
--> $DIR/malformed-plugin-2.rs:2:1

View file

@ -0,0 +1,4 @@
#[path = 123] //~ ERROR malformed `path` attribute
mod foo;
fn main() {}

View file

@ -0,0 +1,8 @@
error: malformed `path` attribute input
--> $DIR/path-invalid-form.rs:1:1
|
LL | #[path = 123]
| ^^^^^^^^^^^^^ help: must be of the form: `#[path = "file"]`
error: aborting due to previous error

View file

@ -0,0 +1,8 @@
macro_rules! foo {
() => {"bar.rs"};
}
#[path = foo!()] //~ ERROR malformed `path` attribute
mod abc;
fn main() {}

View file

@ -0,0 +1,8 @@
error: malformed `path` attribute input
--> $DIR/path-macro.rs:5:1
|
LL | #[path = foo!()]
| ^^^^^^^^^^^^^^^^ help: must be of the form: `#[path = "file"]`
error: aborting due to previous error

View file

@ -2,7 +2,7 @@ error: malformed `crate_type` attribute input
--> $DIR/no_crate_type.rs:2:1
|
LL | #![crate_type]
| ^^^^^^^^^^^^^^ help: must be of the form: `#[crate_type = "bin|lib|..."]`
| ^^^^^^^^^^^^^^ help: must be of the form: `#![crate_type = "bin|lib|..."]`
error: aborting due to previous error

View file

@ -0,0 +1,3 @@
#![recursion_limit = 123] //~ ERROR malformed `recursion_limit` attribute
fn main() {}

View file

@ -0,0 +1,8 @@
error: malformed `recursion_limit` attribute input
--> $DIR/invalid_digit_type.rs:1:1
|
LL | #![recursion_limit = 123]
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#![recursion_limit = "N"]`
error: aborting due to previous error

View file

@ -0,0 +1,7 @@
#![recursion_limit = foo!()] //~ ERROR malformed `recursion_limit` attribute
macro_rules! foo {
() => {"128"};
}
fn main() {}

View file

@ -0,0 +1,8 @@
error: malformed `recursion_limit` attribute input
--> $DIR/invalid_macro.rs:1:1
|
LL | #![recursion_limit = foo!()]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#![recursion_limit = "N"]`
error: aborting due to previous error