Skip to content

Commit 30306fd

Browse files
split up define into define_extern and define_local
1 parent 8231065 commit 30306fd

File tree

3 files changed

+67
-30
lines changed

3 files changed

+67
-30
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,19 +42,19 @@ 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
) {
52-
if let Err(old_binding) = self.try_define(parent, ident, ns, binding, false) {
52+
if let Err(old_binding) = self.try_define_local(parent, key, binding, false) {
5353
self.report_conflict(parent, ident, ns, old_binding, binding);
5454
}
5555
}
5656

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

7193
/// Walks up the tree of definitions starting at `def_id`,
@@ -188,7 +210,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
188210
visitor.parent_scope.macro_rules
189211
}
190212

191-
pub(crate) fn build_reduced_graph_external(&mut self, module: Module<'ra>) {
213+
pub(crate) fn build_reduced_graph_external(&self, module: Module<'ra>) {
192214
for child in self.tcx.module_children(module.def_id()) {
193215
let parent_scope = ParentScope::module(module, self);
194216
self.build_reduced_graph_for_external_crate_res(child, parent_scope)
@@ -197,7 +219,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
197219

198220
/// Builds the reduced graph for a single item in an external crate.
199221
fn build_reduced_graph_for_external_crate_res(
200-
&mut self,
222+
&self,
201223
child: &ModChild,
202224
parent_scope: ParentScope<'ra>,
203225
) {
@@ -228,7 +250,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
228250
_,
229251
)
230252
| Res::PrimTy(..)
231-
| Res::ToolMod => self.define(parent, ident, TypeNS, res, vis, span, expansion),
253+
| Res::ToolMod => self.define_extern(parent, ident, TypeNS, res, vis, span, expansion),
232254
Res::Def(
233255
DefKind::Fn
234256
| DefKind::AssocFn
@@ -237,9 +259,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
237259
| DefKind::AssocConst
238260
| DefKind::Ctor(..),
239261
_,
240-
) => self.define(parent, ident, ValueNS, res, vis, span, expansion),
262+
) => self.define_extern(parent, ident, ValueNS, res, vis, span, expansion),
241263
Res::Def(DefKind::Macro(..), _) | Res::NonMacroAttr(..) => {
242-
self.define(parent, ident, MacroNS, res, vis, span, expansion)
264+
self.define_extern(parent, ident, MacroNS, res, vis, span, expansion)
243265
}
244266
Res::Def(
245267
DefKind::TyParam
@@ -705,7 +727,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
705727
let expansion = parent_scope.expansion;
706728

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

@@ -757,7 +779,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
757779
}
758780

759781
ItemKind::Mod(_, ident, ref mod_kind) => {
760-
self.r.define(parent, ident, TypeNS, res, vis, sp, expansion);
782+
self.r.define_local(parent, ident, TypeNS, res, vis, sp, expansion);
761783

762784
if let ast::ModKind::Loaded(_, _, _, Err(_)) = mod_kind {
763785
self.r.mods_with_parse_errors.insert(def_id);
@@ -776,10 +798,10 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
776798
ItemKind::Const(box ConstItem { ident, .. })
777799
| ItemKind::Delegation(box Delegation { ident, .. })
778800
| ItemKind::Static(box StaticItem { ident, .. }) => {
779-
self.r.define(parent, ident, ValueNS, res, vis, sp, expansion);
801+
self.r.define_local(parent, ident, ValueNS, res, vis, sp, expansion);
780802
}
781803
ItemKind::Fn(box Fn { ident, .. }) => {
782-
self.r.define(parent, ident, ValueNS, res, vis, sp, expansion);
804+
self.r.define_local(parent, ident, ValueNS, res, vis, sp, expansion);
783805

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

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

794816
ItemKind::Enum(ident, _, _) | ItemKind::Trait(box ast::Trait { ident, .. }) => {
795-
self.r.define(parent, ident, TypeNS, res, vis, sp, expansion);
817+
self.r.define_local(parent, ident, TypeNS, res, vis, sp, expansion);
796818

797819
self.parent_scope.module = self.r.new_local_module(
798820
Some(parent),
@@ -844,7 +866,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
844866
let feed = self.r.feed(ctor_node_id);
845867
let ctor_def_id = feed.key();
846868
let ctor_res = self.res(ctor_def_id);
847-
self.r.define(parent, ident, ValueNS, ctor_res, ctor_vis, sp, expansion);
869+
self.r.define_local(parent, ident, ValueNS, ctor_res, ctor_vis, sp, expansion);
848870
self.r.feed_visibility(feed, ctor_vis);
849871
// We need the field visibility spans also for the constructor for E0603.
850872
self.insert_field_visibilities_local(ctor_def_id.to_def_id(), vdata.fields());
@@ -965,7 +987,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
965987
);
966988
}
967989
}
968-
self.r.define_binding(parent, ident, TypeNS, imported_binding);
990+
self.r.define_binding_local(parent, ident, TypeNS, imported_binding);
969991
}
970992

971993
/// Constructs the reduced graph for one foreign item.
@@ -982,7 +1004,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
9821004
let parent = self.parent_scope.module;
9831005
let expansion = self.parent_scope.expansion;
9841006
let vis = self.resolve_visibility(&item.vis);
985-
self.r.define(parent, ident, ns, self.res(def_id), vis, item.span, expansion);
1007+
self.r.define_local(parent, ident, ns, self.res(def_id), vis, item.span, expansion);
9861008
self.r.feed_visibility(feed, vis);
9871009
}
9881010

@@ -1241,7 +1263,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12411263
});
12421264
self.r.import_use_map.insert(import, Used::Other);
12431265
let import_binding = self.r.import(binding, import);
1244-
self.r.define_binding(self.r.graph_root, ident, MacroNS, import_binding);
1266+
self.r.define_binding_local(self.r.graph_root, ident, MacroNS, import_binding);
12451267
} else {
12461268
self.r.check_reserved_macro_name(ident, res);
12471269
self.insert_unused_macro(ident, def_id, item.id);
@@ -1269,7 +1291,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12691291
if !vis.is_public() {
12701292
self.insert_unused_macro(ident, def_id, item.id);
12711293
}
1272-
self.r.define(module, ident, MacroNS, res, vis, span, expansion);
1294+
self.r.define_local(module, ident, MacroNS, res, vis, span, expansion);
12731295
self.r.feed_visibility(feed, vis);
12741296
self.parent_scope.macro_rules
12751297
}
@@ -1405,7 +1427,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
14051427
if ctxt == AssocCtxt::Trait {
14061428
let parent = self.parent_scope.module;
14071429
let expansion = self.parent_scope.expansion;
1408-
self.r.define(parent, ident, ns, self.res(def_id), vis, item.span, expansion);
1430+
self.r.define_local(parent, ident, ns, self.res(def_id), vis, item.span, expansion);
14091431
} else if !matches!(&item.kind, AssocItemKind::Delegation(deleg) if deleg.from_glob)
14101432
&& ident.name != kw::Underscore
14111433
{
@@ -1493,7 +1515,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
14931515
let feed = self.r.feed(variant.id);
14941516
let def_id = feed.key();
14951517
let vis = self.resolve_visibility(&variant.vis);
1496-
self.r.define(parent, ident, TypeNS, self.res(def_id), vis, variant.span, expn_id);
1518+
self.r.define_local(parent, ident, TypeNS, self.res(def_id), vis, variant.span, expn_id);
14971519
self.r.feed_visibility(feed, vis);
14981520

14991521
// If the variant is marked as non_exhaustive then lower the visibility to within the crate.
@@ -1509,7 +1531,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
15091531
let feed = self.r.feed(ctor_node_id);
15101532
let ctor_def_id = feed.key();
15111533
let ctor_res = self.res(ctor_def_id);
1512-
self.r.define(parent, ident, ValueNS, ctor_res, ctor_vis, variant.span, expn_id);
1534+
self.r.define_local(parent, ident, ValueNS, ctor_res, ctor_vis, variant.span, expn_id);
15131535
self.r.feed_visibility(feed, ctor_vis);
15141536
}
15151537

compiler/rustc_resolve/src/imports.rs

Lines changed: 20 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
ident: Ident,
@@ -496,7 +511,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
496511
};
497512
if self.is_accessible_from(binding.vis, scope) {
498513
let imported_binding = self.import(binding, *import);
499-
let _ = self.try_define(
514+
let _ = self.try_define_local(
500515
import.parent_scope.module,
501516
ident,
502517
key.ns,
@@ -522,7 +537,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
522537
let dummy_binding = self.import(dummy_binding, import);
523538
self.per_ns(|this, ns| {
524539
let module = import.parent_scope.module;
525-
let _ = this.try_define(module, target, ns, dummy_binding, false);
540+
let _ = this.try_define_local(module, target, ns, dummy_binding, false);
526541
// Don't remove underscores from `single_imports`, they were never added.
527542
if target.name != kw::Underscore {
528543
let key = BindingKey::new(target, ns);
@@ -902,7 +917,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
902917
}
903918
// We need the `target`, `source` can be extracted.
904919
let imported_binding = this.import(binding, import);
905-
this.define_binding(parent, target, ns, imported_binding);
920+
this.define_binding_local(parent, target, ns, imported_binding);
906921
PendingBinding::Ready(Some(imported_binding))
907922
}
908923
Err(Determinacy::Determined) => {
@@ -1519,7 +1534,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15191534
.borrow()
15201535
.binding()
15211536
.is_some_and(|binding| binding.warn_ambiguity_recursive());
1522-
let _ = self.try_define(
1537+
let _ = self.try_define_local(
15231538
import.parent_scope.module,
15241539
key.ident,
15251540
key.ns,

compiler/rustc_resolve/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,7 +1895,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18951895
import_ids
18961896
}
18971897

1898-
fn resolutions(&mut self, module: Module<'ra>) -> &'ra Resolutions<'ra> {
1898+
fn resolutions(&self, module: Module<'ra>) -> &'ra Resolutions<'ra> {
18991899
if module.populate_on_access.get() {
19001900
module.populate_on_access.set(false);
19011901
self.build_reduced_graph_external(module);
@@ -1904,7 +1904,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
19041904
}
19051905

19061906
fn resolution(
1907-
&mut self,
1907+
&self,
19081908
module: Module<'ra>,
19091909
key: BindingKey,
19101910
) -> &'ra RefCell<NameResolution<'ra>> {

0 commit comments

Comments
 (0)