Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions crates/hir-def/src/nameres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,6 @@ struct DefMapCrateData {
exported_derives: FxHashMap<MacroId, Box<[Name]>>,
fn_proc_macro_mapping: FxHashMap<FunctionId, ProcMacroId>,

/// Custom attributes registered with `#![register_attr]`.
registered_attrs: Vec<Symbol>,
/// Custom tool modules registered with `#![register_tool]`.
registered_tools: Vec<Symbol>,
/// Unstable features of Rust enabled with `#![feature(A, B)]`.
Expand All @@ -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,
Expand All @@ -227,7 +224,6 @@ impl DefMapCrateData {
let Self {
exported_derives,
fn_proc_macro_mapping,
registered_attrs,
registered_tools,
unstable_features,
rustc_coherence_is_core: _,
Expand All @@ -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();
}
Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 2 additions & 7 deletions crates/hir-def/src/nameres/attr_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions crates/hir-def/src/nameres/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
38 changes: 7 additions & 31 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<base_db::Crate>,
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<Self> {
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<Self> {
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<AttributeTemplate> {
match self.krate {
Some(_) => None,
None => {
Some(hir_expand::inert_attr_macro::INERT_ATTRIBUTES[self.idx as usize].template)
}
}
pub fn template(&self) -> Option<AttributeTemplate> {
Some(hir_expand::inert_attr_macro::INERT_ATTRIBUTES[self.idx as usize].template)
}
}

Expand Down
3 changes: 1 addition & 2 deletions crates/hir/src/source_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
6 changes: 3 additions & 3 deletions crates/ide-db/src/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 1 addition & 4 deletions crates/ide-diagnostics/src/handlers/macro_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
"#,
);
Expand Down
Loading