Move malformed attribute code to a function and fix inner attribute suggestion.

Moving to a dedicated function in preparation for other validation.

The suggestion given didn't consider if it was an inner attribute.
This commit is contained in:
Eric Huss 2021-09-16 17:48:06 -07:00
parent e9f29a8519
commit 5f8c571e50
8 changed files with 69 additions and 68 deletions

View File

@ -91,27 +91,39 @@ pub fn check_builtin_attribute(
// Some special attributes like `cfg` must be checked
// before the generic check, so we skip them here.
let should_skip = |name| name == sym::cfg;
// Some of previously accepted forms were used in practice,
// report them as warnings for now.
let should_warn = |name| {
name == sym::doc
|| name == sym::ignore
|| name == sym::inline
|| name == sym::link
|| name == sym::test
|| name == sym::bench
};
match parse_meta(sess, attr) {
Ok(meta) => {
if !should_skip(name) && !is_attr_template_compatible(&template, &meta.kind) {
emit_malformed_attribute(sess, attr, name, template);
}
}
Err(mut err) => {
err.emit();
}
}
}
fn emit_malformed_attribute(
sess: &ParseSess,
attr: &Attribute,
name: Symbol,
template: AttributeTemplate,
) {
// Some of previously accepted forms were used in practice,
// report them as warnings for now.
let should_warn = |name| {
matches!(name, sym::doc | sym::ignore | sym::inline | sym::link | sym::test | sym::bench)
};
let error_msg = format!("malformed `{}` attribute input", name);
let mut msg = "attribute must be of the form ".to_owned();
let mut suggestions = vec![];
let mut first = true;
let inner = if attr.style == ast::AttrStyle::Inner { "!" } else { "" };
if template.word {
first = false;
let code = format!("#[{}]", name);
let code = format!("#{}[{}]", inner, name);
msg.push_str(&format!("`{}`", &code));
suggestions.push(code);
}
@ -120,7 +132,7 @@ pub fn check_builtin_attribute(
msg.push_str(" or ");
}
first = false;
let code = format!("#[{}({})]", name, descr);
let code = format!("#{}[{}({})]", inner, name, descr);
msg.push_str(&format!("`{}`", &code));
suggestions.push(code);
}
@ -128,22 +140,17 @@ pub fn check_builtin_attribute(
if !first {
msg.push_str(" or ");
}
let code = format!("#[{} = \"{}\"]", name, descr);
let code = format!("#{}[{} = \"{}\"]", inner, name, descr);
msg.push_str(&format!("`{}`", &code));
suggestions.push(code);
}
if should_warn(name) {
sess.buffer_lint(
&ILL_FORMED_ATTRIBUTE_INPUT,
meta.span,
ast::CRATE_NODE_ID,
&msg,
);
sess.buffer_lint(&ILL_FORMED_ATTRIBUTE_INPUT, attr.span, ast::CRATE_NODE_ID, &msg);
} else {
sess.span_diagnostic
.struct_span_err(meta.span, &error_msg)
.struct_span_err(attr.span, &error_msg)
.span_suggestions(
meta.span,
attr.span,
if suggestions.len() == 1 {
"must be of the form"
} else {
@ -154,10 +161,4 @@ pub fn check_builtin_attribute(
)
.emit();
}
}
}
Err(mut err) => {
err.emit();
}
}
}

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

@ -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

@ -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