Skip to content

Commit 957e505

Browse files
Add target checking to #[register_tool]
1 parent 49b19d3 commit 957e505

5 files changed

Lines changed: 75 additions & 34 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/crate_level.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ impl CombineAttributeParser for RegisterToolParser {
314314
const PATH: &[Symbol] = &[sym::register_tool];
315315
type Item = Ident;
316316
const CONVERT: ConvertFn<Self::Item> = |tools, _span| AttributeKind::RegisterTool(tools);
317-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
317+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
318318
const TEMPLATE: AttributeTemplate = template!(List: &["tool1, tool2, ..."]);
319319
const STABILITY: AttributeStability = unstable!(register_tool);
320320

tests/ui/attributes/attr-on-mac-call.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@ check-fail
22
// Regression test for https://github.com/rust-lang/rust/issues/145779
33
#![warn(unused_attributes)]
4+
#![feature(register_tool)]
45
#![feature(sanitize)]
56

67
fn main() {
@@ -106,4 +107,7 @@ fn main() {
106107
//~^ WARN attribute cannot be used on macro calls
107108
//~| WARN previously accepted
108109
unreachable!();
110+
#[register_tool(xyz)]
111+
//~^ WARN crate-level attribute should be an inner attribute
112+
unreachable!();
109113
}

tests/ui/attributes/attr-on-mac-call.stderr

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error: `#[sanitize]` attribute cannot be used on macro calls
2-
--> $DIR/attr-on-mac-call.rs:76:5
2+
--> $DIR/attr-on-mac-call.rs:77:5
33
|
44
LL | #[sanitize(address = "off")]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= help: `#[sanitize]` can be applied to crates, functions, impl blocks, modules, and statics
88

99
warning: `#[export_name]` attribute cannot be used on macro calls
10-
--> $DIR/attr-on-mac-call.rs:7:5
10+
--> $DIR/attr-on-mac-call.rs:8:5
1111
|
1212
LL | #[export_name = "x"]
1313
| ^^^^^^^^^^^^^^^^^^^^
@@ -21,7 +21,7 @@ LL | #![warn(unused_attributes)]
2121
| ^^^^^^^^^^^^^^^^^
2222

2323
warning: `#[naked]` attribute cannot be used on macro calls
24-
--> $DIR/attr-on-mac-call.rs:10:5
24+
--> $DIR/attr-on-mac-call.rs:11:5
2525
|
2626
LL | #[unsafe(naked)]
2727
| ^^^^^^^^^^^^^^^^
@@ -30,7 +30,7 @@ LL | #[unsafe(naked)]
3030
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
3131

3232
warning: `#[track_caller]` attribute cannot be used on macro calls
33-
--> $DIR/attr-on-mac-call.rs:13:5
33+
--> $DIR/attr-on-mac-call.rs:14:5
3434
|
3535
LL | #[track_caller]
3636
| ^^^^^^^^^^^^^^^
@@ -39,7 +39,7 @@ LL | #[track_caller]
3939
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
4040

4141
warning: `#[used]` attribute cannot be used on macro calls
42-
--> $DIR/attr-on-mac-call.rs:16:5
42+
--> $DIR/attr-on-mac-call.rs:17:5
4343
|
4444
LL | #[used]
4545
| ^^^^^^^
@@ -48,7 +48,7 @@ LL | #[used]
4848
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
4949

5050
warning: `#[target_feature]` attribute cannot be used on macro calls
51-
--> $DIR/attr-on-mac-call.rs:19:5
51+
--> $DIR/attr-on-mac-call.rs:20:5
5252
|
5353
LL | #[target_feature(enable = "x")]
5454
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -57,7 +57,7 @@ LL | #[target_feature(enable = "x")]
5757
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
5858

5959
warning: `#[deprecated]` attribute cannot be used on macro calls
60-
--> $DIR/attr-on-mac-call.rs:22:5
60+
--> $DIR/attr-on-mac-call.rs:23:5
6161
|
6262
LL | #[deprecated]
6363
| ^^^^^^^^^^^^^
@@ -66,7 +66,7 @@ LL | #[deprecated]
6666
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
6767

6868
warning: `#[inline]` attribute cannot be used on macro calls
69-
--> $DIR/attr-on-mac-call.rs:25:5
69+
--> $DIR/attr-on-mac-call.rs:26:5
7070
|
7171
LL | #[inline]
7272
| ^^^^^^^^^
@@ -75,7 +75,7 @@ LL | #[inline]
7575
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
7676

7777
warning: `#[link_name]` attribute cannot be used on macro calls
78-
--> $DIR/attr-on-mac-call.rs:28:5
78+
--> $DIR/attr-on-mac-call.rs:29:5
7979
|
8080
LL | #[link_name = "x"]
8181
| ^^^^^^^^^^^^^^^^^^
@@ -84,7 +84,7 @@ LL | #[link_name = "x"]
8484
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
8585

8686
warning: `#[link_section]` attribute cannot be used on macro calls
87-
--> $DIR/attr-on-mac-call.rs:31:5
87+
--> $DIR/attr-on-mac-call.rs:32:5
8888
|
8989
LL | #[link_section = "__TEXT,__text"]
9090
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -93,7 +93,7 @@ LL | #[link_section = "__TEXT,__text"]
9393
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
9494

9595
warning: `#[link_ordinal]` attribute cannot be used on macro calls
96-
--> $DIR/attr-on-mac-call.rs:34:5
96+
--> $DIR/attr-on-mac-call.rs:35:5
9797
|
9898
LL | #[link_ordinal(42)]
9999
| ^^^^^^^^^^^^^^^^^^^
@@ -102,7 +102,7 @@ LL | #[link_ordinal(42)]
102102
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
103103

104104
warning: `#[non_exhaustive]` attribute cannot be used on macro calls
105-
--> $DIR/attr-on-mac-call.rs:37:5
105+
--> $DIR/attr-on-mac-call.rs:38:5
106106
|
107107
LL | #[non_exhaustive]
108108
| ^^^^^^^^^^^^^^^^^
@@ -111,7 +111,7 @@ LL | #[non_exhaustive]
111111
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
112112

113113
warning: `#[proc_macro]` attribute cannot be used on macro calls
114-
--> $DIR/attr-on-mac-call.rs:40:5
114+
--> $DIR/attr-on-mac-call.rs:41:5
115115
|
116116
LL | #[proc_macro]
117117
| ^^^^^^^^^^^^^
@@ -120,7 +120,7 @@ LL | #[proc_macro]
120120
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
121121

122122
warning: `#[cold]` attribute cannot be used on macro calls
123-
--> $DIR/attr-on-mac-call.rs:43:5
123+
--> $DIR/attr-on-mac-call.rs:44:5
124124
|
125125
LL | #[cold]
126126
| ^^^^^^^
@@ -129,7 +129,7 @@ LL | #[cold]
129129
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
130130

131131
warning: `#[no_mangle]` attribute cannot be used on macro calls
132-
--> $DIR/attr-on-mac-call.rs:46:5
132+
--> $DIR/attr-on-mac-call.rs:47:5
133133
|
134134
LL | #[no_mangle]
135135
| ^^^^^^^^^^^^
@@ -138,7 +138,7 @@ LL | #[no_mangle]
138138
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
139139

140140
warning: `#[deprecated]` attribute cannot be used on macro calls
141-
--> $DIR/attr-on-mac-call.rs:49:5
141+
--> $DIR/attr-on-mac-call.rs:50:5
142142
|
143143
LL | #[deprecated]
144144
| ^^^^^^^^^^^^^
@@ -147,7 +147,7 @@ LL | #[deprecated]
147147
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
148148

149149
warning: `#[automatically_derived]` attribute cannot be used on macro calls
150-
--> $DIR/attr-on-mac-call.rs:52:5
150+
--> $DIR/attr-on-mac-call.rs:53:5
151151
|
152152
LL | #[automatically_derived]
153153
| ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -156,7 +156,7 @@ LL | #[automatically_derived]
156156
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
157157

158158
warning: `#[macro_use]` attribute cannot be used on macro calls
159-
--> $DIR/attr-on-mac-call.rs:55:5
159+
--> $DIR/attr-on-mac-call.rs:56:5
160160
|
161161
LL | #[macro_use]
162162
| ^^^^^^^^^^^^
@@ -165,7 +165,7 @@ LL | #[macro_use]
165165
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
166166

167167
warning: `#[must_use]` attribute cannot be used on macro calls
168-
--> $DIR/attr-on-mac-call.rs:58:5
168+
--> $DIR/attr-on-mac-call.rs:59:5
169169
|
170170
LL | #[must_use]
171171
| ^^^^^^^^^^^
@@ -174,7 +174,7 @@ LL | #[must_use]
174174
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
175175

176176
warning: `#[no_implicit_prelude]` attribute cannot be used on macro calls
177-
--> $DIR/attr-on-mac-call.rs:61:5
177+
--> $DIR/attr-on-mac-call.rs:62:5
178178
|
179179
LL | #[no_implicit_prelude]
180180
| ^^^^^^^^^^^^^^^^^^^^^^
@@ -183,7 +183,7 @@ LL | #[no_implicit_prelude]
183183
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
184184

185185
warning: `#[path]` attribute cannot be used on macro calls
186-
--> $DIR/attr-on-mac-call.rs:64:5
186+
--> $DIR/attr-on-mac-call.rs:65:5
187187
|
188188
LL | #[path = ""]
189189
| ^^^^^^^^^^^^
@@ -192,7 +192,7 @@ LL | #[path = ""]
192192
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
193193

194194
warning: `#[ignore]` attribute cannot be used on macro calls
195-
--> $DIR/attr-on-mac-call.rs:67:5
195+
--> $DIR/attr-on-mac-call.rs:68:5
196196
|
197197
LL | #[ignore]
198198
| ^^^^^^^^^
@@ -201,7 +201,7 @@ LL | #[ignore]
201201
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
202202

203203
warning: `#[should_panic]` attribute cannot be used on macro calls
204-
--> $DIR/attr-on-mac-call.rs:70:5
204+
--> $DIR/attr-on-mac-call.rs:71:5
205205
|
206206
LL | #[should_panic]
207207
| ^^^^^^^^^^^^^^^
@@ -210,7 +210,7 @@ LL | #[should_panic]
210210
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
211211

212212
warning: `#[link_name]` attribute cannot be used on macro calls
213-
--> $DIR/attr-on-mac-call.rs:73:5
213+
--> $DIR/attr-on-mac-call.rs:74:5
214214
|
215215
LL | #[link_name = "x"]
216216
| ^^^^^^^^^^^^^^^^^^
@@ -219,7 +219,7 @@ LL | #[link_name = "x"]
219219
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
220220

221221
warning: `#[repr()]` attribute cannot be used on macro calls
222-
--> $DIR/attr-on-mac-call.rs:80:5
222+
--> $DIR/attr-on-mac-call.rs:81:5
223223
|
224224
LL | #[repr()]
225225
| ^^^^^^^^^
@@ -228,15 +228,15 @@ LL | #[repr()]
228228
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
229229

230230
warning: unused attribute
231-
--> $DIR/attr-on-mac-call.rs:80:5
231+
--> $DIR/attr-on-mac-call.rs:81:5
232232
|
233233
LL | #[repr()]
234234
| ^^^^^^^^^ help: remove this attribute
235235
|
236236
= note: using `repr` with an empty list has no effect
237237

238238
warning: `#[repr(u8)]` attribute cannot be used on macro calls
239-
--> $DIR/attr-on-mac-call.rs:85:5
239+
--> $DIR/attr-on-mac-call.rs:86:5
240240
|
241241
LL | #[repr(u8)]
242242
| ^^^^^^^^^^^
@@ -245,7 +245,7 @@ LL | #[repr(u8)]
245245
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
246246

247247
warning: `#[repr(align(...))]` attribute cannot be used on macro calls
248-
--> $DIR/attr-on-mac-call.rs:89:5
248+
--> $DIR/attr-on-mac-call.rs:90:5
249249
|
250250
LL | #[repr(align(8))]
251251
| ^^^^^^^^^^^^^^^^^
@@ -254,7 +254,7 @@ LL | #[repr(align(8))]
254254
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
255255

256256
warning: `#[repr(packed)]` attribute cannot be used on macro calls
257-
--> $DIR/attr-on-mac-call.rs:93:5
257+
--> $DIR/attr-on-mac-call.rs:94:5
258258
|
259259
LL | #[repr(packed)]
260260
| ^^^^^^^^^^^^^^^
@@ -263,7 +263,7 @@ LL | #[repr(packed)]
263263
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
264264

265265
warning: `#[repr(C)]` attribute cannot be used on macro calls
266-
--> $DIR/attr-on-mac-call.rs:97:5
266+
--> $DIR/attr-on-mac-call.rs:98:5
267267
|
268268
LL | #[repr(C)]
269269
| ^^^^^^^^^^
@@ -272,7 +272,7 @@ LL | #[repr(C)]
272272
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
273273

274274
warning: `#[repr(Rust)]` attribute cannot be used on macro calls
275-
--> $DIR/attr-on-mac-call.rs:101:5
275+
--> $DIR/attr-on-mac-call.rs:102:5
276276
|
277277
LL | #[repr(Rust)]
278278
| ^^^^^^^^^^^^^
@@ -281,13 +281,25 @@ LL | #[repr(Rust)]
281281
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
282282

283283
warning: `#[repr(simd)]` attribute cannot be used on macro calls
284-
--> $DIR/attr-on-mac-call.rs:105:5
284+
--> $DIR/attr-on-mac-call.rs:106:5
285285
|
286286
LL | #[repr(simd)]
287287
| ^^^^^^^^^^^^^
288288
|
289289
= help: `#[repr(simd)]` can only be applied to structs
290290
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
291291

292-
error: aborting due to 1 previous error; 31 warnings emitted
292+
warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![register_tool]`
293+
--> $DIR/attr-on-mac-call.rs:110:5
294+
|
295+
LL | #[register_tool(xyz)]
296+
| ^^^^^^^^^^^^^^^^^^^^^
297+
|
298+
note: this attribute does not have an `!`, which means it is applied to this macro call
299+
--> $DIR/attr-on-mac-call.rs:112:5
300+
|
301+
LL | unreachable!();
302+
| ^^^^^^^^^^^^^^
303+
304+
error: aborting due to 1 previous error; 32 warnings emitted
293305

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//@ check-pass
2+
#![feature(register_tool)]
3+
4+
#[register_tool(no_valid_target)]
5+
//~^ WARN crate-level attribute should be an inner attribute
6+
fn main() {
7+
8+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![register_tool]`
2+
--> $DIR/register-tool-target.rs:4:1
3+
|
4+
LL | #[register_tool(no_valid_target)]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: this attribute does not have an `!`, which means it is applied to this function
8+
--> $DIR/register-tool-target.rs:6:1
9+
|
10+
LL | / fn main() {
11+
LL | |
12+
LL | | }
13+
| |_^
14+
= note: requested on the command line with `-W unused-attributes`
15+
16+
warning: 1 warning emitted
17+

0 commit comments

Comments
 (0)