Skip to content

Commit 82f7da2

Browse files
committed
Auto merge of #143884 - LorrensP-2158466:resolve-split-define, r=petrochenkov
Resolve: refactor `define` into `define_local` and `define_extern` Follow up on #143550 and part of [#gsoc > Project: Parallel Macro Expansion](https://rust-lang.zulipchat.com/#narrow/channel/421156-gsoc/topic/Project.3A.20Parallel.20Macro.20Expansion/with/527348747). Split up `define` into `define_local` and `define_extern`. Refactor usages of `define` into either one where it's "correct" (idk if everything is correct atm). Big part of this is that `resolution` can now take a `&Resolver` instead of a mutable one. r? `@petrochenkov`
2 parents 052114f + e889081 commit 82f7da2

File tree

7 files changed

+98
-56
lines changed

7 files changed

+98
-56
lines changed

compiler/rustc_expand/src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,7 @@ pub trait ResolverExpand {
11411141

11421142
/// Names of specific methods to which glob delegation expands.
11431143
fn glob_delegation_suffixes(
1144-
&mut self,
1144+
&self,
11451145
trait_def_id: DefId,
11461146
impl_def_id: LocalDefId,
11471147
) -> Result<Vec<(Ident, Option<Ident>)>, Indeterminate>;

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ use rustc_hir::def::{self, *};
2323
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
2424
use rustc_index::bit_set::DenseBitSet;
2525
use rustc_metadata::creader::LoadedMacro;
26-
use rustc_middle::bug;
2726
use rustc_middle::metadata::ModChild;
2827
use rustc_middle::ty::{Feed, Visibility};
28+
use rustc_middle::{bug, span_bug};
2929
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind};
3030
use rustc_span::{Ident, Span, Symbol, kw, sym};
3131
use thin_vec::ThinVec;
@@ -46,30 +46,53 @@ type Res = def::Res<NodeId>;
4646
impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
4747
/// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined;
4848
/// otherwise, reports an error.
49-
pub(crate) fn define_binding(
49+
pub(crate) fn define_binding_local(
5050
&mut self,
5151
parent: Module<'ra>,
5252
ident: Ident,
5353
ns: Namespace,
5454
binding: NameBinding<'ra>,
5555
) {
56-
if let Err(old_binding) = self.try_define(parent, ident, ns, binding, false) {
56+
if let Err(old_binding) = self.try_define_local(parent, ident, ns, binding, false) {
5757
self.report_conflict(parent, ident, ns, old_binding, binding);
5858
}
5959
}
6060

61-
fn define(
61+
fn define_local(
6262
&mut self,
6363
parent: Module<'ra>,
6464
ident: Ident,
6565
ns: Namespace,
6666
res: Res,
67-
vis: Visibility<impl Into<DefId>>,
67+
vis: Visibility,
6868
span: Span,
6969
expn_id: LocalExpnId,
7070
) {
7171
let binding = self.arenas.new_res_binding(res, vis.to_def_id(), span, expn_id);
72-
self.define_binding(parent, ident, ns, binding)
72+
self.define_binding_local(parent, ident, ns, binding);
73+
}
74+
75+
fn define_extern(
76+
&self,
77+
parent: Module<'ra>,
78+
ident: Ident,
79+
ns: Namespace,
80+
res: Res,
81+
vis: Visibility<DefId>,
82+
span: Span,
83+
expn_id: LocalExpnId,
84+
) {
85+
let binding = self.arenas.new_res_binding(res, vis, span, expn_id);
86+
// Even if underscore names cannot be looked up, we still need to add them to modules,
87+
// because they can be fetched by glob imports from those modules, and bring traits
88+
// into scope both directly and through glob imports.
89+
let key = BindingKey::new_disambiguated(ident, ns, || {
90+
parent.underscore_disambiguator.update(|d| d + 1);
91+
parent.underscore_disambiguator.get()
92+
});
93+
if self.resolution(parent, key).borrow_mut().non_glob_binding.replace(binding).is_some() {
94+
span_bug!(span, "an external binding was already defined");
95+
}
7396
}
7497

7598
/// Walks up the tree of definitions starting at `def_id`,
@@ -192,7 +215,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
192215
visitor.parent_scope.macro_rules
193216
}
194217

195-
pub(crate) fn build_reduced_graph_external(&mut self, module: Module<'ra>) {
218+
pub(crate) fn build_reduced_graph_external(&self, module: Module<'ra>) {
196219
for child in self.tcx.module_children(module.def_id()) {
197220
let parent_scope = ParentScope::module(module, self);
198221
self.build_reduced_graph_for_external_crate_res(child, parent_scope)
@@ -201,7 +224,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
201224

202225
/// Builds the reduced graph for a single item in an external crate.
203226
fn build_reduced_graph_for_external_crate_res(
204-
&mut self,
227+
&self,
205228
child: &ModChild,
206229
parent_scope: ParentScope<'ra>,
207230
) {
@@ -232,7 +255,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
232255
_,
233256
)
234257
| Res::PrimTy(..)
235-
| Res::ToolMod => self.define(parent, ident, TypeNS, res, vis, span, expansion),
258+
| Res::ToolMod => self.define_extern(parent, ident, TypeNS, res, vis, span, expansion),
236259
Res::Def(
237260
DefKind::Fn
238261
| DefKind::AssocFn
@@ -241,9 +264,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
241264
| DefKind::AssocConst
242265
| DefKind::Ctor(..),
243266
_,
244-
) => self.define(parent, ident, ValueNS, res, vis, span, expansion),
267+
) => self.define_extern(parent, ident, ValueNS, res, vis, span, expansion),
245268
Res::Def(DefKind::Macro(..), _) | Res::NonMacroAttr(..) => {
246-
self.define(parent, ident, MacroNS, res, vis, span, expansion)
269+
self.define_extern(parent, ident, MacroNS, res, vis, span, expansion)
247270
}
248271
Res::Def(
249272
DefKind::TyParam
@@ -711,7 +734,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
711734
let expansion = parent_scope.expansion;
712735

713736
// Define a name in the type namespace if it is not anonymous.
714-
self.r.define(parent, ident, TypeNS, adt_res, adt_vis, adt_span, expansion);
737+
self.r.define_local(parent, ident, TypeNS, adt_res, adt_vis, adt_span, expansion);
715738
self.r.feed_visibility(feed, adt_vis);
716739
let def_id = feed.key();
717740

@@ -763,7 +786,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
763786
}
764787

765788
ItemKind::Mod(_, ident, ref mod_kind) => {
766-
self.r.define(parent, ident, TypeNS, res, vis, sp, expansion);
789+
self.r.define_local(parent, ident, TypeNS, res, vis, sp, expansion);
767790

768791
if let ast::ModKind::Loaded(_, _, _, Err(_)) = mod_kind {
769792
self.r.mods_with_parse_errors.insert(def_id);
@@ -782,10 +805,10 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
782805
ItemKind::Const(box ConstItem { ident, .. })
783806
| ItemKind::Delegation(box Delegation { ident, .. })
784807
| ItemKind::Static(box StaticItem { ident, .. }) => {
785-
self.r.define(parent, ident, ValueNS, res, vis, sp, expansion);
808+
self.r.define_local(parent, ident, ValueNS, res, vis, sp, expansion);
786809
}
787810
ItemKind::Fn(box Fn { ident, .. }) => {
788-
self.r.define(parent, ident, ValueNS, res, vis, sp, expansion);
811+
self.r.define_local(parent, ident, ValueNS, res, vis, sp, expansion);
789812

790813
// Functions introducing procedural macros reserve a slot
791814
// in the macro namespace as well (see #52225).
@@ -794,11 +817,11 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
794817

795818
// These items live in the type namespace.
796819
ItemKind::TyAlias(box TyAlias { ident, .. }) | ItemKind::TraitAlias(ident, ..) => {
797-
self.r.define(parent, ident, TypeNS, res, vis, sp, expansion);
820+
self.r.define_local(parent, ident, TypeNS, res, vis, sp, expansion);
798821
}
799822

800823
ItemKind::Enum(ident, _, _) | ItemKind::Trait(box ast::Trait { ident, .. }) => {
801-
self.r.define(parent, ident, TypeNS, res, vis, sp, expansion);
824+
self.r.define_local(parent, ident, TypeNS, res, vis, sp, expansion);
802825

803826
self.parent_scope.module = self.r.new_local_module(
804827
Some(parent),
@@ -850,7 +873,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
850873
let feed = self.r.feed(ctor_node_id);
851874
let ctor_def_id = feed.key();
852875
let ctor_res = self.res(ctor_def_id);
853-
self.r.define(parent, ident, ValueNS, ctor_res, ctor_vis, sp, expansion);
876+
self.r.define_local(parent, ident, ValueNS, ctor_res, ctor_vis, sp, expansion);
854877
self.r.feed_visibility(feed, ctor_vis);
855878
// We need the field visibility spans also for the constructor for E0603.
856879
self.insert_field_visibilities_local(ctor_def_id.to_def_id(), vdata.fields());
@@ -974,7 +997,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
974997
);
975998
}
976999
}
977-
self.r.define_binding(parent, ident, TypeNS, imported_binding);
1000+
self.r.define_binding_local(parent, ident, TypeNS, imported_binding);
9781001
}
9791002

9801003
/// Constructs the reduced graph for one foreign item.
@@ -991,7 +1014,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
9911014
let parent = self.parent_scope.module;
9921015
let expansion = self.parent_scope.expansion;
9931016
let vis = self.resolve_visibility(&item.vis);
994-
self.r.define(parent, ident, ns, self.res(def_id), vis, item.span, expansion);
1017+
self.r.define_local(parent, ident, ns, self.res(def_id), vis, item.span, expansion);
9951018
self.r.feed_visibility(feed, vis);
9961019
}
9971020

@@ -1074,7 +1097,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10741097
if let Some(span) = import_all {
10751098
let import = macro_use_import(self, span, false);
10761099
self.r.potentially_unused_imports.push(import);
1077-
module.for_each_child(self, |this, ident, ns, binding| {
1100+
module.for_each_child_mut(self, |this, ident, ns, binding| {
10781101
if ns == MacroNS {
10791102
let import = if this.r.is_accessible_from(binding.vis, this.parent_scope.module)
10801103
{
@@ -1239,7 +1262,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12391262
});
12401263
self.r.import_use_map.insert(import, Used::Other);
12411264
let import_binding = self.r.import(binding, import);
1242-
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);
12431266
} else {
12441267
self.r.check_reserved_macro_name(ident, res);
12451268
self.insert_unused_macro(ident, def_id, item.id);
@@ -1267,7 +1290,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12671290
if !vis.is_public() {
12681291
self.insert_unused_macro(ident, def_id, item.id);
12691292
}
1270-
self.r.define(module, ident, MacroNS, res, vis, span, expansion);
1293+
self.r.define_local(module, ident, MacroNS, res, vis, span, expansion);
12711294
self.r.feed_visibility(feed, vis);
12721295
self.parent_scope.macro_rules
12731296
}
@@ -1403,7 +1426,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
14031426
if ctxt == AssocCtxt::Trait {
14041427
let parent = self.parent_scope.module;
14051428
let expansion = self.parent_scope.expansion;
1406-
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);
14071430
} else if !matches!(&item.kind, AssocItemKind::Delegation(deleg) if deleg.from_glob)
14081431
&& ident.name != kw::Underscore
14091432
{
@@ -1491,7 +1514,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
14911514
let feed = self.r.feed(variant.id);
14921515
let def_id = feed.key();
14931516
let vis = self.resolve_visibility(&variant.vis);
1494-
self.r.define(parent, ident, TypeNS, self.res(def_id), vis, variant.span, expn_id);
1517+
self.r.define_local(parent, ident, TypeNS, self.res(def_id), vis, variant.span, expn_id);
14951518
self.r.feed_visibility(feed, vis);
14961519

14971520
// If the variant is marked as non_exhaustive then lower the visibility to within the crate.
@@ -1507,7 +1530,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
15071530
let feed = self.r.feed(ctor_node_id);
15081531
let ctor_def_id = feed.key();
15091532
let ctor_res = self.res(ctor_def_id);
1510-
self.r.define(parent, ident, ValueNS, ctor_res, ctor_vis, variant.span, expn_id);
1533+
self.r.define_local(parent, ident, ValueNS, ctor_res, ctor_vis, variant.span, expn_id);
15111534
self.r.feed_visibility(feed, ctor_vis);
15121535
}
15131536

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
523523
}
524524

525525
pub(crate) fn add_module_candidates(
526-
&mut self,
526+
&self,
527527
module: Module<'ra>,
528528
names: &mut Vec<TypoSuggestion>,
529529
filter_fn: &impl Fn(Res) -> bool,
@@ -1150,7 +1150,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11501150
}
11511151

11521152
fn lookup_import_candidates_from_module<FilterFn>(
1153-
&mut self,
1153+
&self,
11541154
lookup_ident: Ident,
11551155
namespace: Namespace,
11561156
parent_scope: &ParentScope<'ra>,

compiler/rustc_resolve/src/imports.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
334334
}
335335

336336
/// Define the name or return the existing binding if there is a collision.
337-
/// `update` indicates if the definition is a redefinition of an existing binding.
338-
pub(crate) fn try_define(
337+
pub(crate) fn try_define_local(
339338
&mut self,
340339
module: Module<'ra>,
341340
ident: Ident,
@@ -353,7 +352,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
353352
module.underscore_disambiguator.update(|d| d + 1);
354353
module.underscore_disambiguator.get()
355354
});
356-
self.update_resolution(module, key, warn_ambiguity, |this, resolution| {
355+
self.update_local_resolution(module, key, warn_ambiguity, |this, resolution| {
357356
if let Some(old_binding) = resolution.best_binding() {
358357
if res == Res::Err && old_binding.res() != Res::Err {
359358
// Do not override real bindings with `Res::Err`s from error recovery.
@@ -456,7 +455,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
456455

457456
// Use `f` to mutate the resolution of the name in the module.
458457
// If the resolution becomes a success, define it in the module's glob importers.
459-
fn update_resolution<T, F>(
458+
fn update_local_resolution<T, F>(
460459
&mut self,
461460
module: Module<'ra>,
462461
key: BindingKey,
@@ -466,6 +465,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
466465
where
467466
F: FnOnce(&Resolver<'ra, 'tcx>, &mut NameResolution<'ra>) -> T,
468467
{
468+
assert!(module.is_local());
469469
// Ensure that `resolution` isn't borrowed when defining in the module's glob importers,
470470
// during which the resolution might end up getting re-defined via a glob cycle.
471471
let (binding, t, warn_ambiguity) = {
@@ -497,7 +497,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
497497
};
498498
if self.is_accessible_from(binding.vis, scope) {
499499
let imported_binding = self.import(binding, *import);
500-
let _ = self.try_define(
500+
let _ = self.try_define_local(
501501
import.parent_scope.module,
502502
ident,
503503
key.ns,
@@ -523,11 +523,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
523523
let dummy_binding = self.import(dummy_binding, import);
524524
self.per_ns(|this, ns| {
525525
let module = import.parent_scope.module;
526-
let _ = this.try_define(module, target, ns, dummy_binding, false);
526+
let _ = this.try_define_local(module, target, ns, dummy_binding, false);
527527
// Don't remove underscores from `single_imports`, they were never added.
528528
if target.name != kw::Underscore {
529529
let key = BindingKey::new(target, ns);
530-
this.update_resolution(module, key, false, |_, resolution| {
530+
this.update_local_resolution(module, key, false, |_, resolution| {
531531
resolution.single_imports.swap_remove(&import);
532532
})
533533
}
@@ -902,14 +902,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
902902
}
903903
// We need the `target`, `source` can be extracted.
904904
let imported_binding = this.import(binding, import);
905-
this.define_binding(parent, target, ns, imported_binding);
905+
this.define_binding_local(parent, target, ns, imported_binding);
906906
PendingBinding::Ready(Some(imported_binding))
907907
}
908908
Err(Determinacy::Determined) => {
909909
// Don't remove underscores from `single_imports`, they were never added.
910910
if target.name != kw::Underscore {
911911
let key = BindingKey::new(target, ns);
912-
this.update_resolution(parent, key, false, |_, resolution| {
912+
this.update_local_resolution(parent, key, false, |_, resolution| {
913913
resolution.single_imports.swap_remove(&import);
914914
});
915915
}
@@ -1516,7 +1516,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15161516
.resolution(import.parent_scope.module, key)
15171517
.and_then(|r| r.binding())
15181518
.is_some_and(|binding| binding.warn_ambiguity_recursive());
1519-
let _ = self.try_define(
1519+
let _ = self.try_define_local(
15201520
import.parent_scope.module,
15211521
key.ident,
15221522
key.ns,

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2633,7 +2633,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
26332633
false
26342634
}
26352635

2636-
fn find_module(&mut self, def_id: DefId) -> Option<(Module<'ra>, ImportSuggestion)> {
2636+
fn find_module(&self, def_id: DefId) -> Option<(Module<'ra>, ImportSuggestion)> {
26372637
let mut result = None;
26382638
let mut seen_modules = FxHashSet::default();
26392639
let root_did = self.r.graph_root.def_id();
@@ -2690,7 +2690,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
26902690
result
26912691
}
26922692

2693-
fn collect_enum_ctors(&mut self, def_id: DefId) -> Option<Vec<(Path, DefId, CtorKind)>> {
2693+
fn collect_enum_ctors(&self, def_id: DefId) -> Option<Vec<(Path, DefId, CtorKind)>> {
26942694
self.find_module(def_id).map(|(enum_module, enum_import_suggestion)| {
26952695
let mut variants = Vec::new();
26962696
enum_module.for_each_child(self.r, |_, ident, _, name_binding| {
@@ -2707,7 +2707,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
27072707

27082708
/// Adds a suggestion for using an enum's variant when an enum is used instead.
27092709
fn suggest_using_enum_variant(
2710-
&mut self,
2710+
&self,
27112711
err: &mut Diag<'_>,
27122712
source: PathSource<'_, '_, '_>,
27132713
def_id: DefId,

0 commit comments

Comments
 (0)