Skip to content

Commit f04f38d

Browse files
nameres: collect unnamed consts
1 parent 3e7ac2b commit f04f38d

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

crates/hir_def/src/item_scope.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
1111
use stdx::format_to;
1212

1313
use crate::{
14-
db::DefDatabase, per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType, ImplId,
14+
db::DefDatabase, per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType, ConstId, ImplId,
1515
LocalModuleId, MacroDefId, ModuleDefId, ModuleId, TraitId,
1616
};
1717

@@ -37,6 +37,7 @@ pub struct ItemScope {
3737

3838
defs: Vec<ModuleDefId>,
3939
impls: Vec<ImplId>,
40+
unnamed_consts: Vec<ConstId>,
4041
/// Traits imported via `use Trait as _;`.
4142
unnamed_trait_imports: FxHashMap<TraitId, Visibility>,
4243
/// Macros visible in current module in legacy textual scope
@@ -156,6 +157,10 @@ impl ItemScope {
156157
self.impls.push(imp)
157158
}
158159

160+
pub(crate) fn define_unnamed_const(&mut self, konst: ConstId) {
161+
self.unnamed_consts.push(konst);
162+
}
163+
159164
pub(crate) fn define_legacy_macro(&mut self, name: Name, mac: MacroDefId) {
160165
self.legacy_macros.insert(name, mac);
161166
}
@@ -295,6 +300,7 @@ impl ItemScope {
295300
unresolved,
296301
defs,
297302
impls,
303+
unnamed_consts,
298304
unnamed_trait_imports,
299305
legacy_macros,
300306
} = self;
@@ -304,6 +310,7 @@ impl ItemScope {
304310
unresolved.shrink_to_fit();
305311
defs.shrink_to_fit();
306312
impls.shrink_to_fit();
313+
unnamed_consts.shrink_to_fit();
307314
unnamed_trait_imports.shrink_to_fit();
308315
legacy_macros.shrink_to_fit();
309316
}

crates/hir_def/src/nameres/collector.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,19 +1163,27 @@ impl ModCollector<'_, '_> {
11631163
}
11641164
ModItem::Const(id) => {
11651165
let it = &self.item_tree[id];
1166-
1167-
if let Some(name) = &it.name {
1168-
def = Some(DefData {
1169-
id: ConstLoc {
1170-
container: module.into(),
1171-
id: ItemTreeId::new(self.file_id, id),
1172-
}
1173-
.intern(self.def_collector.db)
1174-
.into(),
1175-
name,
1176-
visibility: &self.item_tree[it.visibility],
1177-
has_constructor: false,
1178-
});
1166+
let const_id = ConstLoc {
1167+
container: module.into(),
1168+
id: ItemTreeId::new(self.file_id, id),
1169+
}
1170+
.intern(self.def_collector.db);
1171+
1172+
match &it.name {
1173+
Some(name) => {
1174+
def = Some(DefData {
1175+
id: const_id.into(),
1176+
name,
1177+
visibility: &self.item_tree[it.visibility],
1178+
has_constructor: false,
1179+
});
1180+
}
1181+
None => {
1182+
// const _: T = ...;
1183+
self.def_collector.def_map.modules[self.module_id]
1184+
.scope
1185+
.define_unnamed_const(const_id);
1186+
}
11791187
}
11801188
}
11811189
ModItem::Static(id) => {

0 commit comments

Comments
 (0)