Skip to content

Commit 02640f9

Browse files
committed
resolve: Make bindings for crate roots unique
instead of creating a new every time `crate` or `$crate` is used
1 parent d1f8ea4 commit 02640f9

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

compiler/rustc_resolve/src/ident.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -833,9 +833,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
833833
if ns == TypeNS {
834834
if ident.name == kw::Crate || ident.name == kw::DollarCrate {
835835
let module = self.resolve_crate_root(ident);
836-
let binding = (module, Visibility::Public, module.span, LocalExpnId::ROOT)
837-
.to_name_binding(self.arenas);
838-
return Ok(binding);
836+
return Ok(self.module_self_bindings[&module]);
839837
} else if ident.name == kw::Super || ident.name == kw::SelfLower {
840838
// FIXME: Implement these with renaming requirements so that e.g.
841839
// `use super;` doesn't work, but `use super as name;` does.

compiler/rustc_resolve/src/lib.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ struct ModuleData<'a> {
519519

520520
/// All modules are unique and allocated on a same arena,
521521
/// so we can use referential equality to compare them.
522-
#[derive(Clone, Copy, PartialEq)]
522+
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
523523
#[rustc_pass_by_value]
524524
struct Module<'a>(Interned<'a, ModuleData<'a>>);
525525

@@ -1007,6 +1007,9 @@ pub struct Resolver<'a, 'tcx> {
10071007
builtin_types_bindings: FxHashMap<Symbol, NameBinding<'a>>,
10081008
builtin_attrs_bindings: FxHashMap<Symbol, NameBinding<'a>>,
10091009
registered_tool_bindings: FxHashMap<Ident, NameBinding<'a>>,
1010+
/// Binding for implicitly declared names that come with a module,
1011+
/// like `self` (not yet used), or `crate`/`$crate` (for root modules).
1012+
module_self_bindings: FxHashMap<Module<'a>, NameBinding<'a>>,
10101013

10111014
used_extern_options: FxHashSet<Symbol>,
10121015
macro_names: FxHashSet<Ident>,
@@ -1122,6 +1125,7 @@ impl<'a> ResolverArenas<'a> {
11221125
span: Span,
11231126
no_implicit_prelude: bool,
11241127
module_map: &mut FxHashMap<DefId, Module<'a>>,
1128+
module_self_bindings: &mut FxHashMap<Module<'a>, NameBinding<'a>>,
11251129
) -> Module<'a> {
11261130
let module = Module(Interned::new_unchecked(self.modules.alloc(ModuleData::new(
11271131
parent,
@@ -1136,6 +1140,9 @@ impl<'a> ResolverArenas<'a> {
11361140
}
11371141
if let Some(def_id) = def_id {
11381142
module_map.insert(def_id, module);
1143+
let vis = ty::Visibility::<DefId>::Public;
1144+
let binding = (module, vis, module.span, LocalExpnId::ROOT).to_name_binding(self);
1145+
module_self_bindings.insert(module, binding);
11391146
}
11401147
module
11411148
}
@@ -1247,13 +1254,15 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12471254
) -> Resolver<'a, 'tcx> {
12481255
let root_def_id = CRATE_DEF_ID.to_def_id();
12491256
let mut module_map = FxHashMap::default();
1257+
let mut module_self_bindings = FxHashMap::default();
12501258
let graph_root = arenas.new_module(
12511259
None,
12521260
ModuleKind::Def(DefKind::Mod, root_def_id, kw::Empty),
12531261
ExpnId::root(),
12541262
crate_span,
12551263
attr::contains_name(attrs, sym::no_implicit_prelude),
12561264
&mut module_map,
1265+
&mut module_self_bindings,
12571266
);
12581267
let empty_module = arenas.new_module(
12591268
None,
@@ -1262,6 +1271,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12621271
DUMMY_SP,
12631272
true,
12641273
&mut FxHashMap::default(),
1274+
&mut FxHashMap::default(),
12651275
);
12661276

12671277
let mut visibilities = FxHashMap::default();
@@ -1368,6 +1378,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13681378
(*ident, binding)
13691379
})
13701380
.collect(),
1381+
module_self_bindings,
13711382

13721383
used_extern_options: Default::default(),
13731384
macro_names: FxHashSet::default(),
@@ -1437,7 +1448,16 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14371448
no_implicit_prelude: bool,
14381449
) -> Module<'a> {
14391450
let module_map = &mut self.module_map;
1440-
self.arenas.new_module(parent, kind, expn_id, span, no_implicit_prelude, module_map)
1451+
let module_self_bindings = &mut self.module_self_bindings;
1452+
self.arenas.new_module(
1453+
parent,
1454+
kind,
1455+
expn_id,
1456+
span,
1457+
no_implicit_prelude,
1458+
module_map,
1459+
module_self_bindings,
1460+
)
14411461
}
14421462

14431463
fn next_node_id(&mut self) -> NodeId {

0 commit comments

Comments
 (0)