From cd7d40d97507d75b4d0341e19a24edfaf654c6c5 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 15 Jan 2026 15:06:29 +0100 Subject: [PATCH] Update `rustc_attr_parsing::SharedContext::target` type from `Option` to `Target` --- compiler/rustc_attr_parsing/src/attributes/cfg.rs | 3 ++- compiler/rustc_attr_parsing/src/attributes/cfg_select.rs | 4 +++- compiler/rustc_attr_parsing/src/attributes/doc.rs | 4 ++-- compiler/rustc_attr_parsing/src/context.rs | 2 +- compiler/rustc_attr_parsing/src/interface.rs | 9 ++++++--- compiler/rustc_builtin_macros/src/cfg.rs | 4 +++- compiler/rustc_expand/src/config.rs | 3 +++ compiler/rustc_expand/src/expand.rs | 2 ++ 8 files changed, 22 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_attr_parsing/src/attributes/cfg.rs b/compiler/rustc_attr_parsing/src/attributes/cfg.rs index ccf0a394afd0..3e430cf59485 100644 --- a/compiler/rustc_attr_parsing/src/attributes/cfg.rs +++ b/compiler/rustc_attr_parsing/src/attributes/cfg.rs @@ -9,7 +9,7 @@ use rustc_feature::{ }; use rustc_hir::attrs::CfgEntry; use rustc_hir::lints::AttributeLintKind; -use rustc_hir::{AttrPath, RustcVersion}; +use rustc_hir::{AttrPath, RustcVersion, Target}; use rustc_parse::parser::{ForceCollect, Parser}; use rustc_parse::{exp, parse_in}; use rustc_session::Session; @@ -374,6 +374,7 @@ fn parse_cfg_attr_internal<'a>( ParsedDescription::Attribute, pred_span, CRATE_NODE_ID, + Target::Crate, features, ShouldEmit::ErrorsAndLints, &meta, diff --git a/compiler/rustc_attr_parsing/src/attributes/cfg_select.rs b/compiler/rustc_attr_parsing/src/attributes/cfg_select.rs index 24b989e22a2b..e80084021a84 100644 --- a/compiler/rustc_attr_parsing/src/attributes/cfg_select.rs +++ b/compiler/rustc_attr_parsing/src/attributes/cfg_select.rs @@ -2,8 +2,8 @@ use rustc_ast::token::Token; use rustc_ast::tokenstream::TokenStream; use rustc_ast::{AttrStyle, NodeId, token}; use rustc_feature::{AttributeTemplate, Features}; -use rustc_hir::AttrPath; use rustc_hir::attrs::CfgEntry; +use rustc_hir::{AttrPath, Target}; use rustc_parse::exp; use rustc_parse::parser::Parser; use rustc_session::Session; @@ -91,6 +91,8 @@ pub fn parse_cfg_select( ParsedDescription::Macro, cfg_span, lint_node_id, + // Doesn't matter what the target actually is here. + Target::Crate, features, ShouldEmit::ErrorsAndLints, &meta, diff --git a/compiler/rustc_attr_parsing/src/attributes/doc.rs b/compiler/rustc_attr_parsing/src/attributes/doc.rs index 6cc4ac35eadb..99825d93216f 100644 --- a/compiler/rustc_attr_parsing/src/attributes/doc.rs +++ b/compiler/rustc_attr_parsing/src/attributes/doc.rs @@ -50,7 +50,7 @@ fn check_attr_not_crate_level( span: Span, attr_name: Symbol, ) -> bool { - if cx.shared.target.is_some_and(|target| target == Target::Crate) { + if cx.shared.target == Target::Crate { cx.emit_err(DocAttrNotCrateLevel { span, attr_name }); return false; } @@ -59,7 +59,7 @@ fn check_attr_not_crate_level( /// Checks that an attribute is used at the crate level. Returns `true` if valid. fn check_attr_crate_level(cx: &mut AcceptContext<'_, '_, S>, span: Span) -> bool { - if cx.shared.target.is_some_and(|target| target != Target::Crate) { + if cx.shared.target != Target::Crate { cx.emit_lint( rustc_session::lint::builtin::INVALID_DOC_ATTRIBUTES, AttributeLintKind::AttrCrateLevelOnly, diff --git a/compiler/rustc_attr_parsing/src/context.rs b/compiler/rustc_attr_parsing/src/context.rs index 8305d027d13c..f885151330a3 100644 --- a/compiler/rustc_attr_parsing/src/context.rs +++ b/compiler/rustc_attr_parsing/src/context.rs @@ -665,7 +665,7 @@ pub struct SharedContext<'p, 'sess, S: Stage> { pub(crate) target_span: Span, /// The id ([`NodeId`] if `S` is `Early`, [`HirId`] if `S` is `Late`) of the syntactical component this attribute was applied to pub(crate) target_id: S::Id, - pub(crate) target: Option, + pub(crate) target: rustc_hir::Target, pub(crate) emit_lint: &'p mut dyn FnMut(AttributeLint), } diff --git a/compiler/rustc_attr_parsing/src/interface.rs b/compiler/rustc_attr_parsing/src/interface.rs index b7137c60e63a..8f2c36fa8c9e 100644 --- a/compiler/rustc_attr_parsing/src/interface.rs +++ b/compiler/rustc_attr_parsing/src/interface.rs @@ -135,6 +135,7 @@ impl<'sess> AttributeParser<'sess, Early> { attr: &ast::Attribute, target_span: Span, target_node_id: NodeId, + target: Target, features: Option<&'sess Features>, emit_errors: ShouldEmit, parse_fn: fn(cx: &mut AcceptContext<'_, '_, Early>, item: &ArgParser) -> Option, @@ -163,6 +164,7 @@ impl<'sess> AttributeParser<'sess, Early> { ParsedDescription::Attribute, target_span, target_node_id, + target, features, emit_errors, &args, @@ -183,6 +185,7 @@ impl<'sess> AttributeParser<'sess, Early> { parsed_description: ParsedDescription, target_span: Span, target_node_id: NodeId, + target: Target, features: Option<&'sess Features>, emit_errors: ShouldEmit, args: &I, @@ -218,7 +221,7 @@ impl<'sess> AttributeParser<'sess, Early> { cx: &mut parser, target_span, target_id: target_node_id, - target: None, + target, emit_lint: &mut emit_lint, }, attr_span, @@ -379,7 +382,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> { cx: self, target_span, target_id, - target: Some(target), + target, emit_lint: &mut emit_lint, }, attr_span, @@ -431,7 +434,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> { cx: self, target_span, target_id, - target: Some(target), + target, emit_lint: &mut emit_lint, }, all_attrs: &attr_paths, diff --git a/compiler/rustc_builtin_macros/src/cfg.rs b/compiler/rustc_builtin_macros/src/cfg.rs index 557daa94b98e..be1ce5a06d5e 100644 --- a/compiler/rustc_builtin_macros/src/cfg.rs +++ b/compiler/rustc_builtin_macros/src/cfg.rs @@ -10,8 +10,8 @@ use rustc_attr_parsing::{ AttributeParser, CFG_TEMPLATE, ParsedDescription, ShouldEmit, parse_cfg_entry, }; use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacEager, MacroExpanderResult}; -use rustc_hir::AttrPath; use rustc_hir::attrs::CfgEntry; +use rustc_hir::{AttrPath, Target}; use rustc_parse::exp; use rustc_span::{ErrorGuaranteed, Span, sym}; @@ -52,6 +52,8 @@ fn parse_cfg(cx: &ExtCtxt<'_>, span: Span, tts: TokenStream) -> Result StripUnconfigured<'a> { attr, attr.span, self.lint_node_id, + // Doesn't matter what the target actually is here. + Target::Crate, self.features, emit_errors, parse_cfg, diff --git a/compiler/rustc_expand/src/expand.rs b/compiler/rustc_expand/src/expand.rs index c130d9f59940..fabe1f5a8c5d 100644 --- a/compiler/rustc_expand/src/expand.rs +++ b/compiler/rustc_expand/src/expand.rs @@ -2218,6 +2218,8 @@ impl<'a, 'b> InvocationCollector<'a, 'b> { &attr, attr.span, self.cfg().lint_node_id, + // Target doesn't matter for `cfg` parsing. + Target::Crate, self.cfg().features, ShouldEmit::ErrorsAndLints, parse_cfg,