Skip to content

Commit 3b059e0

Browse files
Rollup merge of #112758 - bvanjoi:clean-up-resolve, r=petrochenkov
refactor(resolve): delete update_resolution function The `{ resolution.single_imports.remove(); }` code block does not modify the `binding` of this `resolution`. Therefore, the result of `resolution.binding()` before and after `let t = f(self, resolution)` will remain the same, and it will always satisfy the result: `_ if old_binding.is_some() => return t` or `None => return t`. And then we delete the `update_resolution` function because it only called once. r? ``@petrochenkov``
2 parents d70be67 + 894ab2c commit 3b059e0

File tree

1 file changed

+40
-53
lines changed

1 file changed

+40
-53
lines changed

compiler/rustc_resolve/src/imports.rs

+40-53
Original file line numberDiff line numberDiff line change
@@ -304,21 +304,23 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
304304
let res = binding.res();
305305
self.check_reserved_macro_name(key.ident, res);
306306
self.set_binding_parent_module(binding, module);
307-
self.update_resolution(module, key, |this, resolution| {
308-
if let Some(old_binding) = resolution.binding {
309-
if res == Res::Err && old_binding.res() != Res::Err {
310-
// Do not override real bindings with `Res::Err`s from error recovery.
311-
return Ok(());
312-
}
307+
308+
let mut resolution = self.resolution(module, key).borrow_mut();
309+
let old_binding = resolution.binding();
310+
let mut t = Ok(());
311+
if let Some(old_binding) = resolution.binding {
312+
if res == Res::Err && old_binding.res() != Res::Err {
313+
// Do not override real bindings with `Res::Err`s from error recovery.
314+
} else {
313315
match (old_binding.is_glob_import(), binding.is_glob_import()) {
314316
(true, true) => {
315317
if res != old_binding.res() {
316-
resolution.binding = Some(this.ambiguity(
318+
resolution.binding = Some(self.ambiguity(
317319
AmbiguityKind::GlobVsGlob,
318320
old_binding,
319321
binding,
320322
));
321-
} else if !old_binding.vis.is_at_least(binding.vis, this.tcx) {
323+
} else if !old_binding.vis.is_at_least(binding.vis, self.tcx) {
322324
// We are glob-importing the same item but with greater visibility.
323325
resolution.binding = Some(binding);
324326
}
@@ -330,7 +332,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
330332
&& key.ns == MacroNS
331333
&& nonglob_binding.expansion != LocalExpnId::ROOT
332334
{
333-
resolution.binding = Some(this.ambiguity(
335+
resolution.binding = Some(self.ambiguity(
334336
AmbiguityKind::GlobVsExpanded,
335337
nonglob_binding,
336338
glob_binding,
@@ -342,66 +344,40 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
342344
if let Some(old_binding) = resolution.shadowed_glob {
343345
assert!(old_binding.is_glob_import());
344346
if glob_binding.res() != old_binding.res() {
345-
resolution.shadowed_glob = Some(this.ambiguity(
347+
resolution.shadowed_glob = Some(self.ambiguity(
346348
AmbiguityKind::GlobVsGlob,
347349
old_binding,
348350
glob_binding,
349351
));
350-
} else if !old_binding.vis.is_at_least(binding.vis, this.tcx) {
352+
} else if !old_binding.vis.is_at_least(binding.vis, self.tcx) {
351353
resolution.shadowed_glob = Some(glob_binding);
352354
}
353355
} else {
354356
resolution.shadowed_glob = Some(glob_binding);
355357
}
356358
}
357359
(false, false) => {
358-
return Err(old_binding);
360+
t = Err(old_binding);
359361
}
360362
}
361-
} else {
362-
resolution.binding = Some(binding);
363363
}
364+
} else {
365+
resolution.binding = Some(binding);
366+
};
364367

365-
Ok(())
366-
})
367-
}
368-
369-
fn ambiguity(
370-
&self,
371-
kind: AmbiguityKind,
372-
primary_binding: &'a NameBinding<'a>,
373-
secondary_binding: &'a NameBinding<'a>,
374-
) -> &'a NameBinding<'a> {
375-
self.arenas.alloc_name_binding(NameBinding {
376-
ambiguity: Some((secondary_binding, kind)),
377-
..primary_binding.clone()
378-
})
379-
}
380-
381-
// Use `f` to mutate the resolution of the name in the module.
382-
// If the resolution becomes a success, define it in the module's glob importers.
383-
fn update_resolution<T, F>(&mut self, module: Module<'a>, key: BindingKey, f: F) -> T
384-
where
385-
F: FnOnce(&mut Resolver<'a, 'tcx>, &mut NameResolution<'a>) -> T,
386-
{
387368
// Ensure that `resolution` isn't borrowed when defining in the module's glob importers,
388369
// during which the resolution might end up getting re-defined via a glob cycle.
389-
let (binding, t) = {
390-
let resolution = &mut *self.resolution(module, key).borrow_mut();
391-
let old_binding = resolution.binding();
392-
393-
let t = f(self, resolution);
394-
395-
match resolution.binding() {
396-
_ if old_binding.is_some() => return t,
397-
None => return t,
398-
Some(binding) => match old_binding {
399-
Some(old_binding) if ptr::eq(old_binding, binding) => return t,
400-
_ => (binding, t),
401-
},
402-
}
370+
let (binding, t) = match resolution.binding() {
371+
_ if old_binding.is_some() => return t,
372+
None => return t,
373+
Some(binding) => match old_binding {
374+
Some(old_binding) if ptr::eq(old_binding, binding) => return t,
375+
_ => (binding, t),
376+
},
403377
};
404378

379+
drop(resolution);
380+
405381
// Define `binding` in `module`s glob importers.
406382
for import in module.glob_importers.borrow_mut().iter() {
407383
let mut ident = key.ident;
@@ -420,6 +396,18 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
420396
t
421397
}
422398

399+
fn ambiguity(
400+
&self,
401+
kind: AmbiguityKind,
402+
primary_binding: &'a NameBinding<'a>,
403+
secondary_binding: &'a NameBinding<'a>,
404+
) -> &'a NameBinding<'a> {
405+
self.arenas.alloc_name_binding(NameBinding {
406+
ambiguity: Some((secondary_binding, kind)),
407+
..primary_binding.clone()
408+
})
409+
}
410+
423411
// Define a dummy resolution containing a `Res::Err` as a placeholder for a failed
424412
// or indeterminate resolution, also mark such failed imports as used to avoid duplicate diagnostics.
425413
fn import_dummy_binding(&mut self, import: &'a Import<'a>, is_indeterminate: bool) {
@@ -769,9 +757,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
769757
.emit();
770758
}
771759
let key = BindingKey::new(target, ns);
772-
this.update_resolution(parent, key, |_, resolution| {
773-
resolution.single_imports.remove(&Interned::new_unchecked(import));
774-
});
760+
let mut resolution = this.resolution(parent, key).borrow_mut();
761+
resolution.single_imports.remove(&Interned::new_unchecked(import));
775762
}
776763
}
777764
}

0 commit comments

Comments
 (0)