Skip to content

Commit

Permalink
Move malformed attribute code to a function and fix inner attribute s…
Browse files Browse the repository at this point in the history
…uggestion.

Moving to a dedicated function in preparation for other validation.

The suggestion given didn't consider if it was an inner attribute.
  • Loading branch information
ehuss committed Sep 25, 2021
1 parent e9f29a8 commit 5f8c571
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 68 deletions.
119 changes: 60 additions & 59 deletions compiler/rustc_parse/src/validate_attr.rs
Expand Up @@ -91,73 +91,74 @@ 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) {
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;
if template.word {
first = false;
let code = format!("#[{}]", name);
msg.push_str(&format!("`{}`", &code));
suggestions.push(code);
}
if let Some(descr) = template.list {
if !first {
msg.push_str(" or ");
}
first = false;
let code = format!("#[{}({})]", name, descr);
msg.push_str(&format!("`{}`", &code));
suggestions.push(code);
}
if let Some(descr) = template.name_value_str {
if !first {
msg.push_str(" or ");
}
let code = format!("#[{} = \"{}\"]", 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,
);
} else {
sess.span_diagnostic
.struct_span_err(meta.span, &error_msg)
.span_suggestions(
meta.span,
if suggestions.len() == 1 {
"must be of the form"
} else {
"the following are the possible correct uses"
},
suggestions.into_iter(),
Applicability::HasPlaceholders,
)
.emit();
}
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!("#{}[{}]", inner, name);
msg.push_str(&format!("`{}`", &code));
suggestions.push(code);
}
if let Some(descr) = template.list {
if !first {
msg.push_str(" or ");
}
first = false;
let code = format!("#{}[{}({})]", inner, name, descr);
msg.push_str(&format!("`{}`", &code));
suggestions.push(code);
}
if let Some(descr) = template.name_value_str {
if !first {
msg.push_str(" or ");
}
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, attr.span, ast::CRATE_NODE_ID, &msg);
} else {
sess.span_diagnostic
.struct_span_err(attr.span, &error_msg)
.span_suggestions(
attr.span,
if suggestions.len() == 1 {
"must be of the form"
} else {
"the following are the possible correct uses"
},
suggestions.into_iter(),
Applicability::HasPlaceholders,
)
.emit();
}
}
4 changes: 2 additions & 2 deletions src/test/ui/attributes/register-attr-tool-fail.stderr
Expand Up @@ -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

4 changes: 2 additions & 2 deletions src/test/ui/gated-bad-feature.stderr
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/invalid_crate_type_syntax.stderr
Expand Up @@ -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

2 changes: 1 addition & 1 deletion src/test/ui/lint/lint-malformed.stderr
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/malformed/malformed-plugin-1.stderr
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/malformed/malformed-plugin-2.stderr
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/no_crate_type.stderr
Expand Up @@ -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

0 comments on commit 5f8c571

Please sign in to comment.