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
This commit is contained in:
Stuart Cook
2025-08-08 12:52:52 +10:00
committed by GitHub
8 changed files with 337 additions and 189 deletions

View File

@@ -223,7 +223,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
pub(crate) fn build_reduced_graph_external(&self, module: Module<'ra>) {
for child in self.tcx.module_children(module.def_id()) {
let parent_scope = ParentScope::module(module, self);
let parent_scope = ParentScope::module(module, self.arenas);
self.build_reduced_graph_for_external_crate_res(child, parent_scope)
}
}
@@ -373,7 +373,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
res,
))
};
match self.r.resolve_path(
match self.r.cm().resolve_path(
&segments,
None,
parent_scope,
@@ -1128,7 +1128,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
});
} else {
for ident in single_imports.iter().cloned() {
let result = self.r.maybe_resolve_ident_in_module(
let result = self.r.cm().maybe_resolve_ident_in_module(
ModuleOrUniformRoot::Module(module),
ident,
MacroNS,