Skip to content

Commit 1cf8c6f

Browse files
authored
Unrolled build for #157291
Rollup merge of #157291 - JonathanBrouwer:cleanup-target-checking, r=mejrs Clean up attribute target checking diagnostics Split off from me trying to process your review comments in #157215 Thanks to @GuillaumeGomez for making this possible <3 r? @mejrs
2 parents 4f84d9f + 397dd46 commit 1cf8c6f

27 files changed

Lines changed: 208 additions & 242 deletions

compiler/rustc_attr_parsing/src/errors.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -132,26 +132,6 @@ pub(crate) struct EmptyAttributeList {
132132
pub valid_without_list: bool,
133133
}
134134

135-
#[derive(Diagnostic)]
136-
#[diag("`#[{$name}]` attribute cannot be used on {$target}")]
137-
#[warning(
138-
"this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!"
139-
)]
140-
#[help("`#[{$name}]` can {$only}be applied to {$applied}")]
141-
pub(crate) struct InvalidTargetLint {
142-
pub name: String,
143-
pub target: &'static str,
144-
pub applied: DiagArgValue,
145-
pub only: &'static str,
146-
#[suggestion(
147-
"remove the attribute",
148-
code = "",
149-
applicability = "machine-applicable",
150-
style = "tool-only"
151-
)]
152-
pub attr_span: Span,
153-
}
154-
155135
#[derive(Diagnostic)]
156136
#[diag(
157137
"{$is_used_as_inner ->

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,10 @@ pub(crate) struct InvalidTarget {
353353
pub target: &'static str,
354354
pub applied: DiagArgValue,
355355
pub only: &'static str,
356+
#[warning(
357+
"this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!"
358+
)]
359+
pub previously_accepted: bool,
356360
}
357361

358362
#[derive(Diagnostic)]

compiler/rustc_attr_parsing/src/target_checking.rs

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
use std::borrow::Cow;
22

33
use rustc_ast::AttrStyle;
4-
use rustc_errors::{DiagArgValue, Diagnostic, MultiSpan, StashKey};
4+
use rustc_errors::{DiagArgValue, MultiSpan, StashKey};
55
use rustc_feature::Features;
66
use rustc_hir::attrs::AttributeKind;
77
use rustc_hir::{AttrItem, Attribute, MethodKind, Target};
88
use rustc_span::{BytePos, FileName, RemapPathScopeComponents, Span, Symbol, sym};
99

1010
use crate::context::AcceptContext;
1111
use crate::errors::{
12-
InvalidAttrAtCrateLevel, InvalidTargetLint, ItemFollowingInnerAttr,
13-
UnsupportedAttributesInWhere,
12+
InvalidAttrAtCrateLevel, ItemFollowingInnerAttr, UnsupportedAttributesInWhere,
1413
};
1514
use crate::session_diagnostics::InvalidTarget;
1615
use crate::target_checking::Policy::Allow;
@@ -122,15 +121,26 @@ impl<'sess> AttributeParser<'sess> {
122121
.emit();
123122
}
124123

125-
match allowed_targets.is_allowed(cx.target) {
126-
AllowedResult::Allowed => {}
127-
AllowedResult::Warn => {
128-
let allowed_targets = allowed_targets.allowed_targets();
129-
let (applied, only) =
130-
allowed_targets_applied(allowed_targets, cx.target, cx.features);
131-
let name = cx.attr_path.clone();
124+
let result = allowed_targets.is_allowed(cx.target);
125+
if matches!(result, AllowedResult::Allowed) {
126+
return;
127+
}
132128

133-
let lint = if name.segments[0] == sym::deprecated
129+
let allowed_targets = allowed_targets.allowed_targets();
130+
let (applied, only) = allowed_targets_applied(allowed_targets, cx.target, cx.features);
131+
let diag = InvalidTarget {
132+
span: cx.attr_span.clone(),
133+
name: cx.attr_path.clone(),
134+
target: cx.target.plural_name(),
135+
only: if only { "only " } else { "" },
136+
applied: DiagArgValue::StrListSepByAnd(applied.into_iter().map(Cow::Owned).collect()),
137+
previously_accepted: matches!(result, AllowedResult::Warn),
138+
};
139+
140+
match result {
141+
AllowedResult::Allowed => unreachable!("Should have early returned above"),
142+
AllowedResult::Warn => {
143+
let lint = if cx.attr_path.segments[0] == sym::deprecated
134144
&& ![
135145
Target::Closure,
136146
Target::Expression,
@@ -145,39 +155,11 @@ impl<'sess> AttributeParser<'sess> {
145155
rustc_session::lint::builtin::UNUSED_ATTRIBUTES
146156
};
147157

148-
let attr_span = cx.attr_span;
149-
let target = cx.target;
150-
cx.emit_lint_with_sess(
151-
lint,
152-
move |dcx, level, _| {
153-
InvalidTargetLint {
154-
name: name.to_string(),
155-
target: target.plural_name(),
156-
only: if only { "only " } else { "" },
157-
applied: DiagArgValue::StrListSepByAnd(
158-
applied.iter().map(|i| Cow::Owned(i.to_string())).collect(),
159-
),
160-
attr_span,
161-
}
162-
.into_diag(dcx, level)
163-
},
164-
attr_span,
165-
);
158+
let attr_span = cx.attr_span.clone();
159+
cx.emit_lint(lint, diag, attr_span);
166160
}
167161
AllowedResult::Error => {
168-
let allowed_targets = allowed_targets.allowed_targets();
169-
let (applied, only) =
170-
allowed_targets_applied(allowed_targets, cx.target, cx.features);
171-
let name = cx.attr_path.clone();
172-
cx.dcx().emit_err(InvalidTarget {
173-
span: cx.attr_span.clone(),
174-
name,
175-
target: cx.target.plural_name(),
176-
only: if only { "only " } else { "" },
177-
applied: DiagArgValue::StrListSepByAnd(
178-
applied.into_iter().map(Cow::Owned).collect(),
179-
),
180-
});
162+
cx.dcx().emit_err(diag);
181163
}
182164
}
183165
}

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ warning: `#[export_name]` attribute cannot be used on macro calls
44
LL | #[export_name = "x"]
55
| ^^^^^^^^^^^^^^^^^^^^
66
|
7-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
87
= help: `#[export_name]` can be applied to functions and statics
8+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
99
note: the lint level is defined here
1010
--> $DIR/attr-on-mac-call.rs:3:9
1111
|
@@ -18,188 +18,188 @@ warning: `#[naked]` attribute cannot be used on macro calls
1818
LL | #[unsafe(naked)]
1919
| ^^^^^^^^^^^^^^^^
2020
|
21-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
2221
= help: `#[naked]` can only be applied to functions
22+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
2323

2424
warning: `#[track_caller]` attribute cannot be used on macro calls
2525
--> $DIR/attr-on-mac-call.rs:12:5
2626
|
2727
LL | #[track_caller]
2828
| ^^^^^^^^^^^^^^^
2929
|
30-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
3130
= help: `#[track_caller]` can only be applied to functions
31+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
3232

3333
warning: `#[used]` attribute cannot be used on macro calls
3434
--> $DIR/attr-on-mac-call.rs:15:5
3535
|
3636
LL | #[used]
3737
| ^^^^^^^
3838
|
39-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
4039
= help: `#[used]` can only be applied to statics
40+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
4141

4242
warning: `#[target_feature]` attribute cannot be used on macro calls
4343
--> $DIR/attr-on-mac-call.rs:18:5
4444
|
4545
LL | #[target_feature(enable = "x")]
4646
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4747
|
48-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
4948
= help: `#[target_feature]` can only be applied to functions
49+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
5050

5151
warning: `#[deprecated]` attribute cannot be used on macro calls
5252
--> $DIR/attr-on-mac-call.rs:21:5
5353
|
5454
LL | #[deprecated]
5555
| ^^^^^^^^^^^^^
5656
|
57-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
5857
= help: `#[deprecated]` can be applied to associated consts, associated types, constants, crates, data types, enum variants, foreign statics, functions, inherent impl blocks, macro defs, modules, statics, struct fields, traits, type aliases, and use statements
58+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
5959

6060
warning: `#[inline]` attribute cannot be used on macro calls
6161
--> $DIR/attr-on-mac-call.rs:24:5
6262
|
6363
LL | #[inline]
6464
| ^^^^^^^^^
6565
|
66-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
6766
= help: `#[inline]` can only be applied to functions
67+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
6868

6969
warning: `#[link_name]` attribute cannot be used on macro calls
7070
--> $DIR/attr-on-mac-call.rs:27:5
7171
|
7272
LL | #[link_name = "x"]
7373
| ^^^^^^^^^^^^^^^^^^
7474
|
75-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
7675
= help: `#[link_name]` can be applied to foreign functions and foreign statics
76+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
7777

7878
warning: `#[link_section]` attribute cannot be used on macro calls
7979
--> $DIR/attr-on-mac-call.rs:30:5
8080
|
8181
LL | #[link_section = "__TEXT,__text"]
8282
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8383
|
84-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
8584
= help: `#[link_section]` can be applied to functions and statics
85+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
8686

8787
warning: `#[link_ordinal]` attribute cannot be used on macro calls
8888
--> $DIR/attr-on-mac-call.rs:33:5
8989
|
9090
LL | #[link_ordinal(42)]
9191
| ^^^^^^^^^^^^^^^^^^^
9292
|
93-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
9493
= help: `#[link_ordinal]` can be applied to foreign functions and foreign statics
94+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
9595

9696
warning: `#[non_exhaustive]` attribute cannot be used on macro calls
9797
--> $DIR/attr-on-mac-call.rs:36:5
9898
|
9999
LL | #[non_exhaustive]
100100
| ^^^^^^^^^^^^^^^^^
101101
|
102-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
103102
= help: `#[non_exhaustive]` can be applied to data types and enum variants
103+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
104104

105105
warning: `#[proc_macro]` attribute cannot be used on macro calls
106106
--> $DIR/attr-on-mac-call.rs:39:5
107107
|
108108
LL | #[proc_macro]
109109
| ^^^^^^^^^^^^^
110110
|
111-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
112111
= help: `#[proc_macro]` can only be applied to functions
112+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
113113

114114
warning: `#[cold]` attribute cannot be used on macro calls
115115
--> $DIR/attr-on-mac-call.rs:42:5
116116
|
117117
LL | #[cold]
118118
| ^^^^^^^
119119
|
120-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
121120
= help: `#[cold]` can only be applied to functions
121+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
122122

123123
warning: `#[no_mangle]` attribute cannot be used on macro calls
124124
--> $DIR/attr-on-mac-call.rs:45:5
125125
|
126126
LL | #[no_mangle]
127127
| ^^^^^^^^^^^^
128128
|
129-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
130129
= help: `#[no_mangle]` can be applied to functions and statics
130+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
131131

132132
warning: `#[deprecated]` attribute cannot be used on macro calls
133133
--> $DIR/attr-on-mac-call.rs:48:5
134134
|
135135
LL | #[deprecated]
136136
| ^^^^^^^^^^^^^
137137
|
138-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
139138
= help: `#[deprecated]` can be applied to associated consts, associated types, constants, crates, data types, enum variants, foreign statics, functions, inherent impl blocks, macro defs, modules, statics, struct fields, traits, type aliases, and use statements
139+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
140140

141141
warning: `#[automatically_derived]` attribute cannot be used on macro calls
142142
--> $DIR/attr-on-mac-call.rs:51:5
143143
|
144144
LL | #[automatically_derived]
145145
| ^^^^^^^^^^^^^^^^^^^^^^^^
146146
|
147-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
148147
= help: `#[automatically_derived]` can only be applied to trait impl blocks
148+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
149149

150150
warning: `#[macro_use]` attribute cannot be used on macro calls
151151
--> $DIR/attr-on-mac-call.rs:54:5
152152
|
153153
LL | #[macro_use]
154154
| ^^^^^^^^^^^^
155155
|
156-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
157156
= help: `#[macro_use]` can be applied to crates, extern crates, and modules
157+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
158158

159159
warning: `#[must_use]` attribute cannot be used on macro calls
160160
--> $DIR/attr-on-mac-call.rs:57:5
161161
|
162162
LL | #[must_use]
163163
| ^^^^^^^^^^^
164164
|
165-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
166165
= help: `#[must_use]` can be applied to data types, functions, and traits
166+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
167167

168168
warning: `#[no_implicit_prelude]` attribute cannot be used on macro calls
169169
--> $DIR/attr-on-mac-call.rs:60:5
170170
|
171171
LL | #[no_implicit_prelude]
172172
| ^^^^^^^^^^^^^^^^^^^^^^
173173
|
174-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
175174
= help: `#[no_implicit_prelude]` can be applied to crates and modules
175+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
176176

177177
warning: `#[path]` attribute cannot be used on macro calls
178178
--> $DIR/attr-on-mac-call.rs:63:5
179179
|
180180
LL | #[path = ""]
181181
| ^^^^^^^^^^^^
182182
|
183-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
184183
= help: `#[path]` can only be applied to modules
184+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
185185

186186
warning: `#[ignore]` attribute cannot be used on macro calls
187187
--> $DIR/attr-on-mac-call.rs:66:5
188188
|
189189
LL | #[ignore]
190190
| ^^^^^^^^^
191191
|
192-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
193192
= help: `#[ignore]` can only be applied to functions
193+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
194194

195195
warning: `#[should_panic]` attribute cannot be used on macro calls
196196
--> $DIR/attr-on-mac-call.rs:69:5
197197
|
198198
LL | #[should_panic]
199199
| ^^^^^^^^^^^^^^^
200200
|
201-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
202201
= help: `#[should_panic]` can only be applied to functions
202+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
203203

204204
warning: 22 warnings emitted
205205

tests/ui/attributes/codegen_attr_on_required_trait_method.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error: `#[cold]` attribute cannot be used on required trait methods
44
LL | #[cold]
55
| ^^^^^^^
66
|
7-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
87
= help: `#[cold]` can be applied to closures, foreign functions, functions, inherent methods, provided trait methods, and trait methods in impl blocks
8+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
99
note: the lint level is defined here
1010
--> $DIR/codegen_attr_on_required_trait_method.rs:1:9
1111
|
@@ -18,17 +18,17 @@ error: `#[link_section]` attribute cannot be used on required trait methods
1818
LL | #[link_section = "__TEXT,__text"]
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2020
|
21-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
2221
= help: `#[link_section]` can be applied to functions, inherent methods, provided trait methods, statics, and trait methods in impl blocks
22+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
2323

2424
error: `#[linkage]` attribute cannot be used on required trait methods
2525
--> $DIR/codegen_attr_on_required_trait_method.rs:14:5
2626
|
2727
LL | #[linkage = "common"]
2828
| ^^^^^^^^^^^^^^^^^^^^^
2929
|
30-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
3130
= help: `#[linkage]` can be applied to foreign functions, foreign statics, functions, inherent methods, provided trait methods, statics, and trait methods in impl blocks
31+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
3232

3333
error: aborting due to 3 previous errors
3434

0 commit comments

Comments
 (0)