Skip to content

Commit b08c11d

Browse files
committed
Auto merge of #151162 - GuillaumeGomez:cleanup-attr-parsing, r=JonathanBrouwer
Clean up `rustc_attr_parsing` Follow-up of #150934. It removes the `Option<>` wrapping for `SharedContext::target` field and completely removed the `target_id` field. Considering this type contains a closure and never updates its `target_id` field, there is no need to keep it around, it can be used directly in the lint emitter. r? @JonathanBrouwer
2 parents c40c51f + 8205792 commit b08c11d

11 files changed

Lines changed: 57 additions & 61 deletions

File tree

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use rustc_hir::attrs::AttributeKind;
5151
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5252
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
5353
use rustc_hir::definitions::{DefPathData, DisambiguatorState};
54-
use rustc_hir::lints::DelayedLint;
54+
use rustc_hir::lints::{AttributeLint, DelayedLint};
5555
use rustc_hir::{
5656
self as hir, AngleBrackets, ConstArg, GenericArg, HirId, ItemLocalMap, LifetimeSource,
5757
LifetimeSyntax, ParamName, Target, TraitCandidate, find_attr,
@@ -1022,12 +1022,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10221022
self.attribute_parser.parse_attribute_list(
10231023
attrs,
10241024
target_span,
1025-
target_hir_id,
10261025
target,
10271026
OmitDoc::Lower,
10281027
|s| l.lower(s),
1029-
|l| {
1030-
self.delayed_lints.push(DelayedLint::AttributeParsing(l));
1028+
|lint_id, span, kind| {
1029+
self.delayed_lints.push(DelayedLint::AttributeParsing(AttributeLint {
1030+
lint_id,
1031+
id: target_hir_id,
1032+
span,
1033+
kind,
1034+
}));
10311035
},
10321036
)
10331037
}

compiler/rustc_attr_parsing/src/attributes/cfg.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_feature::{
99
};
1010
use rustc_hir::attrs::CfgEntry;
1111
use rustc_hir::lints::AttributeLintKind;
12-
use rustc_hir::{AttrPath, RustcVersion};
12+
use rustc_hir::{AttrPath, RustcVersion, Target};
1313
use rustc_parse::parser::{ForceCollect, Parser};
1414
use rustc_parse::{exp, parse_in};
1515
use rustc_session::Session;
@@ -373,6 +373,7 @@ fn parse_cfg_attr_internal<'a>(
373373
ParsedDescription::Attribute,
374374
pred_span,
375375
CRATE_NODE_ID,
376+
Target::Crate,
376377
features,
377378
ShouldEmit::ErrorsAndLints,
378379
&meta,

compiler/rustc_attr_parsing/src/attributes/cfg_select.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use rustc_ast::token::Token;
22
use rustc_ast::tokenstream::TokenStream;
33
use rustc_ast::{AttrStyle, NodeId, token};
44
use rustc_feature::{AttributeTemplate, Features};
5-
use rustc_hir::AttrPath;
65
use rustc_hir::attrs::CfgEntry;
6+
use rustc_hir::{AttrPath, Target};
77
use rustc_parse::exp;
88
use rustc_parse::parser::Parser;
99
use rustc_session::Session;
@@ -91,6 +91,8 @@ pub fn parse_cfg_select(
9191
ParsedDescription::Macro,
9292
cfg_span,
9393
lint_node_id,
94+
// Doesn't matter what the target actually is here.
95+
Target::Crate,
9496
features,
9597
ShouldEmit::ErrorsAndLints,
9698
&meta,

compiler/rustc_attr_parsing/src/attributes/doc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn check_attr_not_crate_level<S: Stage>(
5050
span: Span,
5151
attr_name: Symbol,
5252
) -> bool {
53-
if cx.shared.target.is_some_and(|target| target == Target::Crate) {
53+
if cx.shared.target == Target::Crate {
5454
cx.emit_err(DocAttrNotCrateLevel { span, attr_name });
5555
return false;
5656
}
@@ -59,7 +59,7 @@ fn check_attr_not_crate_level<S: Stage>(
5959

6060
/// Checks that an attribute is used at the crate level. Returns `true` if valid.
6161
fn check_attr_crate_level<S: Stage>(cx: &mut AcceptContext<'_, '_, S>, span: Span) -> bool {
62-
if cx.shared.target.is_some_and(|target| target != Target::Crate) {
62+
if cx.shared.target != Target::Crate {
6363
cx.emit_lint(
6464
rustc_session::lint::builtin::INVALID_DOC_ATTRIBUTES,
6565
AttributeLintKind::AttrCrateLevelOnly,

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_ast::{AttrStyle, MetaItemLit, NodeId};
88
use rustc_errors::{Diag, Diagnostic, Level};
99
use rustc_feature::{AttrSuggestionStyle, AttributeTemplate};
1010
use rustc_hir::attrs::AttributeKind;
11-
use rustc_hir::lints::{AttributeLint, AttributeLintKind};
11+
use rustc_hir::lints::AttributeLintKind;
1212
use rustc_hir::{AttrPath, HirId};
1313
use rustc_session::Session;
1414
use rustc_session::lint::{Lint, LintId};
@@ -419,8 +419,7 @@ impl<'f, 'sess: 'f, S: Stage> SharedContext<'f, 'sess, S> {
419419
) {
420420
return;
421421
}
422-
let id = self.target_id;
423-
(self.emit_lint)(AttributeLint { lint_id: LintId::of(lint), id, span, kind });
422+
(self.emit_lint)(LintId::of(lint), span, kind);
424423
}
425424

426425
pub(crate) fn warn_unused_duplicate(&mut self, used_span: Span, unused_span: Span) {
@@ -665,11 +664,11 @@ pub struct SharedContext<'p, 'sess, S: Stage> {
665664
pub(crate) cx: &'p mut AttributeParser<'sess, S>,
666665
/// The span of the syntactical component this attribute was applied to
667666
pub(crate) target_span: Span,
668-
/// The id ([`NodeId`] if `S` is `Early`, [`HirId`] if `S` is `Late`) of the syntactical component this attribute was applied to
669-
pub(crate) target_id: S::Id,
670-
pub(crate) target: Option<rustc_hir::Target>,
667+
pub(crate) target: rustc_hir::Target,
671668

672-
pub(crate) emit_lint: &'p mut dyn FnMut(AttributeLint<S::Id>),
669+
/// The second argument of the closure is a [`NodeId`] if `S` is `Early` and a [`HirId`] if `S`
670+
/// is `Late` and is the ID of the syntactical component this attribute was applied to.
671+
pub(crate) emit_lint: &'p mut dyn FnMut(LintId, Span, AttributeLintKind),
673672
}
674673

675674
/// Context given to every attribute parser during finalization.

compiler/rustc_attr_parsing/src/interface.rs

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use rustc_ast::{AttrItemKind, AttrStyle, NodeId, Safety};
66
use rustc_errors::DiagCtxtHandle;
77
use rustc_feature::{AttributeTemplate, Features};
88
use rustc_hir::attrs::AttributeKind;
9-
use rustc_hir::lints::AttributeLint;
9+
use rustc_hir::lints::AttributeLintKind;
1010
use rustc_hir::{AttrArgs, AttrItem, AttrPath, Attribute, HashIgnoredAttrId, Target};
1111
use rustc_session::Session;
12-
use rustc_session::lint::BuiltinLintDiag;
12+
use rustc_session::lint::{BuiltinLintDiag, LintId};
1313
use rustc_span::{DUMMY_SP, Span, Symbol, sym};
1414

1515
use crate::context::{AcceptContext, FinalizeContext, SharedContext, Stage};
@@ -113,16 +113,15 @@ impl<'sess> AttributeParser<'sess, Early> {
113113
p.parse_attribute_list(
114114
attrs,
115115
target_span,
116-
target_node_id,
117116
target,
118117
OmitDoc::Skip,
119118
std::convert::identity,
120-
|lint| {
119+
|lint_id, span, kind| {
121120
sess.psess.buffer_lint(
122-
lint.lint_id.lint,
123-
lint.span,
124-
lint.id,
125-
BuiltinLintDiag::AttributeLint(lint.kind),
121+
lint_id.lint,
122+
span,
123+
target_node_id,
124+
BuiltinLintDiag::AttributeLint(kind),
126125
)
127126
},
128127
)
@@ -135,6 +134,7 @@ impl<'sess> AttributeParser<'sess, Early> {
135134
attr: &ast::Attribute,
136135
target_span: Span,
137136
target_node_id: NodeId,
137+
target: Target,
138138
features: Option<&'sess Features>,
139139
emit_errors: ShouldEmit,
140140
parse_fn: fn(cx: &mut AcceptContext<'_, '_, Early>, item: &ArgParser) -> Option<T>,
@@ -163,6 +163,7 @@ impl<'sess> AttributeParser<'sess, Early> {
163163
ParsedDescription::Attribute,
164164
target_span,
165165
target_node_id,
166+
target,
166167
features,
167168
emit_errors,
168169
&args,
@@ -183,6 +184,7 @@ impl<'sess> AttributeParser<'sess, Early> {
183184
parsed_description: ParsedDescription,
184185
target_span: Span,
185186
target_node_id: NodeId,
187+
target: Target,
186188
features: Option<&'sess Features>,
187189
emit_errors: ShouldEmit,
188190
args: &I,
@@ -196,29 +198,22 @@ impl<'sess> AttributeParser<'sess, Early> {
196198
sess,
197199
stage: Early { emit_errors },
198200
};
199-
let mut emit_lint = |lint: AttributeLint<NodeId>| {
201+
let mut emit_lint = |lint_id: LintId, span: Span, kind: AttributeLintKind| {
200202
sess.psess.buffer_lint(
201-
lint.lint_id.lint,
202-
lint.span,
203-
lint.id,
204-
BuiltinLintDiag::AttributeLint(lint.kind),
203+
lint_id.lint,
204+
span,
205+
target_node_id,
206+
BuiltinLintDiag::AttributeLint(kind),
205207
)
206208
};
207209
if let Some(safety) = attr_safety {
208-
parser.check_attribute_safety(
209-
&attr_path,
210-
inner_span,
211-
safety,
212-
&mut emit_lint,
213-
target_node_id,
214-
)
210+
parser.check_attribute_safety(&attr_path, inner_span, safety, &mut emit_lint)
215211
}
216212
let mut cx: AcceptContext<'_, 'sess, Early> = AcceptContext {
217213
shared: SharedContext {
218214
cx: &mut parser,
219215
target_span,
220-
target_id: target_node_id,
221-
target: None,
216+
target,
222217
emit_lint: &mut emit_lint,
223218
},
224219
attr_span,
@@ -266,11 +261,10 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
266261
&mut self,
267262
attrs: &[ast::Attribute],
268263
target_span: Span,
269-
target_id: S::Id,
270264
target: Target,
271265
omit_doc: OmitDoc,
272266
lower_span: impl Copy + Fn(Span) -> Span,
273-
mut emit_lint: impl FnMut(AttributeLint<S::Id>),
267+
mut emit_lint: impl FnMut(LintId, Span, AttributeLintKind),
274268
) -> Vec<Attribute> {
275269
let mut attributes = Vec::new();
276270
let mut attr_paths: Vec<RefPathParser<'_>> = Vec::new();
@@ -326,7 +320,6 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
326320
lower_span(n.item.span()),
327321
n.item.unsafety,
328322
&mut emit_lint,
329-
target_id,
330323
);
331324

332325
let parts =
@@ -378,8 +371,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
378371
shared: SharedContext {
379372
cx: self,
380373
target_span,
381-
target_id,
382-
target: Some(target),
374+
target,
383375
emit_lint: &mut emit_lint,
384376
},
385377
attr_span,
@@ -427,13 +419,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
427419
early_parsed_state.finalize_early_parsed_attributes(&mut attributes);
428420
for f in &S::parsers().finalizers {
429421
if let Some(attr) = f(&mut FinalizeContext {
430-
shared: SharedContext {
431-
cx: self,
432-
target_span,
433-
target_id,
434-
target: Some(target),
435-
emit_lint: &mut emit_lint,
436-
},
422+
shared: SharedContext { cx: self, target_span, target, emit_lint: &mut emit_lint },
437423
all_attrs: &attr_paths,
438424
}) {
439425
attributes.push(Attribute::Parsed(attr));

compiler/rustc_attr_parsing/src/safety.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_ast::Safety;
22
use rustc_feature::{AttributeSafety, BUILTIN_ATTRIBUTE_MAP};
33
use rustc_hir::AttrPath;
4-
use rustc_hir::lints::{AttributeLint, AttributeLintKind};
4+
use rustc_hir::lints::AttributeLintKind;
55
use rustc_session::lint::LintId;
66
use rustc_session::lint::builtin::UNSAFE_ATTR_OUTSIDE_UNSAFE;
77
use rustc_span::Span;
@@ -15,8 +15,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
1515
attr_path: &AttrPath,
1616
attr_span: Span,
1717
attr_safety: Safety,
18-
emit_lint: &mut impl FnMut(AttributeLint<S::Id>),
19-
target_id: S::Id,
18+
emit_lint: &mut impl FnMut(LintId, Span, AttributeLintKind),
2019
) {
2120
if matches!(self.stage.should_emit(), ShouldEmit::Nothing) {
2221
return;
@@ -82,16 +81,15 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
8281
},
8382
);
8483
} else {
85-
emit_lint(AttributeLint {
86-
lint_id: LintId::of(UNSAFE_ATTR_OUTSIDE_UNSAFE),
87-
id: target_id,
88-
span: path_span,
89-
kind: AttributeLintKind::UnsafeAttrOutsideUnsafe {
84+
emit_lint(
85+
LintId::of(UNSAFE_ATTR_OUTSIDE_UNSAFE),
86+
path_span,
87+
AttributeLintKind::UnsafeAttrOutsideUnsafe {
9088
attribute_name_span: path_span,
9189
sugg_spans: not_from_proc_macro
9290
.then(|| (diag_span.shrink_to_lo(), diag_span.shrink_to_hi())),
9391
},
94-
})
92+
)
9593
}
9694
}
9795

compiler/rustc_builtin_macros/src/cfg.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use rustc_attr_parsing::{
1010
AttributeParser, CFG_TEMPLATE, ParsedDescription, ShouldEmit, parse_cfg_entry,
1111
};
1212
use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacEager, MacroExpanderResult};
13-
use rustc_hir::AttrPath;
1413
use rustc_hir::attrs::CfgEntry;
14+
use rustc_hir::{AttrPath, Target};
1515
use rustc_parse::exp;
1616
use rustc_span::{ErrorGuaranteed, Span, sym};
1717

@@ -52,6 +52,8 @@ fn parse_cfg(cx: &ExtCtxt<'_>, span: Span, tts: TokenStream) -> Result<CfgEntry,
5252
ParsedDescription::Macro,
5353
span,
5454
cx.current_expansion.lint_node_id,
55+
// Doesn't matter what the target actually is here.
56+
Target::Crate,
5557
Some(cx.ecfg.features),
5658
ShouldEmit::ErrorsAndLints,
5759
&meta,

compiler/rustc_expand/src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_feature::{
1919
ACCEPTED_LANG_FEATURES, EnabledLangFeature, EnabledLibFeature, Features, REMOVED_LANG_FEATURES,
2020
UNSTABLE_LANG_FEATURES,
2121
};
22+
use rustc_hir::Target;
2223
use rustc_session::Session;
2324
use rustc_session::parse::feature_err;
2425
use rustc_span::{STDLIB_STABLE_CRATES, Span, Symbol, sym};
@@ -403,6 +404,8 @@ impl<'a> StripUnconfigured<'a> {
403404
attr,
404405
attr.span,
405406
self.lint_node_id,
407+
// Doesn't matter what the target actually is here.
408+
Target::Crate,
406409
self.features,
407410
emit_errors,
408411
parse_cfg,

compiler/rustc_expand/src/expand.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2218,6 +2218,8 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
22182218
&attr,
22192219
attr.span,
22202220
self.cfg().lint_node_id,
2221+
// Target doesn't matter for `cfg` parsing.
2222+
Target::Crate,
22212223
self.cfg().features,
22222224
ShouldEmit::ErrorsAndLints,
22232225
parse_cfg,

0 commit comments

Comments
 (0)