Skip to content

Commit 3309dfa

Browse files
authored
Merge pull request #18702 from ChayimFriedman2/prep
minor: Use a record struct instead of a tuple for each namespace in `PerNs`
2 parents 4200da4 + cf32a6f commit 3309dfa

File tree

6 files changed

+162
-133
lines changed

6 files changed

+162
-133
lines changed

src/tools/rust-analyzer/crates/hir-def/src/item_scope.rs

+68-56
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use syntax::ast;
1616

1717
use crate::{
1818
db::DefDatabase,
19-
per_ns::PerNs,
19+
per_ns::{Item, MacrosItem, PerNs, TypesItem, ValuesItem},
2020
visibility::{Visibility, VisibilityExplicitness},
2121
AdtId, BuiltinType, ConstId, ExternCrateId, FxIndexMap, HasModule, ImplId, LocalModuleId,
2222
Lookup, MacroId, ModuleDefId, ModuleId, TraitId, UseId,
@@ -80,9 +80,9 @@ pub struct ItemScope {
8080
/// Defs visible in this scope. This includes `declarations`, but also
8181
/// imports. The imports belong to this module and can be resolved by using them on
8282
/// the `use_imports_*` fields.
83-
types: FxIndexMap<Name, (ModuleDefId, Visibility, Option<ImportOrExternCrate>)>,
84-
values: FxIndexMap<Name, (ModuleDefId, Visibility, Option<ImportId>)>,
85-
macros: FxIndexMap<Name, (MacroId, Visibility, Option<ImportId>)>,
83+
types: FxIndexMap<Name, TypesItem>,
84+
values: FxIndexMap<Name, ValuesItem>,
85+
macros: FxIndexMap<Name, MacrosItem>,
8686
unresolved: FxHashSet<Name>,
8787

8888
/// The defs declared in this scope. Each def has a single scope where it is
@@ -92,7 +92,7 @@ pub struct ItemScope {
9292
impls: Vec<ImplId>,
9393
unnamed_consts: Vec<ConstId>,
9494
/// Traits imported via `use Trait as _;`.
95-
unnamed_trait_imports: FxHashMap<TraitId, (Visibility, Option<ImportId>)>,
95+
unnamed_trait_imports: FxHashMap<TraitId, Item<()>>,
9696

9797
// the resolutions of the imports of this scope
9898
use_imports_types: FxHashMap<ImportOrExternCrate, ImportOrDef>,
@@ -187,7 +187,7 @@ impl ItemScope {
187187
import = i;
188188
}
189189
ImportOrDef::Def(ModuleDefId::MacroId(def)) => {
190-
res.macros = Some((def, Visibility::Public, None));
190+
res.macros = Some(Item { def, vis: Visibility::Public, import: None });
191191
break;
192192
}
193193
_ => break,
@@ -203,7 +203,7 @@ impl ItemScope {
203203
import = i;
204204
}
205205
ImportOrDef::Def(def) => {
206-
res.types = Some((def, Visibility::Public, None));
206+
res.types = Some(Item { def, vis: Visibility::Public, import: None });
207207
break;
208208
}
209209
_ => break,
@@ -219,7 +219,7 @@ impl ItemScope {
219219
import = i;
220220
}
221221
ImportOrDef::Def(def) => {
222-
res.values = Some((def, Visibility::Public, None));
222+
res.values = Some(Item { def, vis: Visibility::Public, import: None });
223223
break;
224224
}
225225
_ => break,
@@ -253,8 +253,8 @@ impl ItemScope {
253253
}
254254

255255
pub(crate) fn modules_in_scope(&self) -> impl Iterator<Item = (ModuleId, Visibility)> + '_ {
256-
self.types.values().copied().filter_map(|(def, vis, _)| match def {
257-
ModuleDefId::ModuleId(module) => Some((module, vis)),
256+
self.types.values().filter_map(|ns| match ns.def {
257+
ModuleDefId::ModuleId(module) => Some((module, ns.vis)),
258258
_ => None,
259259
})
260260
}
@@ -283,20 +283,20 @@ impl ItemScope {
283283
}
284284

285285
pub(crate) fn type_(&self, name: &Name) -> Option<(ModuleDefId, Visibility)> {
286-
self.types.get(name).copied().map(|(a, b, _)| (a, b))
286+
self.types.get(name).map(|item| (item.def, item.vis))
287287
}
288288

289289
/// XXX: this is O(N) rather than O(1), try to not introduce new usages.
290290
pub(crate) fn name_of(&self, item: ItemInNs) -> Option<(&Name, Visibility, /*declared*/ bool)> {
291291
match item {
292-
ItemInNs::Macros(def) => self.macros.iter().find_map(|(name, &(other_def, vis, i))| {
293-
(other_def == def).then_some((name, vis, i.is_none()))
292+
ItemInNs::Macros(def) => self.macros.iter().find_map(|(name, other_def)| {
293+
(other_def.def == def).then_some((name, other_def.vis, other_def.import.is_none()))
294294
}),
295-
ItemInNs::Types(def) => self.types.iter().find_map(|(name, &(other_def, vis, i))| {
296-
(other_def == def).then_some((name, vis, i.is_none()))
295+
ItemInNs::Types(def) => self.types.iter().find_map(|(name, other_def)| {
296+
(other_def.def == def).then_some((name, other_def.vis, other_def.import.is_none()))
297297
}),
298-
ItemInNs::Values(def) => self.values.iter().find_map(|(name, &(other_def, vis, i))| {
299-
(other_def == def).then_some((name, vis, i.is_none()))
298+
ItemInNs::Values(def) => self.values.iter().find_map(|(name, other_def)| {
299+
(other_def.def == def).then_some((name, other_def.vis, other_def.import.is_none()))
300300
}),
301301
}
302302
}
@@ -311,22 +311,34 @@ impl ItemScope {
311311
ItemInNs::Macros(def) => self
312312
.macros
313313
.iter()
314-
.filter_map(|(name, &(other_def, vis, i))| {
315-
(other_def == def).then_some((name, vis, i.is_none()))
314+
.filter_map(|(name, other_def)| {
315+
(other_def.def == def).then_some((
316+
name,
317+
other_def.vis,
318+
other_def.import.is_none(),
319+
))
316320
})
317321
.find_map(|(a, b, c)| cb(a, b, c)),
318322
ItemInNs::Types(def) => self
319323
.types
320324
.iter()
321-
.filter_map(|(name, &(other_def, vis, i))| {
322-
(other_def == def).then_some((name, vis, i.is_none()))
325+
.filter_map(|(name, other_def)| {
326+
(other_def.def == def).then_some((
327+
name,
328+
other_def.vis,
329+
other_def.import.is_none(),
330+
))
323331
})
324332
.find_map(|(a, b, c)| cb(a, b, c)),
325333
ItemInNs::Values(def) => self
326334
.values
327335
.iter()
328-
.filter_map(|(name, &(other_def, vis, i))| {
329-
(other_def == def).then_some((name, vis, i.is_none()))
336+
.filter_map(|(name, other_def)| {
337+
(other_def.def == def).then_some((
338+
name,
339+
other_def.vis,
340+
other_def.import.is_none(),
341+
))
330342
})
331343
.find_map(|(a, b, c)| cb(a, b, c)),
332344
}
@@ -335,7 +347,7 @@ impl ItemScope {
335347
pub(crate) fn traits(&self) -> impl Iterator<Item = TraitId> + '_ {
336348
self.types
337349
.values()
338-
.filter_map(|&(def, _, _)| match def {
350+
.filter_map(|def| match def.def {
339351
ModuleDefId::TraitId(t) => Some(t),
340352
_ => None,
341353
})
@@ -344,13 +356,13 @@ impl ItemScope {
344356

345357
pub(crate) fn resolutions(&self) -> impl Iterator<Item = (Option<Name>, PerNs)> + '_ {
346358
self.entries().map(|(name, res)| (Some(name.clone()), res)).chain(
347-
self.unnamed_trait_imports.iter().map(|(tr, (vis, i))| {
359+
self.unnamed_trait_imports.iter().map(|(tr, trait_)| {
348360
(
349361
None,
350362
PerNs::types(
351363
ModuleDefId::TraitId(*tr),
352-
*vis,
353-
i.map(ImportOrExternCrate::Import),
364+
trait_.vis,
365+
trait_.import.map(ImportOrExternCrate::Import),
354366
),
355367
)
356368
}),
@@ -464,12 +476,12 @@ impl ItemScope {
464476

465477
// FIXME: This is only used in collection, we should move the relevant parts of it out of ItemScope
466478
pub(crate) fn unnamed_trait_vis(&self, tr: TraitId) -> Option<Visibility> {
467-
self.unnamed_trait_imports.get(&tr).copied().map(|(a, _)| a)
479+
self.unnamed_trait_imports.get(&tr).map(|trait_| trait_.vis)
468480
}
469481

470482
pub(crate) fn push_unnamed_trait(&mut self, tr: TraitId, vis: Visibility) {
471483
// FIXME: import
472-
self.unnamed_trait_imports.insert(tr, (vis, None));
484+
self.unnamed_trait_imports.insert(tr, Item { def: (), vis, import: None });
473485
}
474486

475487
pub(crate) fn push_res_with_import(
@@ -502,7 +514,7 @@ impl ItemScope {
502514
}
503515
None | Some(ImportType::Glob(_)) => None,
504516
};
505-
let prev = std::mem::replace(&mut fld.2, import);
517+
let prev = std::mem::replace(&mut fld.import, import);
506518
if let Some(import) = import {
507519
self.use_imports_types.insert(
508520
import,
@@ -513,7 +525,7 @@ impl ItemScope {
513525
Some(ImportOrExternCrate::ExternCrate(import)) => {
514526
ImportOrDef::ExternCrate(import)
515527
}
516-
None => ImportOrDef::Def(fld.0),
528+
None => ImportOrDef::Def(fld.def),
517529
},
518530
);
519531
}
@@ -540,7 +552,7 @@ impl ItemScope {
540552
}
541553
None | Some(ImportType::Glob(_)) => None,
542554
};
543-
let prev = std::mem::replace(&mut fld.2, import);
555+
let prev = std::mem::replace(&mut fld.import, import);
544556
if let Some(import) = import {
545557
self.use_imports_types.insert(
546558
import,
@@ -551,7 +563,7 @@ impl ItemScope {
551563
Some(ImportOrExternCrate::ExternCrate(import)) => {
552564
ImportOrDef::ExternCrate(import)
553565
}
554-
None => ImportOrDef::Def(fld.0),
566+
None => ImportOrDef::Def(fld.def),
555567
},
556568
);
557569
}
@@ -579,13 +591,13 @@ impl ItemScope {
579591
Some(ImportType::Import(import)) => Some(import),
580592
_ => None,
581593
};
582-
let prev = std::mem::replace(&mut fld.2, import);
594+
let prev = std::mem::replace(&mut fld.import, import);
583595
if let Some(import) = import {
584596
self.use_imports_values.insert(
585597
import,
586598
match prev {
587599
Some(import) => ImportOrDef::Import(import),
588-
None => ImportOrDef::Def(fld.0),
600+
None => ImportOrDef::Def(fld.def),
589601
},
590602
);
591603
}
@@ -599,13 +611,13 @@ impl ItemScope {
599611
Some(ImportType::Import(import)) => Some(import),
600612
_ => None,
601613
};
602-
let prev = std::mem::replace(&mut fld.2, import);
614+
let prev = std::mem::replace(&mut fld.import, import);
603615
if let Some(import) = import {
604616
self.use_imports_values.insert(
605617
import,
606618
match prev {
607619
Some(import) => ImportOrDef::Import(import),
608-
None => ImportOrDef::Def(fld.0),
620+
None => ImportOrDef::Def(fld.def),
609621
},
610622
);
611623
}
@@ -631,13 +643,13 @@ impl ItemScope {
631643
Some(ImportType::Import(import)) => Some(import),
632644
_ => None,
633645
};
634-
let prev = std::mem::replace(&mut fld.2, import);
646+
let prev = std::mem::replace(&mut fld.import, import);
635647
if let Some(import) = import {
636648
self.use_imports_macros.insert(
637649
import,
638650
match prev {
639651
Some(import) => ImportOrDef::Import(import),
640-
None => ImportOrDef::Def(fld.0.into()),
652+
None => ImportOrDef::Def(fld.def.into()),
641653
},
642654
);
643655
}
@@ -651,13 +663,13 @@ impl ItemScope {
651663
Some(ImportType::Import(import)) => Some(import),
652664
_ => None,
653665
};
654-
let prev = std::mem::replace(&mut fld.2, import);
666+
let prev = std::mem::replace(&mut fld.import, import);
655667
if let Some(import) = import {
656668
self.use_imports_macros.insert(
657669
import,
658670
match prev {
659671
Some(import) => ImportOrDef::Import(import),
660-
None => ImportOrDef::Def(fld.0.into()),
672+
None => ImportOrDef::Def(fld.def.into()),
661673
},
662674
);
663675
}
@@ -680,19 +692,19 @@ impl ItemScope {
680692
pub(crate) fn censor_non_proc_macros(&mut self, this_module: ModuleId) {
681693
self.types
682694
.values_mut()
683-
.map(|(_, vis, _)| vis)
684-
.chain(self.values.values_mut().map(|(_, vis, _)| vis))
685-
.chain(self.unnamed_trait_imports.values_mut().map(|(vis, _)| vis))
695+
.map(|def| &mut def.vis)
696+
.chain(self.values.values_mut().map(|def| &mut def.vis))
697+
.chain(self.unnamed_trait_imports.values_mut().map(|def| &mut def.vis))
686698
.for_each(|vis| {
687699
*vis = Visibility::Module(this_module, VisibilityExplicitness::Implicit)
688700
});
689701

690-
for (mac, vis, import) in self.macros.values_mut() {
691-
if matches!(mac, MacroId::ProcMacroId(_) if import.is_none()) {
702+
for mac in self.macros.values_mut() {
703+
if matches!(mac.def, MacroId::ProcMacroId(_) if mac.import.is_none()) {
692704
continue;
693705
}
694706

695-
*vis = Visibility::Module(this_module, VisibilityExplicitness::Implicit);
707+
mac.vis = Visibility::Module(this_module, VisibilityExplicitness::Implicit);
696708
}
697709
}
698710

@@ -707,23 +719,23 @@ impl ItemScope {
707719
name.map_or("_".to_owned(), |name| name.display(db, Edition::LATEST).to_string())
708720
);
709721

710-
if let Some((.., i)) = def.types {
722+
if let Some(Item { import, .. }) = def.types {
711723
buf.push_str(" t");
712-
match i {
724+
match import {
713725
Some(ImportOrExternCrate::Import(_)) => buf.push('i'),
714726
Some(ImportOrExternCrate::ExternCrate(_)) => buf.push('e'),
715727
None => (),
716728
}
717729
}
718-
if let Some((.., i)) = def.values {
730+
if let Some(Item { import, .. }) = def.values {
719731
buf.push_str(" v");
720-
if i.is_some() {
732+
if import.is_some() {
721733
buf.push('i');
722734
}
723735
}
724-
if let Some((.., i)) = def.macros {
736+
if let Some(Item { import, .. }) = def.macros {
725737
buf.push_str(" m");
726-
if i.is_some() {
738+
if import.is_some() {
727739
buf.push('i');
728740
}
729741
}
@@ -781,19 +793,19 @@ impl ItemScope {
781793
pub(crate) fn update_visibility_types(&mut self, name: &Name, vis: Visibility) {
782794
let res =
783795
self.types.get_mut(name).expect("tried to update visibility of non-existent type");
784-
res.1 = vis;
796+
res.vis = vis;
785797
}
786798

787799
pub(crate) fn update_visibility_values(&mut self, name: &Name, vis: Visibility) {
788800
let res =
789801
self.values.get_mut(name).expect("tried to update visibility of non-existent value");
790-
res.1 = vis;
802+
res.vis = vis;
791803
}
792804

793805
pub(crate) fn update_visibility_macros(&mut self, name: &Name, vis: Visibility) {
794806
let res =
795807
self.macros.get_mut(name).expect("tried to update visibility of non-existent macro");
796-
res.1 = vis;
808+
res.vis = vis;
797809
}
798810
}
799811

0 commit comments

Comments
 (0)