Skip to content

Commit 079cd50

Browse files
split up define into define_extern and define_local
1 parent dc1bd28 commit 079cd50

File tree

3 files changed

+74
-35
lines changed

3 files changed

+74
-35
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,20 @@ type Res = def::Res<NodeId>;
4242
impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
4343
/// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined;
4444
/// otherwise, reports an error.
45-
pub(crate) fn define_binding(
45+
pub(crate) fn define_binding_local(
4646
&mut self,
4747
parent: Module<'ra>,
4848
ident: Ident,
4949
ns: Namespace,
5050
binding: NameBinding<'ra>,
5151
) {
5252
let key = self.new_disambiguated_key(ident, ns);
53-
if let Err(old_binding) = self.try_define(parent, key, binding, false) {
53+
if let Err(old_binding) = self.try_define_local(parent, key, binding, false) {
5454
self.report_conflict(parent, ident, ns, old_binding, binding);
5555
}
5656
}
5757

58-
fn define(
58+
fn define_local(
5959
&mut self,
6060
parent: Module<'ra>,
6161
ident: Ident,
@@ -66,7 +66,29 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
6666
expn_id: LocalExpnId,
6767
) {
6868
let binding = self.arenas.new_res_binding(res, vis.to_def_id(), span, expn_id);
69-
self.define_binding(parent, ident, ns, binding)
69+
self.define_binding_local(parent, ident, ns, binding)
70+
}
71+
72+
// Panics when a binding already exists.
73+
fn define_extern(
74+
&self,
75+
parent: Module<'ra>,
76+
ident: Ident,
77+
ns: Namespace,
78+
res: Res,
79+
vis: Visibility<impl Into<DefId>>,
80+
span: Span,
81+
expn_id: LocalExpnId,
82+
) {
83+
let binding = self.arenas.new_res_binding(res, vis.to_def_id(), span, expn_id);
84+
let key = self.new_disambiguated_key(ident, ns);
85+
self.check_reserved_macro_name(key.ident, binding.res());
86+
let resolution = &mut *self.resolution(parent, key).borrow_mut();
87+
if resolution.binding.is_some() {
88+
panic!("An external binding was already defined");
89+
}
90+
// FIXME: maybe some handling of glob-importers
91+
resolution.binding = Some(binding);
7092
}
7193

7294
/// Walks up the tree of definitions starting at `def_id`,
@@ -189,7 +211,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
189211
visitor.parent_scope.macro_rules
190212
}
191213

192-
pub(crate) fn build_reduced_graph_external(&mut self, module: Module<'ra>) {
214+
pub(crate) fn build_reduced_graph_external(&self, module: Module<'ra>) {
193215
for child in self.tcx.module_children(module.def_id()) {
194216
let parent_scope = ParentScope::module(module, self);
195217
self.build_reduced_graph_for_external_crate_res(child, parent_scope)
@@ -198,7 +220,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
198220

199221
/// Builds the reduced graph for a single item in an external crate.
200222
fn build_reduced_graph_for_external_crate_res(
201-
&mut self,
223+
&self,
202224
child: &ModChild,
203225
parent_scope: ParentScope<'ra>,
204226
) {
@@ -229,7 +251,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
229251
_,
230252
)
231253
| Res::PrimTy(..)
232-
| Res::ToolMod => self.define(parent, ident, TypeNS, res, vis, span, expansion),
254+
| Res::ToolMod => self.define_extern(parent, ident, TypeNS, res, vis, span, expansion),
233255
Res::Def(
234256
DefKind::Fn
235257
| DefKind::AssocFn
@@ -238,9 +260,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
238260
| DefKind::AssocConst
239261
| DefKind::Ctor(..),
240262
_,
241-
) => self.define(parent, ident, ValueNS, res, vis, span, expansion),
263+
) => self.define_extern(parent, ident, ValueNS, res, vis, span, expansion),
242264
Res::Def(DefKind::Macro(..), _) | Res::NonMacroAttr(..) => {
243-
self.define(parent, ident, MacroNS, res, vis, span, expansion)
265+
self.define_extern(parent, ident, MacroNS, res, vis, span, expansion)
244266
}
245267
Res::Def(
246268
DefKind::TyParam
@@ -713,7 +735,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
713735
let expansion = parent_scope.expansion;
714736

715737
// Define a name in the type namespace if it is not anonymous.
716-
self.r.define(parent, ident, TypeNS, adt_res, adt_vis, adt_span, expansion);
738+
self.r.define_local(parent, ident, TypeNS, adt_res, adt_vis, adt_span, expansion);
717739
self.r.feed_visibility(feed, adt_vis);
718740
let def_id = feed.key();
719741

@@ -765,7 +787,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
765787
}
766788

767789
ItemKind::Mod(_, ident, ref mod_kind) => {
768-
self.r.define(parent, ident, TypeNS, res, vis, sp, expansion);
790+
self.r.define_local(parent, ident, TypeNS, res, vis, sp, expansion);
769791

770792
if let ast::ModKind::Loaded(_, _, _, Err(_)) = mod_kind {
771793
self.r.mods_with_parse_errors.insert(def_id);
@@ -784,10 +806,10 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
784806
ItemKind::Const(box ConstItem { ident, .. })
785807
| ItemKind::Delegation(box Delegation { ident, .. })
786808
| ItemKind::Static(box StaticItem { ident, .. }) => {
787-
self.r.define(parent, ident, ValueNS, res, vis, sp, expansion);
809+
self.r.define_local(parent, ident, ValueNS, res, vis, sp, expansion);
788810
}
789811
ItemKind::Fn(box Fn { ident, .. }) => {
790-
self.r.define(parent, ident, ValueNS, res, vis, sp, expansion);
812+
self.r.define_local(parent, ident, ValueNS, res, vis, sp, expansion);
791813

792814
// Functions introducing procedural macros reserve a slot
793815
// in the macro namespace as well (see #52225).
@@ -796,11 +818,11 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
796818

797819
// These items live in the type namespace.
798820
ItemKind::TyAlias(box TyAlias { ident, .. }) | ItemKind::TraitAlias(ident, ..) => {
799-
self.r.define(parent, ident, TypeNS, res, vis, sp, expansion);
821+
self.r.define_local(parent, ident, TypeNS, res, vis, sp, expansion);
800822
}
801823

802824
ItemKind::Enum(ident, _, _) | ItemKind::Trait(box ast::Trait { ident, .. }) => {
803-
self.r.define(parent, ident, TypeNS, res, vis, sp, expansion);
825+
self.r.define_local(parent, ident, TypeNS, res, vis, sp, expansion);
804826

805827
self.parent_scope.module = self.r.new_local_module(
806828
Some(parent),
@@ -852,7 +874,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
852874
let feed = self.r.feed(ctor_node_id);
853875
let ctor_def_id = feed.key();
854876
let ctor_res = self.res(ctor_def_id);
855-
self.r.define(parent, ident, ValueNS, ctor_res, ctor_vis, sp, expansion);
877+
self.r.define_local(parent, ident, ValueNS, ctor_res, ctor_vis, sp, expansion);
856878
self.r.feed_visibility(feed, ctor_vis);
857879
// We need the field visibility spans also for the constructor for E0603.
858880
self.insert_field_visibilities_local(ctor_def_id.to_def_id(), vdata.fields());
@@ -973,7 +995,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
973995
);
974996
}
975997
}
976-
self.r.define_binding(parent, ident, TypeNS, imported_binding);
998+
self.r.define_binding_local(parent, ident, TypeNS, imported_binding);
977999
}
9781000

9791001
/// Constructs the reduced graph for one foreign item.
@@ -990,7 +1012,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
9901012
let parent = self.parent_scope.module;
9911013
let expansion = self.parent_scope.expansion;
9921014
let vis = self.resolve_visibility(&item.vis);
993-
self.r.define(parent, ident, ns, self.res(def_id), vis, item.span, expansion);
1015+
self.r.define_local(parent, ident, ns, self.res(def_id), vis, item.span, expansion);
9941016
self.r.feed_visibility(feed, vis);
9951017
}
9961018

@@ -1249,7 +1271,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12491271
});
12501272
self.r.import_use_map.insert(import, Used::Other);
12511273
let import_binding = self.r.import(binding, import);
1252-
self.r.define_binding(self.r.graph_root, ident, MacroNS, import_binding);
1274+
self.r.define_binding_local(self.r.graph_root, ident, MacroNS, import_binding);
12531275
} else {
12541276
self.r.check_reserved_macro_name(ident, res);
12551277
self.insert_unused_macro(ident, def_id, item.id);
@@ -1277,7 +1299,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12771299
if !vis.is_public() {
12781300
self.insert_unused_macro(ident, def_id, item.id);
12791301
}
1280-
self.r.define(module, ident, MacroNS, res, vis, span, expansion);
1302+
self.r.define_local(module, ident, MacroNS, res, vis, span, expansion);
12811303
self.r.feed_visibility(feed, vis);
12821304
self.parent_scope.macro_rules
12831305
}
@@ -1413,7 +1435,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
14131435
if ctxt == AssocCtxt::Trait {
14141436
let parent = self.parent_scope.module;
14151437
let expansion = self.parent_scope.expansion;
1416-
self.r.define(parent, ident, ns, self.res(def_id), vis, item.span, expansion);
1438+
self.r.define_local(parent, ident, ns, self.res(def_id), vis, item.span, expansion);
14171439
} else if !matches!(&item.kind, AssocItemKind::Delegation(deleg) if deleg.from_glob) {
14181440
let impl_def_id = self.r.tcx.local_parent(local_def_id);
14191441
let key = BindingKey::new(ident.normalize_to_macros_2_0(), ns);
@@ -1498,7 +1520,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
14981520
let feed = self.r.feed(variant.id);
14991521
let def_id = feed.key();
15001522
let vis = self.resolve_visibility(&variant.vis);
1501-
self.r.define(parent, ident, TypeNS, self.res(def_id), vis, variant.span, expn_id);
1523+
self.r.define_local(parent, ident, TypeNS, self.res(def_id), vis, variant.span, expn_id);
15021524
self.r.feed_visibility(feed, vis);
15031525

15041526
// If the variant is marked as non_exhaustive then lower the visibility to within the crate.
@@ -1514,7 +1536,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
15141536
let feed = self.r.feed(ctor_node_id);
15151537
let ctor_def_id = feed.key();
15161538
let ctor_res = self.res(ctor_def_id);
1517-
self.r.define(parent, ident, ValueNS, ctor_res, ctor_vis, variant.span, expn_id);
1539+
self.r.define_local(parent, ident, ValueNS, ctor_res, ctor_vis, variant.span, expn_id);
15181540
self.r.feed_visibility(feed, ctor_vis);
15191541
}
15201542

compiler/rustc_resolve/src/imports.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,24 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
326326
})
327327
}
328328

329+
// pub(crate) fn try_define(
330+
// &mut self,
331+
// module: Module<'ra>,
332+
// key: BindingKey,
333+
// binding: NameBinding<'ra>,
334+
// warn_ambiguity: bool,
335+
// ) -> Result<(), NameBinding<'ra>> {
336+
// if module.is_local_module() {
337+
// self.try_define_local(module, key, binding, warn_ambiguity)
338+
// } else {
339+
// self.define_extern(module, key, binding);
340+
// Ok(())
341+
// }
342+
// }
343+
329344
/// Define the name or return the existing binding if there is a collision.
330345
/// `update` indicates if the definition is a redefinition of an existing binding.
331-
pub(crate) fn try_define(
346+
pub(crate) fn try_define_local(
332347
&mut self,
333348
module: Module<'ra>,
334349
key: BindingKey,
@@ -477,7 +492,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
477492
if self.is_accessible_from(binding.vis, scope) {
478493
let imported_binding = self.import(binding, *import);
479494
let key = BindingKey { ident, ..key };
480-
let _ = self.try_define(
495+
// FIXME: local or external?
496+
let _ = self.try_define_local(
481497
import.parent_scope.module,
482498
key,
483499
imported_binding,
@@ -501,7 +517,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
501517
let dummy_binding = self.import(dummy_binding, import);
502518
self.per_ns(|this, ns| {
503519
let key = BindingKey::new(target, ns);
504-
let _ = this.try_define(import.parent_scope.module, key, dummy_binding, false);
520+
let _ =
521+
this.try_define_local(import.parent_scope.module, key, dummy_binding, false);
505522
this.update_resolution(import.parent_scope.module, key, false, |_, resolution| {
506523
resolution.single_imports.swap_remove(&import);
507524
})
@@ -866,7 +883,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
866883

867884
let imported_binding = this.import(binding, import);
868885
target_bindings[ns].set(Some(imported_binding));
869-
this.define_binding(parent, target, ns, imported_binding);
886+
this.define_binding_local(parent, target, ns, imported_binding);
870887
}
871888
Err(Determined) => {
872889
// Don't update the resolution for underscores, because it was never added.
@@ -1484,7 +1501,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14841501
.borrow()
14851502
.binding()
14861503
.is_some_and(|binding| binding.warn_ambiguity_recursive());
1487-
let _ = self.try_define(
1504+
let _ = self.try_define_local(
14881505
import.parent_scope.module,
14891506
key,
14901507
imported_binding,

compiler/rustc_resolve/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,7 @@ pub struct Resolver<'ra, 'tcx> {
10781078
extern_module_map: RefCell<FxIndexMap<DefId, Module<'ra>>>,
10791079
binding_parent_modules: FxHashMap<NameBinding<'ra>, Module<'ra>>,
10801080

1081-
underscore_disambiguator: u32,
1081+
underscore_disambiguator: Cell<u32>,
10821082

10831083
/// Maps glob imports to the names of items actually imported.
10841084
glob_map: FxIndexMap<LocalDefId, FxIndexSet<Symbol>>,
@@ -1491,7 +1491,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14911491
extern_crate_map: Default::default(),
14921492
module_children: Default::default(),
14931493
trait_map: NodeMap::default(),
1494-
underscore_disambiguator: 0,
1494+
underscore_disambiguator: Cell::new(0),
14951495
empty_module,
14961496
local_module_map,
14971497
extern_module_map: Default::default(),
@@ -1879,18 +1879,18 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18791879
import_ids
18801880
}
18811881

1882-
fn new_disambiguated_key(&mut self, ident: Ident, ns: Namespace) -> BindingKey {
1882+
fn new_disambiguated_key(&self, ident: Ident, ns: Namespace) -> BindingKey {
18831883
let ident = ident.normalize_to_macros_2_0();
18841884
let disambiguator = if ident.name == kw::Underscore {
1885-
self.underscore_disambiguator += 1;
1886-
self.underscore_disambiguator
1885+
self.underscore_disambiguator.update(|x| x + 1);
1886+
self.underscore_disambiguator.get()
18871887
} else {
18881888
0
18891889
};
18901890
BindingKey { ident, ns, disambiguator }
18911891
}
18921892

1893-
fn resolutions(&mut self, module: Module<'ra>) -> &'ra Resolutions<'ra> {
1893+
fn resolutions(&self, module: Module<'ra>) -> &'ra Resolutions<'ra> {
18941894
if module.populate_on_access.get() {
18951895
module.populate_on_access.set(false);
18961896
self.build_reduced_graph_external(module);
@@ -1899,7 +1899,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18991899
}
19001900

19011901
fn resolution(
1902-
&mut self,
1902+
&self,
19031903
module: Module<'ra>,
19041904
key: BindingKey,
19051905
) -> &'ra RefCell<NameResolution<'ra>> {

0 commit comments

Comments
 (0)