From cb7bb90a9de5d49981d708eb803944b9789ec01d Mon Sep 17 00:00:00 2001 From: Richard Tjokroutomo Date: Wed, 9 Sep 2026 00:17:36 +0800 Subject: [PATCH 01/13] move check_naked() from check_attr.rs to codegen_attrs.rs Signed-off-by: Richard Tjokroutomo --- compiler/rustc_ast/src/ast.rs | 19 ++++++++ .../src/attributes/codegen_attrs.rs | 46 +++++++++++++++++++ compiler/rustc_passes/src/check_attr.rs | 2 +- 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 6a86e9af71826..47a25e8fd704d 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -3781,6 +3781,25 @@ impl Extern { Extern::Implicit(span) | Extern::Explicit(_, span) => Some(span), } } + + /// An ABI "like Rust" + /// + /// These ABIs are fully controlled by the Rust compiler, which means they + /// - support unwinding with `-Cpanic=unwind`, unlike `extern "C"` + /// - often diverge from the C ABI + /// - are subject to change between compiler versions + pub fn is_rustic_abi(self) -> bool { + match self { + Extern::None => true, + Extern::Implicit(_) => false, + Extern::Explicit(name, _) => { + matches!( + name.symbol_unescaped.as_str(), + "Rust" | "rust-call" | "rust-cold" | "rust-preserve-none" | "rust-tail" + ) + } + } + } } /// A function header. diff --git a/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs b/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs index 2553829f8c5ce..b27f3b1c63626 100644 --- a/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs +++ b/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs @@ -1,3 +1,4 @@ +use rustc_ast::ItemKind; use rustc_attr_ir::{ CoverageAttrKind, InstrumentFnAttr, OptimizeAttr, RtsanSetting, UsedBy, find_attr, }; @@ -8,6 +9,7 @@ use rustc_structures::SanitizerSet; use super::prelude::*; use crate::attributes::AttributeSafety; +use crate::context::FinalizeCheckFn; use crate::diagnostics::{ EmptyExportName, EmptySection, NakedFunctionIncompatibleAttribute, NullOnExport, NullOnObjcClass, NullOnObjcSelector, NullOnSection, ObjcClassExpectedStringLiteral, @@ -327,6 +329,50 @@ impl AttributeParser for NakedParser { Some(AttributeKind::Naked(span)) } + + fn deferred_finalize_check(&self) -> Option<(FinalizeCheckFn, Span)> { + Some(( + |cx, _| match cx.target { + Target::Fn + | Target::Method( + MethodKind::Trait { body: true } | MethodKind::TraitImpl | MethodKind::Inherent, + ) => { + let Some(item) = cx.target_item else { + return; + }; + + let ItemKind::Fn(fn_item) = &item.kind else { + return; + }; + + let fn_sig = &fn_item.sig; + let abi = fn_sig.header.ext; + + if abi.is_rustic_abi() && !cx.features().naked_functions_rustic_abi() { + let abi_type = match abi { + rustc_ast::ast::Extern::None => "Rust".into(), + rustc_ast::ast::Extern::Explicit(name, _) => { + name.symbol_unescaped.to_string() + } + rustc_ast::ast::Extern::Implicit(_) => unreachable!(), + }; + feature_err( + cx.sess(), + sym::naked_functions_rustic_abi, + fn_sig.span, + format!( + "`#[naked]` is currently unstable on `extern \"{}\"` functions", + abi_type + ), + ) + .emit(); + } + } + _ => {} + }, + self.span?, + )) + } } pub(crate) struct TrackCallerParser; diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 3eb6892177b05..c70728c930fe7 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -38,7 +38,6 @@ use rustc_middle::query::Providers; use rustc_middle::traits::ObligationCause; use rustc_middle::ty::error::{ExpectedFound, TypeError}; use rustc_middle::ty::{self, TyCtxt, TypingMode, Unnormalized}; -use rustc_session::diagnostics::feature_err; use rustc_span::edition::Edition; use rustc_span::{DUMMY_SP, Ident, Span, Symbol, bug, kw, span_bug, sym}; use rustc_structures::CrateType; @@ -272,6 +271,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> { AttributeKind::MoveSizeLimit { .. } => (), AttributeKind::MustNotSupend { .. } => (), AttributeKind::MustUse { .. } => (), + AttributeKind::Naked(..) => (), AttributeKind::NeedsAllocator => (), AttributeKind::NeedsPanicRuntime => (), AttributeKind::NoBuiltins => (), From ba530aa450a549c932e0b0ffc15bde1ad0709c19 Mon Sep 17 00:00:00 2001 From: Richard Tjokroutomo Date: Fri, 11 Sep 2026 21:45:10 +0800 Subject: [PATCH 02/13] also consider associate item Signed-off-by: Richard Tjokroutomo --- compiler/rustc_ast_lowering/src/block.rs | 2 +- compiler/rustc_ast_lowering/src/contract.rs | 2 +- compiler/rustc_ast_lowering/src/expr.rs | 21 ++++++--- .../rustc_ast_lowering/src/expr/closure.rs | 2 +- compiler/rustc_ast_lowering/src/item.rs | 20 ++++++--- compiler/rustc_ast_lowering/src/lib.rs | 20 ++++++--- compiler/rustc_ast_lowering/src/pat.rs | 2 +- .../src/attributes/codegen_attrs.rs | 45 ++++++++++++++++--- compiler/rustc_attr_parsing/src/context.rs | 4 ++ compiler/rustc_attr_parsing/src/interface.rs | 3 ++ compiler/rustc_passes/src/check_attr.rs | 27 ----------- compiler/rustc_resolve/src/def_collector.rs | 1 + 12 files changed, 93 insertions(+), 56 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/block.rs b/compiler/rustc_ast_lowering/src/block.rs index b2cb1718b86a3..4e1b36c01f5af 100644 --- a/compiler/rustc_ast_lowering/src/block.rs +++ b/compiler/rustc_ast_lowering/src/block.rs @@ -104,7 +104,7 @@ impl<'hir> LoweringContext<'_, 'hir> { }; let span = self.lower_span(l.span); let source = hir::LocalSource::Normal; - self.lower_attrs(hir_id, &l.attrs, l.span, Target::Statement); + self.lower_attrs(hir_id, &l.attrs, l.span, Target::Statement, None); self.arena.alloc(hir::LetStmt { hir_id, super_, ty, pat, init, els, span, source }) } diff --git a/compiler/rustc_ast_lowering/src/contract.rs b/compiler/rustc_ast_lowering/src/contract.rs index cbfeda33040e7..2958b65131502 100644 --- a/compiler/rustc_ast_lowering/src/contract.rs +++ b/compiler/rustc_ast_lowering/src/contract.rs @@ -352,7 +352,7 @@ impl<'hir> LoweringContext<'_, 'hir> { )); let attrs: rustc_ast::AttrVec = thin_vec![self.unreachable_code_attr(span)]; - self.lower_attrs(contract_check.hir_id, &attrs, span, Target::Expression); + self.lower_attrs(contract_check.hir_id, &attrs, span, Target::Expression, None); let ret_block = self.block_all(span, arena_vec![self; ret_stmt], Some(contract_check)); self.arena.alloc(self.expr_block(self.arena.alloc(ret_block))) diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index f4bfec24e3b1b..272cc0600b2a1 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -231,7 +231,14 @@ impl<'hir> LoweringContext<'_, 'hir> { let old_attrs = self.curr_owner.attrs.get(&ex.hir_id.local_id).copied().unwrap_or(&[]); let new_attrs = self - .lower_attrs_vec(&e.attrs, e.span, ex.hir_id, Target::from_expr(e), None) + .lower_attrs_vec( + &e.attrs, + e.span, + ex.hir_id, + Target::from_expr(e), + None, + None, + ) .into_iter() .chain(old_attrs.iter().cloned()); let new_attrs = &*self.arena.alloc_from_iter(new_attrs); @@ -255,7 +262,7 @@ impl<'hir> LoweringContext<'_, 'hir> { } let expr_hir_id = self.lower_node_id(e.id); - self.lower_attrs(expr_hir_id, &e.attrs, e.span, Target::from_expr(e)); + self.lower_attrs(expr_hir_id, &e.attrs, e.span, Target::from_expr(e), None); let kind = match &e.kind { ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)), @@ -795,7 +802,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let guard = arm.guard.as_ref().map(|guard| self.lower_expr(&guard.cond)); let hir_id = self.next_id(); let span = self.lower_span(arm.span); - self.lower_attrs(hir_id, &arm.attrs, arm.span, Target::Arm); + self.lower_attrs(hir_id, &arm.attrs, arm.span, Target::Arm, None); let is_never_pattern = pat.is_never_pattern(); // We need to lower the body even if it's unneeded for never pattern in match, // ensure that we can get HirId for DefId if need (issue #137708). @@ -1659,7 +1666,7 @@ impl<'hir> LoweringContext<'_, 'hir> { fn lower_expr_field(&mut self, f: &ExprField) -> hir::ExprField<'hir> { let hir_id = self.lower_node_id(f.id); - self.lower_attrs(hir_id, &f.attrs, f.span, Target::ExprField); + self.lower_attrs(hir_id, &f.attrs, f.span, Target::ExprField, None); hir::ExprField { hir_id, ident: self.lower_ident(f.ident), @@ -1926,7 +1933,7 @@ impl<'hir> LoweringContext<'_, 'hir> { // // Also, add the attributes to the outer returned expr node. let expr = self.expr_drop_temps_mut(for_span, match_expr); - self.lower_attrs(expr.hir_id, &e.attrs, e.span, Target::from_expr(e)); + self.lower_attrs(expr.hir_id, &e.attrs, e.span, Target::from_expr(e), None); expr } @@ -1974,7 +1981,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let val_ident = Ident::with_dummy_span(sym::val); let (val_pat, val_pat_nid) = self.pat_ident(span, val_ident); let val_expr = self.expr_ident(span, val_ident, val_pat_nid); - self.lower_attrs(val_expr.hir_id, &attrs, span, Target::Expression); + self.lower_attrs(val_expr.hir_id, &attrs, span, Target::Expression, None); let continue_pat = self.pat_cf_continue(unstable_span, val_pat); self.arm(continue_pat, val_expr, try_span) }; @@ -2016,7 +2023,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let ret_expr = self.checked_return(Some(from_residual_expr)); self.arena.alloc(self.expr(try_span, ret_expr)) }; - self.lower_attrs(ret_expr.hir_id, &attrs, span, Target::Expression); + self.lower_attrs(ret_expr.hir_id, &attrs, span, Target::Expression, None); let break_pat = self.pat_cf_break(try_span, residual_local); self.arm(break_pat, ret_expr, try_span) diff --git a/compiler/rustc_ast_lowering/src/expr/closure.rs b/compiler/rustc_ast_lowering/src/expr/closure.rs index 5924d53dd11e4..c1fd17362cd09 100644 --- a/compiler/rustc_ast_lowering/src/expr/closure.rs +++ b/compiler/rustc_ast_lowering/src/expr/closure.rs @@ -20,7 +20,7 @@ impl<'hir> LoweringContext<'_, 'hir> { closure: &Closure, ) -> hir::Expr<'hir> { let expr_hir_id = self.lower_node_id(e.id); - let attrs = self.lower_attrs(expr_hir_id, &e.attrs, e.span, Target::from_expr(e)); + let attrs = self.lower_attrs(expr_hir_id, &e.attrs, e.span, Target::from_expr(e), None); match closure.coroutine_marker { Some(coroutine_marker) => self.lower_expr_coroutine_closure_with_move_exprs( diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 9698ab0aedb83..739ca3e0692e1 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -166,6 +166,7 @@ impl<'hir> LoweringContext<'_, 'hir> { i.span, Target::from_ast_item(i), Some(i), + None, &extra_hir_attributes, ); @@ -614,8 +615,13 @@ impl<'hir> LoweringContext<'_, 'hir> { pub(super) fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> { let owner_id = self.curr_owner.owner_id(); let hir_id: HirId = owner_id.into(); - let attrs = - self.lower_attrs(hir_id, &i.attrs, i.span, Target::from_foreign_item_kind(&i.kind)); + let attrs = self.lower_attrs( + hir_id, + &i.attrs, + i.span, + Target::from_foreign_item_kind(&i.kind), + None, + ); let (ident, kind) = match &i.kind { ForeignItemKind::Fn(Fn { sig, ident, generics, define_opaque, .. }) => { let fdec = &sig.decl; @@ -693,7 +699,7 @@ impl<'hir> LoweringContext<'_, 'hir> { self.dcx().span_fatal(v.span, "unnamed enum variants are not yet implemented"); } let hir_id = self.lower_node_id(v.id); - self.lower_attrs(hir_id, &v.attrs, v.span, Target::Variant); + self.lower_attrs(hir_id, &v.attrs, v.span, Target::Variant, None); hir::Variant { hir_id, def_id: self.local_def_id(v.id), @@ -780,7 +786,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let ty = self.lower_ty_alloc(&f.ty, ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy)); let hir_id = self.lower_node_id(f.id); - self.lower_attrs(hir_id, &f.attrs, f.span, Target::Field); + self.lower_attrs(hir_id, &f.attrs, f.span, Target::Field, None); hir::FieldDef { span: self.lower_span(f.span), hir_id, @@ -808,6 +814,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &i.attrs, i.span, Target::from_assoc_item_kind(&i.kind, AssocCtxt::Trait), + Some(i), ); let (ident, generics, kind, has_value) = match &i.kind { @@ -1070,6 +1077,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &i.attrs, i.span, Target::from_assoc_item_kind(&i.kind, AssocCtxt::Impl { of_trait: is_in_trait_impl }), + Some(i), ); let (ident, (generics, kind)) = match &i.kind { @@ -1238,7 +1246,7 @@ impl<'hir> LoweringContext<'_, 'hir> { fn lower_param(&mut self, param: &Param) -> hir::Param<'hir> { let hir_id = self.lower_node_id(param.id); - self.lower_attrs(hir_id, ¶m.attrs, param.span, Target::Param); + self.lower_attrs(hir_id, ¶m.attrs, param.span, Target::Param, None); hir::Param { hir_id, pat: self.lower_pat(¶m.pat), @@ -1938,7 +1946,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ) -> hir::WherePredicate<'hir> { let hir_id = self.lower_node_id(pred.id); let span = self.lower_span(pred.span); - self.lower_attrs(hir_id, &pred.attrs, span, Target::WherePredicate); + self.lower_attrs(hir_id, &pred.attrs, span, Target::WherePredicate, None); let kind = self.arena.alloc(match &pred.kind { WherePredicateKind::BoundPredicate(WhereBoundPredicate { bound_generic_params, diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index b6d1017665ec6..fa68ddc2cbee0 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -1145,8 +1145,9 @@ impl<'hir> LoweringContext<'_, 'hir> { attrs: &[Attribute], target_span: Span, target: Target, + target_assoc_item: Option<&ast::AssocItem>, ) -> &'hir [rustc_attr_ir::Attribute] { - self.lower_attrs_with_extra(id, attrs, target_span, target, None, &[]) + self.lower_attrs_with_extra(id, attrs, target_span, target, None, target_assoc_item, &[]) } fn lower_attrs_with_extra( @@ -1156,13 +1157,20 @@ impl<'hir> LoweringContext<'_, 'hir> { target_span: Span, target: Target, target_item: Option<&ast::Item>, + target_assoc_item: Option<&ast::AssocItem>, extra_hir_attributes: &[rustc_attr_ir::Attribute], ) -> &'hir [rustc_attr_ir::Attribute] { if attrs.is_empty() && extra_hir_attributes.is_empty() { &[] } else { - let mut lowered_attrs = - self.lower_attrs_vec(attrs, self.lower_span(target_span), id, target, target_item); + let mut lowered_attrs = self.lower_attrs_vec( + attrs, + self.lower_span(target_span), + id, + target, + target_item, + target_assoc_item, + ); lowered_attrs.extend(extra_hir_attributes.iter().cloned()); assert_eq!(id.owner, self.curr_owner.owner_id()); @@ -1190,6 +1198,7 @@ impl<'hir> LoweringContext<'_, 'hir> { target_hir_id: HirId, target: Target, target_item: Option<&ast::Item>, + target_assoc_item: Option<&ast::AssocItem>, ) -> Vec { let l = self.span_lowerer(); self.attribute_parser.parse_attribute_list( @@ -1197,6 +1206,7 @@ impl<'hir> LoweringContext<'_, 'hir> { target_span, target, target_item, + target_assoc_item, |s| l.lower(s), |lint_id, span, kind| { self.curr_owner.delayed_lints.push(DelayedLint { @@ -2282,7 +2292,7 @@ impl<'hir> LoweringContext<'_, 'hir> { colon_span: param.colon_span.map(|s| self.lower_span(s)), source, }; - self.lower_attrs(hir_id, param_attrs, param_span, Target::from(¶m)); + self.lower_attrs(hir_id, param_attrs, param_span, Target::from(¶m), None); param } @@ -2888,7 +2898,7 @@ impl<'hir> LoweringContext<'_, 'hir> { // FIXME(mgca): This might result in lowering attributes that // then go unused as the `Target::ExprField` is not actually // corresponding to `Node::ExprField`. - self.lower_attrs(hir_id, &f.attrs, f.span, Target::ExprField); + self.lower_attrs(hir_id, &f.attrs, f.span, Target::ExprField, None); let expr = self.lower_expr_to_const_arg_direct(&f.expr, None); &*self.arena.alloc(hir::ConstArgExprField { diff --git a/compiler/rustc_ast_lowering/src/pat.rs b/compiler/rustc_ast_lowering/src/pat.rs index df3d7b1a3c0a6..c7a7f81a5b4d7 100644 --- a/compiler/rustc_ast_lowering/src/pat.rs +++ b/compiler/rustc_ast_lowering/src/pat.rs @@ -92,7 +92,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let fs = self.arena.alloc_from_iter(fields.iter().map(|f| { let hir_id = self.lower_node_id(f.id); - self.lower_attrs(hir_id, &f.attrs, f.span, Target::PatField); + self.lower_attrs(hir_id, &f.attrs, f.span, Target::PatField, None); hir::PatField { hir_id, diff --git a/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs b/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs index b27f3b1c63626..d3f48c46a79d2 100644 --- a/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs +++ b/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs @@ -1,4 +1,4 @@ -use rustc_ast::ItemKind; +use rustc_ast::{AssocItemKind, ItemKind}; use rustc_attr_ir::{ CoverageAttrKind, InstrumentFnAttr, OptimizeAttr, RtsanSetting, UsedBy, find_attr, }; @@ -333,16 +333,47 @@ impl AttributeParser for NakedParser { fn deferred_finalize_check(&self) -> Option<(FinalizeCheckFn, Span)> { Some(( |cx, _| match cx.target { - Target::Fn - | Target::Method( - MethodKind::Trait { body: true } | MethodKind::TraitImpl | MethodKind::Inherent, - ) => { + Target::Fn => { let Some(item) = cx.target_item else { - return; + panic!("expected struct AST target item for {:?}", cx.target); }; let ItemKind::Fn(fn_item) = &item.kind else { - return; + panic!("expected struct AST target item for {:?}", cx.target); + }; + + let fn_sig = &fn_item.sig; + let abi = fn_sig.header.ext; + + if abi.is_rustic_abi() && !cx.features().naked_functions_rustic_abi() { + let abi_type = match abi { + rustc_ast::ast::Extern::None => "Rust".into(), + rustc_ast::ast::Extern::Explicit(name, _) => { + name.symbol_unescaped.to_string() + } + rustc_ast::ast::Extern::Implicit(_) => unreachable!(), + }; + feature_err( + cx.sess(), + sym::naked_functions_rustic_abi, + fn_sig.span, + format!( + "`#[naked]` is currently unstable on `extern \"{}\"` functions", + abi_type + ), + ) + .emit(); + } + } + Target::Method( + MethodKind::Trait { body: true } | MethodKind::TraitImpl | MethodKind::Inherent, + ) => { + let Some(assoc_item) = cx.target_assoc_item else { + panic!("expected struct AST target associated item for {:?}", cx.target); + }; + + let AssocItemKind::Fn(fn_item) = &assoc_item.kind else { + panic!("expected struct AST target associated item for {:?}", cx.target); }; let fn_sig = &fn_item.sig; diff --git a/compiler/rustc_attr_parsing/src/context.rs b/compiler/rustc_attr_parsing/src/context.rs index 060fd74c1db21..f3773461bd64a 100644 --- a/compiler/rustc_attr_parsing/src/context.rs +++ b/compiler/rustc_attr_parsing/src/context.rs @@ -858,6 +858,10 @@ pub(crate) struct FinalizeCheckContext<'p, 'sess> { /// The AST item these attributes were applied to, when the target is an item. /// Used by `finalize_check` to inspect item structure that is not encoded in [`Target`]. pub(crate) target_item: Option<&'p rustc_ast::ast::Item>, + + /// The AST associated item these attributes were applied to, when the target is an associated item. + /// Used by `finalize_check` to inspect associated item structure that is not encoded in [`Target`]. + pub(crate) target_assoc_item: Option<&'p rustc_ast::ast::AssocItem>, } impl<'p, 'sess: 'p> Deref for FinalizeCheckContext<'p, 'sess> { diff --git a/compiler/rustc_attr_parsing/src/interface.rs b/compiler/rustc_attr_parsing/src/interface.rs index 4ce284a0a9d5a..143f63e909ecd 100644 --- a/compiler/rustc_attr_parsing/src/interface.rs +++ b/compiler/rustc_attr_parsing/src/interface.rs @@ -162,6 +162,7 @@ impl<'sess> AttributeParser<'sess> { target_span, target, None, + None, std::convert::identity, |lint_id, span, kind| { sess.psess.dyn_buffer_lint_sess(lint_id.lint, span, target_node_id, kind.0) @@ -318,6 +319,7 @@ impl<'sess> AttributeParser<'sess> { target_span: Span, target: Target, target_item: Option<&ast::Item>, + target_assoc_item: Option<&ast::AssocItem>, lower_span: impl Copy + Fn(Span) -> Span, mut emit_lint: impl FnMut(LintId, MultiSpan, EmitAttribute), ) -> Vec { @@ -526,6 +528,7 @@ impl<'sess> AttributeParser<'sess> { all_attrs: &attr_paths, parsed_attrs: &attributes, target_item, + target_assoc_item, }, attr_span, ); diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index c70728c930fe7..c5cd132573273 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -200,7 +200,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> { AttributeKind::RustcAllowConstFnUnstable(_, first_span) => { self.check_rustc_allow_const_fn_unstable(hir_id, *first_span, span, target) } - AttributeKind::Naked(..) => self.check_naked(hir_id, target), AttributeKind::MayDangle(attr_span) => self.check_may_dangle(hir_id, *attr_span), AttributeKind::Doc(attr) => self.check_doc_attrs(attr, hir_id, target), AttributeKind::EiiImpl(eii_impl) => self.check_eii_impl(eii_impl), @@ -732,32 +731,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> { } } - /// Checks if `#[naked]` is applied to a function definition. - fn check_naked(&self, hir_id: HirId, target: Target) { - match target { - Target::Fn - | Target::Method( - MethodKind::Trait { body: true } | MethodKind::TraitImpl | MethodKind::Inherent, - ) => { - let fn_sig = self.tcx.hir_node(hir_id).fn_sig().unwrap(); - let abi = fn_sig.header.abi; - if abi.is_rustic_abi() && !self.tcx.features().naked_functions_rustic_abi() { - feature_err( - &self.tcx.sess, - sym::naked_functions_rustic_abi, - fn_sig.span, - format!( - "`#[naked]` is currently unstable on `extern \"{}\"` functions", - abi.as_str() - ), - ) - .emit(); - } - } - _ => {} - } - } - fn check_doc_alias_value(&self, span: Span, hir_id: HirId, target: Target, alias: Symbol) { if let Some(location) = match target { Target::AssocTy(_) => { diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index f3329c3d0398e..4f834d768b486 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -184,6 +184,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> { i.span, Target::MacroDef, None, + None, std::convert::identity, |_lint_id, _span, _kind| { // FIXME(jdonszelmann): emit lints here properly From 6e2a7e442166225cdd802aca697fd773a9665b2c Mon Sep 17 00:00:00 2001 From: Richard Tjokroutomo Date: Sat, 12 Sep 2026 21:55:29 +0800 Subject: [PATCH 03/13] wrap target_item & target_assoc_item into an enum Signed-off-by: Richard Tjokroutomo --- compiler/rustc_ast/src/ast.rs | 6 ++ compiler/rustc_ast_lowering/src/expr.rs | 9 +-- compiler/rustc_ast_lowering/src/item.rs | 7 +-- compiler/rustc_ast_lowering/src/lib.rs | 16 ++--- .../src/attributes/codegen_attrs.rs | 59 ++++++------------- .../src/attributes/non_exhaustive.rs | 29 +++++---- compiler/rustc_attr_parsing/src/context.rs | 9 +-- compiler/rustc_attr_parsing/src/interface.rs | 7 +-- compiler/rustc_resolve/src/def_collector.rs | 1 - 9 files changed, 56 insertions(+), 87 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 47a25e8fd704d..4583664588de9 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -3704,6 +3704,12 @@ impl VariantData { } } +#[derive(Clone, Copy, Debug)] +pub enum AstItemKind<'a> { + Item(&'a Item), + AssocItem(&'a Item), +} + /// An item definition. #[derive(Clone, Encodable, Decodable, Debug)] pub struct Item { diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 272cc0600b2a1..6d07a71e13901 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -231,14 +231,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let old_attrs = self.curr_owner.attrs.get(&ex.hir_id.local_id).copied().unwrap_or(&[]); let new_attrs = self - .lower_attrs_vec( - &e.attrs, - e.span, - ex.hir_id, - Target::from_expr(e), - None, - None, - ) + .lower_attrs_vec(&e.attrs, e.span, ex.hir_id, Target::from_expr(e), None) .into_iter() .chain(old_attrs.iter().cloned()); let new_attrs = &*self.arena.alloc_from_iter(new_attrs); diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 739ca3e0692e1..7ba336a96c71f 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -165,8 +165,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &i.attrs, i.span, Target::from_ast_item(i), - Some(i), - None, + Some(ast::AstItemKind::Item(i)), &extra_hir_attributes, ); @@ -814,7 +813,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &i.attrs, i.span, Target::from_assoc_item_kind(&i.kind, AssocCtxt::Trait), - Some(i), + Some(ast::AstItemKind::AssocItem(i)), ); let (ident, generics, kind, has_value) = match &i.kind { @@ -1077,7 +1076,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &i.attrs, i.span, Target::from_assoc_item_kind(&i.kind, AssocCtxt::Impl { of_trait: is_in_trait_impl }), - Some(i), + Some(ast::AstItemKind::AssocItem(i)), ); let (ident, (generics, kind)) = match &i.kind { diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index fa68ddc2cbee0..87c420bd218c7 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -1145,9 +1145,9 @@ impl<'hir> LoweringContext<'_, 'hir> { attrs: &[Attribute], target_span: Span, target: Target, - target_assoc_item: Option<&ast::AssocItem>, + ast_target_item: Option>, ) -> &'hir [rustc_attr_ir::Attribute] { - self.lower_attrs_with_extra(id, attrs, target_span, target, None, target_assoc_item, &[]) + self.lower_attrs_with_extra(id, attrs, target_span, target, ast_target_item, &[]) } fn lower_attrs_with_extra( @@ -1156,8 +1156,7 @@ impl<'hir> LoweringContext<'_, 'hir> { attrs: &[Attribute], target_span: Span, target: Target, - target_item: Option<&ast::Item>, - target_assoc_item: Option<&ast::AssocItem>, + ast_target_item: Option>, extra_hir_attributes: &[rustc_attr_ir::Attribute], ) -> &'hir [rustc_attr_ir::Attribute] { if attrs.is_empty() && extra_hir_attributes.is_empty() { @@ -1168,8 +1167,7 @@ impl<'hir> LoweringContext<'_, 'hir> { self.lower_span(target_span), id, target, - target_item, - target_assoc_item, + ast_target_item, ); lowered_attrs.extend(extra_hir_attributes.iter().cloned()); @@ -1197,16 +1195,14 @@ impl<'hir> LoweringContext<'_, 'hir> { target_span: Span, target_hir_id: HirId, target: Target, - target_item: Option<&ast::Item>, - target_assoc_item: Option<&ast::AssocItem>, + ast_target_item: Option>, ) -> Vec { let l = self.span_lowerer(); self.attribute_parser.parse_attribute_list( attrs, target_span, target, - target_item, - target_assoc_item, + ast_target_item, |s| l.lower(s), |lint_id, span, kind| { self.curr_owner.delayed_lints.push(DelayedLint { diff --git a/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs b/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs index d3f48c46a79d2..128cb38c9762e 100644 --- a/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs +++ b/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs @@ -333,50 +333,29 @@ impl AttributeParser for NakedParser { fn deferred_finalize_check(&self) -> Option<(FinalizeCheckFn, Span)> { Some(( |cx, _| match cx.target { - Target::Fn => { - let Some(item) = cx.target_item else { - panic!("expected struct AST target item for {:?}", cx.target); - }; - - let ItemKind::Fn(fn_item) = &item.kind else { - panic!("expected struct AST target item for {:?}", cx.target); - }; - - let fn_sig = &fn_item.sig; - let abi = fn_sig.header.ext; - - if abi.is_rustic_abi() && !cx.features().naked_functions_rustic_abi() { - let abi_type = match abi { - rustc_ast::ast::Extern::None => "Rust".into(), - rustc_ast::ast::Extern::Explicit(name, _) => { - name.symbol_unescaped.to_string() - } - rustc_ast::ast::Extern::Implicit(_) => unreachable!(), - }; - feature_err( - cx.sess(), - sym::naked_functions_rustic_abi, - fn_sig.span, - format!( - "`#[naked]` is currently unstable on `extern \"{}\"` functions", - abi_type - ), - ) - .emit(); - } - } - Target::Method( + Target::Fn + | Target::Method( MethodKind::Trait { body: true } | MethodKind::TraitImpl | MethodKind::Inherent, ) => { - let Some(assoc_item) = cx.target_assoc_item else { - panic!("expected struct AST target associated item for {:?}", cx.target); - }; - - let AssocItemKind::Fn(fn_item) = &assoc_item.kind else { - panic!("expected struct AST target associated item for {:?}", cx.target); + let fn_sig = match cx.ast_target_item { + Some(rustc_ast::ast::AstItemKind::Item(ast_item)) => { + let ItemKind::Fn(fn_item) = &ast_item.kind else { + panic!("expected struct AST target item for {:?}", ast_item); + }; + &fn_item.sig + } + Some(rustc_ast::ast::AstItemKind::AssocItem(assoc_item)) => { + let AssocItemKind::Fn(fn_item) = &assoc_item.kind else { + panic!( + "expected struct AST target associated item for {:?}", + assoc_item + ); + }; + &fn_item.sig + } + _ => panic!("expected enum AST target kind for {:?}", cx.ast_target_item), }; - let fn_sig = &fn_item.sig; let abi = fn_sig.header.ext; if abi.is_rustic_abi() && !cx.features().naked_functions_rustic_abi() { diff --git a/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs b/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs index 2acba84956e67..75c8e68868948 100644 --- a/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs +++ b/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs @@ -1,4 +1,4 @@ -use rustc_ast::{ItemKind, VariantData}; +use rustc_ast::{AstItemKind, ItemKind, VariantData}; use rustc_feature::AttributeStability; use super::prelude::*; @@ -26,17 +26,22 @@ impl NoArgsAttributeParser for NonExhaustiveParser { return; } - let item = cx.target_item.expect("missing AST target item for Target::Struct"); - let ItemKind::Struct(_, _, data) = &item.kind else { - panic!("expected struct AST target item for Target::Struct"); - }; - if let VariantData::Struct { fields, .. } = data - && fields.iter().any(|f| f.default_value().is_some()) - { - cx.emit_err(NonExhaustiveWithDefaultFieldValues { - attr_span, - defn_span: cx.target_span, - }); + let item = cx.ast_target_item.expect("missing AST target item for Target::Struct"); + match item { + AstItemKind::Item(ast_item) => { + let ItemKind::Struct(_, _, data) = &ast_item.kind else { + panic!("expected struct AST target item for Target::Struct"); + }; + if let VariantData::Struct { fields, .. } = data + && fields.iter().any(|f| f.default_value().is_some()) + { + cx.emit_err(NonExhaustiveWithDefaultFieldValues { + attr_span, + defn_span: cx.target_span, + }); + } + } + _ => {} } } } diff --git a/compiler/rustc_attr_parsing/src/context.rs b/compiler/rustc_attr_parsing/src/context.rs index f3773461bd64a..774bd3280121f 100644 --- a/compiler/rustc_attr_parsing/src/context.rs +++ b/compiler/rustc_attr_parsing/src/context.rs @@ -855,13 +855,8 @@ pub(crate) struct FinalizeCheckContext<'p, 'sess> { /// Unlike [`all_attrs`](Self::all_attrs), this contains the fully parsed attributes. pub(crate) parsed_attrs: &'p [Attribute], - /// The AST item these attributes were applied to, when the target is an item. - /// Used by `finalize_check` to inspect item structure that is not encoded in [`Target`]. - pub(crate) target_item: Option<&'p rustc_ast::ast::Item>, - - /// The AST associated item these attributes were applied to, when the target is an associated item. - /// Used by `finalize_check` to inspect associated item structure that is not encoded in [`Target`]. - pub(crate) target_assoc_item: Option<&'p rustc_ast::ast::AssocItem>, + /// The AST item these attributes were applied to. + pub(crate) ast_target_item: Option>, } impl<'p, 'sess: 'p> Deref for FinalizeCheckContext<'p, 'sess> { diff --git a/compiler/rustc_attr_parsing/src/interface.rs b/compiler/rustc_attr_parsing/src/interface.rs index 143f63e909ecd..b1f56dbd72e99 100644 --- a/compiler/rustc_attr_parsing/src/interface.rs +++ b/compiler/rustc_attr_parsing/src/interface.rs @@ -162,7 +162,6 @@ impl<'sess> AttributeParser<'sess> { target_span, target, None, - None, std::convert::identity, |lint_id, span, kind| { sess.psess.dyn_buffer_lint_sess(lint_id.lint, span, target_node_id, kind.0) @@ -318,8 +317,7 @@ impl<'sess> AttributeParser<'sess> { attrs: &[ast::Attribute], target_span: Span, target: Target, - target_item: Option<&ast::Item>, - target_assoc_item: Option<&ast::AssocItem>, + ast_target_item: Option>, lower_span: impl Copy + Fn(Span) -> Span, mut emit_lint: impl FnMut(LintId, MultiSpan, EmitAttribute), ) -> Vec { @@ -527,8 +525,7 @@ impl<'sess> AttributeParser<'sess> { }, all_attrs: &attr_paths, parsed_attrs: &attributes, - target_item, - target_assoc_item, + ast_target_item, }, attr_span, ); diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index 4f834d768b486..f3329c3d0398e 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -184,7 +184,6 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> { i.span, Target::MacroDef, None, - None, std::convert::identity, |_lint_id, _span, _kind| { // FIXME(jdonszelmann): emit lints here properly From 114aa5242cee0147088fb9c779e29cad867a094d Mon Sep 17 00:00:00 2001 From: Richard Tjokroutomo Date: Sun, 13 Sep 2026 09:28:08 +0800 Subject: [PATCH 04/13] addressing review comments Signed-off-by: Richard Tjokroutomo --- compiler/rustc_ast/src/ast.rs | 25 --------- compiler/rustc_ast_lowering/src/item.rs | 7 +-- compiler/rustc_ast_lowering/src/lib.rs | 20 +++---- compiler/rustc_attr_ir/src/target.rs | 53 ++++++++++++++++++- .../src/attributes/codegen_attrs.rs | 35 +++--------- .../src/attributes/non_exhaustive.rs | 6 +-- compiler/rustc_attr_parsing/src/context.rs | 2 +- compiler/rustc_attr_parsing/src/interface.rs | 4 +- 8 files changed, 78 insertions(+), 74 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 4583664588de9..6a86e9af71826 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -3704,12 +3704,6 @@ impl VariantData { } } -#[derive(Clone, Copy, Debug)] -pub enum AstItemKind<'a> { - Item(&'a Item), - AssocItem(&'a Item), -} - /// An item definition. #[derive(Clone, Encodable, Decodable, Debug)] pub struct Item { @@ -3787,25 +3781,6 @@ impl Extern { Extern::Implicit(span) | Extern::Explicit(_, span) => Some(span), } } - - /// An ABI "like Rust" - /// - /// These ABIs are fully controlled by the Rust compiler, which means they - /// - support unwinding with `-Cpanic=unwind`, unlike `extern "C"` - /// - often diverge from the C ABI - /// - are subject to change between compiler versions - pub fn is_rustic_abi(self) -> bool { - match self { - Extern::None => true, - Extern::Implicit(_) => false, - Extern::Explicit(name, _) => { - matches!( - name.symbol_unescaped.as_str(), - "Rust" | "rust-call" | "rust-cold" | "rust-preserve-none" | "rust-tail" - ) - } - } - } } /// A function header. diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 7ba336a96c71f..7efe335c3709f 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -1,6 +1,7 @@ use rustc_abi::ExternAbi; use rustc_ast::visit::AssocCtxt; use rustc_ast::*; +use rustc_attr_ir::target::AstTarget; use rustc_attr_ir::target::Target; use rustc_attr_ir::{AttributeKind, EiiImplResolution, find_attr}; use rustc_errors::{E0570, ErrorGuaranteed, struct_span_code_err}; @@ -165,7 +166,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &i.attrs, i.span, Target::from_ast_item(i), - Some(ast::AstItemKind::Item(i)), + Some(AstTarget::Item(i)), &extra_hir_attributes, ); @@ -813,7 +814,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &i.attrs, i.span, Target::from_assoc_item_kind(&i.kind, AssocCtxt::Trait), - Some(ast::AstItemKind::AssocItem(i)), + Some(AstTarget::AssocItem(i)), ); let (ident, generics, kind, has_value) = match &i.kind { @@ -1076,7 +1077,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &i.attrs, i.span, Target::from_assoc_item_kind(&i.kind, AssocCtxt::Impl { of_trait: is_in_trait_impl }), - Some(ast::AstItemKind::AssocItem(i)), + Some(AstTarget::AssocItem(i)), ); let (ident, (generics, kind)) = match &i.kind { diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 87c420bd218c7..24d032982370c 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -48,6 +48,7 @@ use rustc_ast::{self as ast, *}; use rustc_attr_ir::find_attr; use rustc_attr_ir::lang_items::LangItem; use rustc_attr_ir::target::Target; +use rustc_attr_ir::target::AstTarget; use rustc_attr_parsing::{AttributeParser, Recovery, ShouldEmit}; use rustc_data_structures::fx::FxIndexMap; use rustc_data_structures::sorted_map::SortedMap; @@ -1145,9 +1146,9 @@ impl<'hir> LoweringContext<'_, 'hir> { attrs: &[Attribute], target_span: Span, target: Target, - ast_target_item: Option>, + ast_target: Option>, ) -> &'hir [rustc_attr_ir::Attribute] { - self.lower_attrs_with_extra(id, attrs, target_span, target, ast_target_item, &[]) + self.lower_attrs_with_extra(id, attrs, target_span, target, ast_target, &[]) } fn lower_attrs_with_extra( @@ -1156,19 +1157,14 @@ impl<'hir> LoweringContext<'_, 'hir> { attrs: &[Attribute], target_span: Span, target: Target, - ast_target_item: Option>, + ast_target: Option>, extra_hir_attributes: &[rustc_attr_ir::Attribute], ) -> &'hir [rustc_attr_ir::Attribute] { if attrs.is_empty() && extra_hir_attributes.is_empty() { &[] } else { - let mut lowered_attrs = self.lower_attrs_vec( - attrs, - self.lower_span(target_span), - id, - target, - ast_target_item, - ); + let mut lowered_attrs = + self.lower_attrs_vec(attrs, self.lower_span(target_span), id, target, ast_target); lowered_attrs.extend(extra_hir_attributes.iter().cloned()); assert_eq!(id.owner, self.curr_owner.owner_id()); @@ -1195,14 +1191,14 @@ impl<'hir> LoweringContext<'_, 'hir> { target_span: Span, target_hir_id: HirId, target: Target, - ast_target_item: Option>, + ast_target: Option>, ) -> Vec { let l = self.span_lowerer(); self.attribute_parser.parse_attribute_list( attrs, target_span, target, - ast_target_item, + ast_target, |s| l.lower(s), |lint_id, span, kind| { self.curr_owner.delayed_lints.push(DelayedLint { diff --git a/compiler/rustc_attr_ir/src/target.rs b/compiler/rustc_attr_ir/src/target.rs index c41e78c4a96e0..57875d915e047 100644 --- a/compiler/rustc_attr_ir/src/target.rs +++ b/compiler/rustc_attr_ir/src/target.rs @@ -2,10 +2,19 @@ use std::fmt::{self, Display}; +use rustc_abi::ExternAbi; pub use rustc_ast::visit::AssocCtxt; -use rustc_ast::{AssocItemKind, ForeignItemKind, ast}; +use rustc_ast::{AssocItemKind, ForeignItemKind, Item, ast}; use rustc_macros::StableHash; +// This enum lists all possible types of AST items. +// FIXME: Currently, this enum only lists `Item` and `AssocItem`, but in the future, be exhaustive. +#[derive(Clone, Copy, Debug)] +pub enum AstTarget<'a> { + Item(&'a Item), + AssocItem(&'a Item), +} + #[derive(Copy, Clone, PartialEq, Debug, Eq, StableHash)] pub enum MethodKind { /// Method in a `trait Trait` block @@ -65,6 +74,48 @@ pub enum Target { Break, } +impl AstTarget<'_> { + pub fn get_abi(&self) -> Option { + let ext = match self { + AstTarget::Item(item) => { + let ast::ItemKind::Fn(fn_item) = &item.kind else { + return None; + }; + fn_item.sig.header.ext + } + AstTarget::AssocItem(assoc_item) => { + let ast::AssocItemKind::Fn(fn_item) = &assoc_item.kind else { + return None; + }; + fn_item.sig.header.ext + } + }; + + match ext { + ast::Extern::None => Some(ExternAbi::Rust), + ast::Extern::Implicit(_) => Some(ExternAbi::FALLBACK), + ast::Extern::Explicit(abi, _) => Some(abi.symbol_unescaped.as_str().parse().ok()?), + } + } + + pub fn get_fn_sig(&self) -> Option<&rustc_ast::ast::FnSig> { + match self { + AstTarget::Item(item) => { + let ast::ItemKind::Fn(fn_item) = &item.kind else { + return None; + }; + Some(&fn_item.sig) + } + AstTarget::AssocItem(assoc_item) => { + let ast::AssocItemKind::Fn(fn_item) = &assoc_item.kind else { + return None; + }; + Some(&fn_item.sig) + } + } + } +} + impl Display for Target { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", Self::name(*self)) diff --git a/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs b/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs index 128cb38c9762e..2d74a289b0857 100644 --- a/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs +++ b/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs @@ -1,4 +1,3 @@ -use rustc_ast::{AssocItemKind, ItemKind}; use rustc_attr_ir::{ CoverageAttrKind, InstrumentFnAttr, OptimizeAttr, RtsanSetting, UsedBy, find_attr, }; @@ -337,42 +336,24 @@ impl AttributeParser for NakedParser { | Target::Method( MethodKind::Trait { body: true } | MethodKind::TraitImpl | MethodKind::Inherent, ) => { - let fn_sig = match cx.ast_target_item { - Some(rustc_ast::ast::AstItemKind::Item(ast_item)) => { - let ItemKind::Fn(fn_item) = &ast_item.kind else { - panic!("expected struct AST target item for {:?}", ast_item); - }; - &fn_item.sig - } - Some(rustc_ast::ast::AstItemKind::AssocItem(assoc_item)) => { - let AssocItemKind::Fn(fn_item) = &assoc_item.kind else { - panic!( - "expected struct AST target associated item for {:?}", - assoc_item - ); - }; - &fn_item.sig - } - _ => panic!("expected enum AST target kind for {:?}", cx.ast_target_item), + let Some(ast_target) = cx.ast_target else { + panic!("missing AST target for {:?}", cx.target); }; - let abi = fn_sig.header.ext; + let fn_sig = + ast_target.get_fn_sig().expect("missing fn signature for AST target"); + let Some(abi) = ast_target.get_abi() else { + return; + }; if abi.is_rustic_abi() && !cx.features().naked_functions_rustic_abi() { - let abi_type = match abi { - rustc_ast::ast::Extern::None => "Rust".into(), - rustc_ast::ast::Extern::Explicit(name, _) => { - name.symbol_unescaped.to_string() - } - rustc_ast::ast::Extern::Implicit(_) => unreachable!(), - }; feature_err( cx.sess(), sym::naked_functions_rustic_abi, fn_sig.span, format!( "`#[naked]` is currently unstable on `extern \"{}\"` functions", - abi_type + abi.as_str() ), ) .emit(); diff --git a/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs b/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs index 75c8e68868948..3c8afaefa3953 100644 --- a/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs +++ b/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs @@ -1,4 +1,4 @@ -use rustc_ast::{AstItemKind, ItemKind, VariantData}; +use rustc_ast::{ItemKind, VariantData}; use rustc_feature::AttributeStability; use super::prelude::*; @@ -26,9 +26,9 @@ impl NoArgsAttributeParser for NonExhaustiveParser { return; } - let item = cx.ast_target_item.expect("missing AST target item for Target::Struct"); + let item = cx.ast_target.expect("missing AST target item for Target::Struct"); match item { - AstItemKind::Item(ast_item) => { + rustc_attr_ir::target::AstTarget::Item(ast_item) => { let ItemKind::Struct(_, _, data) = &ast_item.kind else { panic!("expected struct AST target item for Target::Struct"); }; diff --git a/compiler/rustc_attr_parsing/src/context.rs b/compiler/rustc_attr_parsing/src/context.rs index 774bd3280121f..1e550f064fb78 100644 --- a/compiler/rustc_attr_parsing/src/context.rs +++ b/compiler/rustc_attr_parsing/src/context.rs @@ -856,7 +856,7 @@ pub(crate) struct FinalizeCheckContext<'p, 'sess> { pub(crate) parsed_attrs: &'p [Attribute], /// The AST item these attributes were applied to. - pub(crate) ast_target_item: Option>, + pub(crate) ast_target: Option>, } impl<'p, 'sess: 'p> Deref for FinalizeCheckContext<'p, 'sess> { diff --git a/compiler/rustc_attr_parsing/src/interface.rs b/compiler/rustc_attr_parsing/src/interface.rs index b1f56dbd72e99..cc998c8e6d864 100644 --- a/compiler/rustc_attr_parsing/src/interface.rs +++ b/compiler/rustc_attr_parsing/src/interface.rs @@ -317,7 +317,7 @@ impl<'sess> AttributeParser<'sess> { attrs: &[ast::Attribute], target_span: Span, target: Target, - ast_target_item: Option>, + ast_target: Option>, lower_span: impl Copy + Fn(Span) -> Span, mut emit_lint: impl FnMut(LintId, MultiSpan, EmitAttribute), ) -> Vec { @@ -525,7 +525,7 @@ impl<'sess> AttributeParser<'sess> { }, all_attrs: &attr_paths, parsed_attrs: &attributes, - ast_target_item, + ast_target, }, attr_span, ); From cbd16d2a9952bf4a76ea7f33f7e617823c5af9c6 Mon Sep 17 00:00:00 2001 From: Richard Tjokroutomo Date: Tue, 15 Sep 2026 13:31:59 +0800 Subject: [PATCH 05/13] exhaust all possible types of AST Target (obtained from grepping all functions calling lower_attrs() Signed-off-by: Richard Tjokroutomo --- compiler/rustc_ast_lowering/src/block.rs | 8 +++- compiler/rustc_ast_lowering/src/contract.rs | 8 +++- compiler/rustc_ast_lowering/src/expr.rs | 34 +++++++++++++---- .../rustc_ast_lowering/src/expr/closure.rs | 8 +++- compiler/rustc_ast_lowering/src/item.rs | 25 +++++++----- compiler/rustc_ast_lowering/src/lib.rs | 37 +++++++++++++----- compiler/rustc_ast_lowering/src/pat.rs | 8 +++- compiler/rustc_attr_ir/src/target.rs | 38 +++++++++++++++++-- .../src/attributes/codegen_attrs.rs | 8 +--- .../src/attributes/non_exhaustive.rs | 3 +- compiler/rustc_attr_parsing/src/context.rs | 2 +- compiler/rustc_attr_parsing/src/interface.rs | 4 +- compiler/rustc_resolve/src/def_collector.rs | 2 +- 13 files changed, 138 insertions(+), 47 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/block.rs b/compiler/rustc_ast_lowering/src/block.rs index 4e1b36c01f5af..0ebc9c1533b2f 100644 --- a/compiler/rustc_ast_lowering/src/block.rs +++ b/compiler/rustc_ast_lowering/src/block.rs @@ -104,7 +104,13 @@ impl<'hir> LoweringContext<'_, 'hir> { }; let span = self.lower_span(l.span); let source = hir::LocalSource::Normal; - self.lower_attrs(hir_id, &l.attrs, l.span, Target::Statement, None); + self.lower_attrs( + hir_id, + &l.attrs, + l.span, + Target::Statement, + rustc_attr_ir::target::AstTarget::Local(l), + ); self.arena.alloc(hir::LetStmt { hir_id, super_, ty, pat, init, els, span, source }) } diff --git a/compiler/rustc_ast_lowering/src/contract.rs b/compiler/rustc_ast_lowering/src/contract.rs index 2958b65131502..a7663d49e0a4a 100644 --- a/compiler/rustc_ast_lowering/src/contract.rs +++ b/compiler/rustc_ast_lowering/src/contract.rs @@ -352,7 +352,13 @@ impl<'hir> LoweringContext<'_, 'hir> { )); let attrs: rustc_ast::AttrVec = thin_vec![self.unreachable_code_attr(span)]; - self.lower_attrs(contract_check.hir_id, &attrs, span, Target::Expression, None); + self.lower_attrs( + contract_check.hir_id, + &attrs, + span, + Target::Expression, + rustc_attr_ir::target::AstTarget::None, + ); let ret_block = self.block_all(span, arena_vec![self; ret_stmt], Some(contract_check)); self.arena.alloc(self.expr_block(self.arena.alloc(ret_block))) diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 6d07a71e13901..bf0f819865343 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -6,7 +6,7 @@ use rustc_ast::node_id::NodeMap; use rustc_ast::visit::{Visitor, walk_expr}; use rustc_ast::*; use rustc_attr_ir::lang_items::LangItem; -use rustc_attr_ir::target::Target; +use rustc_attr_ir::target::{AstTarget, Target}; use rustc_errors::msg; use rustc_hir as hir; use rustc_hir::HirId; @@ -231,7 +231,13 @@ impl<'hir> LoweringContext<'_, 'hir> { let old_attrs = self.curr_owner.attrs.get(&ex.hir_id.local_id).copied().unwrap_or(&[]); let new_attrs = self - .lower_attrs_vec(&e.attrs, e.span, ex.hir_id, Target::from_expr(e), None) + .lower_attrs_vec( + &e.attrs, + e.span, + ex.hir_id, + Target::from_expr(e), + AstTarget::None, + ) .into_iter() .chain(old_attrs.iter().cloned()); let new_attrs = &*self.arena.alloc_from_iter(new_attrs); @@ -255,7 +261,7 @@ impl<'hir> LoweringContext<'_, 'hir> { } let expr_hir_id = self.lower_node_id(e.id); - self.lower_attrs(expr_hir_id, &e.attrs, e.span, Target::from_expr(e), None); + self.lower_attrs(expr_hir_id, &e.attrs, e.span, Target::from_expr(e), AstTarget::Expr(e)); let kind = match &e.kind { ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)), @@ -795,7 +801,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let guard = arm.guard.as_ref().map(|guard| self.lower_expr(&guard.cond)); let hir_id = self.next_id(); let span = self.lower_span(arm.span); - self.lower_attrs(hir_id, &arm.attrs, arm.span, Target::Arm, None); + self.lower_attrs(hir_id, &arm.attrs, arm.span, Target::Arm, AstTarget::Arm(arm)); let is_never_pattern = pat.is_never_pattern(); // We need to lower the body even if it's unneeded for never pattern in match, // ensure that we can get HirId for DefId if need (issue #137708). @@ -1659,7 +1665,7 @@ impl<'hir> LoweringContext<'_, 'hir> { fn lower_expr_field(&mut self, f: &ExprField) -> hir::ExprField<'hir> { let hir_id = self.lower_node_id(f.id); - self.lower_attrs(hir_id, &f.attrs, f.span, Target::ExprField, None); + self.lower_attrs(hir_id, &f.attrs, f.span, Target::ExprField, AstTarget::ExprField(f)); hir::ExprField { hir_id, ident: self.lower_ident(f.ident), @@ -1926,7 +1932,7 @@ impl<'hir> LoweringContext<'_, 'hir> { // // Also, add the attributes to the outer returned expr node. let expr = self.expr_drop_temps_mut(for_span, match_expr); - self.lower_attrs(expr.hir_id, &e.attrs, e.span, Target::from_expr(e), None); + self.lower_attrs(expr.hir_id, &e.attrs, e.span, Target::from_expr(e), AstTarget::Expr(e)); expr } @@ -1974,7 +1980,13 @@ impl<'hir> LoweringContext<'_, 'hir> { let val_ident = Ident::with_dummy_span(sym::val); let (val_pat, val_pat_nid) = self.pat_ident(span, val_ident); let val_expr = self.expr_ident(span, val_ident, val_pat_nid); - self.lower_attrs(val_expr.hir_id, &attrs, span, Target::Expression, None); + self.lower_attrs( + val_expr.hir_id, + &attrs, + span, + Target::Expression, + AstTarget::Expr(sub_expr), + ); let continue_pat = self.pat_cf_continue(unstable_span, val_pat); self.arm(continue_pat, val_expr, try_span) }; @@ -2016,7 +2028,13 @@ impl<'hir> LoweringContext<'_, 'hir> { let ret_expr = self.checked_return(Some(from_residual_expr)); self.arena.alloc(self.expr(try_span, ret_expr)) }; - self.lower_attrs(ret_expr.hir_id, &attrs, span, Target::Expression, None); + self.lower_attrs( + ret_expr.hir_id, + &attrs, + span, + Target::Expression, + AstTarget::Expr(sub_expr), + ); let break_pat = self.pat_cf_break(try_span, residual_local); self.arm(break_pat, ret_expr, try_span) diff --git a/compiler/rustc_ast_lowering/src/expr/closure.rs b/compiler/rustc_ast_lowering/src/expr/closure.rs index c1fd17362cd09..99ac5d1743731 100644 --- a/compiler/rustc_ast_lowering/src/expr/closure.rs +++ b/compiler/rustc_ast_lowering/src/expr/closure.rs @@ -20,7 +20,13 @@ impl<'hir> LoweringContext<'_, 'hir> { closure: &Closure, ) -> hir::Expr<'hir> { let expr_hir_id = self.lower_node_id(e.id); - let attrs = self.lower_attrs(expr_hir_id, &e.attrs, e.span, Target::from_expr(e), None); + let attrs = self.lower_attrs( + expr_hir_id, + &e.attrs, + e.span, + Target::from_expr(e), + rustc_attr_ir::target::AstTarget::Closure(closure), + ); match closure.coroutine_marker { Some(coroutine_marker) => self.lower_expr_coroutine_closure_with_move_exprs( diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 7efe335c3709f..7c2a6f13deb58 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -1,8 +1,7 @@ use rustc_abi::ExternAbi; use rustc_ast::visit::AssocCtxt; use rustc_ast::*; -use rustc_attr_ir::target::AstTarget; -use rustc_attr_ir::target::Target; +use rustc_attr_ir::target::{AstTarget, Target}; use rustc_attr_ir::{AttributeKind, EiiImplResolution, find_attr}; use rustc_errors::{E0570, ErrorGuaranteed, struct_span_code_err}; use rustc_hir::def::{DefKind, Res}; @@ -166,7 +165,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &i.attrs, i.span, Target::from_ast_item(i), - Some(AstTarget::Item(i)), + AstTarget::Item(i), &extra_hir_attributes, ); @@ -620,7 +619,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &i.attrs, i.span, Target::from_foreign_item_kind(&i.kind), - None, + AstTarget::ForeignItem(i), ); let (ident, kind) = match &i.kind { ForeignItemKind::Fn(Fn { sig, ident, generics, define_opaque, .. }) => { @@ -699,7 +698,7 @@ impl<'hir> LoweringContext<'_, 'hir> { self.dcx().span_fatal(v.span, "unnamed enum variants are not yet implemented"); } let hir_id = self.lower_node_id(v.id); - self.lower_attrs(hir_id, &v.attrs, v.span, Target::Variant, None); + self.lower_attrs(hir_id, &v.attrs, v.span, Target::Variant, AstTarget::Variant(v)); hir::Variant { hir_id, def_id: self.local_def_id(v.id), @@ -786,7 +785,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let ty = self.lower_ty_alloc(&f.ty, ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy)); let hir_id = self.lower_node_id(f.id); - self.lower_attrs(hir_id, &f.attrs, f.span, Target::Field, None); + self.lower_attrs(hir_id, &f.attrs, f.span, Target::Field, AstTarget::FieldDef(f)); hir::FieldDef { span: self.lower_span(f.span), hir_id, @@ -814,7 +813,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &i.attrs, i.span, Target::from_assoc_item_kind(&i.kind, AssocCtxt::Trait), - Some(AstTarget::AssocItem(i)), + AstTarget::AssocItem(i), ); let (ident, generics, kind, has_value) = match &i.kind { @@ -1077,7 +1076,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &i.attrs, i.span, Target::from_assoc_item_kind(&i.kind, AssocCtxt::Impl { of_trait: is_in_trait_impl }), - Some(AstTarget::AssocItem(i)), + AstTarget::AssocItem(i), ); let (ident, (generics, kind)) = match &i.kind { @@ -1246,7 +1245,7 @@ impl<'hir> LoweringContext<'_, 'hir> { fn lower_param(&mut self, param: &Param) -> hir::Param<'hir> { let hir_id = self.lower_node_id(param.id); - self.lower_attrs(hir_id, ¶m.attrs, param.span, Target::Param, None); + self.lower_attrs(hir_id, ¶m.attrs, param.span, Target::Param, AstTarget::Param(param)); hir::Param { hir_id, pat: self.lower_pat(¶m.pat), @@ -1946,7 +1945,13 @@ impl<'hir> LoweringContext<'_, 'hir> { ) -> hir::WherePredicate<'hir> { let hir_id = self.lower_node_id(pred.id); let span = self.lower_span(pred.span); - self.lower_attrs(hir_id, &pred.attrs, span, Target::WherePredicate, None); + self.lower_attrs( + hir_id, + &pred.attrs, + span, + Target::WherePredicate, + AstTarget::WherePredicate(pred), + ); let kind = self.arena.alloc(match &pred.kind { WherePredicateKind::BoundPredicate(WhereBoundPredicate { bound_generic_params, diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 24d032982370c..8ab6c662023d5 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -47,8 +47,7 @@ use rustc_ast::visit::{self, Visitor}; use rustc_ast::{self as ast, *}; use rustc_attr_ir::find_attr; use rustc_attr_ir::lang_items::LangItem; -use rustc_attr_ir::target::Target; -use rustc_attr_ir::target::AstTarget; +use rustc_attr_ir::target::{AstTarget, Target}; use rustc_attr_parsing::{AttributeParser, Recovery, ShouldEmit}; use rustc_data_structures::fx::FxIndexMap; use rustc_data_structures::sorted_map::SortedMap; @@ -756,7 +755,13 @@ fn lower_to_hir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::MaybeOwner<'_> { AstOwner::Crate(c) => with_lctx(tcx, &*resolver, CRATE_NODE_ID, |lctx| { debug_assert_eq!(lctx.curr_owner.owner_id(), CRATE_OWNER_ID); let module = lctx.lower_mod(&c.items, &c.spans); - lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, c.spans.inner_span, Target::Crate); + lctx.lower_attrs( + hir::CRATE_HIR_ID, + &c.attrs, + c.spans.inner_span, + Target::Crate, + AstTarget::Crate(c), + ); hir::OwnerNode::Crate(module) }), AstOwner::Item(item) => { @@ -1146,7 +1151,7 @@ impl<'hir> LoweringContext<'_, 'hir> { attrs: &[Attribute], target_span: Span, target: Target, - ast_target: Option>, + ast_target: AstTarget<'_>, ) -> &'hir [rustc_attr_ir::Attribute] { self.lower_attrs_with_extra(id, attrs, target_span, target, ast_target, &[]) } @@ -1157,7 +1162,7 @@ impl<'hir> LoweringContext<'_, 'hir> { attrs: &[Attribute], target_span: Span, target: Target, - ast_target: Option>, + ast_target: AstTarget<'_>, extra_hir_attributes: &[rustc_attr_ir::Attribute], ) -> &'hir [rustc_attr_ir::Attribute] { if attrs.is_empty() && extra_hir_attributes.is_empty() { @@ -1191,7 +1196,7 @@ impl<'hir> LoweringContext<'_, 'hir> { target_span: Span, target_hir_id: HirId, target: Target, - ast_target: Option>, + ast_target: AstTarget<'_>, ) -> Vec { let l = self.span_lowerer(); self.attribute_parser.parse_attribute_list( @@ -2274,7 +2279,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let hir_id = self.lower_node_id(param.id); let param_attrs = ¶m.attrs; let param_span = param.span(); - let param = hir::GenericParam { + let param_hir = hir::GenericParam { hir_id, def_id: self.local_def_id(param.id), name, @@ -2284,8 +2289,14 @@ impl<'hir> LoweringContext<'_, 'hir> { colon_span: param.colon_span.map(|s| self.lower_span(s)), source, }; - self.lower_attrs(hir_id, param_attrs, param_span, Target::from(¶m), None); - param + self.lower_attrs( + hir_id, + param_attrs, + param_span, + Target::from(¶m_hir), + AstTarget::GenericParam(param), + ); + param_hir } fn lower_generic_param_kind( @@ -2890,7 +2901,13 @@ impl<'hir> LoweringContext<'_, 'hir> { // FIXME(mgca): This might result in lowering attributes that // then go unused as the `Target::ExprField` is not actually // corresponding to `Node::ExprField`. - self.lower_attrs(hir_id, &f.attrs, f.span, Target::ExprField, None); + self.lower_attrs( + hir_id, + &f.attrs, + f.span, + Target::ExprField, + AstTarget::Expr(expr), + ); let expr = self.lower_expr_to_const_arg_direct(&f.expr, None); &*self.arena.alloc(hir::ConstArgExprField { diff --git a/compiler/rustc_ast_lowering/src/pat.rs b/compiler/rustc_ast_lowering/src/pat.rs index c7a7f81a5b4d7..2f2c5d7747269 100644 --- a/compiler/rustc_ast_lowering/src/pat.rs +++ b/compiler/rustc_ast_lowering/src/pat.rs @@ -92,7 +92,13 @@ impl<'hir> LoweringContext<'_, 'hir> { let fs = self.arena.alloc_from_iter(fields.iter().map(|f| { let hir_id = self.lower_node_id(f.id); - self.lower_attrs(hir_id, &f.attrs, f.span, Target::PatField, None); + self.lower_attrs( + hir_id, + &f.attrs, + f.span, + Target::PatField, + rustc_attr_ir::target::AstTarget::Pat(pattern), + ); hir::PatField { hir_id, diff --git a/compiler/rustc_attr_ir/src/target.rs b/compiler/rustc_attr_ir/src/target.rs index 57875d915e047..1ebe105203b14 100644 --- a/compiler/rustc_attr_ir/src/target.rs +++ b/compiler/rustc_attr_ir/src/target.rs @@ -4,15 +4,33 @@ use std::fmt::{self, Display}; use rustc_abi::ExternAbi; pub use rustc_ast::visit::AssocCtxt; -use rustc_ast::{AssocItemKind, ForeignItemKind, Item, ast}; +use rustc_ast::{ + Arm, AssocItemKind, Closure, Crate, Expr, ExprField, FieldDef, ForeignItemKind, GenericParam, + Item, Local, Param, Pat, Variant, WherePredicate, ast, +}; use rustc_macros::StableHash; // This enum lists all possible types of AST items. -// FIXME: Currently, this enum only lists `Item` and `AssocItem`, but in the future, be exhaustive. #[derive(Clone, Copy, Debug)] pub enum AstTarget<'a> { - Item(&'a Item), AssocItem(&'a Item), + ForeignItem(&'a Item), + Item(&'a Item), + + Arm(&'a Arm), + Closure(&'a Closure), + Crate(&'a Crate), + Expr(&'a Expr), + ExprField(&'a ExprField), + FieldDef(&'a FieldDef), + GenericParam(&'a GenericParam), + Local(&'a Local), + Param(&'a Param), + Pat(&'a Pat), + Variant(&'a Variant), + WherePredicate(&'a WherePredicate), + + None, // Used when it is not possible to get detailed information about the target. } #[derive(Copy, Clone, PartialEq, Debug, Eq, StableHash)] @@ -89,6 +107,13 @@ impl AstTarget<'_> { }; fn_item.sig.header.ext } + AstTarget::ForeignItem(foreign_item) => { + let ast::ForeignItemKind::Fn(fn_item) = &foreign_item.kind else { + return None; + }; + fn_item.sig.header.ext + } + _ => return None, }; match ext { @@ -112,6 +137,13 @@ impl AstTarget<'_> { }; Some(&fn_item.sig) } + AstTarget::ForeignItem(foreign_item) => { + let ast::ForeignItemKind::Fn(fn_item) = &foreign_item.kind else { + return None; + }; + Some(&fn_item.sig) + } + _ => None, } } } diff --git a/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs b/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs index 2d74a289b0857..0feca7c1941b7 100644 --- a/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs +++ b/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs @@ -336,13 +336,9 @@ impl AttributeParser for NakedParser { | Target::Method( MethodKind::Trait { body: true } | MethodKind::TraitImpl | MethodKind::Inherent, ) => { - let Some(ast_target) = cx.ast_target else { - panic!("missing AST target for {:?}", cx.target); - }; - let fn_sig = - ast_target.get_fn_sig().expect("missing fn signature for AST target"); - let Some(abi) = ast_target.get_abi() else { + cx.ast_target.get_fn_sig().expect("missing fn signature for AST target"); + let Some(abi) = cx.ast_target.get_abi() else { return; }; diff --git a/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs b/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs index 3c8afaefa3953..d85c37953103d 100644 --- a/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs +++ b/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs @@ -26,8 +26,7 @@ impl NoArgsAttributeParser for NonExhaustiveParser { return; } - let item = cx.ast_target.expect("missing AST target item for Target::Struct"); - match item { + match cx.ast_target { rustc_attr_ir::target::AstTarget::Item(ast_item) => { let ItemKind::Struct(_, _, data) = &ast_item.kind else { panic!("expected struct AST target item for Target::Struct"); diff --git a/compiler/rustc_attr_parsing/src/context.rs b/compiler/rustc_attr_parsing/src/context.rs index 1e550f064fb78..f439193138261 100644 --- a/compiler/rustc_attr_parsing/src/context.rs +++ b/compiler/rustc_attr_parsing/src/context.rs @@ -856,7 +856,7 @@ pub(crate) struct FinalizeCheckContext<'p, 'sess> { pub(crate) parsed_attrs: &'p [Attribute], /// The AST item these attributes were applied to. - pub(crate) ast_target: Option>, + pub(crate) ast_target: rustc_attr_ir::target::AstTarget<'p>, } impl<'p, 'sess: 'p> Deref for FinalizeCheckContext<'p, 'sess> { diff --git a/compiler/rustc_attr_parsing/src/interface.rs b/compiler/rustc_attr_parsing/src/interface.rs index cc998c8e6d864..7f8e481ce7505 100644 --- a/compiler/rustc_attr_parsing/src/interface.rs +++ b/compiler/rustc_attr_parsing/src/interface.rs @@ -161,7 +161,7 @@ impl<'sess> AttributeParser<'sess> { attrs, target_span, target, - None, + rustc_attr_ir::target::AstTarget::None, std::convert::identity, |lint_id, span, kind| { sess.psess.dyn_buffer_lint_sess(lint_id.lint, span, target_node_id, kind.0) @@ -317,7 +317,7 @@ impl<'sess> AttributeParser<'sess> { attrs: &[ast::Attribute], target_span: Span, target: Target, - ast_target: Option>, + ast_target: rustc_attr_ir::target::AstTarget<'_>, lower_span: impl Copy + Fn(Span) -> Span, mut emit_lint: impl FnMut(LintId, MultiSpan, EmitAttribute), ) -> Vec { diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index f3329c3d0398e..2611e2607627f 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -183,7 +183,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> { &i.attrs, i.span, Target::MacroDef, - None, + rustc_attr_ir::target::AstTarget::Item(i), std::convert::identity, |_lint_id, _span, _kind| { // FIXME(jdonszelmann): emit lints here properly From 2f29a0bc32eb612fcd4c9a962fa7113a1d4b471a Mon Sep 17 00:00:00 2001 From: Richard Tjokroutomo Date: Sun, 27 Sep 2026 10:29:57 +0800 Subject: [PATCH 06/13] addressing comments & fixing code after 0929 rebase Signed-off-by: Richard Tjokroutomo --- compiler/rustc_ast_lowering/src/item.rs | 8 +-- compiler/rustc_attr_ir/src/target.rs | 55 +++++-------------- .../src/attributes/codegen_attrs.rs | 3 +- .../src/attributes/link_attrs.rs | 37 ++++++------- .../src/attributes/non_exhaustive.rs | 7 +-- compiler/rustc_resolve/src/def_collector.rs | 2 +- 6 files changed, 40 insertions(+), 72 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 7c2a6f13deb58..d1663d5bd5876 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -165,7 +165,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &i.attrs, i.span, Target::from_ast_item(i), - AstTarget::Item(i), + AstTarget::Item(&i.kind), &extra_hir_attributes, ); @@ -619,7 +619,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &i.attrs, i.span, Target::from_foreign_item_kind(&i.kind), - AstTarget::ForeignItem(i), + AstTarget::ForeignItem(&i.kind), ); let (ident, kind) = match &i.kind { ForeignItemKind::Fn(Fn { sig, ident, generics, define_opaque, .. }) => { @@ -813,7 +813,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &i.attrs, i.span, Target::from_assoc_item_kind(&i.kind, AssocCtxt::Trait), - AstTarget::AssocItem(i), + AstTarget::AssocItem(&i.kind), ); let (ident, generics, kind, has_value) = match &i.kind { @@ -1076,7 +1076,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &i.attrs, i.span, Target::from_assoc_item_kind(&i.kind, AssocCtxt::Impl { of_trait: is_in_trait_impl }), - AstTarget::AssocItem(i), + AstTarget::AssocItem(&i.kind), ); let (ident, (generics, kind)) = match &i.kind { diff --git a/compiler/rustc_attr_ir/src/target.rs b/compiler/rustc_attr_ir/src/target.rs index 1ebe105203b14..7be670b20b2ca 100644 --- a/compiler/rustc_attr_ir/src/target.rs +++ b/compiler/rustc_attr_ir/src/target.rs @@ -6,16 +6,16 @@ use rustc_abi::ExternAbi; pub use rustc_ast::visit::AssocCtxt; use rustc_ast::{ Arm, AssocItemKind, Closure, Crate, Expr, ExprField, FieldDef, ForeignItemKind, GenericParam, - Item, Local, Param, Pat, Variant, WherePredicate, ast, + ItemKind, Local, Param, Pat, Variant, WherePredicate, ast, }; use rustc_macros::StableHash; -// This enum lists all possible types of AST items. +/// This enum lists all possible types of AST items. #[derive(Clone, Copy, Debug)] pub enum AstTarget<'a> { - AssocItem(&'a Item), - ForeignItem(&'a Item), - Item(&'a Item), + AssocItem(&'a AssocItemKind), + ForeignItem(&'a ForeignItemKind), + Item(&'a ItemKind), Arm(&'a Arm), Closure(&'a Closure), @@ -30,7 +30,8 @@ pub enum AstTarget<'a> { Variant(&'a Variant), WherePredicate(&'a WherePredicate), - None, // Used when it is not possible to get detailed information about the target. + /// Used when it is not possible to get detailed information about the target. + None, } #[derive(Copy, Clone, PartialEq, Debug, Eq, StableHash)] @@ -95,24 +96,9 @@ pub enum Target { impl AstTarget<'_> { pub fn get_abi(&self) -> Option { let ext = match self { - AstTarget::Item(item) => { - let ast::ItemKind::Fn(fn_item) = &item.kind else { - return None; - }; - fn_item.sig.header.ext - } - AstTarget::AssocItem(assoc_item) => { - let ast::AssocItemKind::Fn(fn_item) = &assoc_item.kind else { - return None; - }; - fn_item.sig.header.ext - } - AstTarget::ForeignItem(foreign_item) => { - let ast::ForeignItemKind::Fn(fn_item) = &foreign_item.kind else { - return None; - }; - fn_item.sig.header.ext - } + AstTarget::Item(ast::ItemKind::Fn(fn_item)) => fn_item.sig.header.ext, + AstTarget::AssocItem(ast::AssocItemKind::Fn(fn_item)) => fn_item.sig.header.ext, + AstTarget::ForeignItem(ast::ForeignItemKind::Fn(fn_item)) => fn_item.sig.header.ext, _ => return None, }; @@ -125,24 +111,9 @@ impl AstTarget<'_> { pub fn get_fn_sig(&self) -> Option<&rustc_ast::ast::FnSig> { match self { - AstTarget::Item(item) => { - let ast::ItemKind::Fn(fn_item) = &item.kind else { - return None; - }; - Some(&fn_item.sig) - } - AstTarget::AssocItem(assoc_item) => { - let ast::AssocItemKind::Fn(fn_item) = &assoc_item.kind else { - return None; - }; - Some(&fn_item.sig) - } - AstTarget::ForeignItem(foreign_item) => { - let ast::ForeignItemKind::Fn(fn_item) = &foreign_item.kind else { - return None; - }; - Some(&fn_item.sig) - } + AstTarget::Item(ast::ItemKind::Fn(fn_item)) => Some(&fn_item.sig), + AstTarget::AssocItem(ast::AssocItemKind::Fn(fn_item)) => Some(&fn_item.sig), + AstTarget::ForeignItem(ast::ForeignItemKind::Fn(fn_item)) => Some(&fn_item.sig), _ => None, } } diff --git a/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs b/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs index 0feca7c1941b7..e8ee8ed9f6581 100644 --- a/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs +++ b/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs @@ -348,8 +348,7 @@ impl AttributeParser for NakedParser { sym::naked_functions_rustic_abi, fn_sig.span, format!( - "`#[naked]` is currently unstable on `extern \"{}\"` functions", - abi.as_str() + "`#[naked]` is currently unstable on `extern {abi}` functions", ), ) .emit(); diff --git a/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs b/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs index ed911a63eb7fe..ba9f6be111f47 100644 --- a/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs +++ b/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs @@ -262,25 +262,24 @@ impl CombineAttributeParser for LinkParser { } fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) { - if cx.target != Target::ForeignMod { - return; - } - - let item = cx.target_item.expect("missing AST target item for Target::ForeignMod"); - let ItemKind::ForeignMod(fm) = &item.kind else { - panic!("expected foreign module AST target item for Target::ForeignMod"); - }; - let abi = fm.abi.map_or(ExternAbi::FALLBACK, |abi| { - abi.symbol_unescaped.as_str().parse().unwrap_or_else(|_| { - cx.dcx().span_delayed_bug( - abi.span, - "LinkParser::finalize_check was unable to pre-detect the ABI, so it continues to use the recovery value solely to check for unused_attributes in the lint; a user error E0703 will be reported later in lower_abi", - ); - ExternAbi::Rust - }) - }); - if matches!(abi, ExternAbi::Rust) { - cx.emit_lint(UNUSED_ATTRIBUTES, Link, attr_span); + match cx.ast_target { + rustc_attr_ir::target::AstTarget::Item(ast_item) + if let ItemKind::ForeignMod(fm) = &ast_item => + { + let abi = fm.abi.map_or(ExternAbi::FALLBACK, |abi| { + abi.symbol_unescaped.as_str().parse().unwrap_or_else(|_| { + cx.dcx().span_delayed_bug( + abi.span, + "LinkParser::finalize_check was unable to pre-detect the ABI, so it continues to use the recovery value solely to check for unused_attributes in the lint; a user error E0703 will be reported later in lower_abi", + ); + ExternAbi::Rust + }) + }); + if matches!(abi, ExternAbi::Rust) { + cx.emit_lint(UNUSED_ATTRIBUTES, Link, attr_span); + } + } + _ => {} } } } diff --git a/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs b/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs index d85c37953103d..1a3db27763577 100644 --- a/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs +++ b/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs @@ -27,10 +27,9 @@ impl NoArgsAttributeParser for NonExhaustiveParser { } match cx.ast_target { - rustc_attr_ir::target::AstTarget::Item(ast_item) => { - let ItemKind::Struct(_, _, data) = &ast_item.kind else { - panic!("expected struct AST target item for Target::Struct"); - }; + rustc_attr_ir::target::AstTarget::Item(ast_item) + if let ItemKind::Struct(_, _, data) = &ast_item => + { if let VariantData::Struct { fields, .. } = data && fields.iter().any(|f| f.default_value().is_some()) { diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index 2611e2607627f..27bfc53cb4bdc 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -183,7 +183,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> { &i.attrs, i.span, Target::MacroDef, - rustc_attr_ir::target::AstTarget::Item(i), + rustc_attr_ir::target::AstTarget::Item(&i.kind), std::convert::identity, |_lint_id, _span, _kind| { // FIXME(jdonszelmann): emit lints here properly From 9bf8c82eaf7fbf3254650dc5c87ead36f310dfd1 Mon Sep 17 00:00:00 2001 From: Richard Tjokroutomo Date: Thu, 1 Oct 2026 15:42:45 +0800 Subject: [PATCH 07/13] make AstTarget map 1-1 with Target Signed-off-by: Richard Tjokroutomo --- compiler/rustc_ast_lowering/src/block.rs | 2 +- compiler/rustc_ast_lowering/src/contract.rs | 2 +- compiler/rustc_ast_lowering/src/expr.rs | 32 ++- .../rustc_ast_lowering/src/expr/closure.rs | 2 +- compiler/rustc_ast_lowering/src/item.rs | 22 +- compiler/rustc_ast_lowering/src/lib.rs | 6 +- compiler/rustc_ast_lowering/src/pat.rs | 2 +- compiler/rustc_attr_ir/src/target.rs | 216 ++++++++++++++++-- .../src/attributes/codegen_attrs.rs | 3 + .../src/attributes/link_attrs.rs | 5 +- .../src/attributes/macro_attrs.rs | 20 +- .../src/attributes/non_exhaustive.rs | 5 +- compiler/rustc_attr_parsing/src/interface.rs | 2 +- compiler/rustc_resolve/src/def_collector.rs | 2 +- 14 files changed, 259 insertions(+), 62 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/block.rs b/compiler/rustc_ast_lowering/src/block.rs index 0ebc9c1533b2f..cffd17bc16cee 100644 --- a/compiler/rustc_ast_lowering/src/block.rs +++ b/compiler/rustc_ast_lowering/src/block.rs @@ -109,7 +109,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &l.attrs, l.span, Target::Statement, - rustc_attr_ir::target::AstTarget::Local(l), + rustc_attr_ir::target::AstTarget::Local(Some(l)), ); self.arena.alloc(hir::LetStmt { hir_id, super_, ty, pat, init, els, span, source }) } diff --git a/compiler/rustc_ast_lowering/src/contract.rs b/compiler/rustc_ast_lowering/src/contract.rs index a7663d49e0a4a..804fbf718bd13 100644 --- a/compiler/rustc_ast_lowering/src/contract.rs +++ b/compiler/rustc_ast_lowering/src/contract.rs @@ -357,7 +357,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &attrs, span, Target::Expression, - rustc_attr_ir::target::AstTarget::None, + rustc_attr_ir::target::AstTarget::from(Target::Expression), ); let ret_block = self.block_all(span, arena_vec![self; ret_stmt], Some(contract_check)); diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index bf0f819865343..9012df0448cf7 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -236,7 +236,7 @@ impl<'hir> LoweringContext<'_, 'hir> { e.span, ex.hir_id, Target::from_expr(e), - AstTarget::None, + AstTarget::from(Target::from_expr(e)), ) .into_iter() .chain(old_attrs.iter().cloned()); @@ -261,7 +261,13 @@ impl<'hir> LoweringContext<'_, 'hir> { } let expr_hir_id = self.lower_node_id(e.id); - self.lower_attrs(expr_hir_id, &e.attrs, e.span, Target::from_expr(e), AstTarget::Expr(e)); + self.lower_attrs( + expr_hir_id, + &e.attrs, + e.span, + Target::from_expr(e), + AstTarget::Expression(Some(e)), + ); let kind = match &e.kind { ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)), @@ -801,7 +807,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let guard = arm.guard.as_ref().map(|guard| self.lower_expr(&guard.cond)); let hir_id = self.next_id(); let span = self.lower_span(arm.span); - self.lower_attrs(hir_id, &arm.attrs, arm.span, Target::Arm, AstTarget::Arm(arm)); + self.lower_attrs(hir_id, &arm.attrs, arm.span, Target::Arm, AstTarget::Arm(Some(arm))); let is_never_pattern = pat.is_never_pattern(); // We need to lower the body even if it's unneeded for never pattern in match, // ensure that we can get HirId for DefId if need (issue #137708). @@ -1665,7 +1671,13 @@ impl<'hir> LoweringContext<'_, 'hir> { fn lower_expr_field(&mut self, f: &ExprField) -> hir::ExprField<'hir> { let hir_id = self.lower_node_id(f.id); - self.lower_attrs(hir_id, &f.attrs, f.span, Target::ExprField, AstTarget::ExprField(f)); + self.lower_attrs( + hir_id, + &f.attrs, + f.span, + Target::ExprField, + AstTarget::ExprField(Some(f)), + ); hir::ExprField { hir_id, ident: self.lower_ident(f.ident), @@ -1932,7 +1944,13 @@ impl<'hir> LoweringContext<'_, 'hir> { // // Also, add the attributes to the outer returned expr node. let expr = self.expr_drop_temps_mut(for_span, match_expr); - self.lower_attrs(expr.hir_id, &e.attrs, e.span, Target::from_expr(e), AstTarget::Expr(e)); + self.lower_attrs( + expr.hir_id, + &e.attrs, + e.span, + Target::from_expr(e), + AstTarget::Expression(Some(e)), + ); expr } @@ -1985,7 +2003,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &attrs, span, Target::Expression, - AstTarget::Expr(sub_expr), + AstTarget::Expression(Some(sub_expr)), ); let continue_pat = self.pat_cf_continue(unstable_span, val_pat); self.arm(continue_pat, val_expr, try_span) @@ -2033,7 +2051,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &attrs, span, Target::Expression, - AstTarget::Expr(sub_expr), + AstTarget::Expression(Some(sub_expr)), ); let break_pat = self.pat_cf_break(try_span, residual_local); diff --git a/compiler/rustc_ast_lowering/src/expr/closure.rs b/compiler/rustc_ast_lowering/src/expr/closure.rs index 99ac5d1743731..4d948db3c38b5 100644 --- a/compiler/rustc_ast_lowering/src/expr/closure.rs +++ b/compiler/rustc_ast_lowering/src/expr/closure.rs @@ -25,7 +25,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &e.attrs, e.span, Target::from_expr(e), - rustc_attr_ir::target::AstTarget::Closure(closure), + rustc_attr_ir::target::AstTarget::Closure(Some(closure)), ); match closure.coroutine_marker { diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index d1663d5bd5876..0a169a3b53273 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -165,7 +165,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &i.attrs, i.span, Target::from_ast_item(i), - AstTarget::Item(&i.kind), + AstTarget::from_ast_item(&i.kind), &extra_hir_attributes, ); @@ -619,7 +619,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &i.attrs, i.span, Target::from_foreign_item_kind(&i.kind), - AstTarget::ForeignItem(&i.kind), + AstTarget::from_foreign_item_kind(&i.kind), ); let (ident, kind) = match &i.kind { ForeignItemKind::Fn(Fn { sig, ident, generics, define_opaque, .. }) => { @@ -698,7 +698,7 @@ impl<'hir> LoweringContext<'_, 'hir> { self.dcx().span_fatal(v.span, "unnamed enum variants are not yet implemented"); } let hir_id = self.lower_node_id(v.id); - self.lower_attrs(hir_id, &v.attrs, v.span, Target::Variant, AstTarget::Variant(v)); + self.lower_attrs(hir_id, &v.attrs, v.span, Target::Variant, AstTarget::Variant(Some(v))); hir::Variant { hir_id, def_id: self.local_def_id(v.id), @@ -785,7 +785,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let ty = self.lower_ty_alloc(&f.ty, ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy)); let hir_id = self.lower_node_id(f.id); - self.lower_attrs(hir_id, &f.attrs, f.span, Target::Field, AstTarget::FieldDef(f)); + self.lower_attrs(hir_id, &f.attrs, f.span, Target::Field, AstTarget::Field(Some(f))); hir::FieldDef { span: self.lower_span(f.span), hir_id, @@ -813,7 +813,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &i.attrs, i.span, Target::from_assoc_item_kind(&i.kind, AssocCtxt::Trait), - AstTarget::AssocItem(&i.kind), + AstTarget::from_assoc_item_kind(&i.kind), ); let (ident, generics, kind, has_value) = match &i.kind { @@ -1076,7 +1076,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &i.attrs, i.span, Target::from_assoc_item_kind(&i.kind, AssocCtxt::Impl { of_trait: is_in_trait_impl }), - AstTarget::AssocItem(&i.kind), + AstTarget::from_assoc_item_kind(&i.kind), ); let (ident, (generics, kind)) = match &i.kind { @@ -1245,7 +1245,13 @@ impl<'hir> LoweringContext<'_, 'hir> { fn lower_param(&mut self, param: &Param) -> hir::Param<'hir> { let hir_id = self.lower_node_id(param.id); - self.lower_attrs(hir_id, ¶m.attrs, param.span, Target::Param, AstTarget::Param(param)); + self.lower_attrs( + hir_id, + ¶m.attrs, + param.span, + Target::Param, + AstTarget::Param(Some(param)), + ); hir::Param { hir_id, pat: self.lower_pat(¶m.pat), @@ -1950,7 +1956,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &pred.attrs, span, Target::WherePredicate, - AstTarget::WherePredicate(pred), + AstTarget::WherePredicate(Some(pred)), ); let kind = self.arena.alloc(match &pred.kind { WherePredicateKind::BoundPredicate(WhereBoundPredicate { diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 8ab6c662023d5..34414be853d8c 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -760,7 +760,7 @@ fn lower_to_hir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::MaybeOwner<'_> { &c.attrs, c.spans.inner_span, Target::Crate, - AstTarget::Crate(c), + AstTarget::Crate(Some(c)), ); hir::OwnerNode::Crate(module) }), @@ -2294,7 +2294,7 @@ impl<'hir> LoweringContext<'_, 'hir> { param_attrs, param_span, Target::from(¶m_hir), - AstTarget::GenericParam(param), + AstTarget::GenericParam(Some(param)), ); param_hir } @@ -2906,7 +2906,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &f.attrs, f.span, Target::ExprField, - AstTarget::Expr(expr), + AstTarget::Expression(Some(expr)), ); let expr = self.lower_expr_to_const_arg_direct(&f.expr, None); diff --git a/compiler/rustc_ast_lowering/src/pat.rs b/compiler/rustc_ast_lowering/src/pat.rs index 2f2c5d7747269..d854b4db64d37 100644 --- a/compiler/rustc_ast_lowering/src/pat.rs +++ b/compiler/rustc_ast_lowering/src/pat.rs @@ -97,7 +97,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &f.attrs, f.span, Target::PatField, - rustc_attr_ir::target::AstTarget::Pat(pattern), + rustc_attr_ir::target::AstTarget::Pat(Some(pattern)), ); hir::PatField { diff --git a/compiler/rustc_attr_ir/src/target.rs b/compiler/rustc_attr_ir/src/target.rs index 7be670b20b2ca..6216de0c5eaf0 100644 --- a/compiler/rustc_attr_ir/src/target.rs +++ b/compiler/rustc_attr_ir/src/target.rs @@ -5,33 +5,82 @@ use std::fmt::{self, Display}; use rustc_abi::ExternAbi; pub use rustc_ast::visit::AssocCtxt; use rustc_ast::{ - Arm, AssocItemKind, Closure, Crate, Expr, ExprField, FieldDef, ForeignItemKind, GenericParam, - ItemKind, Local, Param, Pat, Variant, WherePredicate, ast, + Arm, AssocItemKind, Closure, Crate, Expr, ExprField, FieldDef, ForLoop, ForeignItemKind, + GenericParam, ItemKind, Local, Param, Pat, PatField, Stmt, Variant, WherePredicate, ast, }; use rustc_macros::StableHash; /// This enum lists all possible types of AST items. #[derive(Clone, Copy, Debug)] pub enum AstTarget<'a> { + // Target types that may correspond to different kinds of items. + Delegation { target: Option>, mac: bool }, + MacroCall(Option>), + + // Target types that correspond exclusively to `AssocItem` kind. Mapping is obtained from `Target::from_assoc_item_kind`. + AssocConst(Option<&'a AssocItemKind>), + Method(Option<&'a AssocItemKind>), + AssocTy(Option<&'a AssocItemKind>), + + // Target types that correspond exclusively to `ForeignItem` kind. Mapping is obtained from `Target::from_foreign_item_kind`. + ForeignStatic(Option<&'a ForeignItemKind>), + ForeignFn(Option<&'a ForeignItemKind>), + ForeignTy(Option<&'a ForeignItemKind>), + + // Target types that correspond exclusively to `Item` kind. Mapping is obtained from `Target::from_ast_item`. + ExternCrate(Option<&'a ItemKind>), + Use(Option<&'a ItemKind>), + Static(Option<&'a ItemKind>), + Const(Option<&'a ItemKind>), + Fn(Option<&'a ItemKind>), + Mod(Option<&'a ItemKind>), + ForeignMod(Option<&'a ItemKind>), + GlobalAsm(Option<&'a ItemKind>), + TyAlias(Option<&'a ItemKind>), + Enum(Option<&'a ItemKind>), + Struct(Option<&'a ItemKind>), + Union(Option<&'a ItemKind>), + Trait(Option<&'a ItemKind>), + TraitAlias(Option<&'a ItemKind>), + Impl { item: Option<&'a ItemKind>, of_trait: bool }, + MacroDef(Option<&'a ItemKind>), + + // Target types that correspond exclusively to `Expr` kind. Mapping is obtained from `Target::from_expr`. + Closure(Option<&'a Closure>), + Expression(Option<&'a Expr>), + ForLoop(Option<&'a ForLoop>), + Loop, + While, + Break, + + Arm(Option<&'a Arm>), + ConstParam(Option<&'a GenericParam>), + Crate(Option<&'a Crate>), + ExprField(Option<&'a ExprField>), + Field(Option<&'a FieldDef>), + GenericParam(Option<&'a GenericParam>), + LifetimeParam(Option<&'a GenericParam>), + Local(Option<&'a Local>), + Param(Option<&'a Param>), + Pat(Option<&'a Pat>), + PatField(Option<&'a PatField>), + Statement(Option<&'a Stmt>), + TypeParam(Option<&'a GenericParam>), + Variant(Option<&'a Variant>), + WherePredicate(Option<&'a WherePredicate>), +} + +#[derive(Clone, Copy, Debug)] +pub enum DelegationAstTarget<'a> { AssocItem(&'a AssocItemKind), - ForeignItem(&'a ForeignItemKind), Item(&'a ItemKind), +} - Arm(&'a Arm), - Closure(&'a Closure), - Crate(&'a Crate), - Expr(&'a Expr), - ExprField(&'a ExprField), - FieldDef(&'a FieldDef), - GenericParam(&'a GenericParam), - Local(&'a Local), - Param(&'a Param), - Pat(&'a Pat), - Variant(&'a Variant), - WherePredicate(&'a WherePredicate), - - /// Used when it is not possible to get detailed information about the target. - None, +#[derive(Clone, Copy, Debug)] +pub enum MacroCallAstTarget<'a> { + AssocItem(&'a AssocItemKind), + ForeignItem(&'a ForeignItemKind), + Item(&'a ItemKind), } #[derive(Copy, Clone, PartialEq, Debug, Eq, StableHash)] @@ -47,6 +96,8 @@ pub enum MethodKind { Inherent, } +// FIXME(rtjkro): `AstTarget` has nearly one-to-one mapping with `Target`, barring the extra fields from `AssocConst`, `Method`, and `AssocTy`. +// In the future, remove `Target` and use `AstTarget` instead. #[derive(Copy, Clone, PartialEq, Debug, Eq, StableHash)] pub enum Target { ExternCrate, @@ -93,12 +144,12 @@ pub enum Target { Break, } -impl AstTarget<'_> { +impl<'a> AstTarget<'a> { pub fn get_abi(&self) -> Option { let ext = match self { - AstTarget::Item(ast::ItemKind::Fn(fn_item)) => fn_item.sig.header.ext, - AstTarget::AssocItem(ast::AssocItemKind::Fn(fn_item)) => fn_item.sig.header.ext, - AstTarget::ForeignItem(ast::ForeignItemKind::Fn(fn_item)) => fn_item.sig.header.ext, + AstTarget::Method(Some(ast::AssocItemKind::Fn(fn_item))) => fn_item.sig.header.ext, + AstTarget::Fn(Some(ast::ItemKind::Fn(fn_item))) => fn_item.sig.header.ext, + AstTarget::ForeignFn(Some(ast::ForeignItemKind::Fn(fn_item))) => fn_item.sig.header.ext, _ => return None, }; @@ -111,12 +162,127 @@ impl AstTarget<'_> { pub fn get_fn_sig(&self) -> Option<&rustc_ast::ast::FnSig> { match self { - AstTarget::Item(ast::ItemKind::Fn(fn_item)) => Some(&fn_item.sig), - AstTarget::AssocItem(ast::AssocItemKind::Fn(fn_item)) => Some(&fn_item.sig), - AstTarget::ForeignItem(ast::ForeignItemKind::Fn(fn_item)) => Some(&fn_item.sig), + AstTarget::Method(Some(ast::AssocItemKind::Fn(fn_item))) => Some(&fn_item.sig), + AstTarget::Fn(Some(ast::ItemKind::Fn(fn_item))) => Some(&fn_item.sig), + AstTarget::ForeignFn(Some(ast::ForeignItemKind::Fn(fn_item))) => Some(&fn_item.sig), _ => None, } } + + pub fn from_foreign_item_kind(kind: &'a ast::ForeignItemKind) -> Self { + match kind { + ForeignItemKind::Static(_) => AstTarget::ForeignStatic(Some(kind)), + ForeignItemKind::Fn(_) => AstTarget::ForeignFn(Some(kind)), + ForeignItemKind::TyAlias(_) => AstTarget::ForeignTy(Some(kind)), + ForeignItemKind::MacCall(_) => { + AstTarget::MacroCall(Some(MacroCallAstTarget::ForeignItem(kind))) + } + } + } + + pub fn from_assoc_item_kind(kind: &'a ast::AssocItemKind) -> Self { + match kind { + AssocItemKind::Const(_) => AstTarget::AssocConst(Some(kind)), + AssocItemKind::Fn(_) => AstTarget::Method(Some(kind)), + AssocItemKind::Type(_) => AstTarget::AssocTy(Some(kind)), + AssocItemKind::Delegation(_) => AstTarget::Delegation { + target: Some(DelegationAstTarget::AssocItem(kind)), + mac: false, + }, + AssocItemKind::DelegationMac(_) => AstTarget::Delegation { + target: Some(DelegationAstTarget::AssocItem(kind)), + mac: true, + }, + AssocItemKind::MacCall(_) => { + AstTarget::MacroCall(Some(MacroCallAstTarget::AssocItem(kind))) + } + } + } + + pub fn from_ast_item(kind: &'a ast::ItemKind) -> Self { + match kind { + ast::ItemKind::ExternCrate(..) => AstTarget::ExternCrate(Some(kind)), + ast::ItemKind::Use(..) => AstTarget::Use(Some(kind)), + ast::ItemKind::Static { .. } => AstTarget::Static(Some(kind)), + ast::ItemKind::Const(..) => AstTarget::Const(Some(kind)), + ast::ItemKind::ConstBlock(..) => AstTarget::Const(Some(kind)), + ast::ItemKind::Fn { .. } => AstTarget::Fn(Some(kind)), + ast::ItemKind::Mod(..) => AstTarget::Mod(Some(kind)), + ast::ItemKind::ForeignMod { .. } => AstTarget::ForeignMod(Some(kind)), + ast::ItemKind::GlobalAsm { .. } => AstTarget::GlobalAsm(Some(kind)), + ast::ItemKind::TyAlias(..) => AstTarget::TyAlias(Some(kind)), + ast::ItemKind::Enum(..) => AstTarget::Enum(Some(kind)), + ast::ItemKind::Struct(..) => AstTarget::Struct(Some(kind)), + ast::ItemKind::Union(..) => AstTarget::Union(Some(kind)), + ast::ItemKind::Trait(..) => AstTarget::Trait(Some(kind)), + ast::ItemKind::TraitAlias(..) => AstTarget::TraitAlias(Some(kind)), + ast::ItemKind::Impl(i) => { + AstTarget::Impl { item: Some(kind), of_trait: i.of_trait.is_some() } + } + ast::ItemKind::MacCall(..) => { + AstTarget::MacroCall(Some(MacroCallAstTarget::Item(kind))) + } + ast::ItemKind::MacroDef(..) => AstTarget::MacroDef(Some(kind)), + ast::ItemKind::Delegation(..) => { + AstTarget::Delegation { target: Some(DelegationAstTarget::Item(kind)), mac: false } + } + ast::ItemKind::DelegationMac(..) => { + AstTarget::Delegation { target: Some(DelegationAstTarget::Item(kind)), mac: true } + } + ast::ItemKind::TestBinderConstraints(..) => { + AstTarget::MacroCall(Some(MacroCallAstTarget::Item(kind))) + } + } + } +} + +impl<'a> From for AstTarget<'a> { + fn from(target: Target) -> Self { + match target { + Target::ExternCrate => AstTarget::ExternCrate(None), + Target::Use => AstTarget::Use(None), + Target::Static => AstTarget::Static(None), + Target::Const => AstTarget::Const(None), + Target::Fn => AstTarget::Fn(None), + Target::Closure => AstTarget::Closure(None), + Target::Mod => AstTarget::Mod(None), + Target::ForeignMod => AstTarget::ForeignMod(None), + Target::GlobalAsm => AstTarget::GlobalAsm(None), + Target::TyAlias => AstTarget::TyAlias(None), + Target::Enum => AstTarget::Enum(None), + Target::Variant => AstTarget::Variant(None), + Target::Struct => AstTarget::Struct(None), + Target::Field => AstTarget::Field(None), + Target::Union => AstTarget::Union(None), + Target::Trait => AstTarget::Trait(None), + Target::TraitAlias => AstTarget::TraitAlias(None), + Target::Impl { of_trait } => AstTarget::Impl { item: None, of_trait }, + Target::Expression => AstTarget::Expression(None), + Target::Statement => AstTarget::Statement(None), + Target::Arm => AstTarget::Arm(None), + Target::AssocConst(_assoc_ctxt) => AstTarget::AssocConst(None), + Target::Method(_method_kind) => AstTarget::Method(None), + Target::AssocTy(_assoc_ctxt) => AstTarget::AssocTy(None), + Target::ForeignFn => AstTarget::ForeignFn(None), + Target::ForeignStatic => AstTarget::ForeignStatic(None), + Target::ForeignTy => AstTarget::ForeignTy(None), + Target::LifetimeParam => AstTarget::LifetimeParam(None), + Target::TypeParam => AstTarget::TypeParam(None), + Target::ConstParam => AstTarget::ConstParam(None), + Target::MacroDef => AstTarget::MacroDef(None), + Target::Param => AstTarget::Param(None), + Target::PatField => AstTarget::PatField(None), + Target::ExprField => AstTarget::ExprField(None), + Target::WherePredicate => AstTarget::WherePredicate(None), + Target::MacroCall => AstTarget::MacroCall(None), + Target::Crate => AstTarget::Crate(None), + Target::Delegation { mac } => AstTarget::Delegation { target: None, mac }, + Target::ForLoop => AstTarget::ForLoop(None), + Target::While => AstTarget::While, + Target::Loop => AstTarget::Loop, + Target::Break => AstTarget::Break, + } + } } impl Display for Target { diff --git a/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs b/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs index e8ee8ed9f6581..234e217d9f2c8 100644 --- a/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs +++ b/compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs @@ -330,6 +330,9 @@ impl AttributeParser for NakedParser { } fn deferred_finalize_check(&self) -> Option<(FinalizeCheckFn, Span)> { + // FIXME(rtjkro): Currently, we're enum matching on `cx.target` instead of `cx.ast_target` + // because `AstTarget::Method` doesn't contain `MethodKind` yet. In the future, when this field is available to `AstTarget::Method`, + // update this check to use `cx.ast_target` instead of `cx.target`. Some(( |cx, _| match cx.target { Target::Fn diff --git a/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs b/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs index ba9f6be111f47..a122713ce1930 100644 --- a/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs +++ b/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs @@ -263,8 +263,9 @@ impl CombineAttributeParser for LinkParser { fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) { match cx.ast_target { - rustc_attr_ir::target::AstTarget::Item(ast_item) - if let ItemKind::ForeignMod(fm) = &ast_item => + rustc_attr_ir::target::AstTarget::ForeignMod(ast_item) + if let ItemKind::ForeignMod(fm) = + &ast_item.expect("missing AST target item for astTarget::ForeignMod") => { let abi = fm.abi.map_or(ExternAbi::FALLBACK, |abi| { abi.symbol_unescaped.as_str().parse().unwrap_or_else(|_| { diff --git a/compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs b/compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs index 8bb5e6b7fc270..5e2e0db135961 100644 --- a/compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs +++ b/compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs @@ -1,4 +1,5 @@ use rustc_ast::ItemKind; +use rustc_attr_ir::target::AstTarget; use rustc_attr_ir::{MacroUseArgs, find_attr}; use rustc_feature::AttributeStability; use rustc_lint_defs::builtin::{INVALID_MACRO_EXPORT_ARGUMENTS, UNUSED_ATTRIBUTES}; @@ -181,15 +182,16 @@ impl SingleAttributeParser for MacroExportParser { } fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) { - if cx.target != Target::MacroDef { - return; - } - - let item = cx.target_item.unwrap(); - if let ItemKind::MacroDef(_, macro_def) = &item.kind - && !macro_def.macro_rules - { - cx.emit_lint(UNUSED_ATTRIBUTES, MacroExport::OnDeclMacro, attr_span); + match cx.ast_target { + AstTarget::MacroDef(item_kind) => { + if let ItemKind::MacroDef(_, macro_def) = + item_kind.expect("AstTarget::MacroDef should always have an ItemKind::MacroDef") + && !macro_def.macro_rules + { + cx.emit_lint(UNUSED_ATTRIBUTES, MacroExport::OnDeclMacro, attr_span); + } + } + _ => {} } } } diff --git a/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs b/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs index 1a3db27763577..c4ff1f220f581 100644 --- a/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs +++ b/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs @@ -27,8 +27,9 @@ impl NoArgsAttributeParser for NonExhaustiveParser { } match cx.ast_target { - rustc_attr_ir::target::AstTarget::Item(ast_item) - if let ItemKind::Struct(_, _, data) = &ast_item => + rustc_attr_ir::target::AstTarget::Struct(ast_item) + if let ItemKind::Struct(_, _, data) = + &ast_item.expect("missing AST target item for AstTarget::Struct") => { if let VariantData::Struct { fields, .. } = data && fields.iter().any(|f| f.default_value().is_some()) diff --git a/compiler/rustc_attr_parsing/src/interface.rs b/compiler/rustc_attr_parsing/src/interface.rs index 7f8e481ce7505..17cf5805adede 100644 --- a/compiler/rustc_attr_parsing/src/interface.rs +++ b/compiler/rustc_attr_parsing/src/interface.rs @@ -161,7 +161,7 @@ impl<'sess> AttributeParser<'sess> { attrs, target_span, target, - rustc_attr_ir::target::AstTarget::None, + rustc_attr_ir::target::AstTarget::from(target), std::convert::identity, |lint_id, span, kind| { sess.psess.dyn_buffer_lint_sess(lint_id.lint, span, target_node_id, kind.0) diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index 27bfc53cb4bdc..fb2eac959d292 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -183,7 +183,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> { &i.attrs, i.span, Target::MacroDef, - rustc_attr_ir::target::AstTarget::Item(&i.kind), + rustc_attr_ir::target::AstTarget::from_ast_item(&i.kind), std::convert::identity, |_lint_id, _span, _kind| { // FIXME(jdonszelmann): emit lints here properly From 7889ef68fdd0b7e88b8fa30eab531cceda0d52f9 Mon Sep 17 00:00:00 2001 From: Richard Tjokroutomo Date: Mon, 5 Oct 2026 22:51:29 +0800 Subject: [PATCH 08/13] remove options Signed-off-by: Richard Tjokroutomo --- compiler/rustc_ast_lowering/src/block.rs | 2 +- compiler/rustc_ast_lowering/src/contract.rs | 2 +- compiler/rustc_ast_lowering/src/expr.rs | 20 +- .../rustc_ast_lowering/src/expr/closure.rs | 2 +- compiler/rustc_ast_lowering/src/item.rs | 14 +- compiler/rustc_ast_lowering/src/lib.rs | 14 +- compiler/rustc_ast_lowering/src/pat.rs | 2 +- compiler/rustc_attr_ir/src/target.rs | 222 +++++++----------- .../src/attributes/link_attrs.rs | 3 +- .../src/attributes/macro_attrs.rs | 3 +- .../src/attributes/non_exhaustive.rs | 3 +- compiler/rustc_attr_parsing/src/interface.rs | 2 +- 12 files changed, 114 insertions(+), 175 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/block.rs b/compiler/rustc_ast_lowering/src/block.rs index cffd17bc16cee..0ebc9c1533b2f 100644 --- a/compiler/rustc_ast_lowering/src/block.rs +++ b/compiler/rustc_ast_lowering/src/block.rs @@ -109,7 +109,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &l.attrs, l.span, Target::Statement, - rustc_attr_ir::target::AstTarget::Local(Some(l)), + rustc_attr_ir::target::AstTarget::Local(l), ); self.arena.alloc(hir::LetStmt { hir_id, super_, ty, pat, init, els, span, source }) } diff --git a/compiler/rustc_ast_lowering/src/contract.rs b/compiler/rustc_ast_lowering/src/contract.rs index 804fbf718bd13..a7663d49e0a4a 100644 --- a/compiler/rustc_ast_lowering/src/contract.rs +++ b/compiler/rustc_ast_lowering/src/contract.rs @@ -357,7 +357,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &attrs, span, Target::Expression, - rustc_attr_ir::target::AstTarget::from(Target::Expression), + rustc_attr_ir::target::AstTarget::None, ); let ret_block = self.block_all(span, arena_vec![self; ret_stmt], Some(contract_check)); diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 9012df0448cf7..8533dc7e3de42 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -236,7 +236,7 @@ impl<'hir> LoweringContext<'_, 'hir> { e.span, ex.hir_id, Target::from_expr(e), - AstTarget::from(Target::from_expr(e)), + AstTarget::Expression(e), ) .into_iter() .chain(old_attrs.iter().cloned()); @@ -266,7 +266,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &e.attrs, e.span, Target::from_expr(e), - AstTarget::Expression(Some(e)), + AstTarget::Expression(e), ); let kind = match &e.kind { @@ -807,7 +807,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let guard = arm.guard.as_ref().map(|guard| self.lower_expr(&guard.cond)); let hir_id = self.next_id(); let span = self.lower_span(arm.span); - self.lower_attrs(hir_id, &arm.attrs, arm.span, Target::Arm, AstTarget::Arm(Some(arm))); + self.lower_attrs(hir_id, &arm.attrs, arm.span, Target::Arm, AstTarget::Arm(arm)); let is_never_pattern = pat.is_never_pattern(); // We need to lower the body even if it's unneeded for never pattern in match, // ensure that we can get HirId for DefId if need (issue #137708). @@ -1671,13 +1671,7 @@ impl<'hir> LoweringContext<'_, 'hir> { fn lower_expr_field(&mut self, f: &ExprField) -> hir::ExprField<'hir> { let hir_id = self.lower_node_id(f.id); - self.lower_attrs( - hir_id, - &f.attrs, - f.span, - Target::ExprField, - AstTarget::ExprField(Some(f)), - ); + self.lower_attrs(hir_id, &f.attrs, f.span, Target::ExprField, AstTarget::ExprField(f)); hir::ExprField { hir_id, ident: self.lower_ident(f.ident), @@ -1949,7 +1943,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &e.attrs, e.span, Target::from_expr(e), - AstTarget::Expression(Some(e)), + AstTarget::Expression(e), ); expr } @@ -2003,7 +1997,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &attrs, span, Target::Expression, - AstTarget::Expression(Some(sub_expr)), + AstTarget::Expression(sub_expr), ); let continue_pat = self.pat_cf_continue(unstable_span, val_pat); self.arm(continue_pat, val_expr, try_span) @@ -2051,7 +2045,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &attrs, span, Target::Expression, - AstTarget::Expression(Some(sub_expr)), + AstTarget::Expression(sub_expr), ); let break_pat = self.pat_cf_break(try_span, residual_local); diff --git a/compiler/rustc_ast_lowering/src/expr/closure.rs b/compiler/rustc_ast_lowering/src/expr/closure.rs index 4d948db3c38b5..99ac5d1743731 100644 --- a/compiler/rustc_ast_lowering/src/expr/closure.rs +++ b/compiler/rustc_ast_lowering/src/expr/closure.rs @@ -25,7 +25,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &e.attrs, e.span, Target::from_expr(e), - rustc_attr_ir::target::AstTarget::Closure(Some(closure)), + rustc_attr_ir::target::AstTarget::Closure(closure), ); match closure.coroutine_marker { diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 0a169a3b53273..3a871e85daa24 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -698,7 +698,7 @@ impl<'hir> LoweringContext<'_, 'hir> { self.dcx().span_fatal(v.span, "unnamed enum variants are not yet implemented"); } let hir_id = self.lower_node_id(v.id); - self.lower_attrs(hir_id, &v.attrs, v.span, Target::Variant, AstTarget::Variant(Some(v))); + self.lower_attrs(hir_id, &v.attrs, v.span, Target::Variant, AstTarget::Variant(v)); hir::Variant { hir_id, def_id: self.local_def_id(v.id), @@ -785,7 +785,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let ty = self.lower_ty_alloc(&f.ty, ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy)); let hir_id = self.lower_node_id(f.id); - self.lower_attrs(hir_id, &f.attrs, f.span, Target::Field, AstTarget::Field(Some(f))); + self.lower_attrs(hir_id, &f.attrs, f.span, Target::Field, AstTarget::Field(f)); hir::FieldDef { span: self.lower_span(f.span), hir_id, @@ -1245,13 +1245,7 @@ impl<'hir> LoweringContext<'_, 'hir> { fn lower_param(&mut self, param: &Param) -> hir::Param<'hir> { let hir_id = self.lower_node_id(param.id); - self.lower_attrs( - hir_id, - ¶m.attrs, - param.span, - Target::Param, - AstTarget::Param(Some(param)), - ); + self.lower_attrs(hir_id, ¶m.attrs, param.span, Target::Param, AstTarget::Param(param)); hir::Param { hir_id, pat: self.lower_pat(¶m.pat), @@ -1956,7 +1950,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &pred.attrs, span, Target::WherePredicate, - AstTarget::WherePredicate(Some(pred)), + AstTarget::WherePredicate(pred), ); let kind = self.arena.alloc(match &pred.kind { WherePredicateKind::BoundPredicate(WhereBoundPredicate { diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 34414be853d8c..74ff46aac7444 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -760,7 +760,7 @@ fn lower_to_hir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::MaybeOwner<'_> { &c.attrs, c.spans.inner_span, Target::Crate, - AstTarget::Crate(Some(c)), + AstTarget::Crate(c), ); hir::OwnerNode::Crate(module) }), @@ -1962,7 +1962,13 @@ impl<'hir> LoweringContext<'_, 'hir> { // FIXME This uses `fn_hir_id`, which is not correct, it should use the parameter hir id instead // The parameter is currently not lowered for functions without bodies, so there is no place to store the lowered hir id // This should be fixed by storing function parameters in the `hir::FnSig` instead of `hir::Body` - self.lower_attrs(fn_hir_id, ¶m.attrs, param.span, Target::Param); + self.lower_attrs( + fn_hir_id, + ¶m.attrs, + param.span, + Target::Param, + AstTarget::None, + ); } let itctx = match kind { FnDeclKind::Fn | FnDeclKind::Inherent | FnDeclKind::Impl | FnDeclKind::Trait => { @@ -2294,7 +2300,7 @@ impl<'hir> LoweringContext<'_, 'hir> { param_attrs, param_span, Target::from(¶m_hir), - AstTarget::GenericParam(Some(param)), + AstTarget::GenericParam(param), ); param_hir } @@ -2906,7 +2912,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &f.attrs, f.span, Target::ExprField, - AstTarget::Expression(Some(expr)), + AstTarget::Expression(expr), ); let expr = self.lower_expr_to_const_arg_direct(&f.expr, None); diff --git a/compiler/rustc_ast_lowering/src/pat.rs b/compiler/rustc_ast_lowering/src/pat.rs index d854b4db64d37..2f2c5d7747269 100644 --- a/compiler/rustc_ast_lowering/src/pat.rs +++ b/compiler/rustc_ast_lowering/src/pat.rs @@ -97,7 +97,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &f.attrs, f.span, Target::PatField, - rustc_attr_ir::target::AstTarget::Pat(Some(pattern)), + rustc_attr_ir::target::AstTarget::Pat(pattern), ); hir::PatField { diff --git a/compiler/rustc_attr_ir/src/target.rs b/compiler/rustc_attr_ir/src/target.rs index 6216de0c5eaf0..831a86dd7296a 100644 --- a/compiler/rustc_attr_ir/src/target.rs +++ b/compiler/rustc_attr_ir/src/target.rs @@ -14,60 +14,63 @@ use rustc_macros::StableHash; #[derive(Clone, Copy, Debug)] pub enum AstTarget<'a> { // Target types that may correspond to different kinds of items. - Delegation { target: Option>, mac: bool }, - MacroCall(Option>), + Delegation { target: DelegationAstTarget<'a>, mac: bool }, + MacroCall(MacroCallAstTarget<'a>), // Target types that correspond exclusively to `AssocItem` kind. Mapping is obtained from `Target::from_assoc_item_kind`. - AssocConst(Option<&'a AssocItemKind>), - Method(Option<&'a AssocItemKind>), - AssocTy(Option<&'a AssocItemKind>), + AssocConst(&'a AssocItemKind), + Method(&'a AssocItemKind), + AssocTy(&'a AssocItemKind), // Target types that correspond exclusively to `ForeignItem` kind. Mapping is obtained from `Target::from_foreign_item_kind`. - ForeignStatic(Option<&'a ForeignItemKind>), - ForeignFn(Option<&'a ForeignItemKind>), - ForeignTy(Option<&'a ForeignItemKind>), + ForeignStatic(&'a ForeignItemKind), + ForeignFn(&'a ForeignItemKind), + ForeignTy(&'a ForeignItemKind), // Target types that correspond exclusively to `Item` kind. Mapping is obtained from `Target::from_ast_item`. - ExternCrate(Option<&'a ItemKind>), - Use(Option<&'a ItemKind>), - Static(Option<&'a ItemKind>), - Const(Option<&'a ItemKind>), - Fn(Option<&'a ItemKind>), - Mod(Option<&'a ItemKind>), - ForeignMod(Option<&'a ItemKind>), - GlobalAsm(Option<&'a ItemKind>), - TyAlias(Option<&'a ItemKind>), - Enum(Option<&'a ItemKind>), - Struct(Option<&'a ItemKind>), - Union(Option<&'a ItemKind>), - Trait(Option<&'a ItemKind>), - TraitAlias(Option<&'a ItemKind>), - Impl { item: Option<&'a ItemKind>, of_trait: bool }, - MacroDef(Option<&'a ItemKind>), + ExternCrate(&'a ItemKind), + Use(&'a ItemKind), + Static(&'a ItemKind), + Const(&'a ItemKind), + Fn(&'a ItemKind), + Mod(&'a ItemKind), + ForeignMod(&'a ItemKind), + GlobalAsm(&'a ItemKind), + TyAlias(&'a ItemKind), + Enum(&'a ItemKind), + Struct(&'a ItemKind), + Union(&'a ItemKind), + Trait(&'a ItemKind), + TraitAlias(&'a ItemKind), + Impl { item: &'a ItemKind, of_trait: bool }, + MacroDef(&'a ItemKind), // Target types that correspond exclusively to `Expr` kind. Mapping is obtained from `Target::from_expr`. - Closure(Option<&'a Closure>), - Expression(Option<&'a Expr>), - ForLoop(Option<&'a ForLoop>), + Closure(&'a Closure), + Expression(&'a Expr), + ForLoop(&'a ForLoop), Loop, While, Break, - Arm(Option<&'a Arm>), - ConstParam(Option<&'a GenericParam>), - Crate(Option<&'a Crate>), - ExprField(Option<&'a ExprField>), - Field(Option<&'a FieldDef>), - GenericParam(Option<&'a GenericParam>), - LifetimeParam(Option<&'a GenericParam>), - Local(Option<&'a Local>), - Param(Option<&'a Param>), - Pat(Option<&'a Pat>), - PatField(Option<&'a PatField>), - Statement(Option<&'a Stmt>), - TypeParam(Option<&'a GenericParam>), - Variant(Option<&'a Variant>), - WherePredicate(Option<&'a WherePredicate>), + Arm(&'a Arm), + ConstParam(&'a GenericParam), + Crate(&'a Crate), + ExprField(&'a ExprField), + Field(&'a FieldDef), + GenericParam(&'a GenericParam), + LifetimeParam(&'a GenericParam), + Local(&'a Local), + Param(&'a Param), + Pat(&'a Pat), + PatField(&'a PatField), + Statement(&'a Stmt), + TypeParam(&'a GenericParam), + Variant(&'a Variant), + WherePredicate(&'a WherePredicate), + + // Only reserved for cases when it is not possible to obtain detailed Ast Target + None, } #[derive(Clone, Copy, Debug)] @@ -147,9 +150,9 @@ pub enum Target { impl<'a> AstTarget<'a> { pub fn get_abi(&self) -> Option { let ext = match self { - AstTarget::Method(Some(ast::AssocItemKind::Fn(fn_item))) => fn_item.sig.header.ext, - AstTarget::Fn(Some(ast::ItemKind::Fn(fn_item))) => fn_item.sig.header.ext, - AstTarget::ForeignFn(Some(ast::ForeignItemKind::Fn(fn_item))) => fn_item.sig.header.ext, + AstTarget::Method(ast::AssocItemKind::Fn(fn_item)) => fn_item.sig.header.ext, + AstTarget::Fn(ast::ItemKind::Fn(fn_item)) => fn_item.sig.header.ext, + AstTarget::ForeignFn(ast::ForeignItemKind::Fn(fn_item)) => fn_item.sig.header.ext, _ => return None, }; @@ -162,129 +165,74 @@ impl<'a> AstTarget<'a> { pub fn get_fn_sig(&self) -> Option<&rustc_ast::ast::FnSig> { match self { - AstTarget::Method(Some(ast::AssocItemKind::Fn(fn_item))) => Some(&fn_item.sig), - AstTarget::Fn(Some(ast::ItemKind::Fn(fn_item))) => Some(&fn_item.sig), - AstTarget::ForeignFn(Some(ast::ForeignItemKind::Fn(fn_item))) => Some(&fn_item.sig), + AstTarget::Method(ast::AssocItemKind::Fn(fn_item)) => Some(&fn_item.sig), + AstTarget::Fn(ast::ItemKind::Fn(fn_item)) => Some(&fn_item.sig), + AstTarget::ForeignFn(ast::ForeignItemKind::Fn(fn_item)) => Some(&fn_item.sig), _ => None, } } pub fn from_foreign_item_kind(kind: &'a ast::ForeignItemKind) -> Self { match kind { - ForeignItemKind::Static(_) => AstTarget::ForeignStatic(Some(kind)), - ForeignItemKind::Fn(_) => AstTarget::ForeignFn(Some(kind)), - ForeignItemKind::TyAlias(_) => AstTarget::ForeignTy(Some(kind)), + ForeignItemKind::Static(_) => AstTarget::ForeignStatic(kind), + ForeignItemKind::Fn(_) => AstTarget::ForeignFn(kind), + ForeignItemKind::TyAlias(_) => AstTarget::ForeignTy(kind), ForeignItemKind::MacCall(_) => { - AstTarget::MacroCall(Some(MacroCallAstTarget::ForeignItem(kind))) + AstTarget::MacroCall(MacroCallAstTarget::ForeignItem(kind)) } } } pub fn from_assoc_item_kind(kind: &'a ast::AssocItemKind) -> Self { match kind { - AssocItemKind::Const(_) => AstTarget::AssocConst(Some(kind)), - AssocItemKind::Fn(_) => AstTarget::Method(Some(kind)), - AssocItemKind::Type(_) => AstTarget::AssocTy(Some(kind)), - AssocItemKind::Delegation(_) => AstTarget::Delegation { - target: Some(DelegationAstTarget::AssocItem(kind)), - mac: false, - }, - AssocItemKind::DelegationMac(_) => AstTarget::Delegation { - target: Some(DelegationAstTarget::AssocItem(kind)), - mac: true, - }, - AssocItemKind::MacCall(_) => { - AstTarget::MacroCall(Some(MacroCallAstTarget::AssocItem(kind))) + AssocItemKind::Const(_) => AstTarget::AssocConst(kind), + AssocItemKind::Fn(_) => AstTarget::Method(kind), + AssocItemKind::Type(_) => AstTarget::AssocTy(kind), + AssocItemKind::Delegation(_) => { + AstTarget::Delegation { target: DelegationAstTarget::AssocItem(kind), mac: false } } + AssocItemKind::DelegationMac(_) => { + AstTarget::Delegation { target: DelegationAstTarget::AssocItem(kind), mac: true } + } + AssocItemKind::MacCall(_) => AstTarget::MacroCall(MacroCallAstTarget::AssocItem(kind)), } } pub fn from_ast_item(kind: &'a ast::ItemKind) -> Self { match kind { - ast::ItemKind::ExternCrate(..) => AstTarget::ExternCrate(Some(kind)), - ast::ItemKind::Use(..) => AstTarget::Use(Some(kind)), - ast::ItemKind::Static { .. } => AstTarget::Static(Some(kind)), - ast::ItemKind::Const(..) => AstTarget::Const(Some(kind)), - ast::ItemKind::ConstBlock(..) => AstTarget::Const(Some(kind)), - ast::ItemKind::Fn { .. } => AstTarget::Fn(Some(kind)), - ast::ItemKind::Mod(..) => AstTarget::Mod(Some(kind)), - ast::ItemKind::ForeignMod { .. } => AstTarget::ForeignMod(Some(kind)), - ast::ItemKind::GlobalAsm { .. } => AstTarget::GlobalAsm(Some(kind)), - ast::ItemKind::TyAlias(..) => AstTarget::TyAlias(Some(kind)), - ast::ItemKind::Enum(..) => AstTarget::Enum(Some(kind)), - ast::ItemKind::Struct(..) => AstTarget::Struct(Some(kind)), - ast::ItemKind::Union(..) => AstTarget::Union(Some(kind)), - ast::ItemKind::Trait(..) => AstTarget::Trait(Some(kind)), - ast::ItemKind::TraitAlias(..) => AstTarget::TraitAlias(Some(kind)), + ast::ItemKind::ExternCrate(..) => AstTarget::ExternCrate(kind), + ast::ItemKind::Use(..) => AstTarget::Use(kind), + ast::ItemKind::Static { .. } => AstTarget::Static(kind), + ast::ItemKind::Const(..) => AstTarget::Const(kind), + ast::ItemKind::ConstBlock(..) => AstTarget::Const(kind), + ast::ItemKind::Fn { .. } => AstTarget::Fn(kind), + ast::ItemKind::Mod(..) => AstTarget::Mod(kind), + ast::ItemKind::ForeignMod { .. } => AstTarget::ForeignMod(kind), + ast::ItemKind::GlobalAsm { .. } => AstTarget::GlobalAsm(kind), + ast::ItemKind::TyAlias(..) => AstTarget::TyAlias(kind), + ast::ItemKind::Enum(..) => AstTarget::Enum(kind), + ast::ItemKind::Struct(..) => AstTarget::Struct(kind), + ast::ItemKind::Union(..) => AstTarget::Union(kind), + ast::ItemKind::Trait(..) => AstTarget::Trait(kind), + ast::ItemKind::TraitAlias(..) => AstTarget::TraitAlias(kind), ast::ItemKind::Impl(i) => { - AstTarget::Impl { item: Some(kind), of_trait: i.of_trait.is_some() } - } - ast::ItemKind::MacCall(..) => { - AstTarget::MacroCall(Some(MacroCallAstTarget::Item(kind))) + AstTarget::Impl { item: kind, of_trait: i.of_trait.is_some() } } - ast::ItemKind::MacroDef(..) => AstTarget::MacroDef(Some(kind)), + ast::ItemKind::MacCall(..) => AstTarget::MacroCall(MacroCallAstTarget::Item(kind)), + ast::ItemKind::MacroDef(..) => AstTarget::MacroDef(kind), ast::ItemKind::Delegation(..) => { - AstTarget::Delegation { target: Some(DelegationAstTarget::Item(kind)), mac: false } + AstTarget::Delegation { target: DelegationAstTarget::Item(kind), mac: false } } ast::ItemKind::DelegationMac(..) => { - AstTarget::Delegation { target: Some(DelegationAstTarget::Item(kind)), mac: true } + AstTarget::Delegation { target: DelegationAstTarget::Item(kind), mac: true } } ast::ItemKind::TestBinderConstraints(..) => { - AstTarget::MacroCall(Some(MacroCallAstTarget::Item(kind))) + AstTarget::MacroCall(MacroCallAstTarget::Item(kind)) } } } } -impl<'a> From for AstTarget<'a> { - fn from(target: Target) -> Self { - match target { - Target::ExternCrate => AstTarget::ExternCrate(None), - Target::Use => AstTarget::Use(None), - Target::Static => AstTarget::Static(None), - Target::Const => AstTarget::Const(None), - Target::Fn => AstTarget::Fn(None), - Target::Closure => AstTarget::Closure(None), - Target::Mod => AstTarget::Mod(None), - Target::ForeignMod => AstTarget::ForeignMod(None), - Target::GlobalAsm => AstTarget::GlobalAsm(None), - Target::TyAlias => AstTarget::TyAlias(None), - Target::Enum => AstTarget::Enum(None), - Target::Variant => AstTarget::Variant(None), - Target::Struct => AstTarget::Struct(None), - Target::Field => AstTarget::Field(None), - Target::Union => AstTarget::Union(None), - Target::Trait => AstTarget::Trait(None), - Target::TraitAlias => AstTarget::TraitAlias(None), - Target::Impl { of_trait } => AstTarget::Impl { item: None, of_trait }, - Target::Expression => AstTarget::Expression(None), - Target::Statement => AstTarget::Statement(None), - Target::Arm => AstTarget::Arm(None), - Target::AssocConst(_assoc_ctxt) => AstTarget::AssocConst(None), - Target::Method(_method_kind) => AstTarget::Method(None), - Target::AssocTy(_assoc_ctxt) => AstTarget::AssocTy(None), - Target::ForeignFn => AstTarget::ForeignFn(None), - Target::ForeignStatic => AstTarget::ForeignStatic(None), - Target::ForeignTy => AstTarget::ForeignTy(None), - Target::LifetimeParam => AstTarget::LifetimeParam(None), - Target::TypeParam => AstTarget::TypeParam(None), - Target::ConstParam => AstTarget::ConstParam(None), - Target::MacroDef => AstTarget::MacroDef(None), - Target::Param => AstTarget::Param(None), - Target::PatField => AstTarget::PatField(None), - Target::ExprField => AstTarget::ExprField(None), - Target::WherePredicate => AstTarget::WherePredicate(None), - Target::MacroCall => AstTarget::MacroCall(None), - Target::Crate => AstTarget::Crate(None), - Target::Delegation { mac } => AstTarget::Delegation { target: None, mac }, - Target::ForLoop => AstTarget::ForLoop(None), - Target::While => AstTarget::While, - Target::Loop => AstTarget::Loop, - Target::Break => AstTarget::Break, - } - } -} - impl Display for Target { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", Self::name(*self)) diff --git a/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs b/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs index a122713ce1930..baf0496ef7a36 100644 --- a/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs +++ b/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs @@ -264,8 +264,7 @@ impl CombineAttributeParser for LinkParser { fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) { match cx.ast_target { rustc_attr_ir::target::AstTarget::ForeignMod(ast_item) - if let ItemKind::ForeignMod(fm) = - &ast_item.expect("missing AST target item for astTarget::ForeignMod") => + if let ItemKind::ForeignMod(fm) = &ast_item => { let abi = fm.abi.map_or(ExternAbi::FALLBACK, |abi| { abi.symbol_unescaped.as_str().parse().unwrap_or_else(|_| { diff --git a/compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs b/compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs index 5e2e0db135961..335fc16c6105b 100644 --- a/compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs +++ b/compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs @@ -184,8 +184,7 @@ impl SingleAttributeParser for MacroExportParser { fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) { match cx.ast_target { AstTarget::MacroDef(item_kind) => { - if let ItemKind::MacroDef(_, macro_def) = - item_kind.expect("AstTarget::MacroDef should always have an ItemKind::MacroDef") + if let ItemKind::MacroDef(_, macro_def) = item_kind && !macro_def.macro_rules { cx.emit_lint(UNUSED_ATTRIBUTES, MacroExport::OnDeclMacro, attr_span); diff --git a/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs b/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs index c4ff1f220f581..09fd2d46bc0bb 100644 --- a/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs +++ b/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs @@ -28,8 +28,7 @@ impl NoArgsAttributeParser for NonExhaustiveParser { match cx.ast_target { rustc_attr_ir::target::AstTarget::Struct(ast_item) - if let ItemKind::Struct(_, _, data) = - &ast_item.expect("missing AST target item for AstTarget::Struct") => + if let ItemKind::Struct(_, _, data) = &ast_item => { if let VariantData::Struct { fields, .. } = data && fields.iter().any(|f| f.default_value().is_some()) diff --git a/compiler/rustc_attr_parsing/src/interface.rs b/compiler/rustc_attr_parsing/src/interface.rs index 17cf5805adede..7f8e481ce7505 100644 --- a/compiler/rustc_attr_parsing/src/interface.rs +++ b/compiler/rustc_attr_parsing/src/interface.rs @@ -161,7 +161,7 @@ impl<'sess> AttributeParser<'sess> { attrs, target_span, target, - rustc_attr_ir::target::AstTarget::from(target), + rustc_attr_ir::target::AstTarget::None, std::convert::identity, |lint_id, span, kind| { sess.psess.dyn_buffer_lint_sess(lint_id.lint, span, target_node_id, kind.0) From 35b1fabe6ebcd47b59a877ceb115e6c15aeca860 Mon Sep 17 00:00:00 2001 From: Richard Tjokroutomo Date: Tue, 6 Oct 2026 10:32:09 +0800 Subject: [PATCH 09/13] rebase latest changes & addressing latest comments Signed-off-by: Richard Tjokroutomo --- compiler/rustc_ast_lowering/src/block.rs | 2 +- .../rustc_attr_parsing/src/attributes/rustc_internal.rs | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/block.rs b/compiler/rustc_ast_lowering/src/block.rs index 0ebc9c1533b2f..e55dd4992270c 100644 --- a/compiler/rustc_ast_lowering/src/block.rs +++ b/compiler/rustc_ast_lowering/src/block.rs @@ -109,7 +109,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &l.attrs, l.span, Target::Statement, - rustc_attr_ir::target::AstTarget::Local(l), + rustc_attr_ir::target::AstTarget::None, ); self.arena.alloc(hir::LetStmt { hir_id, super_, ty, pat, init, els, span, source }) } diff --git a/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs b/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs index b9c03883d7c0b..583f76f9dea80 100644 --- a/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs +++ b/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs @@ -171,12 +171,11 @@ impl SingleAttributeParser for RustcLegacyConstGenericsParser { } fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) { - if cx.target != Target::Fn { - // Invalid targets are already diagnosed by target checking. + let rustc_attr_ir::target::AstTarget::Fn(item_kind) = cx.ast_target else { return; - } - let item = cx.target_item.expect("missing AST target item for Target::Fn"); - let ItemKind::Fn(function) = &item.kind else { + }; + + let ItemKind::Fn(function) = item_kind else { panic!("expected fn AST target item for Target::Fn"); }; let index_list = rustc_attr_ir::find_attr!(cx.parsed_attrs, RustcLegacyConstGenerics { fn_indexes, .. } => fn_indexes) From b567ca06fdeb41c16ecfdaa494a5110672e1676b Mon Sep 17 00:00:00 2001 From: Richard Tjokroutomo Date: Wed, 7 Oct 2026 20:36:38 +0800 Subject: [PATCH 10/13] some tidy ups Signed-off-by: Richard Tjokroutomo --- compiler/rustc_ast_lowering/src/block.rs | 6 +-- compiler/rustc_ast_lowering/src/lib.rs | 4 +- compiler/rustc_attr_ir/src/target.rs | 45 +++++-------------- .../src/attributes/link_attrs.rs | 21 ++++----- .../src/attributes/macro_attrs.rs | 17 ++++--- .../src/attributes/non_exhaustive.rs | 29 ++++++------ 6 files changed, 47 insertions(+), 75 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/block.rs b/compiler/rustc_ast_lowering/src/block.rs index e55dd4992270c..52f92a96c8ce3 100644 --- a/compiler/rustc_ast_lowering/src/block.rs +++ b/compiler/rustc_ast_lowering/src/block.rs @@ -37,7 +37,7 @@ impl<'hir> LoweringContext<'_, 'hir> { match &s.kind { StmtKind::Let(local) => { let hir_id = self.lower_node_id(s.id); - let local = self.lower_local(local); + let local = self.lower_local(local, &s); self.alias_attrs(hir_id, local.hir_id); let kind = hir::StmtKind::Let(local); let span = self.lower_span(s.span); @@ -88,7 +88,7 @@ impl<'hir> LoweringContext<'_, 'hir> { } } - fn lower_local(&mut self, l: &Local) -> &'hir hir::LetStmt<'hir> { + fn lower_local(&mut self, l: &Local, s: &Stmt) -> &'hir hir::LetStmt<'hir> { // Let statements are allowed to have impl trait in bindings. let super_ = l.super_.map(|span| self.lower_span(span)); let ty = l.ty.as_ref().map(|t| { @@ -109,7 +109,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &l.attrs, l.span, Target::Statement, - rustc_attr_ir::target::AstTarget::None, + rustc_attr_ir::target::AstTarget::Statement(s), ); self.arena.alloc(hir::LetStmt { hir_id, super_, ty, pat, init, els, span, source }) } diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 74ff46aac7444..eac871388cce5 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -1967,7 +1967,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ¶m.attrs, param.span, Target::Param, - AstTarget::None, + AstTarget::Param(param), ); } let itctx = match kind { @@ -2912,7 +2912,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &f.attrs, f.span, Target::ExprField, - AstTarget::Expression(expr), + AstTarget::ExprField(f), ); let expr = self.lower_expr_to_const_arg_direct(&f.expr, None); diff --git a/compiler/rustc_attr_ir/src/target.rs b/compiler/rustc_attr_ir/src/target.rs index 831a86dd7296a..d1be4b38aaea9 100644 --- a/compiler/rustc_attr_ir/src/target.rs +++ b/compiler/rustc_attr_ir/src/target.rs @@ -14,8 +14,8 @@ use rustc_macros::StableHash; #[derive(Clone, Copy, Debug)] pub enum AstTarget<'a> { // Target types that may correspond to different kinds of items. - Delegation { target: DelegationAstTarget<'a>, mac: bool }, - MacroCall(MacroCallAstTarget<'a>), + Delegation, + MacroCall, // Target types that correspond exclusively to `AssocItem` kind. Mapping is obtained from `Target::from_assoc_item_kind`. AssocConst(&'a AssocItemKind), @@ -73,19 +73,6 @@ pub enum AstTarget<'a> { None, } -#[derive(Clone, Copy, Debug)] -pub enum DelegationAstTarget<'a> { - AssocItem(&'a AssocItemKind), - Item(&'a ItemKind), -} - -#[derive(Clone, Copy, Debug)] -pub enum MacroCallAstTarget<'a> { - AssocItem(&'a AssocItemKind), - ForeignItem(&'a ForeignItemKind), - Item(&'a ItemKind), -} - #[derive(Copy, Clone, PartialEq, Debug, Eq, StableHash)] pub enum MethodKind { /// Method in a `trait Trait` block @@ -177,9 +164,7 @@ impl<'a> AstTarget<'a> { ForeignItemKind::Static(_) => AstTarget::ForeignStatic(kind), ForeignItemKind::Fn(_) => AstTarget::ForeignFn(kind), ForeignItemKind::TyAlias(_) => AstTarget::ForeignTy(kind), - ForeignItemKind::MacCall(_) => { - AstTarget::MacroCall(MacroCallAstTarget::ForeignItem(kind)) - } + ForeignItemKind::MacCall(_) => AstTarget::MacroCall, } } @@ -188,13 +173,9 @@ impl<'a> AstTarget<'a> { AssocItemKind::Const(_) => AstTarget::AssocConst(kind), AssocItemKind::Fn(_) => AstTarget::Method(kind), AssocItemKind::Type(_) => AstTarget::AssocTy(kind), - AssocItemKind::Delegation(_) => { - AstTarget::Delegation { target: DelegationAstTarget::AssocItem(kind), mac: false } - } - AssocItemKind::DelegationMac(_) => { - AstTarget::Delegation { target: DelegationAstTarget::AssocItem(kind), mac: true } - } - AssocItemKind::MacCall(_) => AstTarget::MacroCall(MacroCallAstTarget::AssocItem(kind)), + AssocItemKind::Delegation(_) => AstTarget::Delegation, + AssocItemKind::DelegationMac(_) => AstTarget::Delegation, + AssocItemKind::MacCall(_) => AstTarget::MacroCall, } } @@ -218,17 +199,11 @@ impl<'a> AstTarget<'a> { ast::ItemKind::Impl(i) => { AstTarget::Impl { item: kind, of_trait: i.of_trait.is_some() } } - ast::ItemKind::MacCall(..) => AstTarget::MacroCall(MacroCallAstTarget::Item(kind)), + ast::ItemKind::MacCall(..) => AstTarget::MacroCall, ast::ItemKind::MacroDef(..) => AstTarget::MacroDef(kind), - ast::ItemKind::Delegation(..) => { - AstTarget::Delegation { target: DelegationAstTarget::Item(kind), mac: false } - } - ast::ItemKind::DelegationMac(..) => { - AstTarget::Delegation { target: DelegationAstTarget::Item(kind), mac: true } - } - ast::ItemKind::TestBinderConstraints(..) => { - AstTarget::MacroCall(MacroCallAstTarget::Item(kind)) - } + ast::ItemKind::Delegation(..) => AstTarget::Delegation, + ast::ItemKind::DelegationMac(..) => AstTarget::Delegation, + ast::ItemKind::TestBinderConstraints(..) => AstTarget::MacroCall, } } } diff --git a/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs b/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs index baf0496ef7a36..bb6e9eca5820a 100644 --- a/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs +++ b/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs @@ -262,11 +262,15 @@ impl CombineAttributeParser for LinkParser { } fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) { - match cx.ast_target { - rustc_attr_ir::target::AstTarget::ForeignMod(ast_item) - if let ItemKind::ForeignMod(fm) = &ast_item => - { - let abi = fm.abi.map_or(ExternAbi::FALLBACK, |abi| { + let rustc_attr_ir::target::AstTarget::ForeignMod(ast_item) = cx.ast_target else { + return; + }; + + let ItemKind::ForeignMod(fm) = &ast_item else { + panic!("expected field ForeignMod for AstTarget::ForeignMod"); + }; + + let abi = fm.abi.map_or(ExternAbi::FALLBACK, |abi| { abi.symbol_unescaped.as_str().parse().unwrap_or_else(|_| { cx.dcx().span_delayed_bug( abi.span, @@ -275,11 +279,8 @@ impl CombineAttributeParser for LinkParser { ExternAbi::Rust }) }); - if matches!(abi, ExternAbi::Rust) { - cx.emit_lint(UNUSED_ATTRIBUTES, Link, attr_span); - } - } - _ => {} + if matches!(abi, ExternAbi::Rust) { + cx.emit_lint(UNUSED_ATTRIBUTES, Link, attr_span); } } } diff --git a/compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs b/compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs index 335fc16c6105b..252a9f5e379bc 100644 --- a/compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs +++ b/compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs @@ -182,15 +182,14 @@ impl SingleAttributeParser for MacroExportParser { } fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) { - match cx.ast_target { - AstTarget::MacroDef(item_kind) => { - if let ItemKind::MacroDef(_, macro_def) = item_kind - && !macro_def.macro_rules - { - cx.emit_lint(UNUSED_ATTRIBUTES, MacroExport::OnDeclMacro, attr_span); - } - } - _ => {} + let AstTarget::MacroDef(item_kind) = cx.ast_target else { + return; + }; + + if let ItemKind::MacroDef(_, macro_def) = item_kind + && !macro_def.macro_rules + { + cx.emit_lint(UNUSED_ATTRIBUTES, MacroExport::OnDeclMacro, attr_span); } } } diff --git a/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs b/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs index 09fd2d46bc0bb..3c4a77b3bb673 100644 --- a/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs +++ b/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs @@ -22,24 +22,21 @@ impl NoArgsAttributeParser for NonExhaustiveParser { const CREATE: fn(Span) -> AttributeKind = AttributeKind::NonExhaustive; fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) { - if cx.target != Target::Struct { + let rustc_attr_ir::target::AstTarget::Struct(ast_item) = cx.ast_target else { return; - } + }; + + let ItemKind::Struct(_, _, data) = ast_item else { + panic!("expected struct AST target item for AstTarget::Struct"); + }; - match cx.ast_target { - rustc_attr_ir::target::AstTarget::Struct(ast_item) - if let ItemKind::Struct(_, _, data) = &ast_item => - { - if let VariantData::Struct { fields, .. } = data - && fields.iter().any(|f| f.default_value().is_some()) - { - cx.emit_err(NonExhaustiveWithDefaultFieldValues { - attr_span, - defn_span: cx.target_span, - }); - } - } - _ => {} + if let VariantData::Struct { fields, .. } = data + && fields.iter().any(|f| f.default_value().is_some()) + { + cx.emit_err(NonExhaustiveWithDefaultFieldValues { + attr_span, + defn_span: cx.target_span, + }); } } } From bba59d2cac9f60017f292c965f5a18000769686e Mon Sep 17 00:00:00 2001 From: Richard Tjokroutomo Date: Wed, 7 Oct 2026 22:37:19 +0800 Subject: [PATCH 11/13] remove Item, AsocItem, & ForeignItem Signed-off-by: Richard Tjokroutomo --- compiler/rustc_attr_ir/src/target.rs | 119 ++++++++++-------- .../src/attributes/link_attrs.rs | 7 +- .../src/attributes/macro_attrs.rs | 7 +- .../src/attributes/non_exhaustive.rs | 8 +- .../src/attributes/rustc_internal.rs | 7 +- 5 files changed, 72 insertions(+), 76 deletions(-) diff --git a/compiler/rustc_attr_ir/src/target.rs b/compiler/rustc_attr_ir/src/target.rs index d1be4b38aaea9..a6ad343bd2785 100644 --- a/compiler/rustc_attr_ir/src/target.rs +++ b/compiler/rustc_attr_ir/src/target.rs @@ -5,10 +5,13 @@ use std::fmt::{self, Display}; use rustc_abi::ExternAbi; pub use rustc_ast::visit::AssocCtxt; use rustc_ast::{ - Arm, AssocItemKind, Closure, Crate, Expr, ExprField, FieldDef, ForLoop, ForeignItemKind, - GenericParam, ItemKind, Local, Param, Pat, PatField, Stmt, Variant, WherePredicate, ast, + Arm, AssocItemKind, Closure, ConstBlockItem, ConstItem, Crate, EnumDef, Expr, ExprField, + FieldDef, Fn, ForLoop, ForeignItemKind, ForeignMod, GenericParam, Generics, Impl, InlineAsm, + Local, MacroDef, ModKind, Param, Pat, PatField, Safety, StaticItem, Stmt, Trait, TraitAlias, + TyAlias, UseTree, Variant, VariantData, WherePredicate, ast, }; use rustc_macros::StableHash; +use rustc_span::{Ident, Symbol}; /// This enum lists all possible types of AST items. #[derive(Clone, Copy, Debug)] @@ -18,32 +21,32 @@ pub enum AstTarget<'a> { MacroCall, // Target types that correspond exclusively to `AssocItem` kind. Mapping is obtained from `Target::from_assoc_item_kind`. - AssocConst(&'a AssocItemKind), - Method(&'a AssocItemKind), - AssocTy(&'a AssocItemKind), + AssocConst(&'a ConstItem), + Method(&'a Fn), + AssocTy(&'a TyAlias), // Target types that correspond exclusively to `ForeignItem` kind. Mapping is obtained from `Target::from_foreign_item_kind`. - ForeignStatic(&'a ForeignItemKind), - ForeignFn(&'a ForeignItemKind), - ForeignTy(&'a ForeignItemKind), + ForeignStatic(&'a StaticItem), + ForeignFn(&'a Fn), + ForeignTy(&'a TyAlias), // Target types that correspond exclusively to `Item` kind. Mapping is obtained from `Target::from_ast_item`. - ExternCrate(&'a ItemKind), - Use(&'a ItemKind), - Static(&'a ItemKind), - Const(&'a ItemKind), - Fn(&'a ItemKind), - Mod(&'a ItemKind), - ForeignMod(&'a ItemKind), - GlobalAsm(&'a ItemKind), - TyAlias(&'a ItemKind), - Enum(&'a ItemKind), - Struct(&'a ItemKind), - Union(&'a ItemKind), - Trait(&'a ItemKind), - TraitAlias(&'a ItemKind), - Impl { item: &'a ItemKind, of_trait: bool }, - MacroDef(&'a ItemKind), + ExternCrate(&'a Option, &'a Ident), + Use(&'a UseTree), + Static(&'a StaticItem), + Const(ConstTypeAstTarget<'a>), + Fn(&'a Fn), + Mod(&'a Safety, &'a Ident, &'a ModKind), + ForeignMod(&'a ForeignMod), + GlobalAsm(&'a InlineAsm), + TyAlias(&'a TyAlias), + Enum(&'a Ident, &'a Generics, &'a EnumDef), + Struct(&'a Ident, &'a Generics, &'a VariantData), + Union(&'a Ident, &'a Generics, &'a VariantData), + Trait(&'a Trait), + TraitAlias(&'a TraitAlias), + Impl(&'a Impl), + MacroDef(&'a Ident, &'a MacroDef), // Target types that correspond exclusively to `Expr` kind. Mapping is obtained from `Target::from_expr`. Closure(&'a Closure), @@ -73,6 +76,12 @@ pub enum AstTarget<'a> { None, } +#[derive(Clone, Copy, Debug)] +pub enum ConstTypeAstTarget<'a> { + ConstItem(&'a ConstItem), + ConstBlockItem(&'a ConstBlockItem), +} + #[derive(Copy, Clone, PartialEq, Debug, Eq, StableHash)] pub enum MethodKind { /// Method in a `trait Trait` block @@ -137,9 +146,9 @@ pub enum Target { impl<'a> AstTarget<'a> { pub fn get_abi(&self) -> Option { let ext = match self { - AstTarget::Method(ast::AssocItemKind::Fn(fn_item)) => fn_item.sig.header.ext, - AstTarget::Fn(ast::ItemKind::Fn(fn_item)) => fn_item.sig.header.ext, - AstTarget::ForeignFn(ast::ForeignItemKind::Fn(fn_item)) => fn_item.sig.header.ext, + AstTarget::Method(fn_item) => fn_item.sig.header.ext, + AstTarget::Fn(fn_item) => fn_item.sig.header.ext, + AstTarget::ForeignFn(fn_item) => fn_item.sig.header.ext, _ => return None, }; @@ -152,27 +161,27 @@ impl<'a> AstTarget<'a> { pub fn get_fn_sig(&self) -> Option<&rustc_ast::ast::FnSig> { match self { - AstTarget::Method(ast::AssocItemKind::Fn(fn_item)) => Some(&fn_item.sig), - AstTarget::Fn(ast::ItemKind::Fn(fn_item)) => Some(&fn_item.sig), - AstTarget::ForeignFn(ast::ForeignItemKind::Fn(fn_item)) => Some(&fn_item.sig), + AstTarget::Method(fn_item) => Some(&fn_item.sig), + AstTarget::Fn(fn_item) => Some(&fn_item.sig), + AstTarget::ForeignFn(fn_item) => Some(&fn_item.sig), _ => None, } } pub fn from_foreign_item_kind(kind: &'a ast::ForeignItemKind) -> Self { match kind { - ForeignItemKind::Static(_) => AstTarget::ForeignStatic(kind), - ForeignItemKind::Fn(_) => AstTarget::ForeignFn(kind), - ForeignItemKind::TyAlias(_) => AstTarget::ForeignTy(kind), + ForeignItemKind::Static(static_item) => AstTarget::ForeignStatic(static_item), + ForeignItemKind::Fn(f) => AstTarget::ForeignFn(f), + ForeignItemKind::TyAlias(ty_alias) => AstTarget::ForeignTy(ty_alias), ForeignItemKind::MacCall(_) => AstTarget::MacroCall, } } pub fn from_assoc_item_kind(kind: &'a ast::AssocItemKind) -> Self { match kind { - AssocItemKind::Const(_) => AstTarget::AssocConst(kind), - AssocItemKind::Fn(_) => AstTarget::Method(kind), - AssocItemKind::Type(_) => AstTarget::AssocTy(kind), + AssocItemKind::Const(const_item) => AstTarget::AssocConst(const_item), + AssocItemKind::Fn(f) => AstTarget::Method(f), + AssocItemKind::Type(type_alias) => AstTarget::AssocTy(type_alias), AssocItemKind::Delegation(_) => AstTarget::Delegation, AssocItemKind::DelegationMac(_) => AstTarget::Delegation, AssocItemKind::MacCall(_) => AstTarget::MacroCall, @@ -181,26 +190,28 @@ impl<'a> AstTarget<'a> { pub fn from_ast_item(kind: &'a ast::ItemKind) -> Self { match kind { - ast::ItemKind::ExternCrate(..) => AstTarget::ExternCrate(kind), - ast::ItemKind::Use(..) => AstTarget::Use(kind), - ast::ItemKind::Static { .. } => AstTarget::Static(kind), - ast::ItemKind::Const(..) => AstTarget::Const(kind), - ast::ItemKind::ConstBlock(..) => AstTarget::Const(kind), - ast::ItemKind::Fn { .. } => AstTarget::Fn(kind), - ast::ItemKind::Mod(..) => AstTarget::Mod(kind), - ast::ItemKind::ForeignMod { .. } => AstTarget::ForeignMod(kind), - ast::ItemKind::GlobalAsm { .. } => AstTarget::GlobalAsm(kind), - ast::ItemKind::TyAlias(..) => AstTarget::TyAlias(kind), - ast::ItemKind::Enum(..) => AstTarget::Enum(kind), - ast::ItemKind::Struct(..) => AstTarget::Struct(kind), - ast::ItemKind::Union(..) => AstTarget::Union(kind), - ast::ItemKind::Trait(..) => AstTarget::Trait(kind), - ast::ItemKind::TraitAlias(..) => AstTarget::TraitAlias(kind), - ast::ItemKind::Impl(i) => { - AstTarget::Impl { item: kind, of_trait: i.of_trait.is_some() } + ast::ItemKind::ExternCrate(symbol, ident) => AstTarget::ExternCrate(symbol, ident), + ast::ItemKind::Use(use_tree) => AstTarget::Use(use_tree), + ast::ItemKind::Static(static_item) => AstTarget::Static(static_item), + ast::ItemKind::Const(const_item) => { + AstTarget::Const(ConstTypeAstTarget::ConstItem(const_item)) + } + ast::ItemKind::ConstBlock(const_block_item) => { + AstTarget::Const(ConstTypeAstTarget::ConstBlockItem(const_block_item)) } + ast::ItemKind::Fn(f) => AstTarget::Fn(f), + ast::ItemKind::Mod(s, i, mk) => AstTarget::Mod(s, i, mk), + ast::ItemKind::ForeignMod(foreign_mod) => AstTarget::ForeignMod(foreign_mod), + ast::ItemKind::GlobalAsm(inline_asm) => AstTarget::GlobalAsm(inline_asm), + ast::ItemKind::TyAlias(ty_alias) => AstTarget::TyAlias(ty_alias), + ast::ItemKind::Enum(i, g, ed) => AstTarget::Enum(i, g, ed), + ast::ItemKind::Struct(i, g, vd) => AstTarget::Struct(i, g, vd), + ast::ItemKind::Union(i, g, vd) => AstTarget::Union(i, g, vd), + ast::ItemKind::Trait(trait_kind) => AstTarget::Trait(trait_kind), + ast::ItemKind::TraitAlias(trait_alias) => AstTarget::TraitAlias(trait_alias), + ast::ItemKind::Impl(i) => AstTarget::Impl(i), ast::ItemKind::MacCall(..) => AstTarget::MacroCall, - ast::ItemKind::MacroDef(..) => AstTarget::MacroDef(kind), + ast::ItemKind::MacroDef(ident, macro_def) => AstTarget::MacroDef(ident, macro_def), ast::ItemKind::Delegation(..) => AstTarget::Delegation, ast::ItemKind::DelegationMac(..) => AstTarget::Delegation, ast::ItemKind::TestBinderConstraints(..) => AstTarget::MacroCall, diff --git a/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs b/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs index bb6e9eca5820a..40ca63e2a4da7 100644 --- a/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs +++ b/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs @@ -1,5 +1,4 @@ use rustc_abi::ExternAbi; -use rustc_ast::ItemKind; use rustc_attr_ir::AttributeKind::{LinkName, LinkOrdinal, LinkSection}; use rustc_attr_ir::*; use rustc_errors::msg; @@ -262,14 +261,10 @@ impl CombineAttributeParser for LinkParser { } fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) { - let rustc_attr_ir::target::AstTarget::ForeignMod(ast_item) = cx.ast_target else { + let rustc_attr_ir::target::AstTarget::ForeignMod(fm) = cx.ast_target else { return; }; - let ItemKind::ForeignMod(fm) = &ast_item else { - panic!("expected field ForeignMod for AstTarget::ForeignMod"); - }; - let abi = fm.abi.map_or(ExternAbi::FALLBACK, |abi| { abi.symbol_unescaped.as_str().parse().unwrap_or_else(|_| { cx.dcx().span_delayed_bug( diff --git a/compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs b/compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs index 252a9f5e379bc..8a0c31400b100 100644 --- a/compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs +++ b/compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs @@ -1,4 +1,3 @@ -use rustc_ast::ItemKind; use rustc_attr_ir::target::AstTarget; use rustc_attr_ir::{MacroUseArgs, find_attr}; use rustc_feature::AttributeStability; @@ -182,13 +181,11 @@ impl SingleAttributeParser for MacroExportParser { } fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) { - let AstTarget::MacroDef(item_kind) = cx.ast_target else { + let AstTarget::MacroDef(_, macro_def) = cx.ast_target else { return; }; - if let ItemKind::MacroDef(_, macro_def) = item_kind - && !macro_def.macro_rules - { + if !macro_def.macro_rules { cx.emit_lint(UNUSED_ATTRIBUTES, MacroExport::OnDeclMacro, attr_span); } } diff --git a/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs b/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs index 3c4a77b3bb673..ea670a70083a7 100644 --- a/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs +++ b/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs @@ -1,4 +1,4 @@ -use rustc_ast::{ItemKind, VariantData}; +use rustc_ast::VariantData; use rustc_feature::AttributeStability; use super::prelude::*; @@ -22,14 +22,10 @@ impl NoArgsAttributeParser for NonExhaustiveParser { const CREATE: fn(Span) -> AttributeKind = AttributeKind::NonExhaustive; fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) { - let rustc_attr_ir::target::AstTarget::Struct(ast_item) = cx.ast_target else { + let rustc_attr_ir::target::AstTarget::Struct(_, _, data) = cx.ast_target else { return; }; - let ItemKind::Struct(_, _, data) = ast_item else { - panic!("expected struct AST target item for AstTarget::Struct"); - }; - if let VariantData::Struct { fields, .. } = data && fields.iter().any(|f| f.default_value().is_some()) { diff --git a/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs b/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs index 583f76f9dea80..e2cd60b973a9d 100644 --- a/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs +++ b/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs @@ -1,6 +1,6 @@ use std::path::PathBuf; -use rustc_ast::{GenericParamKind, ItemKind, LitIntType, LitKind, MetaItemLit}; +use rustc_ast::{GenericParamKind, LitIntType, LitKind, MetaItemLit}; use rustc_attr_ir::lang_items::LangItem; use rustc_attr_ir::{ BorrowckGraphvizFormatKind, CguFields, CguKind, RustcCleanAttribute, RustcCleanQueries, @@ -171,13 +171,10 @@ impl SingleAttributeParser for RustcLegacyConstGenericsParser { } fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) { - let rustc_attr_ir::target::AstTarget::Fn(item_kind) = cx.ast_target else { + let rustc_attr_ir::target::AstTarget::Fn(function) = cx.ast_target else { return; }; - let ItemKind::Fn(function) = item_kind else { - panic!("expected fn AST target item for Target::Fn"); - }; let index_list = rustc_attr_ir::find_attr!(cx.parsed_attrs, RustcLegacyConstGenerics { fn_indexes, .. } => fn_indexes) .expect("missing parsed RustcLegacyConstGenerics attribute in finalize_check"); let generics = &function.generics; From 62543f6518765cf3faa56080a605920a6edc721c Mon Sep 17 00:00:00 2001 From: Richard Tjokroutomo Date: Wed, 7 Oct 2026 23:19:36 +0800 Subject: [PATCH 12/13] pass AstTarget::Expression too Signed-off-by: Richard Tjokroutomo --- compiler/rustc_ast_lowering/src/block.rs | 10 +---- compiler/rustc_ast_lowering/src/contract.rs | 41 ++++++++++++------- compiler/rustc_ast_lowering/src/expr.rs | 19 +++++---- .../rustc_ast_lowering/src/expr/closure.rs | 4 +- compiler/rustc_ast_lowering/src/pat.rs | 4 +- .../src/attributes/link_attrs.rs | 16 ++++---- .../src/attributes/non_exhaustive.rs | 3 +- .../src/attributes/rustc_internal.rs | 3 +- compiler/rustc_attr_parsing/src/interface.rs | 4 +- compiler/rustc_resolve/src/def_collector.rs | 4 +- 10 files changed, 61 insertions(+), 47 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/block.rs b/compiler/rustc_ast_lowering/src/block.rs index 52f92a96c8ce3..dbed9093fc29b 100644 --- a/compiler/rustc_ast_lowering/src/block.rs +++ b/compiler/rustc_ast_lowering/src/block.rs @@ -1,5 +1,5 @@ use rustc_ast::{Block, BlockCheckMode, Local, LocalKind, Stmt, StmtKind}; -use rustc_attr_ir::target::Target; +use rustc_attr_ir::target::{AstTarget, Target}; use rustc_hir as hir; use rustc_span::sym; use smallvec::SmallVec; @@ -104,13 +104,7 @@ impl<'hir> LoweringContext<'_, 'hir> { }; let span = self.lower_span(l.span); let source = hir::LocalSource::Normal; - self.lower_attrs( - hir_id, - &l.attrs, - l.span, - Target::Statement, - rustc_attr_ir::target::AstTarget::Statement(s), - ); + self.lower_attrs(hir_id, &l.attrs, l.span, Target::Statement, AstTarget::Statement(s)); self.arena.alloc(hir::LetStmt { hir_id, super_, ty, pat, init, els, span, source }) } diff --git a/compiler/rustc_ast_lowering/src/contract.rs b/compiler/rustc_ast_lowering/src/contract.rs index a7663d49e0a4a..66c007813698d 100644 --- a/compiler/rustc_ast_lowering/src/contract.rs +++ b/compiler/rustc_ast_lowering/src/contract.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use rustc_attr_ir::lang_items::LangItem; -use rustc_attr_ir::target::Target; +use rustc_attr_ir::target::{AstTarget, Target}; use rustc_span::sym; use thin_vec::thin_vec; @@ -62,8 +62,12 @@ impl<'hir> LoweringContext<'_, 'hir> { postcond_checker, ); - let wrapped_body = - self.wrap_body_with_contract_check(body, contract_check, postcond_checker.span); + let wrapped_body = self.wrap_body_with_contract_check( + body, + contract_check, + postcond_checker.span, + ens, + ); self.expr_block(wrapped_body) } (None, Some(ens)) => { @@ -92,8 +96,12 @@ impl<'hir> LoweringContext<'_, 'hir> { let contract_check = self.lower_contract_check_with_postcond(contract_decls, None, postcond_checker); - let wrapped_body = - self.wrap_body_with_contract_check(body, contract_check, postcond_checker.span); + let wrapped_body = self.wrap_body_with_contract_check( + body, + contract_check, + postcond_checker.span, + ens, + ); self.expr_block(wrapped_body) } (Some(req), None) => { @@ -238,6 +246,7 @@ impl<'hir> LoweringContext<'_, 'hir> { body: impl FnOnce(&mut Self) -> rustc_hir::Expr<'hir>, contract_check: &'hir rustc_hir::Expr<'hir>, postcond_span: rustc_span::Span, + ast_expr: &rustc_ast::Expr, ) -> &'hir rustc_hir::Block<'hir> { let check_ident: rustc_span::Ident = rustc_span::Ident::new(sym::__ensures_checker, postcond_span); @@ -266,7 +275,8 @@ impl<'hir> LoweringContext<'_, 'hir> { let body = self.arena.alloc(body(self)); // Finally, inject an ensures check on the implicit return of the body. - let body = self.inject_ensures_check(body, postcond_span, check_ident, check_hir_id); + let body = + self.inject_ensures_check(body, postcond_span, check_ident, check_hir_id, ast_expr); // Flatten the body into precond, then postcond, then wrapped body. let wrapped_body = self.block_all( @@ -282,14 +292,16 @@ impl<'hir> LoweringContext<'_, 'hir> { pub(super) fn checked_return( &mut self, opt_expr: Option<&'hir rustc_hir::Expr<'hir>>, + ast_expr: &rustc_ast::Expr, ) -> rustc_hir::ExprKind<'hir> { - let checked_ret = - if let Some((check_span, check_ident, check_hir_id)) = self.contract_ensures { - let expr = opt_expr.unwrap_or_else(|| self.expr_unit(check_span)); - Some(self.inject_ensures_check(expr, check_span, check_ident, check_hir_id)) - } else { - opt_expr - }; + let checked_ret = if let Some((check_span, check_ident, check_hir_id)) = + self.contract_ensures + { + let expr = opt_expr.unwrap_or_else(|| self.expr_unit(check_span)); + Some(self.inject_ensures_check(expr, check_span, check_ident, check_hir_id, ast_expr)) + } else { + opt_expr + }; rustc_hir::ExprKind::Ret(checked_ret) } @@ -300,6 +312,7 @@ impl<'hir> LoweringContext<'_, 'hir> { span: rustc_span::Span, cond_ident: rustc_span::Ident, cond_hir_id: rustc_hir::HirId, + ast_expr: &rustc_ast::Expr, ) -> &'hir rustc_hir::Expr<'hir> { // { // let ret = { body }; @@ -357,7 +370,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &attrs, span, Target::Expression, - rustc_attr_ir::target::AstTarget::None, + AstTarget::Expression(ast_expr), ); let ret_block = self.block_all(span, arena_vec![self; ret_stmt], Some(contract_check)); diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 8533dc7e3de42..42c4f451e4a8d 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -492,11 +492,11 @@ impl<'hir> LoweringContext<'_, 'hir> { ExprKind::Continue(opt_label) => { hir::ExprKind::Continue(self.lower_jump_destination(e.id, *opt_label)) } - ExprKind::Ret(e) => { - let expr = e.as_ref().map(|x| self.lower_expr(x)); - self.checked_return(expr) + ExprKind::Ret(box_e) => { + let expr = box_e.as_ref().map(|x| self.lower_expr(x)); + self.checked_return(expr, e) } - ExprKind::Yeet(sub_expr) => self.lower_expr_yeet(e.span, sub_expr.as_deref()), + ExprKind::Yeet(sub_expr) => self.lower_expr_yeet(e.span, sub_expr.as_deref(), e), ExprKind::Become(sub_expr) => { let sub_expr = self.lower_expr(sub_expr); hir::ExprKind::Become(sub_expr) @@ -2037,7 +2037,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ), )) } else { - let ret_expr = self.checked_return(Some(from_residual_expr)); + let ret_expr = self.checked_return(Some(from_residual_expr), sub_expr); self.arena.alloc(self.expr(try_span, ret_expr)) }; self.lower_attrs( @@ -2068,7 +2068,12 @@ impl<'hir> LoweringContext<'_, 'hir> { /// ``` /// But to simplify this, there's a `from_yeet` lang item function which /// handles the combined `FromResidual::from_residual(Yeet(residual))`. - fn lower_expr_yeet(&mut self, span: Span, sub_expr: Option<&Expr>) -> hir::ExprKind<'hir> { + fn lower_expr_yeet( + &mut self, + span: Span, + sub_expr: Option<&Expr>, + ast_expr: &rustc_ast::Expr, + ) -> hir::ExprKind<'hir> { // The expression (if present) or `()` otherwise. let (yeeted_span, yeeted_expr) = if let Some(sub_expr) = sub_expr { (sub_expr.span, self.lower_expr(sub_expr)) @@ -2096,7 +2101,7 @@ impl<'hir> LoweringContext<'_, 'hir> { Some(from_yeet_expr), ) } - TryBlockScope::Function => self.checked_return(Some(from_yeet_expr)), + TryBlockScope::Function => self.checked_return(Some(from_yeet_expr), ast_expr), } } diff --git a/compiler/rustc_ast_lowering/src/expr/closure.rs b/compiler/rustc_ast_lowering/src/expr/closure.rs index 99ac5d1743731..808248db3204e 100644 --- a/compiler/rustc_ast_lowering/src/expr/closure.rs +++ b/compiler/rustc_ast_lowering/src/expr/closure.rs @@ -1,6 +1,6 @@ use rustc_ast::*; use rustc_attr_ir::find_attr; -use rustc_attr_ir::target::Target; +use rustc_attr_ir::target::{AstTarget, Target}; use rustc_hir as hir; use rustc_hir::HirId; use rustc_span::{Span, span_bug}; @@ -25,7 +25,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &e.attrs, e.span, Target::from_expr(e), - rustc_attr_ir::target::AstTarget::Closure(closure), + AstTarget::Closure(closure), ); match closure.coroutine_marker { diff --git a/compiler/rustc_ast_lowering/src/pat.rs b/compiler/rustc_ast_lowering/src/pat.rs index 2f2c5d7747269..cce1b7039e7b2 100644 --- a/compiler/rustc_ast_lowering/src/pat.rs +++ b/compiler/rustc_ast_lowering/src/pat.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use rustc_ast::*; use rustc_attr_ir::lang_items::LangItem; -use rustc_attr_ir::target::Target; +use rustc_attr_ir::target::{AstTarget, Target}; use rustc_hir as hir; use rustc_hir::def::{DefKind, Res}; use rustc_span::{DesugaringKind, Ident, Span, Spanned, respan, span_bug}; @@ -97,7 +97,7 @@ impl<'hir> LoweringContext<'_, 'hir> { &f.attrs, f.span, Target::PatField, - rustc_attr_ir::target::AstTarget::Pat(pattern), + AstTarget::Pat(pattern), ); hir::PatField { diff --git a/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs b/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs index 40ca63e2a4da7..09a82c6e7db0e 100644 --- a/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs +++ b/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs @@ -266,14 +266,14 @@ impl CombineAttributeParser for LinkParser { }; let abi = fm.abi.map_or(ExternAbi::FALLBACK, |abi| { - abi.symbol_unescaped.as_str().parse().unwrap_or_else(|_| { - cx.dcx().span_delayed_bug( - abi.span, - "LinkParser::finalize_check was unable to pre-detect the ABI, so it continues to use the recovery value solely to check for unused_attributes in the lint; a user error E0703 will be reported later in lower_abi", - ); - ExternAbi::Rust - }) - }); + abi.symbol_unescaped.as_str().parse().unwrap_or_else(|_| { + cx.dcx().span_delayed_bug( + abi.span, + "LinkParser::finalize_check was unable to pre-detect the ABI, so it continues to use the recovery value solely to check for unused_attributes in the lint; a user error E0703 will be reported later in lower_abi", + ); + ExternAbi::Rust + }) + }); if matches!(abi, ExternAbi::Rust) { cx.emit_lint(UNUSED_ATTRIBUTES, Link, attr_span); } diff --git a/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs b/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs index ea670a70083a7..6611041e0c4aa 100644 --- a/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs +++ b/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs @@ -1,4 +1,5 @@ use rustc_ast::VariantData; +use rustc_attr_ir::target::AstTarget; use rustc_feature::AttributeStability; use super::prelude::*; @@ -22,7 +23,7 @@ impl NoArgsAttributeParser for NonExhaustiveParser { const CREATE: fn(Span) -> AttributeKind = AttributeKind::NonExhaustive; fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) { - let rustc_attr_ir::target::AstTarget::Struct(_, _, data) = cx.ast_target else { + let AstTarget::Struct(_, _, data) = cx.ast_target else { return; }; diff --git a/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs b/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs index e2cd60b973a9d..c5ce41bbb058b 100644 --- a/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs +++ b/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs @@ -2,6 +2,7 @@ use std::path::PathBuf; use rustc_ast::{GenericParamKind, LitIntType, LitKind, MetaItemLit}; use rustc_attr_ir::lang_items::LangItem; +use rustc_attr_ir::target::AstTarget; use rustc_attr_ir::{ BorrowckGraphvizFormatKind, CguFields, CguKind, RustcCleanAttribute, RustcCleanQueries, RustcMirKind, @@ -171,7 +172,7 @@ impl SingleAttributeParser for RustcLegacyConstGenericsParser { } fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) { - let rustc_attr_ir::target::AstTarget::Fn(function) = cx.ast_target else { + let AstTarget::Fn(function) = cx.ast_target else { return; }; diff --git a/compiler/rustc_attr_parsing/src/interface.rs b/compiler/rustc_attr_parsing/src/interface.rs index 7f8e481ce7505..f46f09fc3a859 100644 --- a/compiler/rustc_attr_parsing/src/interface.rs +++ b/compiler/rustc_attr_parsing/src/interface.rs @@ -6,7 +6,7 @@ use std::sync::atomic::{AtomicBool, Ordering}; use rustc_ast as ast; use rustc_ast::token::DocFragmentKind; use rustc_ast::{AttrStyle, CRATE_NODE_ID, NodeId, Safety}; -use rustc_attr_ir::target::Target; +use rustc_attr_ir::target::{AstTarget, Target}; use rustc_attr_ir::{AttrArgs, AttrItem, AttrPath, Attribute, AttributeKind, HashIgnoredAttrId}; use rustc_data_structures::sync::{DynSend, DynSync}; use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, Level, MultiSpan}; @@ -161,7 +161,7 @@ impl<'sess> AttributeParser<'sess> { attrs, target_span, target, - rustc_attr_ir::target::AstTarget::None, + AstTarget::None, std::convert::identity, |lint_id, span, kind| { sess.psess.dyn_buffer_lint_sess(lint_id.lint, span, target_node_id, kind.0) diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index fb2eac959d292..b55a3534bb28f 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -2,7 +2,7 @@ use std::mem; use rustc_ast::visit::FnKind; use rustc_ast::*; -use rustc_attr_ir::target::Target; +use rustc_attr_ir::target::{AstTarget, Target}; use rustc_attr_parsing::{AttributeParser, ShouldEmit}; use rustc_expand::expand::AstFragment; use rustc_hir as hir; @@ -183,7 +183,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> { &i.attrs, i.span, Target::MacroDef, - rustc_attr_ir::target::AstTarget::from_ast_item(&i.kind), + AstTarget::from_ast_item(&i.kind), std::convert::identity, |_lint_id, _span, _kind| { // FIXME(jdonszelmann): emit lints here properly From 2176bb5869f5a54735ae5f047f7f7334d22c05fb Mon Sep 17 00:00:00 2001 From: Richard Tjokroutomo Date: Fri, 9 Oct 2026 23:10:37 +0800 Subject: [PATCH 13/13] remove unused AstTarget types & some tidy ups Signed-off-by: Richard Tjokroutomo --- compiler/rustc_attr_ir/src/target.rs | 15 +++------------ .../src/attributes/link_attrs.rs | 7 ++++--- .../src/attributes/non_exhaustive.rs | 1 - .../src/attributes/rustc_internal.rs | 1 - compiler/rustc_attr_parsing/src/interface.rs | 2 +- 5 files changed, 8 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_attr_ir/src/target.rs b/compiler/rustc_attr_ir/src/target.rs index a6ad343bd2785..180f03f60fc85 100644 --- a/compiler/rustc_attr_ir/src/target.rs +++ b/compiler/rustc_attr_ir/src/target.rs @@ -6,9 +6,9 @@ use rustc_abi::ExternAbi; pub use rustc_ast::visit::AssocCtxt; use rustc_ast::{ Arm, AssocItemKind, Closure, ConstBlockItem, ConstItem, Crate, EnumDef, Expr, ExprField, - FieldDef, Fn, ForLoop, ForeignItemKind, ForeignMod, GenericParam, Generics, Impl, InlineAsm, - Local, MacroDef, ModKind, Param, Pat, PatField, Safety, StaticItem, Stmt, Trait, TraitAlias, - TyAlias, UseTree, Variant, VariantData, WherePredicate, ast, + FieldDef, Fn, ForeignItemKind, ForeignMod, GenericParam, Generics, Impl, InlineAsm, MacroDef, + ModKind, Param, Pat, Safety, StaticItem, Stmt, Trait, TraitAlias, TyAlias, UseTree, Variant, + VariantData, WherePredicate, ast, }; use rustc_macros::StableHash; use rustc_span::{Ident, Symbol}; @@ -51,24 +51,15 @@ pub enum AstTarget<'a> { // Target types that correspond exclusively to `Expr` kind. Mapping is obtained from `Target::from_expr`. Closure(&'a Closure), Expression(&'a Expr), - ForLoop(&'a ForLoop), - Loop, - While, - Break, Arm(&'a Arm), - ConstParam(&'a GenericParam), Crate(&'a Crate), ExprField(&'a ExprField), Field(&'a FieldDef), GenericParam(&'a GenericParam), - LifetimeParam(&'a GenericParam), - Local(&'a Local), Param(&'a Param), Pat(&'a Pat), - PatField(&'a PatField), Statement(&'a Stmt), - TypeParam(&'a GenericParam), Variant(&'a Variant), WherePredicate(&'a WherePredicate), diff --git a/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs b/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs index 09a82c6e7db0e..d5b2f7accb621 100644 --- a/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs +++ b/compiler/rustc_attr_parsing/src/attributes/link_attrs.rs @@ -1,5 +1,6 @@ use rustc_abi::ExternAbi; use rustc_attr_ir::AttributeKind::{LinkName, LinkOrdinal, LinkSection}; +use rustc_attr_ir::target::AstTarget; use rustc_attr_ir::*; use rustc_errors::msg; use rustc_feature::{AttributeStability, Features}; @@ -261,7 +262,7 @@ impl CombineAttributeParser for LinkParser { } fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) { - let rustc_attr_ir::target::AstTarget::ForeignMod(fm) = cx.ast_target else { + let AstTarget::ForeignMod(fm) = cx.ast_target else { return; }; @@ -272,8 +273,8 @@ impl CombineAttributeParser for LinkParser { "LinkParser::finalize_check was unable to pre-detect the ABI, so it continues to use the recovery value solely to check for unused_attributes in the lint; a user error E0703 will be reported later in lower_abi", ); ExternAbi::Rust - }) - }); + }) + }); if matches!(abi, ExternAbi::Rust) { cx.emit_lint(UNUSED_ATTRIBUTES, Link, attr_span); } diff --git a/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs b/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs index 6611041e0c4aa..c2f8875e440e7 100644 --- a/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs +++ b/compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs @@ -26,7 +26,6 @@ impl NoArgsAttributeParser for NonExhaustiveParser { let AstTarget::Struct(_, _, data) = cx.ast_target else { return; }; - if let VariantData::Struct { fields, .. } = data && fields.iter().any(|f| f.default_value().is_some()) { diff --git a/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs b/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs index c5ce41bbb058b..73f376bd39301 100644 --- a/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs +++ b/compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs @@ -175,7 +175,6 @@ impl SingleAttributeParser for RustcLegacyConstGenericsParser { let AstTarget::Fn(function) = cx.ast_target else { return; }; - let index_list = rustc_attr_ir::find_attr!(cx.parsed_attrs, RustcLegacyConstGenerics { fn_indexes, .. } => fn_indexes) .expect("missing parsed RustcLegacyConstGenerics attribute in finalize_check"); let generics = &function.generics; diff --git a/compiler/rustc_attr_parsing/src/interface.rs b/compiler/rustc_attr_parsing/src/interface.rs index f46f09fc3a859..42cd5a4190dfa 100644 --- a/compiler/rustc_attr_parsing/src/interface.rs +++ b/compiler/rustc_attr_parsing/src/interface.rs @@ -317,7 +317,7 @@ impl<'sess> AttributeParser<'sess> { attrs: &[ast::Attribute], target_span: Span, target: Target, - ast_target: rustc_attr_ir::target::AstTarget<'_>, + ast_target: AstTarget<'_>, lower_span: impl Copy + Fn(Span) -> Span, mut emit_lint: impl FnMut(LintId, MultiSpan, EmitAttribute), ) -> Vec {