From bca5d59627ce1afc4f213163095589ac867e946e Mon Sep 17 00:00:00 2001 From: Wilfred Hughes Date: Mon, 8 Sep 2025 15:56:56 +0100 Subject: [PATCH] Remove support for register_attr This was removed in rustc in 2022: https://github.com/rust-lang/rust/pull/101123 Closes #20525. --- crates/hir-def/src/nameres.rs | 9 ----- crates/hir-def/src/nameres/attr_resolution.rs | 9 +---- crates/hir-def/src/nameres/collector.rs | 6 --- crates/hir/src/lib.rs | 38 ++++--------------- crates/hir/src/source_analyzer.rs | 3 +- crates/ide-db/src/defs.rs | 6 +-- .../src/handlers/macro_error.rs | 5 +-- 7 files changed, 14 insertions(+), 62 deletions(-) diff --git a/crates/hir-def/src/nameres.rs b/crates/hir-def/src/nameres.rs index bf6fc15b7d19..7d5e627964eb 100644 --- a/crates/hir-def/src/nameres.rs +++ b/crates/hir-def/src/nameres.rs @@ -192,8 +192,6 @@ struct DefMapCrateData { exported_derives: FxHashMap>, fn_proc_macro_mapping: FxHashMap, - /// Custom attributes registered with `#![register_attr]`. - registered_attrs: Vec, /// Custom tool modules registered with `#![register_tool]`. registered_tools: Vec, /// Unstable features of Rust enabled with `#![feature(A, B)]`. @@ -212,7 +210,6 @@ impl DefMapCrateData { Self { exported_derives: FxHashMap::default(), fn_proc_macro_mapping: FxHashMap::default(), - registered_attrs: Vec::new(), registered_tools: PREDEFINED_TOOLS.iter().map(|it| Symbol::intern(it)).collect(), unstable_features: FxHashSet::default(), rustc_coherence_is_core: false, @@ -227,7 +224,6 @@ impl DefMapCrateData { let Self { exported_derives, fn_proc_macro_mapping, - registered_attrs, registered_tools, unstable_features, rustc_coherence_is_core: _, @@ -238,7 +234,6 @@ impl DefMapCrateData { } = self; exported_derives.shrink_to_fit(); fn_proc_macro_mapping.shrink_to_fit(); - registered_attrs.shrink_to_fit(); registered_tools.shrink_to_fit(); unstable_features.shrink_to_fit(); } @@ -529,10 +524,6 @@ impl DefMap { &self.data.registered_tools } - pub fn registered_attrs(&self) -> &[Symbol] { - &self.data.registered_attrs - } - pub fn is_unstable_feature_enabled(&self, feature: &Symbol) -> bool { self.data.unstable_features.contains(feature) } diff --git a/crates/hir-def/src/nameres/attr_resolution.rs b/crates/hir-def/src/nameres/attr_resolution.rs index e7e96804ae73..2f56d608fcbf 100644 --- a/crates/hir-def/src/nameres/attr_resolution.rs +++ b/crates/hir-def/src/nameres/attr_resolution.rs @@ -90,13 +90,8 @@ impl DefMap { return true; } - if segments.len() == 1 { - if find_builtin_attr_idx(name).is_some() { - return true; - } - if self.data.registered_attrs.iter().any(pred) { - return true; - } + if segments.len() == 1 && find_builtin_attr_idx(name).is_some() { + return true; } } false diff --git a/crates/hir-def/src/nameres/collector.rs b/crates/hir-def/src/nameres/collector.rs index 0f84728dcb4a..a2ce53835651 100644 --- a/crates/hir-def/src/nameres/collector.rs +++ b/crates/hir-def/src/nameres/collector.rs @@ -298,12 +298,6 @@ impl<'db> DefCollector<'db> { ); crate_data.unstable_features.extend(features); } - () if *attr_name == sym::register_attr => { - if let Some(ident) = attr.single_ident_value() { - crate_data.registered_attrs.push(ident.sym.clone()); - cov_mark::hit!(register_attr); - } - } () if *attr_name == sym::register_tool => { if let Some(ident) = attr.single_ident_value() { crate_data.registered_tools.push(ident.sym.clone()); diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 6610375451dd..6eb8a8bf60fd 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -4060,49 +4060,25 @@ impl DeriveHelper { } } -// FIXME: Wrong name? This is could also be a registered attribute #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct BuiltinAttr { - krate: Option, idx: u32, } impl BuiltinAttr { - // FIXME: consider crates\hir_def\src\nameres\attr_resolution.rs? - pub(crate) fn by_name(db: &dyn HirDatabase, krate: Crate, name: &str) -> Option { - if let builtin @ Some(_) = Self::builtin(name) { - return builtin; - } - let idx = crate_def_map(db, krate.id) - .registered_attrs() - .iter() - .position(|it| it.as_str() == name)? as u32; - Some(BuiltinAttr { krate: Some(krate.id), idx }) - } - fn builtin(name: &str) -> Option { hir_expand::inert_attr_macro::find_builtin_attr_idx(&Symbol::intern(name)) - .map(|idx| BuiltinAttr { krate: None, idx: idx as u32 }) + .map(|idx| BuiltinAttr { idx: idx as u32 }) } - pub fn name(&self, db: &dyn HirDatabase) -> Name { - match self.krate { - Some(krate) => Name::new_symbol_root( - crate_def_map(db, krate).registered_attrs()[self.idx as usize].clone(), - ), - None => Name::new_symbol_root(Symbol::intern( - hir_expand::inert_attr_macro::INERT_ATTRIBUTES[self.idx as usize].name, - )), - } + pub fn name(&self) -> Name { + Name::new_symbol_root(Symbol::intern( + hir_expand::inert_attr_macro::INERT_ATTRIBUTES[self.idx as usize].name, + )) } - pub fn template(&self, _: &dyn HirDatabase) -> Option { - match self.krate { - Some(_) => None, - None => { - Some(hir_expand::inert_attr_macro::INERT_ATTRIBUTES[self.idx as usize].template) - } - } + pub fn template(&self) -> Option { + Some(hir_expand::inert_attr_macro::INERT_ATTRIBUTES[self.idx as usize].template) } } diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs index dbc2539a9b91..539b25387aef 100644 --- a/crates/hir/src/source_analyzer.rs +++ b/crates/hir/src/source_analyzer.rs @@ -1062,8 +1062,7 @@ impl<'db> SourceAnalyzer<'db> { // in this case we have to check for inert/builtin attributes and tools and prioritize // resolution of attributes over other namespaces if let Some(name_ref) = path.as_single_name_ref() { - let builtin = - BuiltinAttr::by_name(db, self.resolver.krate().into(), &name_ref.text()); + let builtin = BuiltinAttr::builtin(&name_ref.text()); if builtin.is_some() { return builtin.map(|it| (PathResolution::BuiltinAttr(it), None)); } diff --git a/crates/ide-db/src/defs.rs b/crates/ide-db/src/defs.rs index 61a21ccf2f77..44ff9d6006d6 100644 --- a/crates/ide-db/src/defs.rs +++ b/crates/ide-db/src/defs.rs @@ -259,8 +259,8 @@ impl Definition { Definition::ExternCrateDecl(it) => it.docs_with_rangemap(db), Definition::BuiltinAttr(it) => { - let name = it.name(db); - let AttributeTemplate { word, list, name_value_str } = it.template(db)?; + let name = it.name(); + let AttributeTemplate { word, list, name_value_str } = it.template()?; let mut docs = "Valid forms are:".to_owned(); if word { format_to!(docs, "\n - #\\[{}]", name.display(db, display_target.edition)); @@ -348,7 +348,7 @@ impl Definition { Definition::Label(it) => it.name(db).display(db, display_target.edition).to_string(), Definition::ExternCrateDecl(it) => it.display(db, display_target).to_string(), Definition::BuiltinAttr(it) => { - format!("#[{}]", it.name(db).display(db, display_target.edition)) + format!("#[{}]", it.name().display(db, display_target.edition)) } Definition::ToolModule(it) => { it.name(db).display(db, display_target.edition).to_string() diff --git a/crates/ide-diagnostics/src/handlers/macro_error.rs b/crates/ide-diagnostics/src/handlers/macro_error.rs index c39e00e178f8..6a1ecae65150 100644 --- a/crates/ide-diagnostics/src/handlers/macro_error.rs +++ b/crates/ide-diagnostics/src/handlers/macro_error.rs @@ -144,16 +144,13 @@ macro_rules! concat { () => {} } } #[test] - fn register_attr_and_tool() { - cov_mark::check!(register_attr); + fn register_tool() { cov_mark::check!(register_tool); check_diagnostics( r#" #![register_tool(tool)] -#![register_attr(attr)] #[tool::path] -#[attr] struct S; "#, );