Skip to content

Commit b835228

Browse files
Stop using IllFormedAttributeInputLint for macro_use
1 parent 1da1a39 commit b835228

File tree

9 files changed

+61
-31
lines changed

9 files changed

+61
-31
lines changed

compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_hir::attrs::MacroUseArgs;
33
use rustc_session::lint::builtin::INVALID_MACRO_EXPORT_ARGUMENTS;
44

55
use super::prelude::*;
6-
use crate::session_diagnostics::IllFormedAttributeInputLint;
6+
use crate::session_diagnostics::{AttributeParseErrorReason, IllFormedAttributeInputLint};
77

88
pub(crate) struct MacroEscapeParser;
99
impl<S: Stage> NoArgsAttributeParser<S> for MacroEscapeParser {
@@ -102,14 +102,7 @@ impl<S: Stage> AttributeParser<S> for MacroUseParser {
102102
}
103103
}
104104
ArgParser::NameValue(_) => {
105-
let suggestions = cx.suggestions();
106-
cx.emit_err(IllFormedAttributeInputLint {
107-
num_suggestions: suggestions.len(),
108-
suggestions: DiagArgValue::StrListSepByAnd(
109-
suggestions.into_iter().map(|s| format!("`{s}`").into()).collect(),
110-
),
111-
span,
112-
});
105+
cx.emit_parse_error(span, AttributeParseErrorReason::ExpectedListOrNoArgs);
113106
}
114107
}
115108
},

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ impl<'f, 'sess: 'f, S: Stage> SharedContext<'f, 'sess, S> {
425425
}
426426

427427
impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
428-
fn emit_parse_error(
428+
pub(crate) fn emit_parse_error(
429429
&self,
430430
span: Span,
431431
reason: AttributeParseErrorReason<'_>,

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ pub(crate) enum AttributeParseErrorReason<'a> {
519519
ExpectedAtLeastOneArgument,
520520
ExpectedSingleArgument,
521521
ExpectedList,
522+
ExpectedListOrNoArgs,
522523
UnexpectedLiteral,
523524
ExpectedNameValue(Option<Symbol>),
524525
DuplicateKey(Symbol),
@@ -591,6 +592,9 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError<'_> {
591592
AttributeParseErrorReason::ExpectedList => {
592593
diag.span_label(self.span, "expected this to be a list");
593594
}
595+
AttributeParseErrorReason::ExpectedListOrNoArgs => {
596+
diag.span_label(self.span, "expected a list or no arguments here");
597+
}
594598
AttributeParseErrorReason::DuplicateKey(key) => {
595599
diag.span_label(self.span, format!("found `{key}` used as a key more than once"));
596600
diag.code(E0538);

tests/ui/attributes/invalid-macro-use.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
//~^ NOTE the lint level is defined here
33

44
#[macro_use = 5]
5-
//~^ ERROR valid forms for the attribute are `#[macro_use(name1, name2, ...)]` and `#[macro_use]`
5+
//~^ ERROR malformed `macro_use` attribute input
6+
//~| NOTE expected a list or no arguments here
7+
//~| NOTE for more information, visit
68
extern crate std as s1;
79

810
#[macro_use(5)]

tests/ui/attributes/invalid-macro-use.stderr

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
error[E0469]: imported macro not found
2-
--> $DIR/invalid-macro-use.rs:51:13
2+
--> $DIR/invalid-macro-use.rs:53:13
33
|
44
LL | #[macro_use(a)]
55
| ^
66

77
error[E0469]: imported macro not found
8-
--> $DIR/invalid-macro-use.rs:53:13
8+
--> $DIR/invalid-macro-use.rs:55:13
99
|
1010
LL | #[macro_use(b)]
1111
| ^
1212

13-
error: valid forms for the attribute are `#[macro_use(name1, name2, ...)]` and `#[macro_use]`
13+
error[E0539]: malformed `macro_use` attribute input
1414
--> $DIR/invalid-macro-use.rs:4:1
1515
|
1616
LL | #[macro_use = 5]
17-
| ^^^^^^^^^^^^^^^^
17+
| ^^^^^^^^^^^^^^^^ expected a list or no arguments here
18+
|
19+
= note: for more information, visit <https://doc.rust-lang.org/reference/macros-by-example.html#the-macro_use-attribute>
20+
help: try changing it to one of the following valid forms of the attribute
21+
|
22+
LL - #[macro_use = 5]
23+
LL + #[macro_use(name1, name2, ...)]
24+
|
25+
LL - #[macro_use = 5]
26+
LL + #[macro_use]
27+
|
1828

1929
error[E0539]: malformed `macro_use` attribute input
20-
--> $DIR/invalid-macro-use.rs:8:1
30+
--> $DIR/invalid-macro-use.rs:10:1
2131
|
2232
LL | #[macro_use(5)]
2333
| ^^^^^^^^^^^^-^^
@@ -35,7 +45,7 @@ LL + #[macro_use]
3545
|
3646

3747
error[E0565]: malformed `macro_use` attribute input
38-
--> $DIR/invalid-macro-use.rs:14:1
48+
--> $DIR/invalid-macro-use.rs:16:1
3949
|
4050
LL | #[macro_use(a = "b")]
4151
| ^^^^^^^^^^^^^^-----^^
@@ -53,7 +63,7 @@ LL + #[macro_use]
5363
|
5464

5565
error[E0565]: malformed `macro_use` attribute input
56-
--> $DIR/invalid-macro-use.rs:20:1
66+
--> $DIR/invalid-macro-use.rs:22:1
5767
|
5868
LL | #[macro_use(a(b))]
5969
| ^^^^^^^^^^^^^---^^
@@ -71,7 +81,7 @@ LL + #[macro_use]
7181
|
7282

7383
error[E0539]: malformed `macro_use` attribute input
74-
--> $DIR/invalid-macro-use.rs:26:1
84+
--> $DIR/invalid-macro-use.rs:28:1
7585
|
7686
LL | #[macro_use(a::b)]
7787
| ^^^^^^^^^^^^----^^
@@ -89,13 +99,13 @@ LL + #[macro_use]
8999
|
90100

91101
error: unused attribute
92-
--> $DIR/invalid-macro-use.rs:32:1
102+
--> $DIR/invalid-macro-use.rs:34:1
93103
|
94104
LL | #[macro_use(a)]
95105
| ^^^^^^^^^^^^^^^ help: remove this attribute
96106
|
97107
note: attribute also specified here
98-
--> $DIR/invalid-macro-use.rs:34:1
108+
--> $DIR/invalid-macro-use.rs:36:1
99109
|
100110
LL | #[macro_use]
101111
| ^^^^^^^^^^^^
@@ -106,25 +116,25 @@ LL | #![deny(unused_attributes)]
106116
| ^^^^^^^^^^^^^^^^^
107117

108118
error: unused attribute
109-
--> $DIR/invalid-macro-use.rs:40:1
119+
--> $DIR/invalid-macro-use.rs:42:1
110120
|
111121
LL | #[macro_use(a)]
112122
| ^^^^^^^^^^^^^^^ help: remove this attribute
113123
|
114124
note: attribute also specified here
115-
--> $DIR/invalid-macro-use.rs:38:1
125+
--> $DIR/invalid-macro-use.rs:40:1
116126
|
117127
LL | #[macro_use]
118128
| ^^^^^^^^^^^^
119129

120130
error: unused attribute
121-
--> $DIR/invalid-macro-use.rs:46:1
131+
--> $DIR/invalid-macro-use.rs:48:1
122132
|
123133
LL | #[macro_use]
124134
| ^^^^^^^^^^^^ help: remove this attribute
125135
|
126136
note: attribute also specified here
127-
--> $DIR/invalid-macro-use.rs:44:1
137+
--> $DIR/invalid-macro-use.rs:46:1
128138
|
129139
LL | #[macro_use]
130140
| ^^^^^^^^^^^^

tests/ui/attributes/malformed-attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ static mut TLS: u8 = 42;
214214
#[no_link()]
215215
//~^ ERROR malformed
216216
#[macro_use = 1]
217-
//~^ ERROR valid forms for the attribute are `#[macro_use(name1, name2, ...)]` and `#[macro_use]`
217+
//~^ ERROR malformed
218218
extern crate wloop;
219219
//~^ ERROR can't find crate for `wloop` [E0463]
220220

tests/ui/attributes/malformed-attrs.stderr

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,11 +614,21 @@ LL | #[non_exhaustive = 1]
614614
| | didn't expect any arguments here
615615
| help: must be of the form: `#[non_exhaustive]`
616616

617-
error: valid forms for the attribute are `#[macro_use(name1, name2, ...)]` and `#[macro_use]`
617+
error[E0539]: malformed `macro_use` attribute input
618618
--> $DIR/malformed-attrs.rs:216:1
619619
|
620620
LL | #[macro_use = 1]
621-
| ^^^^^^^^^^^^^^^^
621+
| ^^^^^^^^^^^^^^^^ expected a list or no arguments here
622+
|
623+
= note: for more information, visit <https://doc.rust-lang.org/reference/macros-by-example.html#the-macro_use-attribute>
624+
help: try changing it to one of the following valid forms of the attribute
625+
|
626+
LL - #[macro_use = 1]
627+
LL + #[macro_use(name1, name2, ...)]
628+
|
629+
LL - #[macro_use = 1]
630+
LL + #[macro_use]
631+
|
622632

623633
error: valid forms for the attribute are `#[macro_export(local_inner_macros)]` and `#[macro_export]`
624634
--> $DIR/malformed-attrs.rs:221:1

tests/ui/feature-gates/issue-43106-gating-of-macro_use.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ mod macro_escape {
1313
//~^ ERROR arguments to `macro_use` are not allowed here
1414

1515
#[macro_use = "2700"] struct S;
16-
//~^ ERROR valid forms for the attribute are `#[macro_use(name1, name2, ...)]` and `#[macro_use]`
16+
//~^ ERROR malformed
1717
//~| WARN cannot be used on
1818
//~| WARN previously accepted
1919

tests/ui/feature-gates/issue-43106-gating-of-macro_use.stderr

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,21 @@ error: arguments to `macro_use` are not allowed here
1616
LL | #![macro_use(my_macro)]
1717
| ^^^^^^^^^^^^^^^^^^^^^^^
1818

19-
error: valid forms for the attribute are `#[macro_use(name1, name2, ...)]` and `#[macro_use]`
19+
error[E0539]: malformed `macro_use` attribute input
2020
--> $DIR/issue-43106-gating-of-macro_use.rs:15:5
2121
|
2222
LL | #[macro_use = "2700"] struct S;
23-
| ^^^^^^^^^^^^^^^^^^^^^
23+
| ^^^^^^^^^^^^^^^^^^^^^ expected a list or no arguments here
24+
|
25+
= note: for more information, visit <https://doc.rust-lang.org/reference/macros-by-example.html#the-macro_use-attribute>
26+
help: try changing it to one of the following valid forms of the attribute
27+
|
28+
LL - #[macro_use = "2700"] struct S;
29+
LL + #[macro_use(name1, name2, ...)] struct S;
30+
|
31+
LL - #[macro_use = "2700"] struct S;
32+
LL + #[macro_use] struct S;
33+
|
2434

2535
warning: `#[macro_use]` attribute cannot be used on structs
2636
--> $DIR/issue-43106-gating-of-macro_use.rs:15:5
@@ -61,3 +71,4 @@ LL | #[macro_use] impl S { }
6171

6272
error: aborting due to 4 previous errors; 4 warnings emitted
6373

74+
For more information about this error, try `rustc --explain E0539`.

0 commit comments

Comments
 (0)