Skip to content

Commit 6e9a810

Browse files
Rollup merge of rust-lang#157443 - JonathanBrouwer:crate-level-err, r=mejrs
Make distinction between crate-level attributes that are warned vs errored r? @mejrs This is a follow-up to rust-lang#157377 (comment) This PR makes all unstable crate-level attributes error on incorrect targets. To make the diff more helpful, I'd recommend reviewing the two commits separately. * The first commit introduces the infra & makes all crate-level attributes a warning, the commit should have no effect * The second commit undoes some changes from the first commit, to make all unstable crate-level attributes error
2 parents b1304a9 + 73c631d commit 6e9a810

8 files changed

Lines changed: 71 additions & 57 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/crate_level.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ impl SingleAttributeParser for CrateNameParser {
1313
const PATH: &[Symbol] = &[sym::crate_name];
1414
const ON_DUPLICATE: OnDuplicate = OnDuplicate::WarnButFutureError;
1515
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "name");
16-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
16+
const ALLOWED_TARGETS: AllowedTargets =
17+
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate)]);
1718
const STABILITY: AttributeStability = AttributeStability::Stable;
1819

1920
fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
@@ -31,7 +32,8 @@ impl CombineAttributeParser for CrateTypeParser {
3132
const PATH: &[Symbol] = &[sym::crate_type];
3233
type Item = CrateType;
3334
const CONVERT: ConvertFn<Self::Item> = |items, _| AttributeKind::CrateType(items);
34-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
35+
const ALLOWED_TARGETS: AllowedTargets =
36+
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate)]);
3537
const TEMPLATE: AttributeTemplate =
3638
template!(NameValueStr: "crate type", "https://doc.rust-lang.org/reference/linkage.html");
3739
const STABILITY: AttributeStability = AttributeStability::Stable;
@@ -74,7 +76,8 @@ impl SingleAttributeParser for RecursionLimitParser {
7476
const PATH: &[Symbol] = &[sym::recursion_limit];
7577
const ON_DUPLICATE: OnDuplicate = OnDuplicate::WarnButFutureError;
7678
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "N", "https://doc.rust-lang.org/reference/attributes/limits.html#the-recursion_limit-attribute");
77-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
79+
const ALLOWED_TARGETS: AllowedTargets =
80+
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate)]);
7881
const STABILITY: AttributeStability = AttributeStability::Stable;
7982

8083
fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
@@ -105,7 +108,8 @@ impl SingleAttributeParser for TypeLengthLimitParser {
105108
const PATH: &[Symbol] = &[sym::type_length_limit];
106109
const ON_DUPLICATE: OnDuplicate = OnDuplicate::WarnButFutureError;
107110
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "N");
108-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
111+
const ALLOWED_TARGETS: AllowedTargets =
112+
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate)]);
109113
const STABILITY: AttributeStability = AttributeStability::Stable;
110114

111115
fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
@@ -147,7 +151,8 @@ pub(crate) struct NoStdParser;
147151
impl NoArgsAttributeParser for NoStdParser {
148152
const PATH: &[Symbol] = &[sym::no_std];
149153
const ON_DUPLICATE: OnDuplicate = OnDuplicate::Warn;
150-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
154+
const ALLOWED_TARGETS: AllowedTargets =
155+
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate)]);
151156
const STABILITY: AttributeStability = AttributeStability::Stable;
152157
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::NoStd;
153158
}
@@ -157,7 +162,8 @@ pub(crate) struct NoMainParser;
157162
impl NoArgsAttributeParser for NoMainParser {
158163
const PATH: &[Symbol] = &[sym::no_main];
159164
const ON_DUPLICATE: OnDuplicate = OnDuplicate::Warn;
160-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
165+
const ALLOWED_TARGETS: AllowedTargets =
166+
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate)]);
161167
const STABILITY: AttributeStability = AttributeStability::Stable;
162168
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::NoMain;
163169
}
@@ -176,7 +182,8 @@ pub(crate) struct WindowsSubsystemParser;
176182
impl SingleAttributeParser for WindowsSubsystemParser {
177183
const PATH: &[Symbol] = &[sym::windows_subsystem];
178184
const ON_DUPLICATE: OnDuplicate = OnDuplicate::WarnButFutureError;
179-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
185+
const ALLOWED_TARGETS: AllowedTargets =
186+
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate)]);
180187
const TEMPLATE: AttributeTemplate = template!(NameValueStr: ["windows", "console"], "https://doc.rust-lang.org/reference/runtime.html#the-windows_subsystem-attribute");
181188
const STABILITY: AttributeStability = AttributeStability::Stable;
182189

@@ -231,7 +238,8 @@ pub(crate) struct NoBuiltinsParser;
231238
impl NoArgsAttributeParser for NoBuiltinsParser {
232239
const PATH: &[Symbol] = &[sym::no_builtins];
233240
const ON_DUPLICATE: OnDuplicate = OnDuplicate::Warn;
234-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
241+
const ALLOWED_TARGETS: AllowedTargets =
242+
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate)]);
235243
const STABILITY: AttributeStability = AttributeStability::Stable;
236244
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::NoBuiltins;
237245
}
@@ -269,7 +277,8 @@ impl CombineAttributeParser for FeatureParser {
269277
const PATH: &[Symbol] = &[sym::feature];
270278
type Item = Ident;
271279
const CONVERT: ConvertFn<Self::Item> = AttributeKind::Feature;
272-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
280+
const ALLOWED_TARGETS: AllowedTargets =
281+
AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate)]);
273282
const TEMPLATE: AttributeTemplate = template!(List: &["feature1, feature2, ..."]);
274283
const STABILITY: AttributeStability = AttributeStability::Stable;
275284

compiler/rustc_attr_parsing/src/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ pub(crate) struct InvalidAttrStyle {
148148
pub crate_root_path: String,
149149
#[help("the crate root is at `{$crate_root_path}`")]
150150
pub show_crate_root_help: bool,
151+
#[primary_span]
152+
pub span: Span,
151153
}
152154

153155
#[derive(Diagnostic)]

compiler/rustc_attr_parsing/src/target_checking.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,11 @@ impl<'sess> AttributeParser<'sess> {
114114
// For crate-level attributes we emit a specific set of lints to warn
115115
// people about accidentally not using them on the crate.
116116
if let &AllowedTargets::AllowList(&[Allow(Target::Crate)]) = allowed_targets {
117-
Self::check_crate_level(cx);
117+
Self::check_crate_level(cx, false);
118+
return;
119+
}
120+
if let &AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate)]) = allowed_targets {
121+
Self::check_crate_level(cx, true);
118122
return;
119123
}
120124

@@ -181,7 +185,7 @@ impl<'sess> AttributeParser<'sess> {
181185
}
182186
}
183187

184-
pub(crate) fn check_crate_level(cx: &mut AcceptContext<'_, 'sess>) {
188+
pub(crate) fn check_crate_level(cx: &mut AcceptContext<'_, 'sess>, warn: bool) {
185189
if cx.target == Target::Crate {
186190
return;
187191
}
@@ -205,19 +209,20 @@ impl<'sess> AttributeParser<'sess> {
205209
})
206210
.unwrap_or_default();
207211

208-
let target = cx.target;
209-
cx.emit_lint(
210-
rustc_session::lint::builtin::UNUSED_ATTRIBUTES,
211-
crate::errors::InvalidAttrStyle {
212-
name,
213-
is_used_as_inner,
214-
target_span: (!is_used_as_inner).then_some(target_span),
215-
target: target.name(),
216-
crate_root_path,
217-
show_crate_root_help,
218-
},
219-
attr_span,
220-
);
212+
let diag = crate::errors::InvalidAttrStyle {
213+
name,
214+
is_used_as_inner,
215+
target_span: (!is_used_as_inner).then_some(target_span),
216+
target: cx.target.name(),
217+
crate_root_path,
218+
show_crate_root_help,
219+
span: attr_span,
220+
};
221+
if warn {
222+
cx.emit_lint(rustc_session::lint::builtin::UNUSED_ATTRIBUTES, diag, attr_span);
223+
} else {
224+
cx.emit_err(diag);
225+
}
221226
}
222227

223228
// FIXME: Fix "Cannot determine resolution" error and remove built-in macros

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,6 @@ fn main() {
108108
//~| WARN previously accepted
109109
unreachable!();
110110
#[register_tool(xyz)]
111-
//~^ WARN crate-level attribute should be an inner attribute
111+
//~^ ERROR crate-level attribute should be an inner attribute
112112
unreachable!();
113113
}

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ LL | #[sanitize(address = "off")]
66
|
77
= help: `#[sanitize]` can be applied to crates, functions, impl blocks, modules, and statics
88

9+
error: crate-level attribute should be an inner attribute: add an exclamation mark: `#![register_tool]`
10+
--> $DIR/attr-on-mac-call.rs:110:5
11+
|
12+
LL | #[register_tool(xyz)]
13+
| ^^^^^^^^^^^^^^^^^^^^^
14+
|
15+
note: this attribute does not have an `!`, which means it is applied to this macro call
16+
--> $DIR/attr-on-mac-call.rs:112:5
17+
|
18+
LL | unreachable!();
19+
| ^^^^^^^^^^^^^^
20+
921
warning: `#[export_name]` attribute cannot be used on macro calls
1022
--> $DIR/attr-on-mac-call.rs:8:5
1123
|
@@ -289,17 +301,5 @@ LL | #[repr(simd)]
289301
= help: `#[repr(simd)]` can only be applied to structs
290302
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
291303

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
304+
error: aborting due to 2 previous errors; 31 warnings emitted
305305

tests/ui/attributes/malformed-no-std.stderr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ note: attribute also specified here
106106
LL | #![no_core = "foo"]
107107
| ^^^^^^^^^^^^^^^^^^^
108108

109+
error: crate-level attribute should be an inner attribute: add an exclamation mark: `#![no_core]`
110+
--> $DIR/malformed-no-std.rs:23:1
111+
|
112+
LL | #[no_core]
113+
| ^^^^^^^^^^
114+
|
115+
note: this attribute does not have an `!`, which means it is applied to this extern crate
116+
--> $DIR/malformed-no-std.rs:26:1
117+
|
118+
LL | extern crate core;
119+
| ^^^^^^^^^^^^^^^^^^
120+
109121
error: crate-level attribute should be an inner attribute: add an exclamation mark: `#![no_std]`
110122
--> $DIR/malformed-no-std.rs:21:1
111123
|
@@ -123,18 +135,6 @@ note: the lint level is defined here
123135
LL | #[deny(unused_attributes)]
124136
| ^^^^^^^^^^^^^^^^^
125137

126-
error: crate-level attribute should be an inner attribute: add an exclamation mark: `#![no_core]`
127-
--> $DIR/malformed-no-std.rs:23:1
128-
|
129-
LL | #[no_core]
130-
| ^^^^^^^^^^
131-
|
132-
note: this attribute does not have an `!`, which means it is applied to this extern crate
133-
--> $DIR/malformed-no-std.rs:26:1
134-
|
135-
LL | extern crate core;
136-
| ^^^^^^^^^^^^^^^^^^
137-
138138
warning: unused attribute
139139
--> $DIR/malformed-no-std.rs:5:1
140140
|
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
//@ check-pass
21
#![feature(register_tool)]
32

43
#[register_tool(no_valid_target)]
5-
//~^ WARN crate-level attribute should be an inner attribute
4+
//~^ ERROR crate-level attribute should be an inner attribute
65
fn main() {
76

87
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![register_tool]`
2-
--> $DIR/register-tool-target.rs:4:1
1+
error: crate-level attribute should be an inner attribute: add an exclamation mark: `#![register_tool]`
2+
--> $DIR/register-tool-target.rs:3:1
33
|
44
LL | #[register_tool(no_valid_target)]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
note: this attribute does not have an `!`, which means it is applied to this function
8-
--> $DIR/register-tool-target.rs:6:1
8+
--> $DIR/register-tool-target.rs:5:1
99
|
1010
LL | / fn main() {
1111
LL | |
1212
LL | | }
1313
| |_^
14-
= note: requested on the command line with `-W unused-attributes`
1514

16-
warning: 1 warning emitted
15+
error: aborting due to 1 previous error
1716

0 commit comments

Comments
 (0)