Skip to content

Commit 5f8c571

Browse files
committed
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.
1 parent e9f29a8 commit 5f8c571

8 files changed

+69
-68
lines changed

compiler/rustc_parse/src/validate_attr.rs

+60-59
Original file line numberDiff line numberDiff line change
@@ -91,73 +91,74 @@ pub fn check_builtin_attribute(
9191
// Some special attributes like `cfg` must be checked
9292
// before the generic check, so we skip them here.
9393
let should_skip = |name| name == sym::cfg;
94-
// Some of previously accepted forms were used in practice,
95-
// report them as warnings for now.
96-
let should_warn = |name| {
97-
name == sym::doc
98-
|| name == sym::ignore
99-
|| name == sym::inline
100-
|| name == sym::link
101-
|| name == sym::test
102-
|| name == sym::bench
103-
};
10494

10595
match parse_meta(sess, attr) {
10696
Ok(meta) => {
10797
if !should_skip(name) && !is_attr_template_compatible(&template, &meta.kind) {
108-
let error_msg = format!("malformed `{}` attribute input", name);
109-
let mut msg = "attribute must be of the form ".to_owned();
110-
let mut suggestions = vec![];
111-
let mut first = true;
112-
if template.word {
113-
first = false;
114-
let code = format!("#[{}]", name);
115-
msg.push_str(&format!("`{}`", &code));
116-
suggestions.push(code);
117-
}
118-
if let Some(descr) = template.list {
119-
if !first {
120-
msg.push_str(" or ");
121-
}
122-
first = false;
123-
let code = format!("#[{}({})]", name, descr);
124-
msg.push_str(&format!("`{}`", &code));
125-
suggestions.push(code);
126-
}
127-
if let Some(descr) = template.name_value_str {
128-
if !first {
129-
msg.push_str(" or ");
130-
}
131-
let code = format!("#[{} = \"{}\"]", name, descr);
132-
msg.push_str(&format!("`{}`", &code));
133-
suggestions.push(code);
134-
}
135-
if should_warn(name) {
136-
sess.buffer_lint(
137-
&ILL_FORMED_ATTRIBUTE_INPUT,
138-
meta.span,
139-
ast::CRATE_NODE_ID,
140-
&msg,
141-
);
142-
} else {
143-
sess.span_diagnostic
144-
.struct_span_err(meta.span, &error_msg)
145-
.span_suggestions(
146-
meta.span,
147-
if suggestions.len() == 1 {
148-
"must be of the form"
149-
} else {
150-
"the following are the possible correct uses"
151-
},
152-
suggestions.into_iter(),
153-
Applicability::HasPlaceholders,
154-
)
155-
.emit();
156-
}
98+
emit_malformed_attribute(sess, attr, name, template);
15799
}
158100
}
159101
Err(mut err) => {
160102
err.emit();
161103
}
162104
}
163105
}
106+
107+
fn emit_malformed_attribute(
108+
sess: &ParseSess,
109+
attr: &Attribute,
110+
name: Symbol,
111+
template: AttributeTemplate,
112+
) {
113+
// Some of previously accepted forms were used in practice,
114+
// report them as warnings for now.
115+
let should_warn = |name| {
116+
matches!(name, sym::doc | sym::ignore | sym::inline | sym::link | sym::test | sym::bench)
117+
};
118+
119+
let error_msg = format!("malformed `{}` attribute input", name);
120+
let mut msg = "attribute must be of the form ".to_owned();
121+
let mut suggestions = vec![];
122+
let mut first = true;
123+
let inner = if attr.style == ast::AttrStyle::Inner { "!" } else { "" };
124+
if template.word {
125+
first = false;
126+
let code = format!("#{}[{}]", inner, name);
127+
msg.push_str(&format!("`{}`", &code));
128+
suggestions.push(code);
129+
}
130+
if let Some(descr) = template.list {
131+
if !first {
132+
msg.push_str(" or ");
133+
}
134+
first = false;
135+
let code = format!("#{}[{}({})]", inner, name, descr);
136+
msg.push_str(&format!("`{}`", &code));
137+
suggestions.push(code);
138+
}
139+
if let Some(descr) = template.name_value_str {
140+
if !first {
141+
msg.push_str(" or ");
142+
}
143+
let code = format!("#{}[{} = \"{}\"]", inner, name, descr);
144+
msg.push_str(&format!("`{}`", &code));
145+
suggestions.push(code);
146+
}
147+
if should_warn(name) {
148+
sess.buffer_lint(&ILL_FORMED_ATTRIBUTE_INPUT, attr.span, ast::CRATE_NODE_ID, &msg);
149+
} else {
150+
sess.span_diagnostic
151+
.struct_span_err(attr.span, &error_msg)
152+
.span_suggestions(
153+
attr.span,
154+
if suggestions.len() == 1 {
155+
"must be of the form"
156+
} else {
157+
"the following are the possible correct uses"
158+
},
159+
suggestions.into_iter(),
160+
Applicability::HasPlaceholders,
161+
)
162+
.emit();
163+
}
164+
}

src/test/ui/attributes/register-attr-tool-fail.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ error: malformed `register_attr` attribute input
3030
--> $DIR/register-attr-tool-fail.rs:4:1
3131
|
3232
LL | #![register_attr]
33-
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#[register_attr(attr1, attr2, ...)]`
33+
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#![register_attr(attr1, attr2, ...)]`
3434

3535
error: malformed `register_tool` attribute input
3636
--> $DIR/register-attr-tool-fail.rs:5:1
3737
|
3838
LL | #![register_tool]
39-
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#[register_tool(tool1, tool2, ...)]`
39+
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#![register_tool(tool1, tool2, ...)]`
4040

4141
error: aborting due to 6 previous errors
4242

src/test/ui/gated-bad-feature.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ error: malformed `feature` attribute input
2020
--> $DIR/gated-bad-feature.rs:5:1
2121
|
2222
LL | #![feature]
23-
| ^^^^^^^^^^^ help: must be of the form: `#[feature(name1, name1, ...)]`
23+
| ^^^^^^^^^^^ help: must be of the form: `#![feature(name1, name1, ...)]`
2424

2525
error: malformed `feature` attribute input
2626
--> $DIR/gated-bad-feature.rs:6:1
2727
|
2828
LL | #![feature = "foo"]
29-
| ^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[feature(name1, name1, ...)]`
29+
| ^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#![feature(name1, name1, ...)]`
3030

3131
error: aborting due to 5 previous errors
3232

src/test/ui/invalid_crate_type_syntax.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: malformed `crate_type` attribute input
22
--> $DIR/invalid_crate_type_syntax.rs:2:1
33
|
44
LL | #![crate_type(lib)]
5-
| ^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[crate_type = "bin|lib|..."]`
5+
| ^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#![crate_type = "bin|lib|..."]`
66

77
error: aborting due to previous error
88

src/test/ui/lint/lint-malformed.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ error: malformed `deny` attribute input
1414
--> $DIR/lint-malformed.rs:1:1
1515
|
1616
LL | #![deny = "foo"]
17-
| ^^^^^^^^^^^^^^^^ help: must be of the form: `#[deny(lint1, lint2, ..., /*opt*/ reason = "...")]`
17+
| ^^^^^^^^^^^^^^^^ help: must be of the form: `#![deny(lint1, lint2, ..., /*opt*/ reason = "...")]`
1818

1919
error[E0452]: malformed lint attribute input
2020
--> $DIR/lint-malformed.rs:2:10

src/test/ui/malformed/malformed-plugin-1.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: malformed `plugin` attribute input
22
--> $DIR/malformed-plugin-1.rs:2:1
33
|
44
LL | #![plugin]
5-
| ^^^^^^^^^^ help: must be of the form: `#[plugin(name)]`
5+
| ^^^^^^^^^^ help: must be of the form: `#![plugin(name)]`
66

77
warning: use of deprecated attribute `plugin`: compiler plugins are deprecated. See https://github.com/rust-lang/rust/pull/64675
88
--> $DIR/malformed-plugin-1.rs:2:1

src/test/ui/malformed/malformed-plugin-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: malformed `plugin` attribute input
22
--> $DIR/malformed-plugin-2.rs:2:1
33
|
44
LL | #![plugin="bleh"]
5-
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#[plugin(name)]`
5+
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#![plugin(name)]`
66

77
warning: use of deprecated attribute `plugin`: compiler plugins are deprecated. See https://github.com/rust-lang/rust/pull/64675
88
--> $DIR/malformed-plugin-2.rs:2:1

src/test/ui/no_crate_type.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: malformed `crate_type` attribute input
22
--> $DIR/no_crate_type.rs:2:1
33
|
44
LL | #![crate_type]
5-
| ^^^^^^^^^^^^^^ help: must be of the form: `#[crate_type = "bin|lib|..."]`
5+
| ^^^^^^^^^^^^^^ help: must be of the form: `#![crate_type = "bin|lib|..."]`
66

77
error: aborting due to previous error
88

0 commit comments

Comments
 (0)