Skip to content

Commit c1f412f

Browse files
committed
resolve: Use Interned for Module
1 parent 4abdaeb commit c1f412f

File tree

7 files changed

+59
-65
lines changed

7 files changed

+59
-65
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol};
3131
use rustc_span::Span;
3232

3333
use std::cell::Cell;
34-
use std::ptr;
3534

3635
type Res = def::Res<NodeId>;
3736

@@ -142,8 +141,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
142141
Some(def_id) => self.macro_def_scope(def_id),
143142
None => expn_id
144143
.as_local()
145-
.and_then(|expn_id| self.ast_transform_scopes.get(&expn_id))
146-
.unwrap_or(&self.graph_root),
144+
.and_then(|expn_id| self.ast_transform_scopes.get(&expn_id).copied())
145+
.unwrap_or(self.graph_root),
147146
}
148147
}
149148

@@ -864,7 +863,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
864863
});
865864
self.r.potentially_unused_imports.push(import);
866865
let imported_binding = self.r.import(binding, import);
867-
if ptr::eq(parent, self.r.graph_root) {
866+
if parent == self.r.graph_root {
868867
if let Some(entry) = self.r.extern_prelude.get(&ident.normalize_to_macros_2_0()) {
869868
if expansion != LocalExpnId::ROOT
870869
&& orig_name.is_some()

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::ptr;
2-
31
use rustc_ast::expand::StrippedCfgItem;
42
use rustc_ast::ptr::P;
53
use rustc_ast::visit::{self, Visitor};
@@ -1198,7 +1196,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
11981196
// avoid suggesting anything with a hygienic name
11991197
if ident.name == lookup_ident.name
12001198
&& ns == namespace
1201-
&& !ptr::eq(in_module, parent_scope.module)
1199+
&& in_module != parent_scope.module
12021200
&& !ident.span.normalize_to_macros_2_0().from_expansion()
12031201
{
12041202
let res = name_binding.res();
@@ -1732,7 +1730,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
17321730
pub(crate) fn find_similarly_named_module_or_crate(
17331731
&mut self,
17341732
ident: Symbol,
1735-
current_module: &Module<'a>,
1733+
current_module: Module<'a>,
17361734
) -> Option<Symbol> {
17371735
let mut candidates = self
17381736
.extern_prelude
@@ -1742,7 +1740,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
17421740
self.module_map
17431741
.iter()
17441742
.filter(|(_, module)| {
1745-
current_module.is_ancestor_of(module) && !ptr::eq(current_module, *module)
1743+
current_module.is_ancestor_of(**module) && current_module != **module
17461744
})
17471745
.flat_map(|(_, module)| module.kind.name()),
17481746
)
@@ -1945,7 +1943,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
19451943
}
19461944

19471945
suggestion = suggestion.or_else(|| {
1948-
self.find_similarly_named_module_or_crate(ident.name, &parent_scope.module).map(
1946+
self.find_similarly_named_module_or_crate(ident.name, parent_scope.module).map(
19491947
|sugg| {
19501948
(
19511949
vec![(ident.span, sugg.to_string())],
@@ -2126,9 +2124,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
21262124
crate_module = parent;
21272125
}
21282126

2129-
if ModuleOrUniformRoot::same_def(ModuleOrUniformRoot::Module(crate_module), module) {
2130-
// Don't make a suggestion if the import was already from the root of the
2131-
// crate.
2127+
if module == ModuleOrUniformRoot::Module(crate_module) {
2128+
// Don't make a suggestion if the import was already from the root of the crate.
21322129
return None;
21332130
}
21342131

compiler/rustc_resolve/src/ident.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ use rustc_span::hygiene::{ExpnId, ExpnKind, LocalExpnId, MacroKind, SyntaxContex
1111
use rustc_span::symbol::{kw, Ident};
1212
use rustc_span::{Span, DUMMY_SP};
1313

14-
use std::ptr;
15-
1614
use crate::errors::{ParamKindInEnumDiscriminant, ParamKindInNonTrivialAnonConst};
1715
use crate::late::{
1816
ConstantHasGenerics, HasGenericParams, NoConstantGenericsReason, PathSource, Rib, RibKind,
@@ -538,7 +536,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
538536
),
539537
);
540538
}
541-
let misc_flags = if ptr::eq(module, this.graph_root) {
539+
let misc_flags = if module == this.graph_root {
542540
Flags::MISC_SUGGEST_CRATE
543541
} else if module.is_normal() {
544542
Flags::MISC_SUGGEST_SELF

compiler/rustc_resolve/src/imports.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use rustc_span::Span;
3535
use smallvec::SmallVec;
3636

3737
use std::cell::Cell;
38-
use std::{mem, ptr};
38+
use std::mem;
3939

4040
type Res = def::Res<NodeId>;
4141

@@ -463,7 +463,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
463463

464464
pub(crate) fn finalize_imports(&mut self) {
465465
for module in self.arenas.local_modules().iter() {
466-
self.finalize_resolutions_in(module);
466+
self.finalize_resolutions_in(*module);
467467
}
468468

469469
let mut seen_spans = FxHashSet::default();
@@ -537,7 +537,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
537537
exported_ambiguities: FxHashSet<NameBinding<'a>>,
538538
) {
539539
for module in self.arenas.local_modules().iter() {
540-
for (key, resolution) in self.resolutions(module).borrow().iter() {
540+
for (key, resolution) in self.resolutions(*module).borrow().iter() {
541541
let resolution = resolution.borrow();
542542

543543
if let Some(binding) = resolution.binding {
@@ -812,7 +812,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
812812
PathResult::Module(module) => {
813813
// Consistency checks, analogous to `finalize_macro_resolutions`.
814814
if let Some(initial_module) = import.imported_module.get() {
815-
if !ModuleOrUniformRoot::same_def(module, initial_module) && no_ambiguity {
815+
if module != initial_module && no_ambiguity {
816816
span_bug!(import.span, "inconsistent resolution for an import");
817817
}
818818
} else if self.privacy_errors.is_empty() {
@@ -914,7 +914,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
914914
}
915915

916916
if let ModuleOrUniformRoot::Module(module) = module {
917-
if ptr::eq(module, import.parent_scope.module) {
917+
if module == import.parent_scope.module {
918918
// Importing a module into itself is not allowed.
919919
return Some(UnresolvedImportError {
920920
span: import.span,
@@ -1307,7 +1307,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13071307
if module.is_trait() {
13081308
self.tcx.sess.create_err(ItemsInTraitsAreNotImportable { span: import.span }).emit();
13091309
return;
1310-
} else if ptr::eq(module, import.parent_scope.module) {
1310+
} else if module == import.parent_scope.module {
13111311
return;
13121312
} else if is_prelude {
13131313
self.prelude = Some(module);

compiler/rustc_resolve/src/late.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2972,7 +2972,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
29722972
F: FnOnce(Ident, String, Option<Symbol>) -> ResolutionError<'a>,
29732973
{
29742974
// If there is a TraitRef in scope for an impl, then the method must be in the trait.
2975-
let Some((module, _)) = &self.current_trait_ref else { return; };
2975+
let Some((module, _)) = self.current_trait_ref else { return; };
29762976
ident.span.normalize_to_macros_2_0_and_adjust(module.expansion);
29772977
let key = BindingKey::new(ident, ns);
29782978
let mut binding = self.r.resolution(module, key).try_borrow().ok().and_then(|r| r.binding);

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1593,7 +1593,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
15931593
return None;
15941594
}
15951595

1596-
let resolutions = self.r.resolutions(module);
1596+
let resolutions = self.r.resolutions(*module);
15971597
let targets = resolutions
15981598
.borrow()
15991599
.iter()

compiler/rustc_resolve/src/lib.rs

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![feature(iter_intersperse)]
1515
#![feature(let_chains)]
1616
#![feature(never_type)]
17+
#![feature(rustc_attrs)]
1718
#![recursion_limit = "256"]
1819
#![allow(rustdoc::private_intra_doc_links)]
1920
#![allow(rustc::potential_query_instability)]
@@ -61,7 +62,7 @@ use rustc_span::{Span, DUMMY_SP};
6162
use smallvec::{smallvec, SmallVec};
6263
use std::cell::{Cell, RefCell};
6364
use std::collections::BTreeSet;
64-
use std::{fmt, ptr};
65+
use std::fmt;
6566

6667
use diagnostics::{ImportSuggestion, LabelSuggestion, Suggestion};
6768
use imports::{Import, ImportData, ImportKind, NameResolution};
@@ -365,7 +366,7 @@ impl<'a> LexicalScopeBinding<'a> {
365366
}
366367
}
367368

368-
#[derive(Copy, Clone, Debug)]
369+
#[derive(Copy, Clone, PartialEq, Debug)]
369370
enum ModuleOrUniformRoot<'a> {
370371
/// Regular module.
371372
Module(Module<'a>),
@@ -383,23 +384,6 @@ enum ModuleOrUniformRoot<'a> {
383384
CurrentScope,
384385
}
385386

386-
impl ModuleOrUniformRoot<'_> {
387-
fn same_def(lhs: Self, rhs: Self) -> bool {
388-
match (lhs, rhs) {
389-
(ModuleOrUniformRoot::Module(lhs), ModuleOrUniformRoot::Module(rhs)) => {
390-
ptr::eq(lhs, rhs)
391-
}
392-
(
393-
ModuleOrUniformRoot::CrateRootAndExternPrelude,
394-
ModuleOrUniformRoot::CrateRootAndExternPrelude,
395-
)
396-
| (ModuleOrUniformRoot::ExternPrelude, ModuleOrUniformRoot::ExternPrelude)
397-
| (ModuleOrUniformRoot::CurrentScope, ModuleOrUniformRoot::CurrentScope) => true,
398-
_ => false,
399-
}
400-
}
401-
}
402-
403387
#[derive(Debug)]
404388
enum PathResult<'a> {
405389
Module(ModuleOrUniformRoot<'a>),
@@ -530,7 +514,9 @@ struct ModuleData<'a> {
530514
expansion: ExpnId,
531515
}
532516

533-
type Module<'a> = &'a ModuleData<'a>;
517+
#[derive(Clone, Copy, PartialEq)]
518+
#[rustc_pass_by_value]
519+
struct Module<'a>(Interned<'a, ModuleData<'a>>);
534520

535521
impl<'a> ModuleData<'a> {
536522
fn new(
@@ -558,8 +544,10 @@ impl<'a> ModuleData<'a> {
558544
expansion,
559545
}
560546
}
547+
}
561548

562-
fn for_each_child<'tcx, R, F>(&'a self, resolver: &mut R, mut f: F)
549+
impl<'a> Module<'a> {
550+
fn for_each_child<'tcx, R, F>(self, resolver: &mut R, mut f: F)
563551
where
564552
R: AsMut<Resolver<'a, 'tcx>>,
565553
F: FnMut(&mut R, Ident, Namespace, NameBinding<'a>),
@@ -572,7 +560,7 @@ impl<'a> ModuleData<'a> {
572560
}
573561

574562
/// This modifies `self` in place. The traits will be stored in `self.traits`.
575-
fn ensure_traits<'tcx, R>(&'a self, resolver: &mut R)
563+
fn ensure_traits<'tcx, R>(self, resolver: &mut R)
576564
where
577565
R: AsMut<Resolver<'a, 'tcx>>,
578566
{
@@ -591,35 +579,35 @@ impl<'a> ModuleData<'a> {
591579
}
592580
}
593581

594-
fn res(&self) -> Option<Res> {
582+
fn res(self) -> Option<Res> {
595583
match self.kind {
596584
ModuleKind::Def(kind, def_id, _) => Some(Res::Def(kind, def_id)),
597585
_ => None,
598586
}
599587
}
600588

601589
// Public for rustdoc.
602-
fn def_id(&self) -> DefId {
590+
fn def_id(self) -> DefId {
603591
self.opt_def_id().expect("`ModuleData::def_id` is called on a block module")
604592
}
605593

606-
fn opt_def_id(&self) -> Option<DefId> {
594+
fn opt_def_id(self) -> Option<DefId> {
607595
match self.kind {
608596
ModuleKind::Def(_, def_id, _) => Some(def_id),
609597
_ => None,
610598
}
611599
}
612600

613601
// `self` resolves to the first module ancestor that `is_normal`.
614-
fn is_normal(&self) -> bool {
602+
fn is_normal(self) -> bool {
615603
matches!(self.kind, ModuleKind::Def(DefKind::Mod, _, _))
616604
}
617605

618-
fn is_trait(&self) -> bool {
606+
fn is_trait(self) -> bool {
619607
matches!(self.kind, ModuleKind::Def(DefKind::Trait, _, _))
620608
}
621609

622-
fn nearest_item_scope(&'a self) -> Module<'a> {
610+
fn nearest_item_scope(self) -> Module<'a> {
623611
match self.kind {
624612
ModuleKind::Def(DefKind::Enum | DefKind::Trait, ..) => {
625613
self.parent.expect("enum or trait module without a parent")
@@ -630,15 +618,15 @@ impl<'a> ModuleData<'a> {
630618

631619
/// The [`DefId`] of the nearest `mod` item ancestor (which may be this module).
632620
/// This may be the crate root.
633-
fn nearest_parent_mod(&self) -> DefId {
621+
fn nearest_parent_mod(self) -> DefId {
634622
match self.kind {
635623
ModuleKind::Def(DefKind::Mod, def_id, _) => def_id,
636624
_ => self.parent.expect("non-root module without parent").nearest_parent_mod(),
637625
}
638626
}
639627

640-
fn is_ancestor_of(&self, mut other: &Self) -> bool {
641-
while !ptr::eq(self, other) {
628+
fn is_ancestor_of(self, mut other: Self) -> bool {
629+
while self != other {
642630
if let Some(parent) = other.parent {
643631
other = parent;
644632
} else {
@@ -649,7 +637,15 @@ impl<'a> ModuleData<'a> {
649637
}
650638
}
651639

652-
impl<'a> fmt::Debug for ModuleData<'a> {
640+
impl<'a> std::ops::Deref for Module<'a> {
641+
type Target = ModuleData<'a>;
642+
643+
fn deref(&self) -> &Self::Target {
644+
&self.0
645+
}
646+
}
647+
648+
impl<'a> fmt::Debug for Module<'a> {
653649
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
654650
write!(f, "{:?}", self.res())
655651
}
@@ -810,10 +806,9 @@ impl<'a> NameBindingData<'a> {
810806
NameBindingKind::Import { import, .. } => {
811807
matches!(import.kind, ImportKind::ExternCrate { .. })
812808
}
813-
NameBindingKind::Module(&ModuleData {
814-
kind: ModuleKind::Def(DefKind::Mod, def_id, _),
815-
..
816-
}) => def_id.is_crate_root(),
809+
NameBindingKind::Module(module)
810+
if let ModuleKind::Def(DefKind::Mod, def_id, _) = module.kind
811+
=> def_id.is_crate_root(),
817812
_ => false,
818813
}
819814
}
@@ -1102,8 +1097,13 @@ impl<'a> ResolverArenas<'a> {
11021097
no_implicit_prelude: bool,
11031098
module_map: &mut FxHashMap<DefId, Module<'a>>,
11041099
) -> Module<'a> {
1105-
let module =
1106-
self.modules.alloc(ModuleData::new(parent, kind, expn_id, span, no_implicit_prelude));
1100+
let module = Module(Interned::new_unchecked(self.modules.alloc(ModuleData::new(
1101+
parent,
1102+
kind,
1103+
expn_id,
1104+
span,
1105+
no_implicit_prelude,
1106+
))));
11071107
let def_id = module.opt_def_id();
11081108
if def_id.map_or(true, |def_id| def_id.is_local()) {
11091109
self.local_modules.borrow_mut().push(module);
@@ -1647,7 +1647,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
16471647
module.populate_on_access.set(false);
16481648
self.build_reduced_graph_external(module);
16491649
}
1650-
&module.lazy_resolutions
1650+
&module.0.0.lazy_resolutions
16511651
}
16521652

16531653
fn resolution(
@@ -1827,7 +1827,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
18271827

18281828
fn set_binding_parent_module(&mut self, binding: NameBinding<'a>, module: Module<'a>) {
18291829
if let Some(old_module) = self.binding_parent_modules.insert(binding, module) {
1830-
if !ptr::eq(module, old_module) {
1830+
if module != old_module {
18311831
span_bug!(binding.span, "parent module is reset for binding");
18321832
}
18331833
}
@@ -1847,7 +1847,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
18471847
) {
18481848
(Some(macro_rules), Some(modularized)) => {
18491849
macro_rules.nearest_parent_mod() == modularized.nearest_parent_mod()
1850-
&& modularized.is_ancestor_of(macro_rules)
1850+
&& modularized.is_ancestor_of(*macro_rules)
18511851
}
18521852
_ => false,
18531853
}

0 commit comments

Comments
 (0)