From 4a22dcaa247ee0843b27994a8065754b6ad5ba1f Mon Sep 17 00:00:00 2001 From: mejrs <59372212+mejrs@users.noreply.github.com> Date: Sat, 1 Aug 2026 15:14:38 +0200 Subject: [PATCH 1/2] Move attributes out of rustc_hir --- Cargo.lock | 22 +- compiler/rustc_attr_ir/Cargo.toml | 20 + compiler/rustc_attr_ir/src/attr.rs | 376 ++++++++++++++++++ .../src}/canonical_symbols.rs | 0 .../src}/data_structures.rs | 9 +- .../attrs => rustc_attr_ir/src}/diagnostic.rs | 2 +- .../src/diagnostic_items.rs | 4 +- .../src}/encode_cross_crate.rs | 2 +- .../src/lang_items.rs | 6 +- .../attrs/mod.rs => rustc_attr_ir/src/lib.rs} | 27 +- .../src}/pretty_printing.rs | 0 .../src/stability.rs | 2 +- .../src/target.rs | 93 ----- .../src/weak_lang_items.rs | 0 compiler/rustc_hir/Cargo.toml | 4 +- compiler/rustc_hir/src/arena.rs | 2 +- compiler/rustc_hir/src/def.rs | 2 +- compiler/rustc_hir/src/hir.rs | 366 +---------------- compiler/rustc_hir/src/intravisit.rs | 1 + compiler/rustc_hir/src/lib.rs | 23 +- compiler/rustc_hir/src/stable_hash_impls.rs | 7 - compiler/rustc_hir/src/target_impls.rs | 96 +++++ triagebot.toml | 6 +- 23 files changed, 568 insertions(+), 502 deletions(-) create mode 100644 compiler/rustc_attr_ir/Cargo.toml create mode 100644 compiler/rustc_attr_ir/src/attr.rs rename compiler/{rustc_hir/src/attrs => rustc_attr_ir/src}/canonical_symbols.rs (100%) rename compiler/{rustc_hir/src/attrs => rustc_attr_ir/src}/data_structures.rs (99%) rename compiler/{rustc_hir/src/attrs => rustc_attr_ir/src}/diagnostic.rs (99%) rename compiler/{rustc_hir => rustc_attr_ir}/src/diagnostic_items.rs (82%) rename compiler/{rustc_hir/src/attrs => rustc_attr_ir/src}/encode_cross_crate.rs (99%) rename compiler/{rustc_hir => rustc_attr_ir}/src/lang_items.rs (99%) rename compiler/{rustc_hir/src/attrs/mod.rs => rustc_attr_ir/src/lib.rs} (84%) rename compiler/{rustc_hir/src/attrs => rustc_attr_ir/src}/pretty_printing.rs (100%) rename compiler/{rustc_hir => rustc_attr_ir}/src/stability.rs (99%) rename compiler/{rustc_hir => rustc_attr_ir}/src/target.rs (74%) rename compiler/{rustc_hir => rustc_attr_ir}/src/weak_lang_items.rs (100%) create mode 100644 compiler/rustc_hir/src/target_impls.rs diff --git a/Cargo.lock b/Cargo.lock index 1b57dbf60195d..91a2f6b167d55 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3710,6 +3710,24 @@ dependencies = [ "thin-vec", ] +[[package]] +name = "rustc_attr_ir" +version = "0.0.0" +dependencies = [ + "rustc_abi", + "rustc_ast", + "rustc_ast_pretty", + "rustc_data_structures", + "rustc_error_messages", + "rustc_macros", + "rustc_serialize", + "rustc_span", + "rustc_target", + "smallvec", + "thin-vec", + "tracing", +] + [[package]] name = "rustc_attr_parsing" version = "0.0.0" @@ -4087,7 +4105,7 @@ dependencies = [ "rustc_abi", "rustc_arena", "rustc_ast", - "rustc_ast_pretty", + "rustc_attr_ir", "rustc_data_structures", "rustc_error_messages", "rustc_errors", @@ -4099,8 +4117,6 @@ dependencies = [ "rustc_serialize", "rustc_span", "rustc_target", - "smallvec", - "thin-vec", "tracing", ] diff --git a/compiler/rustc_attr_ir/Cargo.toml b/compiler/rustc_attr_ir/Cargo.toml new file mode 100644 index 0000000000000..28ca2c8839508 --- /dev/null +++ b/compiler/rustc_attr_ir/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "rustc_attr_ir" +version = "0.0.0" +edition = "2024" + +[dependencies] +# tidy-alphabetical-start +rustc_abi = { path = "../rustc_abi" } +rustc_ast = { path = "../rustc_ast" } +rustc_ast_pretty = { path = "../rustc_ast_pretty" } +rustc_data_structures = { path = "../rustc_data_structures" } +rustc_error_messages = { path = "../rustc_error_messages" } +rustc_macros = { path = "../rustc_macros" } +rustc_serialize = { path = "../rustc_serialize" } +rustc_span = { path = "../rustc_span" } +rustc_target = { path = "../rustc_target" } +smallvec = { version = "1.8.1", features = ["union", "may_dangle"] } +thin-vec = "0.2.18" +tracing = "0.1" +# tidy-alphabetical-end diff --git a/compiler/rustc_attr_ir/src/attr.rs b/compiler/rustc_attr_ir/src/attr.rs new file mode 100644 index 0000000000000..6068c11590a23 --- /dev/null +++ b/compiler/rustc_attr_ir/src/attr.rs @@ -0,0 +1,376 @@ +use std::fmt; + +use rustc_ast::attr::AttributeExt; +use rustc_ast::token::DocFragmentKind; +use rustc_ast::{AttrStyle, DelimArgs, MetaItemInner, MetaItemLit, ast, join_path_idents}; +use rustc_data_structures::stable_hash::{StableHash, StableHashCtxt, StableHasher}; +use rustc_error_messages::{DiagArgValue, IntoDiagArg}; +use rustc_macros::{Decodable, Encodable, StableHash}; +use rustc_span::{AttrId, DUMMY_SP, Ident, Span, Symbol, sym}; +use smallvec::SmallVec; +use thin_vec::ThinVec; + +use crate::AttributeKind; +/// Arguments passed to an attribute macro. +#[derive(Clone, Debug, StableHash, Encodable, Decodable)] +pub enum AttrArgs { + /// No arguments: `#[attr]`. + Empty, + /// Delimited arguments: `#[attr()/[]/{}]`. + Delimited(DelimArgs), + /// Arguments of a key-value attribute: `#[attr = "value"]`. + Eq { + /// Span of the `=` token. + eq_span: Span, + /// The "value". + expr: MetaItemLit, + }, +} + +#[derive(Clone, Debug, StableHash, Encodable, Decodable)] +pub struct AttrPath { + pub segments: Box<[Symbol]>, + pub span: Span, +} + +impl IntoDiagArg for AttrPath { + fn into_diag_arg(self, path: &mut Option) -> DiagArgValue { + self.to_string().into_diag_arg(path) + } +} + +impl AttrPath { + pub fn from_ast(path: &ast::Path, lower_span: impl Copy + Fn(Span) -> Span) -> Self { + AttrPath { + segments: path + .segments + .iter() + .map(|i| i.ident.name) + .collect::>() + .into_boxed_slice(), + span: lower_span(path.span), + } + } +} + +impl fmt::Display for AttrPath { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + join_path_idents(self.segments.iter().map(|i| Ident { name: *i, span: DUMMY_SP })) + ) + } +} + +#[derive(Clone, Debug, StableHash, Encodable, Decodable)] +pub struct AttrItem { + // Not lowered to hir::Path because we have no NodeId to resolve to. + pub path: AttrPath, + pub args: AttrArgs, + pub id: HashIgnoredAttrId, + /// Denotes if the attribute decorates the following construct (outer) + /// or the construct this attribute is contained within (inner). + pub style: AttrStyle, + /// Span of the entire attribute + pub span: Span, +} + +/// The derived implementation of [`StableHash`] on [`Attribute`]s shouldn't hash +/// [`AttrId`]s. By wrapping them in this, we make sure we never do. +#[derive(Copy, Debug, Encodable, Decodable, Clone)] +pub struct HashIgnoredAttrId { + pub attr_id: AttrId, +} + +impl StableHash for HashIgnoredAttrId { + fn stable_hash(&self, _hcx: &mut Hcx, _hasher: &mut StableHasher) { + /* we don't hash HashIgnoredAttrId, we ignore them */ + } +} + +/// Many functions on this type have their documentation in the [`AttributeExt`] trait, +/// since they defer their implementation directly to that trait. +#[derive(Clone, Debug, Encodable, Decodable, StableHash)] +pub enum Attribute { + /// A parsed built-in attribute. + /// + /// Each attribute has a span connected to it. However, you must be somewhat careful using it. + /// That's because sometimes we merge multiple attributes together, like when an item has + /// multiple `repr` attributes. In this case the span might not be very useful. + Parsed(AttributeKind), + + /// An attribute that could not be parsed, out of a token-like representation. + /// This is the case for custom tool attributes. + Unparsed(Box), +} + +impl Attribute { + pub fn get_normal_item(&self) -> &AttrItem { + match &self { + Attribute::Unparsed(normal) => &normal, + _ => panic!("unexpected parsed attribute"), + } + } + + pub fn value_lit(&self) -> Option<&MetaItemLit> { + match &self { + Attribute::Unparsed(n) => match n.as_ref() { + AttrItem { args: AttrArgs::Eq { eq_span: _, expr }, .. } => Some(expr), + _ => None, + }, + _ => None, + } + } + + pub fn is_parsed_attr(&self) -> bool { + match self { + Attribute::Parsed(_) => true, + Attribute::Unparsed(_) => false, + } + } + + pub fn is_prefix_attr_for_suggestions(&self) -> bool { + match self { + Attribute::Unparsed(attr) => attr.span.desugaring_kind().is_none(), + // Other parsed attributes that can appear on expressions originate from source and + // should make suggestions treat the expression like a prefixed form. + Attribute::Parsed(_) => true, + } + } +} + +impl AttributeExt for Attribute { + #[inline] + fn id(&self) -> AttrId { + match &self { + Attribute::Unparsed(u) => u.id.attr_id, + _ => panic!(), + } + } + + #[inline] + fn meta_item_list(&self) -> Option> { + match &self { + Attribute::Unparsed(n) => match n.as_ref() { + AttrItem { args: AttrArgs::Delimited(d), .. } => { + ast::MetaItemKind::list_from_tokens(d.tokens.clone()) + } + _ => None, + }, + _ => None, + } + } + + #[inline] + fn value_str(&self) -> Option { + self.value_lit().and_then(|x| x.value_as_str()) + } + + #[inline] + fn value_span(&self) -> Option { + self.value_lit().map(|i| i.span) + } + + /// For a single-segment attribute, returns its name; otherwise, returns `None`. + #[inline] + fn name(&self) -> Option { + match &self { + Attribute::Unparsed(n) => { + if let [ident] = n.path.segments.as_ref() { + Some(*ident) + } else { + None + } + } + _ => None, + } + } + + #[inline] + fn path_matches(&self, name: &[Symbol]) -> bool { + match &self { + Attribute::Unparsed(n) => n.path.segments.iter().eq(name), + _ => false, + } + } + + #[inline] + fn is_doc_comment(&self) -> Option { + if let Attribute::Parsed(AttributeKind::DocComment { span, .. }) = self { + Some(*span) + } else { + None + } + } + + #[inline] + fn span(&self) -> Span { + match &self { + Attribute::Unparsed(u) => u.span, + // FIXME: should not be needed anymore when all attrs are parsed + Attribute::Parsed(AttributeKind::DocComment { span, .. }) => *span, + Attribute::Parsed(AttributeKind::Deprecated { span, .. }) => *span, + Attribute::Parsed(AttributeKind::CfgTrace(cfgs)) => cfgs[0].1, + a => panic!("can't get the span of an arbitrary parsed attribute: {a:?}"), + } + } + + #[inline] + fn is_word(&self) -> bool { + match &self { + Attribute::Unparsed(n) => { + matches!(n.args, AttrArgs::Empty) + } + _ => false, + } + } + + #[inline] + fn symbol_path(&self) -> Option> { + match &self { + Attribute::Unparsed(n) => Some(n.path.segments.iter().copied().collect()), + _ => None, + } + } + + fn path_span(&self) -> Option { + match &self { + Attribute::Unparsed(attr) => Some(attr.path.span), + Attribute::Parsed(_) => None, + } + } + + #[inline] + fn doc_str(&self) -> Option { + match &self { + Attribute::Parsed(AttributeKind::DocComment { comment, .. }) => Some(*comment), + _ => None, + } + } + + fn is_automatically_derived_attr(&self) -> bool { + matches!(self, Attribute::Parsed(AttributeKind::AutomaticallyDerived)) + } + + #[inline] + fn doc_str_and_fragment_kind(&self) -> Option<(Symbol, DocFragmentKind)> { + match &self { + Attribute::Parsed(AttributeKind::DocComment { kind, comment, .. }) => { + Some((*comment, *kind)) + } + _ => None, + } + } + + fn doc_resolution_scope(&self) -> Option { + match self { + Attribute::Parsed(AttributeKind::DocComment { style, .. }) => Some(*style), + Attribute::Unparsed(attr) if self.has_name(sym::doc) && self.value_str().is_some() => { + Some(attr.style) + } + _ => None, + } + } + + fn is_proc_macro_attr(&self) -> bool { + matches!( + self, + Attribute::Parsed( + AttributeKind::ProcMacro + | AttributeKind::ProcMacroAttribute + | AttributeKind::ProcMacroDerive { .. } + ) + ) + } + + fn is_doc_hidden(&self) -> bool { + matches!(self, Attribute::Parsed(AttributeKind::Doc(d)) if d.hidden.is_some()) + } + + fn is_doc_keyword_or_attribute(&self) -> bool { + matches!(self, Attribute::Parsed(AttributeKind::Doc(d)) if d.attribute.is_some() || d.keyword.is_some()) + } + + fn is_rustc_doc_primitive(&self) -> bool { + matches!(self, Attribute::Parsed(AttributeKind::RustcDocPrimitive(..))) + } +} + +// FIXME(fn_delegation): use function delegation instead of manually forwarding +impl Attribute { + #[inline] + pub fn id(&self) -> AttrId { + AttributeExt::id(self) + } + + #[inline] + pub fn name(&self) -> Option { + AttributeExt::name(self) + } + + #[inline] + pub fn meta_item_list(&self) -> Option> { + AttributeExt::meta_item_list(self) + } + + #[inline] + pub fn value_str(&self) -> Option { + AttributeExt::value_str(self) + } + + #[inline] + pub fn value_span(&self) -> Option { + AttributeExt::value_span(self) + } + + #[inline] + pub fn path_matches(&self, name: &[Symbol]) -> bool { + AttributeExt::path_matches(self, name) + } + + #[inline] + pub fn is_doc_comment(&self) -> Option { + AttributeExt::is_doc_comment(self) + } + + #[inline] + pub fn has_name(&self, name: Symbol) -> bool { + AttributeExt::has_name(self, name) + } + + #[inline] + pub fn has_any_name(&self, names: &[Symbol]) -> bool { + AttributeExt::has_any_name(self, names) + } + + #[inline] + pub fn span(&self) -> Span { + AttributeExt::span(self) + } + + #[inline] + pub fn is_word(&self) -> bool { + AttributeExt::is_word(self) + } + + #[inline] + pub fn path(&self) -> SmallVec<[Symbol; 1]> { + AttributeExt::path(self) + } + + #[inline] + pub fn doc_str(&self) -> Option { + AttributeExt::doc_str(self) + } + + #[inline] + pub fn is_proc_macro_attr(&self) -> bool { + AttributeExt::is_proc_macro_attr(self) + } + + #[inline] + pub fn doc_str_and_fragment_kind(&self) -> Option<(Symbol, DocFragmentKind)> { + AttributeExt::doc_str_and_fragment_kind(self) + } +} diff --git a/compiler/rustc_hir/src/attrs/canonical_symbols.rs b/compiler/rustc_attr_ir/src/canonical_symbols.rs similarity index 100% rename from compiler/rustc_hir/src/attrs/canonical_symbols.rs rename to compiler/rustc_attr_ir/src/canonical_symbols.rs diff --git a/compiler/rustc_hir/src/attrs/data_structures.rs b/compiler/rustc_attr_ir/src/data_structures.rs similarity index 99% rename from compiler/rustc_hir/src/attrs/data_structures.rs rename to compiler/rustc_attr_ir/src/data_structures.rs index 530483e87329c..aa6ae5cf4a6ff 100644 --- a/compiler/rustc_hir/src/attrs/data_structures.rs +++ b/compiler/rustc_attr_ir/src/data_structures.rs @@ -20,10 +20,11 @@ use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol}; pub use rustc_target::spec::SanitizerSet; use thin_vec::ThinVec; -pub use crate::attrs::canonical_symbols::{CanonicalSymbol, CanonicalSymbols}; -use crate::attrs::diagnostic::*; -use crate::attrs::pretty_printing::PrintAttribute; -use crate::{DefaultBodyStability, LangItem, PartialConstStability, Stability}; +pub use crate::canonical_symbols::{CanonicalSymbol, CanonicalSymbols}; +use crate::diagnostic::*; +use crate::lang_items::LangItem; +use crate::pretty_printing::PrintAttribute; +use crate::stability::{DefaultBodyStability, PartialConstStability, Stability}; #[derive(Copy, Clone, Debug, StableHash, Encodable, Decodable, PrintAttribute)] pub enum EiiImplResolution { diff --git a/compiler/rustc_hir/src/attrs/diagnostic.rs b/compiler/rustc_attr_ir/src/diagnostic.rs similarity index 99% rename from compiler/rustc_hir/src/attrs/diagnostic.rs rename to compiler/rustc_attr_ir/src/diagnostic.rs index 8b309888e98a6..f0bdeb7243816 100644 --- a/compiler/rustc_hir/src/attrs/diagnostic.rs +++ b/compiler/rustc_attr_ir/src/diagnostic.rs @@ -7,7 +7,7 @@ use rustc_span::{DesugaringKind, Span, Symbol, kw}; use thin_vec::ThinVec; use tracing::debug; -use crate::attrs::PrintAttribute; +use crate::PrintAttribute; #[derive(Clone, Default, Debug, StableHash, Encodable, Decodable, PrintAttribute)] pub struct Directive { diff --git a/compiler/rustc_hir/src/diagnostic_items.rs b/compiler/rustc_attr_ir/src/diagnostic_items.rs similarity index 82% rename from compiler/rustc_hir/src/diagnostic_items.rs rename to compiler/rustc_attr_ir/src/diagnostic_items.rs index 5a1901fe88f28..984fea6d43242 100644 --- a/compiler/rustc_hir/src/diagnostic_items.rs +++ b/compiler/rustc_attr_ir/src/diagnostic_items.rs @@ -1,9 +1,7 @@ use rustc_data_structures::fx::FxIndexMap; use rustc_macros::StableHash; use rustc_span::Symbol; -use rustc_span::def_id::DefIdMap; - -use crate::def_id::DefId; +use rustc_span::def_id::{DefId, DefIdMap}; #[derive(Debug, Default, StableHash)] pub struct DiagnosticItems { diff --git a/compiler/rustc_hir/src/attrs/encode_cross_crate.rs b/compiler/rustc_attr_ir/src/encode_cross_crate.rs similarity index 99% rename from compiler/rustc_hir/src/attrs/encode_cross_crate.rs rename to compiler/rustc_attr_ir/src/encode_cross_crate.rs index 455af47142446..6a9f37f80868a 100644 --- a/compiler/rustc_hir/src/attrs/encode_cross_crate.rs +++ b/compiler/rustc_attr_ir/src/encode_cross_crate.rs @@ -1,4 +1,4 @@ -use crate::attrs::AttributeKind; +use crate::AttributeKind; #[derive(PartialEq)] pub enum EncodeCrossCrate { diff --git a/compiler/rustc_hir/src/lang_items.rs b/compiler/rustc_attr_ir/src/lang_items.rs similarity index 99% rename from compiler/rustc_hir/src/lang_items.rs rename to compiler/rustc_attr_ir/src/lang_items.rs index 00a2fc299efd8..ebd98aed609ca 100644 --- a/compiler/rustc_hir/src/lang_items.rs +++ b/compiler/rustc_attr_ir/src/lang_items.rs @@ -10,11 +10,11 @@ use rustc_data_structures::fx::FxIndexMap; use rustc_data_structures::stable_hash::{StableHash, StableHashCtxt, StableHasher}; use rustc_macros::{BlobDecodable, Encodable, PrintAttribute, StableHash}; +use rustc_span::def_id::DefId; use rustc_span::{Symbol, kw, sym}; -use crate::attrs::PrintAttribute; -use crate::def_id::DefId; -use crate::{MethodKind, Target}; +use crate::PrintAttribute; +use crate::target::{MethodKind, Target}; /// All of the lang items, defined or not. /// Defined lang items can come from the current crate or its dependencies. diff --git a/compiler/rustc_hir/src/attrs/mod.rs b/compiler/rustc_attr_ir/src/lib.rs similarity index 84% rename from compiler/rustc_hir/src/attrs/mod.rs rename to compiler/rustc_attr_ir/src/lib.rs index 5103784b7b689..588bcfafb208d 100644 --- a/compiler/rustc_hir/src/attrs/mod.rs +++ b/compiler/rustc_attr_ir/src/lib.rs @@ -1,18 +1,37 @@ //! Data structures for representing parsed attributes in the Rust compiler. -//! Formerly `rustc_attr_data_structures`. //! //! For detailed documentation about attribute processing, //! see [rustc_attr_parsing](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_attr_parsing/index.html). +// tidy-alphabetical-start +#![feature(const_default)] +#![feature(const_trait_impl)] +#![feature(default_field_values)] +#![feature(derive_const)] +#![feature(exhaustive_patterns)] +#![feature(variant_count)] +#![recursion_limit = "256"] +// tidy-alphabetical-end + +pub use attr::*; pub use data_structures::*; pub use encode_cross_crate::EncodeCrossCrate; +pub use lang_items::*; pub use pretty_printing::PrintAttribute; +pub use stability::*; +// FIXME remove pub on some of these modules? It's fairly inconsistent. +mod attr; mod canonical_symbols; mod data_structures; pub mod diagnostic; +pub mod diagnostic_items; mod encode_cross_crate; +pub mod lang_items; mod pretty_printing; +mod stability; +pub mod target; +pub mod weak_lang_items; /// A trait for types that can provide a list of attributes given a `TyCtxt`. /// @@ -20,7 +39,7 @@ mod pretty_printing; /// It is defined here with a generic `Tcx` because `rustc_hir` can't depend on `rustc_middle`. /// The concrete implementations are in `rustc_middle`. pub trait HasAttrs<'tcx, Tcx> { - fn get_attrs(self, tcx: &Tcx) -> &'tcx [crate::Attribute]; + fn get_attrs(self, tcx: &Tcx) -> &'tcx [crate::attr::Attribute]; } /// Finds attributes in sequences of attributes by pattern matching. @@ -72,7 +91,7 @@ macro_rules! find_attr { }; ($tcx: expr, $id: expr, $pattern: pat $(if $guard: expr)? => $e: expr) => {{ $crate::find_attr!( - $crate::attrs::HasAttrs::get_attrs($id, &$tcx), + $crate::HasAttrs::get_attrs($id, &$tcx), $pattern $(if $guard)? => $e ) }}; @@ -86,7 +105,7 @@ macro_rules! find_attr { 'done: { for i in $attributes_list { #[allow(unused_imports)] - use $crate::attrs::AttributeKind::*; + use $crate::AttributeKind::*; let i: &$crate::Attribute = i; match i { $crate::Attribute::Parsed($pattern) $(if $guard)? => { diff --git a/compiler/rustc_hir/src/attrs/pretty_printing.rs b/compiler/rustc_attr_ir/src/pretty_printing.rs similarity index 100% rename from compiler/rustc_hir/src/attrs/pretty_printing.rs rename to compiler/rustc_attr_ir/src/pretty_printing.rs diff --git a/compiler/rustc_hir/src/stability.rs b/compiler/rustc_attr_ir/src/stability.rs similarity index 99% rename from compiler/rustc_hir/src/stability.rs rename to compiler/rustc_attr_ir/src/stability.rs index 55f0260fc033d..1cba0b59c0f6c 100644 --- a/compiler/rustc_hir/src/stability.rs +++ b/compiler/rustc_attr_ir/src/stability.rs @@ -4,7 +4,7 @@ use rustc_ast::attr::version::RustcVersion; use rustc_macros::{BlobDecodable, Decodable, Encodable, PrintAttribute, StableHash}; use rustc_span::{ErrorGuaranteed, Symbol, sym}; -use crate::attrs::PrintAttribute; +use crate::PrintAttribute; /// The version placeholder that recently stabilized features contain inside the /// `since` field of the `#[stable]` attribute. diff --git a/compiler/rustc_hir/src/target.rs b/compiler/rustc_attr_ir/src/target.rs similarity index 74% rename from compiler/rustc_hir/src/target.rs rename to compiler/rustc_attr_ir/src/target.rs index 2097e860468ec..762ae3b6129e1 100644 --- a/compiler/rustc_hir/src/target.rs +++ b/compiler/rustc_attr_ir/src/target.rs @@ -6,9 +6,6 @@ use rustc_ast::visit::AssocCtxt; use rustc_ast::{AssocItemKind, ForeignItemKind, ast}; use rustc_macros::StableHash; -use crate::def::DefKind; -use crate::{self as hir, ItemKind, TraitItemKind}; - #[derive(Copy, Clone, PartialEq, Debug, Eq, StableHash)] pub enum GenericParamKind { Type, @@ -299,93 +296,3 @@ impl Target { } } } - -impl From<&hir::ForeignItem<'_>> for Target { - fn from(foreign_item: &hir::ForeignItem<'_>) -> Target { - match foreign_item.kind { - hir::ForeignItemKind::Fn(..) => Target::ForeignFn, - hir::ForeignItemKind::Static(..) => Target::ForeignStatic, - hir::ForeignItemKind::Type => Target::ForeignTy, - } - } -} - -impl From<&hir::GenericParam<'_>> for Target { - fn from(generic_param: &hir::GenericParam<'_>) -> Target { - match generic_param.kind { - hir::GenericParamKind::Type { default, .. } => Target::GenericParam { - kind: GenericParamKind::Type, - has_default: default.is_some(), - }, - hir::GenericParamKind::Lifetime { .. } => { - Target::GenericParam { kind: GenericParamKind::Lifetime, has_default: false } - } - hir::GenericParamKind::Const { default, .. } => Target::GenericParam { - kind: GenericParamKind::Const, - has_default: default.is_some(), - }, - } - } -} - -impl From<&hir::TraitItem<'_>> for Target { - fn from(trait_item: &hir::TraitItem<'_>) -> Target { - match trait_item.kind { - TraitItemKind::Const(..) => Target::AssocConst, - TraitItemKind::Fn(_, hir::TraitFn::Required(_)) => { - Target::Method(MethodKind::Trait { body: false }) - } - TraitItemKind::Fn(_, hir::TraitFn::Provided(_)) => { - Target::Method(MethodKind::Trait { body: true }) - } - TraitItemKind::Type(..) => Target::AssocTy, - } - } -} - -impl From for Target { - fn from(def_kind: DefKind) -> Target { - match def_kind { - DefKind::ExternCrate => Target::ExternCrate, - DefKind::Use => Target::Use, - DefKind::Static { .. } => Target::Static, - DefKind::Const { .. } => Target::Const, - DefKind::Fn => Target::Fn, - DefKind::Macro(..) => Target::MacroDef, - DefKind::Mod => Target::Mod, - DefKind::ForeignMod => Target::ForeignMod, - DefKind::GlobalAsm => Target::GlobalAsm, - DefKind::TyAlias => Target::TyAlias, - DefKind::Enum => Target::Enum, - DefKind::Struct => Target::Struct, - DefKind::Union => Target::Union, - DefKind::Trait => Target::Trait, - DefKind::TraitAlias => Target::TraitAlias, - DefKind::Impl { of_trait } => Target::Impl { of_trait }, - _ => panic!("impossible case reached"), - } - } -} - -impl From<&hir::Item<'_>> for Target { - fn from(item: &hir::Item<'_>) -> Target { - match item.kind { - ItemKind::ExternCrate(..) => Target::ExternCrate, - ItemKind::Use(..) => Target::Use, - ItemKind::Static { .. } => Target::Static, - ItemKind::Const(..) => Target::Const, - ItemKind::Fn { .. } => Target::Fn, - ItemKind::Macro(..) => Target::MacroDef, - ItemKind::Mod(..) => Target::Mod, - ItemKind::ForeignMod { .. } => Target::ForeignMod, - ItemKind::GlobalAsm { .. } => Target::GlobalAsm, - ItemKind::TyAlias(..) => Target::TyAlias, - ItemKind::Enum(..) => Target::Enum, - ItemKind::Struct(..) => Target::Struct, - ItemKind::Union(..) => Target::Union, - ItemKind::Trait { .. } => Target::Trait, - ItemKind::TraitAlias(..) => Target::TraitAlias, - ItemKind::Impl(imp_) => Target::Impl { of_trait: imp_.of_trait.is_some() }, - } - } -} diff --git a/compiler/rustc_hir/src/weak_lang_items.rs b/compiler/rustc_attr_ir/src/weak_lang_items.rs similarity index 100% rename from compiler/rustc_hir/src/weak_lang_items.rs rename to compiler/rustc_attr_ir/src/weak_lang_items.rs diff --git a/compiler/rustc_hir/Cargo.toml b/compiler/rustc_hir/Cargo.toml index 0d4a8c73e971e..991093cd26914 100644 --- a/compiler/rustc_hir/Cargo.toml +++ b/compiler/rustc_hir/Cargo.toml @@ -10,7 +10,7 @@ odht = { version = "0.3.1", features = ["nightly"] } rustc_abi = { path = "../rustc_abi" } rustc_arena = { path = "../rustc_arena" } rustc_ast = { path = "../rustc_ast" } -rustc_ast_pretty = { path = "../rustc_ast_pretty" } +rustc_attr_ir = { path = "../rustc_attr_ir" } rustc_data_structures = { path = "../rustc_data_structures" } rustc_error_messages = { path = "../rustc_error_messages" } rustc_errors = { path = "../rustc_errors" } @@ -22,7 +22,5 @@ rustc_macros = { path = "../rustc_macros" } rustc_serialize = { path = "../rustc_serialize" } rustc_span = { path = "../rustc_span" } rustc_target = { path = "../rustc_target" } -smallvec = { version = "1.8.1", features = ["union", "may_dangle"] } -thin-vec = "0.2.19" tracing = "0.1" # tidy-alphabetical-end diff --git a/compiler/rustc_hir/src/arena.rs b/compiler/rustc_hir/src/arena.rs index 6b99f21353e22..cbbaa3b768804 100644 --- a/compiler/rustc_hir/src/arena.rs +++ b/compiler/rustc_hir/src/arena.rs @@ -4,7 +4,7 @@ rustc_arena::declare_arena! { // HIR types asm_template: rustc_ast::InlineAsmTemplatePiece, - attribute: crate::Attribute, + attribute: rustc_attr_ir::Attribute, owner_info: crate::OwnerInfo<'tcx>, macro_def: rustc_ast::MacroDef, delegation_info: crate::DelegationInfo, diff --git a/compiler/rustc_hir/src/def.rs b/compiler/rustc_hir/src/def.rs index 9715b108da22a..59e4f084ab81b 100644 --- a/compiler/rustc_hir/src/def.rs +++ b/compiler/rustc_hir/src/def.rs @@ -12,8 +12,8 @@ use rustc_span::Symbol; use rustc_span::def_id::{DefId, LocalDefId}; use rustc_span::hygiene::MacroKind; +use crate as hir; use crate::definitions::DefPathData; -use crate::hir; /// Encodes if a `DefKind::Ctor` is the constructor of an enum variant or a struct. #[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug, StableHash)] diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index a465c1d95f6c8..e4d6f052c2246 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -4,18 +4,17 @@ use std::fmt; use std::ops::Not; use rustc_abi::ExternAbi; -use rustc_ast::attr::AttributeExt; -use rustc_ast::token::DocFragmentKind; use rustc_ast::util::parser::ExprPrecedence; use rustc_ast::{ self as ast, FloatTy, InlineAsmOptions, InlineAsmTemplatePiece, IntTy, Label, LitIntType, - LitKind, TraitObjectSyntax, UintTy, UnsafeBinderCastKind, join_path_idents, + LitKind, TraitObjectSyntax, UintTy, UnsafeBinderCastKind, }; pub use rustc_ast::{ AssignOp, AssignOpKind, AttrId, AttrStyle, BinOp, BinOpKind, BindingMode, BorrowKind, BoundConstness, BoundPolarity, ByRef, CaptureBy, DelimArgs, ImplPolarity, IsAuto, MetaItemInner, MetaItemLit, Movability, Mutability, Pinnedness, UnOp, }; +use rustc_attr_ir::Attribute; use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fx::FxIndexSet; use rustc_data_structures::sorted_map::SortedMap; @@ -32,11 +31,8 @@ use rustc_span::{ kw, sym, }; use rustc_target::asm::InlineAsmRegOrRegClass; -use smallvec::SmallVec; -use thin_vec::ThinVec; use tracing::debug; -use crate::attrs::AttributeKind; use crate::def::{CtorKind, DefKind, MacroKinds, PerNS, Res}; use crate::def_id::{DefId, LocalDefIdMap}; use crate::intravisit::{FnKind, VisitorExt}; @@ -1284,364 +1280,6 @@ pub struct ParentedNode<'tcx> { pub node: Node<'tcx>, } -/// Arguments passed to an attribute macro. -#[derive(Clone, Debug, StableHash, Encodable, Decodable)] -pub enum AttrArgs { - /// No arguments: `#[attr]`. - Empty, - /// Delimited arguments: `#[attr()/[]/{}]`. - Delimited(DelimArgs), - /// Arguments of a key-value attribute: `#[attr = "value"]`. - Eq { - /// Span of the `=` token. - eq_span: Span, - /// The "value". - expr: MetaItemLit, - }, -} - -#[derive(Clone, Debug, StableHash, Encodable, Decodable)] -pub struct AttrPath { - pub segments: Box<[Symbol]>, - pub span: Span, -} - -impl IntoDiagArg for AttrPath { - fn into_diag_arg(self, path: &mut Option) -> DiagArgValue { - self.to_string().into_diag_arg(path) - } -} - -impl AttrPath { - pub fn from_ast(path: &ast::Path, lower_span: impl Copy + Fn(Span) -> Span) -> Self { - AttrPath { - segments: path - .segments - .iter() - .map(|i| i.ident.name) - .collect::>() - .into_boxed_slice(), - span: lower_span(path.span), - } - } -} - -impl fmt::Display for AttrPath { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{}", - join_path_idents(self.segments.iter().map(|i| Ident { name: *i, span: DUMMY_SP })) - ) - } -} - -#[derive(Clone, Debug, StableHash, Encodable, Decodable)] -pub struct AttrItem { - // Not lowered to hir::Path because we have no NodeId to resolve to. - pub path: AttrPath, - pub args: AttrArgs, - pub id: HashIgnoredAttrId, - /// Denotes if the attribute decorates the following construct (outer) - /// or the construct this attribute is contained within (inner). - pub style: AttrStyle, - /// Span of the entire attribute - pub span: Span, -} - -/// The derived implementation of [`StableHash`] on [`Attribute`]s shouldn't hash -/// [`AttrId`]s. By wrapping them in this, we make sure we never do. -#[derive(Copy, Debug, Encodable, Decodable, Clone)] -pub struct HashIgnoredAttrId { - pub attr_id: AttrId, -} - -/// Many functions on this type have their documentation in the [`AttributeExt`] trait, -/// since they defer their implementation directly to that trait. -#[derive(Clone, Debug, Encodable, Decodable, StableHash)] -pub enum Attribute { - /// A parsed built-in attribute. - /// - /// Each attribute has a span connected to it. However, you must be somewhat careful using it. - /// That's because sometimes we merge multiple attributes together, like when an item has - /// multiple `repr` attributes. In this case the span might not be very useful. - Parsed(AttributeKind), - - /// An attribute that could not be parsed, out of a token-like representation. - /// This is the case for custom tool attributes. - Unparsed(Box), -} - -impl Attribute { - pub fn get_normal_item(&self) -> &AttrItem { - match &self { - Attribute::Unparsed(normal) => &normal, - _ => panic!("unexpected parsed attribute"), - } - } - - pub fn value_lit(&self) -> Option<&MetaItemLit> { - match &self { - Attribute::Unparsed(n) => match n.as_ref() { - AttrItem { args: AttrArgs::Eq { eq_span: _, expr }, .. } => Some(expr), - _ => None, - }, - _ => None, - } - } - - pub fn is_parsed_attr(&self) -> bool { - match self { - Attribute::Parsed(_) => true, - Attribute::Unparsed(_) => false, - } - } - - pub fn is_prefix_attr_for_suggestions(&self) -> bool { - match self { - Attribute::Unparsed(attr) => attr.span.desugaring_kind().is_none(), - // Other parsed attributes that can appear on expressions originate from source and - // should make suggestions treat the expression like a prefixed form. - Attribute::Parsed(_) => true, - } - } -} - -impl AttributeExt for Attribute { - #[inline] - fn id(&self) -> AttrId { - match &self { - Attribute::Unparsed(u) => u.id.attr_id, - _ => panic!(), - } - } - - #[inline] - fn meta_item_list(&self) -> Option> { - match &self { - Attribute::Unparsed(n) => match n.as_ref() { - AttrItem { args: AttrArgs::Delimited(d), .. } => { - ast::MetaItemKind::list_from_tokens(d.tokens.clone()) - } - _ => None, - }, - _ => None, - } - } - - #[inline] - fn value_str(&self) -> Option { - self.value_lit().and_then(|x| x.value_as_str()) - } - - #[inline] - fn value_span(&self) -> Option { - self.value_lit().map(|i| i.span) - } - - /// For a single-segment attribute, returns its name; otherwise, returns `None`. - #[inline] - fn name(&self) -> Option { - match &self { - Attribute::Unparsed(n) => { - if let [ident] = n.path.segments.as_ref() { - Some(*ident) - } else { - None - } - } - _ => None, - } - } - - #[inline] - fn path_matches(&self, name: &[Symbol]) -> bool { - match &self { - Attribute::Unparsed(n) => n.path.segments.iter().eq(name), - _ => false, - } - } - - #[inline] - fn is_doc_comment(&self) -> Option { - if let Attribute::Parsed(AttributeKind::DocComment { span, .. }) = self { - Some(*span) - } else { - None - } - } - - #[inline] - fn span(&self) -> Span { - match &self { - Attribute::Unparsed(u) => u.span, - // FIXME: should not be needed anymore when all attrs are parsed - Attribute::Parsed(AttributeKind::DocComment { span, .. }) => *span, - Attribute::Parsed(AttributeKind::Deprecated { span, .. }) => *span, - Attribute::Parsed(AttributeKind::CfgTrace(cfgs)) => cfgs[0].1, - a => panic!("can't get the span of an arbitrary parsed attribute: {a:?}"), - } - } - - #[inline] - fn is_word(&self) -> bool { - match &self { - Attribute::Unparsed(n) => { - matches!(n.args, AttrArgs::Empty) - } - _ => false, - } - } - - #[inline] - fn symbol_path(&self) -> Option> { - match &self { - Attribute::Unparsed(n) => Some(n.path.segments.iter().copied().collect()), - _ => None, - } - } - - fn path_span(&self) -> Option { - match &self { - Attribute::Unparsed(attr) => Some(attr.path.span), - Attribute::Parsed(_) => None, - } - } - - #[inline] - fn doc_str(&self) -> Option { - match &self { - Attribute::Parsed(AttributeKind::DocComment { comment, .. }) => Some(*comment), - _ => None, - } - } - - fn is_automatically_derived_attr(&self) -> bool { - matches!(self, Attribute::Parsed(AttributeKind::AutomaticallyDerived)) - } - - #[inline] - fn doc_str_and_fragment_kind(&self) -> Option<(Symbol, DocFragmentKind)> { - match &self { - Attribute::Parsed(AttributeKind::DocComment { kind, comment, .. }) => { - Some((*comment, *kind)) - } - _ => None, - } - } - - fn doc_resolution_scope(&self) -> Option { - match self { - Attribute::Parsed(AttributeKind::DocComment { style, .. }) => Some(*style), - Attribute::Unparsed(attr) if self.has_name(sym::doc) && self.value_str().is_some() => { - Some(attr.style) - } - _ => None, - } - } - - fn is_proc_macro_attr(&self) -> bool { - matches!( - self, - Attribute::Parsed( - AttributeKind::ProcMacro - | AttributeKind::ProcMacroAttribute - | AttributeKind::ProcMacroDerive { .. } - ) - ) - } - - fn is_doc_hidden(&self) -> bool { - matches!(self, Attribute::Parsed(AttributeKind::Doc(d)) if d.hidden.is_some()) - } - - fn is_doc_keyword_or_attribute(&self) -> bool { - matches!(self, Attribute::Parsed(AttributeKind::Doc(d)) if d.attribute.is_some() || d.keyword.is_some()) - } - - fn is_rustc_doc_primitive(&self) -> bool { - matches!(self, Attribute::Parsed(AttributeKind::RustcDocPrimitive(..))) - } -} - -// FIXME(fn_delegation): use function delegation instead of manually forwarding -impl Attribute { - #[inline] - pub fn id(&self) -> AttrId { - AttributeExt::id(self) - } - - #[inline] - pub fn name(&self) -> Option { - AttributeExt::name(self) - } - - #[inline] - pub fn meta_item_list(&self) -> Option> { - AttributeExt::meta_item_list(self) - } - - #[inline] - pub fn value_str(&self) -> Option { - AttributeExt::value_str(self) - } - - #[inline] - pub fn value_span(&self) -> Option { - AttributeExt::value_span(self) - } - - #[inline] - pub fn path_matches(&self, name: &[Symbol]) -> bool { - AttributeExt::path_matches(self, name) - } - - #[inline] - pub fn is_doc_comment(&self) -> Option { - AttributeExt::is_doc_comment(self) - } - - #[inline] - pub fn has_name(&self, name: Symbol) -> bool { - AttributeExt::has_name(self, name) - } - - #[inline] - pub fn has_any_name(&self, names: &[Symbol]) -> bool { - AttributeExt::has_any_name(self, names) - } - - #[inline] - pub fn span(&self) -> Span { - AttributeExt::span(self) - } - - #[inline] - pub fn is_word(&self) -> bool { - AttributeExt::is_word(self) - } - - #[inline] - pub fn path(&self) -> SmallVec<[Symbol; 1]> { - AttributeExt::path(self) - } - - #[inline] - pub fn doc_str(&self) -> Option { - AttributeExt::doc_str(self) - } - - #[inline] - pub fn is_proc_macro_attr(&self) -> bool { - AttributeExt::is_proc_macro_attr(self) - } - - #[inline] - pub fn doc_str_and_fragment_kind(&self) -> Option<(Symbol, DocFragmentKind)> { - AttributeExt::doc_str_and_fragment_kind(self) - } -} - /// Attributes owned by a HIR owner. #[derive(Debug)] pub struct AttributeMap<'tcx> { diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index 25a6bdea3afe2..83b6e08e22b3c 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -66,6 +66,7 @@ use rustc_ast::Label; use rustc_ast::visit::{VisitorResult, try_visit, visit_opt, walk_list}; +use rustc_attr_ir::Attribute; use rustc_hir_id::HirId; use rustc_span::def_id::LocalDefId; use rustc_span::{Ident, Span, Symbol}; diff --git a/compiler/rustc_hir/src/lib.rs b/compiler/rustc_hir/src/lib.rs index 761fc680d2ff6..b073ed4d8ed12 100644 --- a/compiler/rustc_hir/src/lib.rs +++ b/compiler/rustc_hir/src/lib.rs @@ -11,35 +11,38 @@ #![feature(derive_const)] #![feature(exhaustive_patterns)] #![feature(never_type)] -#![feature(variant_count)] #![recursion_limit = "256"] // tidy-alphabetical-end mod arena; -pub mod attrs; pub mod def; pub mod def_path_hash_map; pub mod definitions; -pub mod diagnostic_items; mod hir; pub mod intravisit; -pub mod lang_items; pub mod lints; pub mod pat_util; -mod stability; mod stable_hash_impls; -pub mod target; -pub mod weak_lang_items; +mod target_impls; #[cfg(test)] mod tests; #[doc(no_inline)] pub use hir::*; -pub use lang_items::{LangItem, LanguageItems}; +pub use rustc_attr_ir::{self as attrs, find_attr}; pub use rustc_hir_id::*; pub use rustc_span::def_id; -pub use stability::*; -pub use target::{MethodKind, Target}; +// FIXME: Remove this use tree, replace by `rustc_hir::attrs` or `rustc_attr_ir` imports +#[doc(hidden)] +pub use { + attrs::target::{self, MethodKind, Target}, + attrs::{ + AttrArgs, AttrItem, AttrPath, Attribute, ConstStability, DefaultBodyStability, + HashIgnoredAttrId, LangItem, LanguageItems, PartialConstStability, Stability, + StabilityLevel, StableSince, UnstableReason, VERSION_PLACEHOLDER, + }, + attrs::{diagnostic_items, lang_items, weak_lang_items}, +}; pub use crate::arena::Arena; diff --git a/compiler/rustc_hir/src/stable_hash_impls.rs b/compiler/rustc_hir/src/stable_hash_impls.rs index 3eadf0744df33..dc0511519958d 100644 --- a/compiler/rustc_hir/src/stable_hash_impls.rs +++ b/compiler/rustc_hir/src/stable_hash_impls.rs @@ -1,6 +1,5 @@ use rustc_data_structures::stable_hash::{StableHash, StableHashCtxt, StableHasher}; -use crate::HashIgnoredAttrId; use crate::hir::{AttributeMap, OwnerInfo, OwnerNodes}; // The following implementations of StableHash for `ItemId`, `TraitItemId`, and @@ -36,9 +35,3 @@ impl<'tcx> StableHash for OwnerInfo<'tcx> { opt_hash.unwrap().stable_hash(hcx, hasher); } } - -impl StableHash for HashIgnoredAttrId { - fn stable_hash(&self, _hcx: &mut Hcx, _hasher: &mut StableHasher) { - /* we don't hash HashIgnoredAttrId, we ignore them */ - } -} diff --git a/compiler/rustc_hir/src/target_impls.rs b/compiler/rustc_hir/src/target_impls.rs new file mode 100644 index 0000000000000..57564bd815595 --- /dev/null +++ b/compiler/rustc_hir/src/target_impls.rs @@ -0,0 +1,96 @@ +//! Implements conversions from HIR types to Target. + +use rustc_attr_ir::target::{GenericParamKind, MethodKind, Target}; + +use crate::def::DefKind; +use crate::{self as hir, ItemKind, TraitItemKind}; + +impl From<&hir::ForeignItem<'_>> for Target { + fn from(foreign_item: &hir::ForeignItem<'_>) -> Target { + match foreign_item.kind { + hir::ForeignItemKind::Fn(..) => Target::ForeignFn, + hir::ForeignItemKind::Static(..) => Target::ForeignStatic, + hir::ForeignItemKind::Type => Target::ForeignTy, + } + } +} + +impl From<&hir::GenericParam<'_>> for Target { + fn from(generic_param: &hir::GenericParam<'_>) -> Target { + match generic_param.kind { + hir::GenericParamKind::Type { default, .. } => Target::GenericParam { + kind: GenericParamKind::Type, + has_default: default.is_some(), + }, + hir::GenericParamKind::Lifetime { .. } => { + Target::GenericParam { kind: GenericParamKind::Lifetime, has_default: false } + } + hir::GenericParamKind::Const { default, .. } => Target::GenericParam { + kind: GenericParamKind::Const, + has_default: default.is_some(), + }, + } + } +} + +impl From<&hir::TraitItem<'_>> for Target { + fn from(trait_item: &hir::TraitItem<'_>) -> Target { + match trait_item.kind { + TraitItemKind::Const(..) => Target::AssocConst, + TraitItemKind::Fn(_, hir::TraitFn::Required(_)) => { + Target::Method(MethodKind::Trait { body: false }) + } + TraitItemKind::Fn(_, hir::TraitFn::Provided(_)) => { + Target::Method(MethodKind::Trait { body: true }) + } + TraitItemKind::Type(..) => Target::AssocTy, + } + } +} + +impl From for Target { + fn from(def_kind: DefKind) -> Target { + match def_kind { + DefKind::ExternCrate => Target::ExternCrate, + DefKind::Use => Target::Use, + DefKind::Static { .. } => Target::Static, + DefKind::Const { .. } => Target::Const, + DefKind::Fn => Target::Fn, + DefKind::Macro(..) => Target::MacroDef, + DefKind::Mod => Target::Mod, + DefKind::ForeignMod => Target::ForeignMod, + DefKind::GlobalAsm => Target::GlobalAsm, + DefKind::TyAlias => Target::TyAlias, + DefKind::Enum => Target::Enum, + DefKind::Struct => Target::Struct, + DefKind::Union => Target::Union, + DefKind::Trait => Target::Trait, + DefKind::TraitAlias => Target::TraitAlias, + DefKind::Impl { of_trait } => Target::Impl { of_trait }, + _ => panic!("impossible case reached"), + } + } +} + +impl From<&hir::Item<'_>> for Target { + fn from(item: &hir::Item<'_>) -> Target { + match item.kind { + ItemKind::ExternCrate(..) => Target::ExternCrate, + ItemKind::Use(..) => Target::Use, + ItemKind::Static { .. } => Target::Static, + ItemKind::Const(..) => Target::Const, + ItemKind::Fn { .. } => Target::Fn, + ItemKind::Macro(..) => Target::MacroDef, + ItemKind::Mod(..) => Target::Mod, + ItemKind::ForeignMod { .. } => Target::ForeignMod, + ItemKind::GlobalAsm { .. } => Target::GlobalAsm, + ItemKind::TyAlias(..) => Target::TyAlias, + ItemKind::Enum(..) => Target::Enum, + ItemKind::Struct(..) => Target::Struct, + ItemKind::Union(..) => Target::Union, + ItemKind::Trait { .. } => Target::Trait, + ItemKind::TraitAlias(..) => Target::TraitAlias, + ItemKind::Impl(imp_) => Target::Impl { of_trait: imp_.of_trait.is_some() }, + } + } +} diff --git a/triagebot.toml b/triagebot.toml index d5e58db589b22..54e86aaccc2cc 100644 --- a/triagebot.toml +++ b/triagebot.toml @@ -291,7 +291,7 @@ trigger_files = [ "compiler/rustc_codegen_ssa/src/codegen_attrs.rs", "compiler/rustc_passes/src/check_attr.rs", "compiler/rustc_attr_parsing", - "compiler/rustc_hir/src/attrs", + "compiler/rustc_attr_ir", ] [autolabel."A-compiler-builtins"] @@ -1490,7 +1490,7 @@ cc = ["@BoxyUwU", "@tshepang"] cc = ["@jdonszelmann", "@JonathanBrouwer"] [mentions."compiler/rustc_attr_parsing"] cc = ["@jdonszelmann", "@JonathanBrouwer"] -[mentions."compiler/rustc_hir/src/attrs"] +[mentions."compiler/rustc_attr_ir"] cc = ["@jdonszelmann", "@JonathanBrouwer"] [mentions."src/tools/enzyme"] @@ -1525,7 +1525,7 @@ cc = ["@jieyouxu"] [mentions."compiler/rustc_attr_parsing/src/attributes/diagnostic"] message = "Some changes occurred to diagnostic attributes." cc = ["@mejrs"] -[mentions."compiler/rustc_hir/src/attrs/diagnostic.rs"] +[mentions."compiler/rustc_attr_ir/src/diagnostic.rs"] message = "Some changes occurred to diagnostic attributes." cc = ["@mejrs"] From 597c6c9409d069d35676e03b822cbbb44b0dd18e Mon Sep 17 00:00:00 2001 From: mejrs <59372212+mejrs@users.noreply.github.com> Date: Thu, 6 Aug 2026 00:06:10 +0200 Subject: [PATCH 2/2] bless bootstrap --- .../src/core/builder/cli_paths/snapshots/x_bench.snap | 1 + .../builder/cli_paths/snapshots/x_build_compiler.snap | 1 + .../src/core/builder/cli_paths/snapshots/x_check.snap | 1 + .../builder/cli_paths/snapshots/x_check_compiler.snap | 1 + .../x_check_compiletest_include_default_paths.snap | 1 + .../src/core/builder/cli_paths/snapshots/x_clippy.snap | 1 + .../src/core/builder/cli_paths/snapshots/x_fix.snap | 1 + .../src/core/builder/cli_paths/snapshots/x_test.snap | 1 + .../cli_paths/snapshots/x_test_skip_coverage.snap | 1 + .../cli_paths/snapshots/x_test_skip_coverage_map.snap | 1 + .../cli_paths/snapshots/x_test_skip_coverage_run.snap | 1 + .../builder/cli_paths/snapshots/x_test_skip_tests.snap | 1 + .../snapshots/x_test_skip_tests_coverage.snap | 1 + .../cli_paths/snapshots/x_test_skip_tests_etc.snap | 1 + src/bootstrap/src/core/builder/tests.rs | 10 +++++----- 15 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_bench.snap b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_bench.snap index 2daa06d2f4b78..165e270270702 100644 --- a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_bench.snap +++ b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_bench.snap @@ -29,6 +29,7 @@ expression: bench - Set({compiler/rustc_ast_lowering}) - Set({compiler/rustc_ast_passes}) - Set({compiler/rustc_ast_pretty}) + - Set({compiler/rustc_attr_ir}) - Set({compiler/rustc_attr_parsing}) - Set({compiler/rustc_baked_icu_data}) - Set({compiler/rustc_borrowck}) diff --git a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_build_compiler.snap b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_build_compiler.snap index 829ca411eb0e5..a350c9a92c74b 100644 --- a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_build_compiler.snap +++ b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_build_compiler.snap @@ -11,6 +11,7 @@ expression: build compiler - Set({compiler/rustc_ast_lowering}) - Set({compiler/rustc_ast_passes}) - Set({compiler/rustc_ast_pretty}) + - Set({compiler/rustc_attr_ir}) - Set({compiler/rustc_attr_parsing}) - Set({compiler/rustc_baked_icu_data}) - Set({compiler/rustc_borrowck}) diff --git a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_check.snap b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_check.snap index 325f21b7fdd70..ef5dd1e832e2b 100644 --- a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_check.snap +++ b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_check.snap @@ -13,6 +13,7 @@ expression: check - Set({compiler/rustc_ast_lowering}) - Set({compiler/rustc_ast_passes}) - Set({compiler/rustc_ast_pretty}) + - Set({compiler/rustc_attr_ir}) - Set({compiler/rustc_attr_parsing}) - Set({compiler/rustc_baked_icu_data}) - Set({compiler/rustc_borrowck}) diff --git a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_check_compiler.snap b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_check_compiler.snap index 38693b1f636bf..2ee6f90f68f8c 100644 --- a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_check_compiler.snap +++ b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_check_compiler.snap @@ -13,6 +13,7 @@ expression: check compiler - Set({compiler/rustc_ast_lowering}) - Set({compiler/rustc_ast_passes}) - Set({compiler/rustc_ast_pretty}) + - Set({compiler/rustc_attr_ir}) - Set({compiler/rustc_attr_parsing}) - Set({compiler/rustc_baked_icu_data}) - Set({compiler/rustc_borrowck}) diff --git a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_check_compiletest_include_default_paths.snap b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_check_compiletest_include_default_paths.snap index 1d83aa61fdfc2..ab58341ceb9e4 100644 --- a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_check_compiletest_include_default_paths.snap +++ b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_check_compiletest_include_default_paths.snap @@ -13,6 +13,7 @@ expression: check compiletest --include-default-paths - Set({compiler/rustc_ast_lowering}) - Set({compiler/rustc_ast_passes}) - Set({compiler/rustc_ast_pretty}) + - Set({compiler/rustc_attr_ir}) - Set({compiler/rustc_attr_parsing}) - Set({compiler/rustc_baked_icu_data}) - Set({compiler/rustc_borrowck}) diff --git a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_clippy.snap b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_clippy.snap index b409b12093455..f1430e020aaa4 100644 --- a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_clippy.snap +++ b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_clippy.snap @@ -28,6 +28,7 @@ expression: clippy - Set({compiler/rustc_ast_lowering}) - Set({compiler/rustc_ast_passes}) - Set({compiler/rustc_ast_pretty}) + - Set({compiler/rustc_attr_ir}) - Set({compiler/rustc_attr_parsing}) - Set({compiler/rustc_baked_icu_data}) - Set({compiler/rustc_borrowck}) diff --git a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_fix.snap b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_fix.snap index 15a849db3801e..ce0782792351f 100644 --- a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_fix.snap +++ b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_fix.snap @@ -13,6 +13,7 @@ expression: fix - Set({compiler/rustc_ast_lowering}) - Set({compiler/rustc_ast_passes}) - Set({compiler/rustc_ast_pretty}) + - Set({compiler/rustc_attr_ir}) - Set({compiler/rustc_attr_parsing}) - Set({compiler/rustc_baked_icu_data}) - Set({compiler/rustc_borrowck}) diff --git a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test.snap b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test.snap index 346a86cb5bd6c..59b84f4301feb 100644 --- a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test.snap +++ b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test.snap @@ -77,6 +77,7 @@ expression: test - Set({compiler/rustc_ast_lowering}) - Set({compiler/rustc_ast_passes}) - Set({compiler/rustc_ast_pretty}) + - Set({compiler/rustc_attr_ir}) - Set({compiler/rustc_attr_parsing}) - Set({compiler/rustc_baked_icu_data}) - Set({compiler/rustc_borrowck}) diff --git a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_skip_coverage.snap b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_skip_coverage.snap index d08c6a96942d8..29dc8ee5efcfc 100644 --- a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_skip_coverage.snap +++ b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_skip_coverage.snap @@ -74,6 +74,7 @@ expression: test --skip=coverage - Set({compiler/rustc_ast_lowering}) - Set({compiler/rustc_ast_passes}) - Set({compiler/rustc_ast_pretty}) + - Set({compiler/rustc_attr_ir}) - Set({compiler/rustc_attr_parsing}) - Set({compiler/rustc_baked_icu_data}) - Set({compiler/rustc_borrowck}) diff --git a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_skip_coverage_map.snap b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_skip_coverage_map.snap index b51654bb7d46b..9240e939f946f 100644 --- a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_skip_coverage_map.snap +++ b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_skip_coverage_map.snap @@ -77,6 +77,7 @@ expression: test --skip=coverage-map - Set({compiler/rustc_ast_lowering}) - Set({compiler/rustc_ast_passes}) - Set({compiler/rustc_ast_pretty}) + - Set({compiler/rustc_attr_ir}) - Set({compiler/rustc_attr_parsing}) - Set({compiler/rustc_baked_icu_data}) - Set({compiler/rustc_borrowck}) diff --git a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_skip_coverage_run.snap b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_skip_coverage_run.snap index bffe4909c6305..f30be2939765a 100644 --- a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_skip_coverage_run.snap +++ b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_skip_coverage_run.snap @@ -77,6 +77,7 @@ expression: test --skip=coverage-run - Set({compiler/rustc_ast_lowering}) - Set({compiler/rustc_ast_passes}) - Set({compiler/rustc_ast_pretty}) + - Set({compiler/rustc_attr_ir}) - Set({compiler/rustc_attr_parsing}) - Set({compiler/rustc_baked_icu_data}) - Set({compiler/rustc_borrowck}) diff --git a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_skip_tests.snap b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_skip_tests.snap index 3ee569401504f..f1aa131eb7c3a 100644 --- a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_skip_tests.snap +++ b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_skip_tests.snap @@ -38,6 +38,7 @@ expression: test --skip=tests - Set({compiler/rustc_ast_lowering}) - Set({compiler/rustc_ast_passes}) - Set({compiler/rustc_ast_pretty}) + - Set({compiler/rustc_attr_ir}) - Set({compiler/rustc_attr_parsing}) - Set({compiler/rustc_baked_icu_data}) - Set({compiler/rustc_borrowck}) diff --git a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_skip_tests_coverage.snap b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_skip_tests_coverage.snap index c8212e05b46d0..ddf45bcfdbdb1 100644 --- a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_skip_tests_coverage.snap +++ b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_skip_tests_coverage.snap @@ -74,6 +74,7 @@ expression: test --skip=tests/coverage - Set({compiler/rustc_ast_lowering}) - Set({compiler/rustc_ast_passes}) - Set({compiler/rustc_ast_pretty}) + - Set({compiler/rustc_attr_ir}) - Set({compiler/rustc_attr_parsing}) - Set({compiler/rustc_baked_icu_data}) - Set({compiler/rustc_borrowck}) diff --git a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_skip_tests_etc.snap b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_skip_tests_etc.snap index e41050139f056..c38962f9c12b0 100644 --- a/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_skip_tests_etc.snap +++ b/src/bootstrap/src/core/builder/cli_paths/snapshots/x_test_skip_tests_etc.snap @@ -22,6 +22,7 @@ expression: test --skip=tests --skip=library --skip=tidyselftest - Set({compiler/rustc_ast_lowering}) - Set({compiler/rustc_ast_passes}) - Set({compiler/rustc_ast_pretty}) + - Set({compiler/rustc_attr_ir}) - Set({compiler/rustc_attr_parsing}) - Set({compiler/rustc_baked_icu_data}) - Set({compiler/rustc_borrowck}) diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index dddb70b3fd468..f36173732e58b 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -1729,7 +1729,7 @@ mod snapshot { insta::assert_snapshot!( ctx.config("check") .path("compiler") - .render_steps(), @"[check] rustc 0 -> rustc 1 (74 crates)"); + .render_steps(), @"[check] rustc 0 -> rustc 1 (75 crates)"); } #[test] @@ -1755,7 +1755,7 @@ mod snapshot { ctx.config("check") .path("compiler") .stage(1) - .render_steps(), @"[check] rustc 0 -> rustc 1 (74 crates)"); + .render_steps(), @"[check] rustc 0 -> rustc 1 (75 crates)"); } #[test] @@ -1769,7 +1769,7 @@ mod snapshot { [build] llvm [build] rustc 0 -> rustc 1 [build] rustc 1 -> std 1 - [check] rustc 1 -> rustc 2 (74 crates) + [check] rustc 1 -> rustc 2 (75 crates) "); } @@ -1785,7 +1785,7 @@ mod snapshot { [build] rustc 0 -> rustc 1 [build] rustc 1 -> std 1 [check] rustc 1 -> std 1 - [check] rustc 1 -> rustc 2 (74 crates) + [check] rustc 1 -> rustc 2 (75 crates) [check] rustc 1 -> rustc 2 [check] rustc 1 -> Rustdoc 2 [check] rustc 1 -> rustc_codegen_cranelift 2 @@ -1881,7 +1881,7 @@ mod snapshot { ctx.config("check") .paths(&["library", "compiler"]) .args(&args) - .render_steps(), @"[check] rustc 0 -> rustc 1 (74 crates)"); + .render_steps(), @"[check] rustc 0 -> rustc 1 (75 crates)"); } #[test]