Skip to content

Commit 5d611e5

Browse files
committed
resolve: Move self_binding to ModuleData
1 parent 5795086 commit 5d611e5

File tree

2 files changed

+16
-25
lines changed

2 files changed

+16
-25
lines changed

compiler/rustc_resolve/src/ident.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
841841
if ns == TypeNS {
842842
if ident.name == kw::Crate || ident.name == kw::DollarCrate {
843843
let module = self.resolve_crate_root(ident);
844-
return Ok(self.module_self_bindings[&module]);
844+
return Ok(module.self_binding.unwrap());
845845
} else if ident.name == kw::Super || ident.name == kw::SelfLower {
846846
// FIXME: Implement these with renaming requirements so that e.g.
847847
// `use super;` doesn't work, but `use super as name;` does.

compiler/rustc_resolve/src/lib.rs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,10 @@ struct ModuleData<'ra> {
585585
span: Span,
586586

587587
expansion: ExpnId,
588+
589+
/// Binding for implicitly declared names that come with a module,
590+
/// like `self` (not yet used), or `crate`/`$crate` (for root modules).
591+
self_binding: Option<NameBinding<'ra>>,
588592
}
589593

590594
/// All modules are unique and allocated on a same arena,
@@ -613,6 +617,7 @@ impl<'ra> ModuleData<'ra> {
613617
expansion: ExpnId,
614618
span: Span,
615619
no_implicit_prelude: bool,
620+
self_binding: Option<NameBinding<'ra>>,
616621
) -> Self {
617622
let is_foreign = match kind {
618623
ModuleKind::Def(_, def_id, _) => !def_id.is_local(),
@@ -630,6 +635,7 @@ impl<'ra> ModuleData<'ra> {
630635
traits: RefCell::new(None),
631636
span,
632637
expansion,
638+
self_binding,
633639
}
634640
}
635641
}
@@ -1101,10 +1107,6 @@ pub struct Resolver<'ra, 'tcx> {
11011107
builtin_types_bindings: FxHashMap<Symbol, NameBinding<'ra>>,
11021108
builtin_attrs_bindings: FxHashMap<Symbol, NameBinding<'ra>>,
11031109
registered_tool_bindings: FxHashMap<Ident, NameBinding<'ra>>,
1104-
/// Binding for implicitly declared names that come with a module,
1105-
/// like `self` (not yet used), or `crate`/`$crate` (for root modules).
1106-
module_self_bindings: FxHashMap<Module<'ra>, NameBinding<'ra>>,
1107-
11081110
used_extern_options: FxHashSet<Symbol>,
11091111
macro_names: FxHashSet<Ident>,
11101112
builtin_macros: FxHashMap<Symbol, SyntaxExtensionKind>,
@@ -1264,24 +1266,27 @@ impl<'ra> ResolverArenas<'ra> {
12641266
span: Span,
12651267
no_implicit_prelude: bool,
12661268
module_map: &mut FxIndexMap<DefId, Module<'ra>>,
1267-
module_self_bindings: &mut FxHashMap<Module<'ra>, NameBinding<'ra>>,
12681269
) -> Module<'ra> {
1270+
let (def_id, self_binding) = match kind {
1271+
ModuleKind::Def(def_kind, def_id, _) => (
1272+
Some(def_id),
1273+
Some(self.new_pub_res_binding(Res::Def(def_kind, def_id), span, LocalExpnId::ROOT)),
1274+
),
1275+
ModuleKind::Block => (None, None),
1276+
};
12691277
let module = Module(Interned::new_unchecked(self.modules.alloc(ModuleData::new(
12701278
parent,
12711279
kind,
12721280
expn_id,
12731281
span,
12741282
no_implicit_prelude,
1283+
self_binding,
12751284
))));
1276-
let def_id = module.opt_def_id();
12771285
if def_id.is_none_or(|def_id| def_id.is_local()) {
12781286
self.local_modules.borrow_mut().push(module);
12791287
}
12801288
if let Some(def_id) = def_id {
12811289
module_map.insert(def_id, module);
1282-
let res = module.res().unwrap();
1283-
let binding = self.new_pub_res_binding(res, module.span, LocalExpnId::ROOT);
1284-
module_self_bindings.insert(module, binding);
12851290
}
12861291
module
12871292
}
@@ -1424,15 +1429,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14241429
) -> Resolver<'ra, 'tcx> {
14251430
let root_def_id = CRATE_DEF_ID.to_def_id();
14261431
let mut module_map = FxIndexMap::default();
1427-
let mut module_self_bindings = FxHashMap::default();
14281432
let graph_root = arenas.new_module(
14291433
None,
14301434
ModuleKind::Def(DefKind::Mod, root_def_id, None),
14311435
ExpnId::root(),
14321436
crate_span,
14331437
attr::contains_name(attrs, sym::no_implicit_prelude),
14341438
&mut module_map,
1435-
&mut module_self_bindings,
14361439
);
14371440
let empty_module = arenas.new_module(
14381441
None,
@@ -1441,7 +1444,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14411444
DUMMY_SP,
14421445
true,
14431446
&mut Default::default(),
1444-
&mut Default::default(),
14451447
);
14461448

14471449
let mut node_id_to_def_id = NodeMap::default();
@@ -1544,8 +1546,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15441546
(*ident, binding)
15451547
})
15461548
.collect(),
1547-
module_self_bindings,
1548-
15491549
used_extern_options: Default::default(),
15501550
macro_names: FxHashSet::default(),
15511551
builtin_macros: Default::default(),
@@ -1617,16 +1617,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16171617
no_implicit_prelude: bool,
16181618
) -> Module<'ra> {
16191619
let module_map = &mut self.module_map;
1620-
let module_self_bindings = &mut self.module_self_bindings;
1621-
self.arenas.new_module(
1622-
parent,
1623-
kind,
1624-
expn_id,
1625-
span,
1626-
no_implicit_prelude,
1627-
module_map,
1628-
module_self_bindings,
1629-
)
1620+
self.arenas.new_module(parent, kind, expn_id, span, no_implicit_prelude, module_map)
16301621
}
16311622

16321623
fn new_local_macro(&mut self, def_id: LocalDefId, macro_data: MacroData) -> &'ra MacroData {

0 commit comments

Comments
 (0)