Skip to content

Commit 71d7337

Browse files
split up define into define_extern and define_local
1 parent bf5e6cc commit 71d7337

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
@@ -704,7 +726,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
704726
let expansion = parent_scope.expansion;
705727

706728
// Define a name in the type namespace if it is not anonymous.
707-
self.r.define(parent, ident, TypeNS, adt_res, adt_vis, adt_span, expansion);
729+
self.r.define_local(parent, ident, TypeNS, adt_res, adt_vis, adt_span, expansion);
708730
self.r.feed_visibility(feed, adt_vis);
709731
let def_id = feed.key();
710732

@@ -756,7 +778,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
756778
}
757779

758780
ItemKind::Mod(_, ident, ref mod_kind) => {
759-
self.r.define(parent, ident, TypeNS, res, vis, sp, expansion);
781+
self.r.define_local(parent, ident, TypeNS, res, vis, sp, expansion);
760782

761783
if let ast::ModKind::Loaded(_, _, _, Err(_)) = mod_kind {
762784
self.r.mods_with_parse_errors.insert(def_id);
@@ -775,10 +797,10 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
775797
ItemKind::Const(box ConstItem { ident, .. })
776798
| ItemKind::Delegation(box Delegation { ident, .. })
777799
| ItemKind::Static(box StaticItem { ident, .. }) => {
778-
self.r.define(parent, ident, ValueNS, res, vis, sp, expansion);
800+
self.r.define_local(parent, ident, ValueNS, res, vis, sp, expansion);
779801
}
780802
ItemKind::Fn(box Fn { ident, .. }) => {
781-
self.r.define(parent, ident, ValueNS, res, vis, sp, expansion);
803+
self.r.define_local(parent, ident, ValueNS, res, vis, sp, expansion);
782804

783805
// Functions introducing procedural macros reserve a slot
784806
// in the macro namespace as well (see #52225).
@@ -787,11 +809,11 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
787809

788810
// These items live in the type namespace.
789811
ItemKind::TyAlias(box TyAlias { ident, .. }) | ItemKind::TraitAlias(ident, ..) => {
790-
self.r.define(parent, ident, TypeNS, res, vis, sp, expansion);
812+
self.r.define_local(parent, ident, TypeNS, res, vis, sp, expansion);
791813
}
792814

793815
ItemKind::Enum(ident, _, _) | ItemKind::Trait(box ast::Trait { ident, .. }) => {
794-
self.r.define(parent, ident, TypeNS, res, vis, sp, expansion);
816+
self.r.define_local(parent, ident, TypeNS, res, vis, sp, expansion);
795817

796818
self.parent_scope.module = self.r.new_local_module(
797819
Some(parent),
@@ -843,7 +865,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
843865
let feed = self.r.feed(ctor_node_id);
844866
let ctor_def_id = feed.key();
845867
let ctor_res = self.res(ctor_def_id);
846-
self.r.define(parent, ident, ValueNS, ctor_res, ctor_vis, sp, expansion);
868+
self.r.define_local(parent, ident, ValueNS, ctor_res, ctor_vis, sp, expansion);
847869
self.r.feed_visibility(feed, ctor_vis);
848870
// We need the field visibility spans also for the constructor for E0603.
849871
self.insert_field_visibilities_local(ctor_def_id.to_def_id(), vdata.fields());
@@ -964,7 +986,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
964986
);
965987
}
966988
}
967-
self.r.define_binding(parent, ident, TypeNS, imported_binding);
989+
self.r.define_binding_local(parent, ident, TypeNS, imported_binding);
968990
}
969991

970992
/// Constructs the reduced graph for one foreign item.
@@ -981,7 +1003,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
9811003
let parent = self.parent_scope.module;
9821004
let expansion = self.parent_scope.expansion;
9831005
let vis = self.resolve_visibility(&item.vis);
984-
self.r.define(parent, ident, ns, self.res(def_id), vis, item.span, expansion);
1006+
self.r.define_local(parent, ident, ns, self.res(def_id), vis, item.span, expansion);
9851007
self.r.feed_visibility(feed, vis);
9861008
}
9871009

@@ -1240,7 +1262,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12401262
});
12411263
self.r.import_use_map.insert(import, Used::Other);
12421264
let import_binding = self.r.import(binding, import);
1243-
self.r.define_binding(self.r.graph_root, ident, MacroNS, import_binding);
1265+
self.r.define_binding_local(self.r.graph_root, ident, MacroNS, import_binding);
12441266
} else {
12451267
self.r.check_reserved_macro_name(ident, res);
12461268
self.insert_unused_macro(ident, def_id, item.id);
@@ -1268,7 +1290,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12681290
if !vis.is_public() {
12691291
self.insert_unused_macro(ident, def_id, item.id);
12701292
}
1271-
self.r.define(module, ident, MacroNS, res, vis, span, expansion);
1293+
self.r.define_local(module, ident, MacroNS, res, vis, span, expansion);
12721294
self.r.feed_visibility(feed, vis);
12731295
self.parent_scope.macro_rules
12741296
}
@@ -1404,7 +1426,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
14041426
if ctxt == AssocCtxt::Trait {
14051427
let parent = self.parent_scope.module;
14061428
let expansion = self.parent_scope.expansion;
1407-
self.r.define(parent, ident, ns, self.res(def_id), vis, item.span, expansion);
1429+
self.r.define_local(parent, ident, ns, self.res(def_id), vis, item.span, expansion);
14081430
} else if !matches!(&item.kind, AssocItemKind::Delegation(deleg) if deleg.from_glob) {
14091431
let impl_def_id = self.r.tcx.local_parent(local_def_id);
14101432
let key = BindingKey::new(ident.normalize_to_macros_2_0(), ns);
@@ -1489,7 +1511,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
14891511
let feed = self.r.feed(variant.id);
14901512
let def_id = feed.key();
14911513
let vis = self.resolve_visibility(&variant.vis);
1492-
self.r.define(parent, ident, TypeNS, self.res(def_id), vis, variant.span, expn_id);
1514+
self.r.define_local(parent, ident, TypeNS, self.res(def_id), vis, variant.span, expn_id);
14931515
self.r.feed_visibility(feed, vis);
14941516

14951517
// If the variant is marked as non_exhaustive then lower the visibility to within the crate.
@@ -1505,7 +1527,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
15051527
let feed = self.r.feed(ctor_node_id);
15061528
let ctor_def_id = feed.key();
15071529
let ctor_res = self.res(ctor_def_id);
1508-
self.r.define(parent, ident, ValueNS, ctor_res, ctor_vis, variant.span, expn_id);
1530+
self.r.define_local(parent, ident, ValueNS, ctor_res, ctor_vis, variant.span, expn_id);
15091531
self.r.feed_visibility(feed, ctor_vis);
15101532
}
15111533

compiler/rustc_resolve/src/imports.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,24 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
333333
})
334334
}
335335

336+
// pub(crate) fn try_define(
337+
// &mut self,
338+
// module: Module<'ra>,
339+
// key: BindingKey,
340+
// binding: NameBinding<'ra>,
341+
// warn_ambiguity: bool,
342+
// ) -> Result<(), NameBinding<'ra>> {
343+
// if module.is_local_module() {
344+
// self.try_define_local(module, key, binding, warn_ambiguity)
345+
// } else {
346+
// self.define_extern(module, key, binding);
347+
// Ok(())
348+
// }
349+
// }
350+
336351
/// Define the name or return the existing binding if there is a collision.
337352
/// `update` indicates if the definition is a redefinition of an existing binding.
338-
pub(crate) fn try_define(
353+
pub(crate) fn try_define_local(
339354
&mut self,
340355
module: Module<'ra>,
341356
key: BindingKey,
@@ -490,7 +505,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
490505
if self.is_accessible_from(binding.vis, scope) {
491506
let imported_binding = self.import(binding, *import);
492507
let key = BindingKey { ident, ..key };
493-
let _ = self.try_define(
508+
// FIXME: local or external?
509+
let _ = self.try_define_local(
494510
import.parent_scope.module,
495511
key,
496512
imported_binding,
@@ -515,7 +531,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
515531
let dummy_binding = self.import(dummy_binding, import);
516532
self.per_ns(|this, ns| {
517533
let key = BindingKey::new(target, ns);
518-
let _ = this.try_define(import.parent_scope.module, key, dummy_binding, false);
534+
let _ =
535+
this.try_define_local(import.parent_scope.module, key, dummy_binding, false);
519536
this.update_resolution(import.parent_scope.module, key, false, |_, resolution| {
520537
resolution.single_imports.swap_remove(&import);
521538
})
@@ -891,7 +908,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
891908
}
892909
// We need the `target`, `source` can be extracted.
893910
let imported_binding = this.import(binding, import);
894-
this.define_binding(parent, target, ns, imported_binding);
911+
this.define_binding_local(parent, target, ns, imported_binding);
895912
PendingBinding::Ready(Some(imported_binding))
896913
}
897914
Err(Determinacy::Determined) => {
@@ -1508,7 +1525,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15081525
.borrow()
15091526
.binding()
15101527
.is_some_and(|binding| binding.warn_ambiguity_recursive());
1511-
let _ = self.try_define(
1528+
let _ = self.try_define_local(
15121529
import.parent_scope.module,
15131530
key,
15141531
imported_binding,

compiler/rustc_resolve/src/lib.rs

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

1090-
underscore_disambiguator: u32,
1090+
underscore_disambiguator: Cell<u32>,
10911091

10921092
/// Maps glob imports to the names of items actually imported.
10931093
glob_map: FxIndexMap<LocalDefId, FxIndexSet<Symbol>>,
@@ -1501,7 +1501,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15011501
extern_crate_map: Default::default(),
15021502
module_children: Default::default(),
15031503
trait_map: NodeMap::default(),
1504-
underscore_disambiguator: 0,
1504+
underscore_disambiguator: Cell::new(0),
15051505
empty_module,
15061506
local_module_map,
15071507
extern_module_map: Default::default(),
@@ -1887,18 +1887,18 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18871887
import_ids
18881888
}
18891889

1890-
fn new_disambiguated_key(&mut self, ident: Ident, ns: Namespace) -> BindingKey {
1890+
fn new_disambiguated_key(&self, ident: Ident, ns: Namespace) -> BindingKey {
18911891
let ident = ident.normalize_to_macros_2_0();
18921892
let disambiguator = if ident.name == kw::Underscore {
1893-
self.underscore_disambiguator += 1;
1894-
self.underscore_disambiguator
1893+
self.underscore_disambiguator.update(|x| x + 1);
1894+
self.underscore_disambiguator.get()
18951895
} else {
18961896
0
18971897
};
18981898
BindingKey { ident, ns, disambiguator }
18991899
}
19001900

1901-
fn resolutions(&mut self, module: Module<'ra>) -> &'ra Resolutions<'ra> {
1901+
fn resolutions(&self, module: Module<'ra>) -> &'ra Resolutions<'ra> {
19021902
if module.populate_on_access.get() {
19031903
module.populate_on_access.set(false);
19041904
self.build_reduced_graph_external(module);
@@ -1907,7 +1907,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
19071907
}
19081908

19091909
fn resolution(
1910-
&mut self,
1910+
&self,
19111911
module: Module<'ra>,
19121912
key: BindingKey,
19131913
) -> &'ra RefCell<NameResolution<'ra>> {

0 commit comments

Comments
 (0)