Skip to content

Commit b3edf4c

Browse files
authored
Unrolled build for #149662
Rollup merge of #149662 - JonathanBrouwer:lint-rework, r=jdonszelmann Move attribute lints to `rustc_lint` This PR changes two things: - This decouples the `AttributeLintKind` from the `Lint` it is emitted in. `cx.emit_lint` now takes both as an argument, rather than inferring the `Lint` from the `AttributeLintKind`. This is nice because: - It allows us to remove `AttributeLintKind::InvalidMacroExportArguments` - It allows us to move the choice between `USELESS_DEPRECATED` and `UNUSED_ATTRIBUTES` out of the lint emitting code - It allows the next change: - This moves `AttributeLintKind` to `rustc_lint_defs`, and the decorating code to `rustc_lint`. This is nice because: - It allows attribute lint decorating code to access the TypeCtxt, which unblocks #149215 - It might allow most early buffered attribute lints to become dyn lint diagnostics in the future, as in #147634 - It deduplicates `IllFormedAttributeInput` This PR does not change observable output of the compiler, as can be seen by no uitests being affected. r? `@jdonszelmann`
2 parents b4f1098 + 8f59eb0 commit b3edf4c

File tree

27 files changed

+284
-344
lines changed

27 files changed

+284
-344
lines changed

Cargo.lock

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3945,6 +3945,7 @@ dependencies = [
39453945
"rustc_hashes",
39463946
"rustc_hir_id",
39473947
"rustc_index",
3948+
"rustc_lint_defs",
39483949
"rustc_macros",
39493950
"rustc_serialize",
39503951
"rustc_span",
@@ -3962,14 +3963,14 @@ dependencies = [
39623963
"rustc_abi",
39633964
"rustc_arena",
39643965
"rustc_ast",
3965-
"rustc_attr_parsing",
39663966
"rustc_data_structures",
39673967
"rustc_errors",
39683968
"rustc_feature",
39693969
"rustc_fluent_macro",
39703970
"rustc_hir",
39713971
"rustc_index",
39723972
"rustc_infer",
3973+
"rustc_lint",
39733974
"rustc_lint_defs",
39743975
"rustc_macros",
39753976
"rustc_middle",

compiler/rustc_attr_parsing/messages.ftl

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,6 @@ attr_parsing_deprecated_item_suggestion =
1414
.help = add `#![feature(deprecated_suggestion)]` to the crate root
1515
.note = see #94785 for more details
1616
17-
attr_parsing_empty_attribute =
18-
unused attribute
19-
.suggestion = {$valid_without_list ->
20-
[true] remove these parentheses
21-
*[other] remove this attribute
22-
}
23-
.note = {$valid_without_list ->
24-
[true] using `{$attr_path}` with an empty list is equivalent to not using a list at all
25-
*[other] using `{$attr_path}` with an empty list has no effect
26-
}
27-
28-
2917
attr_parsing_empty_confusables =
3018
expected at least one confusable name
3119
attr_parsing_empty_link_name =
@@ -119,19 +107,9 @@ attr_parsing_invalid_repr_hint_no_value =
119107
attr_parsing_invalid_since =
120108
'since' must be a Rust version number, such as "1.31.0"
121109
122-
attr_parsing_invalid_style = {$is_used_as_inner ->
123-
[false] crate-level attribute should be an inner attribute: add an exclamation mark: `#![{$name}]`
124-
*[other] the `#![{$name}]` attribute can only be used at the crate root
125-
}
126-
.note = This attribute does not have an `!`, which means it is applied to this {$target}
127-
128110
attr_parsing_invalid_target = `#[{$name}]` attribute cannot be used on {$target}
129111
.help = `#[{$name}]` can {$only}be applied to {$applied}
130112
.suggestion = remove the attribute
131-
attr_parsing_invalid_target_lint = `#[{$name}]` attribute cannot be used on {$target}
132-
.warn = {-attr_parsing_previously_accepted}
133-
.help = `#[{$name}]` can {$only}be applied to {$applied}
134-
.suggestion = remove the attribute
135113
136114
attr_parsing_limit_invalid =
137115
`limit` must be a non-negative integer
@@ -250,19 +228,10 @@ attr_parsing_unsupported_literal_generic =
250228
attr_parsing_unsupported_literal_suggestion =
251229
consider removing the prefix
252230
253-
attr_parsing_unused_duplicate =
254-
unused attribute
255-
.suggestion = remove this attribute
256-
.note = attribute also specified here
257-
.warn = {-attr_parsing_previously_accepted}
258-
259231
attr_parsing_unused_multiple =
260232
multiple `{$name}` attributes
261233
.suggestion = remove this attribute
262234
.note = attribute also specified here
263235
264-
-attr_parsing_previously_accepted =
265-
this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
266-
267236
attr_parsing_whole_archive_needs_static =
268237
linking modifier `whole-archive` is only compatible with `static` linking kind

compiler/rustc_attr_parsing/src/attributes/inline.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// SingleAttributeParser which is what we have two of here.
44

55
use rustc_hir::attrs::{AttributeKind, InlineAttr};
6+
use rustc_session::lint::builtin::ILL_FORMED_ATTRIBUTE_INPUT;
67

78
use super::prelude::*;
89

@@ -56,9 +57,7 @@ impl<S: Stage> SingleAttributeParser<S> for InlineParser {
5657
}
5758
}
5859
ArgParser::NameValue(_) => {
59-
let suggestions = cx.suggestions();
60-
let span = cx.attr_span;
61-
cx.emit_lint(AttributeLintKind::IllFormedAttributeInput { suggestions }, span);
60+
cx.warn_ill_formed_attribute_input(ILL_FORMED_ATTRIBUTE_INPUT);
6261
return None;
6362
}
6463
}

compiler/rustc_attr_parsing/src/attributes/link_attrs.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use rustc_feature::Features;
22
use rustc_hir::attrs::AttributeKind::{LinkName, LinkOrdinal, LinkSection};
33
use rustc_hir::attrs::*;
44
use rustc_session::Session;
5+
use rustc_session::lint::builtin::ILL_FORMED_ATTRIBUTE_INPUT;
56
use rustc_session::parse::feature_err;
67
use rustc_span::kw;
78
use rustc_target::spec::{Arch, BinaryFormat};
@@ -71,9 +72,7 @@ impl<S: Stage> CombineAttributeParser<S> for LinkParser {
7172
// Specifically `#[link = "dl"]` is accepted with a FCW
7273
// For more information, see https://github.com/rust-lang/rust/pull/143193
7374
ArgParser::NameValue(nv) if nv.value_as_str().is_some_and(|v| v == sym::dl) => {
74-
let suggestions = cx.suggestions();
75-
let span = cx.attr_span;
76-
cx.emit_lint(AttributeLintKind::IllFormedAttributeInput { suggestions }, span);
75+
cx.warn_ill_formed_attribute_input(ILL_FORMED_ATTRIBUTE_INPUT);
7776
return None;
7877
}
7978
_ => {

compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_errors::DiagArgValue;
22
use rustc_hir::attrs::MacroUseArgs;
3+
use rustc_session::lint::builtin::INVALID_MACRO_EXPORT_ARGUMENTS;
34

45
use super::prelude::*;
56
use crate::session_diagnostics::IllFormedAttributeInputLint;
@@ -152,23 +153,13 @@ impl<S: Stage> SingleAttributeParser<S> for MacroExportParser {
152153
ArgParser::NoArgs => false,
153154
ArgParser::List(list) => {
154155
let Some(l) = list.single() else {
155-
let span = cx.attr_span;
156-
let suggestions = cx.suggestions();
157-
cx.emit_lint(
158-
AttributeLintKind::InvalidMacroExportArguments { suggestions },
159-
span,
160-
);
156+
cx.warn_ill_formed_attribute_input(INVALID_MACRO_EXPORT_ARGUMENTS);
161157
return None;
162158
};
163159
match l.meta_item().and_then(|i| i.path().word_sym()) {
164160
Some(sym::local_inner_macros) => true,
165161
_ => {
166-
let span = cx.attr_span;
167-
let suggestions = cx.suggestions();
168-
cx.emit_lint(
169-
AttributeLintKind::InvalidMacroExportArguments { suggestions },
170-
span,
171-
);
162+
cx.warn_ill_formed_attribute_input(INVALID_MACRO_EXPORT_ARGUMENTS);
172163
return None;
173164
}
174165
}

compiler/rustc_attr_parsing/src/attributes/prelude.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ pub(super) use rustc_feature::{AttributeTemplate, template};
44
#[doc(hidden)]
55
pub(super) use rustc_hir::attrs::AttributeKind;
66
#[doc(hidden)]
7-
pub(super) use rustc_hir::lints::AttributeLintKind;
8-
#[doc(hidden)]
97
pub(super) use rustc_hir::{MethodKind, Target};
108
#[doc(hidden)]
119
pub(super) use rustc_span::{DUMMY_SP, Ident, Span, Symbol, sym};

compiler/rustc_attr_parsing/src/attributes/test_attrs.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use rustc_session::lint::builtin::ILL_FORMED_ATTRIBUTE_INPUT;
2+
13
use super::prelude::*;
24

35
pub(crate) struct IgnoreParser;
@@ -20,20 +22,13 @@ impl<S: Stage> SingleAttributeParser<S> for IgnoreParser {
2022
ArgParser::NoArgs => None,
2123
ArgParser::NameValue(name_value) => {
2224
let Some(str_value) = name_value.value_as_str() else {
23-
let suggestions = cx.suggestions();
24-
let span = cx.attr_span;
25-
cx.emit_lint(
26-
AttributeLintKind::IllFormedAttributeInput { suggestions },
27-
span,
28-
);
25+
cx.warn_ill_formed_attribute_input(ILL_FORMED_ATTRIBUTE_INPUT);
2926
return None;
3027
};
3128
Some(str_value)
3229
}
3330
ArgParser::List(_) => {
34-
let suggestions = cx.suggestions();
35-
let span = cx.attr_span;
36-
cx.emit_lint(AttributeLintKind::IllFormedAttributeInput { suggestions }, span);
31+
cx.warn_ill_formed_attribute_input(ILL_FORMED_ATTRIBUTE_INPUT);
3732
return None;
3833
}
3934
},

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_hir::attrs::AttributeKind;
1111
use rustc_hir::lints::{AttributeLint, AttributeLintKind};
1212
use rustc_hir::{AttrPath, CRATE_HIR_ID, HirId};
1313
use rustc_session::Session;
14+
use rustc_session::lint::{Lint, LintId};
1415
use rustc_span::{ErrorGuaranteed, Span, Symbol};
1516

1617
use crate::AttributeParser;
@@ -381,19 +382,20 @@ impl<'f, 'sess: 'f, S: Stage> SharedContext<'f, 'sess, S> {
381382
/// Emit a lint. This method is somewhat special, since lints emitted during attribute parsing
382383
/// must be delayed until after HIR is built. This method will take care of the details of
383384
/// that.
384-
pub(crate) fn emit_lint(&mut self, lint: AttributeLintKind, span: Span) {
385+
pub(crate) fn emit_lint(&mut self, lint: &'static Lint, kind: AttributeLintKind, span: Span) {
385386
if !matches!(
386387
self.stage.should_emit(),
387388
ShouldEmit::ErrorsAndLints | ShouldEmit::EarlyFatal { also_emit_lints: true }
388389
) {
389390
return;
390391
}
391392
let id = self.target_id;
392-
(self.emit_lint)(AttributeLint { id, span, kind: lint });
393+
(self.emit_lint)(AttributeLint { lint_id: LintId::of(lint), id, span, kind });
393394
}
394395

395396
pub(crate) fn warn_unused_duplicate(&mut self, used_span: Span, unused_span: Span) {
396397
self.emit_lint(
398+
rustc_session::lint::builtin::UNUSED_ATTRIBUTES,
397399
AttributeLintKind::UnusedDuplicate {
398400
this: unused_span,
399401
other: used_span,
@@ -409,6 +411,7 @@ impl<'f, 'sess: 'f, S: Stage> SharedContext<'f, 'sess, S> {
409411
unused_span: Span,
410412
) {
411413
self.emit_lint(
414+
rustc_session::lint::builtin::UNUSED_ATTRIBUTES,
412415
AttributeLintKind::UnusedDuplicate {
413416
this: unused_span,
414417
other: used_span,
@@ -632,14 +635,25 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
632635
}
633636

634637
pub(crate) fn warn_empty_attribute(&mut self, span: Span) {
635-
let attr_path = self.attr_path.clone();
638+
let attr_path = self.attr_path.clone().to_string();
636639
let valid_without_list = self.template.word;
637640
self.emit_lint(
641+
rustc_session::lint::builtin::UNUSED_ATTRIBUTES,
638642
AttributeLintKind::EmptyAttribute { first_span: span, attr_path, valid_without_list },
639643
span,
640644
);
641645
}
642646

647+
pub(crate) fn warn_ill_formed_attribute_input(&mut self, lint: &'static Lint) {
648+
let suggestions = self.suggestions();
649+
let span = self.attr_span;
650+
self.emit_lint(
651+
lint,
652+
AttributeLintKind::IllFormedAttributeInput { suggestions, docs: None },
653+
span,
654+
);
655+
}
656+
643657
pub(crate) fn suggestions(&self) -> Vec<String> {
644658
let style = match self.parsed_description {
645659
// If the outer and inner spans are equal, we are parsing an embedded attribute

compiler/rustc_attr_parsing/src/interface.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_hir::attrs::AttributeKind;
88
use rustc_hir::lints::AttributeLint;
99
use rustc_hir::{AttrArgs, AttrItem, AttrPath, Attribute, HashIgnoredAttrId, Target};
1010
use rustc_session::Session;
11+
use rustc_session::lint::BuiltinLintDiag;
1112
use rustc_span::{DUMMY_SP, Span, Symbol, sym};
1213

1314
use crate::context::{AcceptContext, FinalizeContext, SharedContext, Stage};
@@ -115,7 +116,12 @@ impl<'sess> AttributeParser<'sess, Early> {
115116
OmitDoc::Skip,
116117
std::convert::identity,
117118
|lint| {
118-
crate::lints::emit_attribute_lint(&lint, sess);
119+
sess.psess.buffer_lint(
120+
lint.lint_id.lint,
121+
lint.span,
122+
lint.id,
123+
BuiltinLintDiag::AttributeLint(lint.kind),
124+
)
119125
},
120126
)
121127
}
@@ -183,8 +189,13 @@ impl<'sess> AttributeParser<'sess, Early> {
183189
sess,
184190
stage: Early { emit_errors },
185191
};
186-
let mut emit_lint = |lint| {
187-
crate::lints::emit_attribute_lint(&lint, sess);
192+
let mut emit_lint = |lint: AttributeLint<NodeId>| {
193+
sess.psess.buffer_lint(
194+
lint.lint_id.lint,
195+
lint.span,
196+
lint.id,
197+
BuiltinLintDiag::AttributeLint(lint.kind),
198+
)
188199
};
189200
if let Some(safety) = attr_safety {
190201
parser.check_attribute_safety(

compiler/rustc_attr_parsing/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ mod interface;
9797
/// like lists or name-value pairs.
9898
pub mod parser;
9999

100-
mod lints;
101100
mod safety;
102101
mod session_diagnostics;
103102
mod target_checking;
@@ -111,7 +110,6 @@ pub use attributes::cfg_select::*;
111110
pub use attributes::util::{is_builtin_attr, is_doc_alias_attrs_contain_symbol, parse_version};
112111
pub use context::{Early, Late, OmitDoc, ShouldEmit};
113112
pub use interface::AttributeParser;
114-
pub use lints::emit_attribute_lint;
115113
pub use session_diagnostics::ParsedDescription;
116114

117115
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }

0 commit comments

Comments
 (0)