Skip to content

Commit ecce94c

Browse files
authored
Rollup merge of #144912 - LorrensP-2158466:smart-resolver, r=petrochenkov
Resolver: introduce a conditionally mutable Resolver for (non-)speculative resolution. This pr introduces a `CmResolver`, a wrapper around the main resolver which gives out mutable access given a condition. `CmResolver` only allows mutation when we’re not in speculative import resolution. This ensures we can’t accidentally mutate the resolver during this process, which is important as we move towards a batched resolution algorithm. This also changes functions that are used during speculative import resolution to take a `CmResolver` instead of a `&mut Resolver`. Also introduces a new kind of "smart pointer" which has the behaviour described above: ```rust /// A wrapper around a mutable reference that conditionally allows mutable access. pub(crate) struct RefOrMut<'a, T> { p: &'a mut T, mutable: bool, } type CmResolver<'r, 'ra, 'tcx> = RefOrMut<'r, Resolver<'ra, 'tcx>>; ``` r? petrochenkov
2 parents 3f94101 + 487e5ce commit ecce94c

File tree

8 files changed

+337
-189
lines changed

8 files changed

+337
-189
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
223223

224224
pub(crate) fn build_reduced_graph_external(&self, module: Module<'ra>) {
225225
for child in self.tcx.module_children(module.def_id()) {
226-
let parent_scope = ParentScope::module(module, self);
226+
let parent_scope = ParentScope::module(module, self.arenas);
227227
self.build_reduced_graph_for_external_crate_res(child, parent_scope)
228228
}
229229
}
@@ -373,7 +373,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
373373
res,
374374
))
375375
};
376-
match self.r.resolve_path(
376+
match self.r.cm().resolve_path(
377377
&segments,
378378
None,
379379
parent_scope,
@@ -1128,7 +1128,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
11281128
});
11291129
} else {
11301130
for ident in single_imports.iter().cloned() {
1131-
let result = self.r.maybe_resolve_ident_in_module(
1131+
let result = self.r.cm().maybe_resolve_ident_in_module(
11321132
ModuleOrUniformRoot::Module(module),
11331133
ident,
11341134
MacroNS,

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -469,13 +469,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
469469

470470
pub(crate) fn lint_if_path_starts_with_module(
471471
&mut self,
472-
finalize: Option<Finalize>,
472+
finalize: Finalize,
473473
path: &[Segment],
474474
second_binding: Option<NameBinding<'_>>,
475475
) {
476-
let Some(Finalize { node_id, root_span, .. }) = finalize else {
477-
return;
478-
};
476+
let Finalize { node_id, root_span, .. } = finalize;
479477

480478
let first_name = match path.get(0) {
481479
// In the 2018 edition this lint is a hard error, so nothing to do
@@ -1029,7 +1027,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10291027
) -> Option<TypoSuggestion> {
10301028
let mut suggestions = Vec::new();
10311029
let ctxt = ident.span.ctxt();
1032-
self.visit_scopes(scope_set, parent_scope, ctxt, |this, scope, use_prelude, _| {
1030+
self.cm().visit_scopes(scope_set, parent_scope, ctxt, |this, scope, use_prelude, _| {
10331031
match scope {
10341032
Scope::DeriveHelpers(expn_id) => {
10351033
let res = Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper);
@@ -1048,7 +1046,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10481046
if filter_fn(res) {
10491047
for derive in parent_scope.derives {
10501048
let parent_scope = &ParentScope { derives: &[], ..*parent_scope };
1051-
let Ok((Some(ext), _)) = this.resolve_macro_path(
1049+
let Ok((Some(ext), _)) = this.reborrow().resolve_macro_path(
10521050
derive,
10531051
Some(MacroKind::Derive),
10541052
parent_scope,
@@ -1482,7 +1480,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14821480
) {
14831481
// Bring imported but unused `derive` macros into `macro_map` so we ensure they can be used
14841482
// for suggestions.
1485-
self.visit_scopes(
1483+
self.cm().visit_scopes(
14861484
ScopeSet::Macro(MacroKind::Derive),
14871485
&parent_scope,
14881486
ident.span.ctxt(),
@@ -1591,7 +1589,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15911589
});
15921590
}
15931591
for ns in [Namespace::MacroNS, Namespace::TypeNS, Namespace::ValueNS] {
1594-
let Ok(binding) = self.early_resolve_ident_in_lexical_scope(
1592+
let Ok(binding) = self.cm().early_resolve_ident_in_lexical_scope(
15951593
ident,
15961594
ScopeSet::All(ns),
15971595
parent_scope,
@@ -2271,16 +2269,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
22712269
if ns == TypeNS || ns == ValueNS {
22722270
let ns_to_try = if ns == TypeNS { ValueNS } else { TypeNS };
22732271
let binding = if let Some(module) = module {
2274-
self.resolve_ident_in_module(
2275-
module,
2276-
ident,
2277-
ns_to_try,
2278-
parent_scope,
2279-
None,
2280-
ignore_binding,
2281-
ignore_import,
2282-
)
2283-
.ok()
2272+
self.cm()
2273+
.resolve_ident_in_module(
2274+
module,
2275+
ident,
2276+
ns_to_try,
2277+
parent_scope,
2278+
None,
2279+
ignore_binding,
2280+
ignore_import,
2281+
)
2282+
.ok()
22842283
} else if let Some(ribs) = ribs
22852284
&& let Some(TypeNS | ValueNS) = opt_ns
22862285
{
@@ -2298,16 +2297,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
22982297
_ => None,
22992298
}
23002299
} else {
2301-
self.early_resolve_ident_in_lexical_scope(
2302-
ident,
2303-
ScopeSet::All(ns_to_try),
2304-
parent_scope,
2305-
None,
2306-
false,
2307-
ignore_binding,
2308-
ignore_import,
2309-
)
2310-
.ok()
2300+
self.cm()
2301+
.early_resolve_ident_in_lexical_scope(
2302+
ident,
2303+
ScopeSet::All(ns_to_try),
2304+
parent_scope,
2305+
None,
2306+
false,
2307+
ignore_binding,
2308+
ignore_import,
2309+
)
2310+
.ok()
23112311
};
23122312
if let Some(binding) = binding {
23132313
msg = format!(
@@ -2401,7 +2401,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
24012401
},
24022402
)
24032403
});
2404-
if let Ok(binding) = self.early_resolve_ident_in_lexical_scope(
2404+
if let Ok(binding) = self.cm().early_resolve_ident_in_lexical_scope(
24052405
ident,
24062406
ScopeSet::All(ValueNS),
24072407
parent_scope,
@@ -2531,7 +2531,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
25312531
) -> Option<(Vec<Segment>, Option<String>)> {
25322532
// Replace first ident with `self` and check if that is valid.
25332533
path[0].ident.name = kw::SelfLower;
2534-
let result = self.maybe_resolve_path(&path, None, parent_scope, None);
2534+
let result = self.cm().maybe_resolve_path(&path, None, parent_scope, None);
25352535
debug!(?path, ?result);
25362536
if let PathResult::Module(..) = result { Some((path, None)) } else { None }
25372537
}
@@ -2551,7 +2551,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
25512551
) -> Option<(Vec<Segment>, Option<String>)> {
25522552
// Replace first ident with `crate` and check if that is valid.
25532553
path[0].ident.name = kw::Crate;
2554-
let result = self.maybe_resolve_path(&path, None, parent_scope, None);
2554+
let result = self.cm().maybe_resolve_path(&path, None, parent_scope, None);
25552555
debug!(?path, ?result);
25562556
if let PathResult::Module(..) = result {
25572557
Some((
@@ -2583,7 +2583,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
25832583
) -> Option<(Vec<Segment>, Option<String>)> {
25842584
// Replace first ident with `crate` and check if that is valid.
25852585
path[0].ident.name = kw::Super;
2586-
let result = self.maybe_resolve_path(&path, None, parent_scope, None);
2586+
let result = self.cm().maybe_resolve_path(&path, None, parent_scope, None);
25872587
debug!(?path, ?result);
25882588
if let PathResult::Module(..) = result { Some((path, None)) } else { None }
25892589
}
@@ -2618,7 +2618,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
26182618
for name in extern_crate_names.into_iter() {
26192619
// Replace first ident with a crate name and check if that is valid.
26202620
path[0].ident.name = name;
2621-
let result = self.maybe_resolve_path(&path, None, parent_scope, None);
2621+
let result = self.cm().maybe_resolve_path(&path, None, parent_scope, None);
26222622
debug!(?path, ?name, ?result);
26232623
if let PathResult::Module(..) = result {
26242624
return Some((path, None));

0 commit comments

Comments
 (0)