From f04f38d3d79c3cc51956bc530a34e32945cdb294 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 7 Apr 2021 03:12:40 +0200 Subject: nameres: collect unnamed consts --- crates/hir_def/src/item_scope.rs | 9 ++++++++- crates/hir_def/src/nameres/collector.rs | 34 ++++++++++++++++++++------------- 2 files changed, 29 insertions(+), 14 deletions(-) (limited to 'crates') diff --git a/crates/hir_def/src/item_scope.rs b/crates/hir_def/src/item_scope.rs index a8ee5eeac..08feee1e7 100644 --- a/crates/hir_def/src/item_scope.rs +++ b/crates/hir_def/src/item_scope.rs @@ -11,7 +11,7 @@ use rustc_hash::{FxHashMap, FxHashSet}; use stdx::format_to; use crate::{ - db::DefDatabase, per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType, ImplId, + db::DefDatabase, per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType, ConstId, ImplId, LocalModuleId, MacroDefId, ModuleDefId, ModuleId, TraitId, }; @@ -37,6 +37,7 @@ pub struct ItemScope { defs: Vec, impls: Vec, + unnamed_consts: Vec, /// Traits imported via `use Trait as _;`. unnamed_trait_imports: FxHashMap, /// Macros visible in current module in legacy textual scope @@ -156,6 +157,10 @@ impl ItemScope { self.impls.push(imp) } + pub(crate) fn define_unnamed_const(&mut self, konst: ConstId) { + self.unnamed_consts.push(konst); + } + pub(crate) fn define_legacy_macro(&mut self, name: Name, mac: MacroDefId) { self.legacy_macros.insert(name, mac); } @@ -295,6 +300,7 @@ impl ItemScope { unresolved, defs, impls, + unnamed_consts, unnamed_trait_imports, legacy_macros, } = self; @@ -304,6 +310,7 @@ impl ItemScope { unresolved.shrink_to_fit(); defs.shrink_to_fit(); impls.shrink_to_fit(); + unnamed_consts.shrink_to_fit(); unnamed_trait_imports.shrink_to_fit(); legacy_macros.shrink_to_fit(); } diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index f42f92702..492d8c71f 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -1163,19 +1163,27 @@ impl ModCollector<'_, '_> { } ModItem::Const(id) => { let it = &self.item_tree[id]; - - if let Some(name) = &it.name { - def = Some(DefData { - id: ConstLoc { - container: module.into(), - id: ItemTreeId::new(self.file_id, id), - } - .intern(self.def_collector.db) - .into(), - name, - visibility: &self.item_tree[it.visibility], - has_constructor: false, - }); + let const_id = ConstLoc { + container: module.into(), + id: ItemTreeId::new(self.file_id, id), + } + .intern(self.def_collector.db); + + match &it.name { + Some(name) => { + def = Some(DefData { + id: const_id.into(), + name, + visibility: &self.item_tree[it.visibility], + has_constructor: false, + }); + } + None => { + // const _: T = ...; + self.def_collector.def_map.modules[self.module_id] + .scope + .define_unnamed_const(const_id); + } } } ModItem::Static(id) => { -- cgit v1.2.3