@@ -304,21 +304,23 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
304
304
let res = binding. res ( ) ;
305
305
self . check_reserved_macro_name ( key. ident , res) ;
306
306
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 {
313
315
match ( old_binding. is_glob_import ( ) , binding. is_glob_import ( ) ) {
314
316
( true , true ) => {
315
317
if res != old_binding. res ( ) {
316
- resolution. binding = Some ( this . ambiguity (
318
+ resolution. binding = Some ( self . ambiguity (
317
319
AmbiguityKind :: GlobVsGlob ,
318
320
old_binding,
319
321
binding,
320
322
) ) ;
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 ) {
322
324
// We are glob-importing the same item but with greater visibility.
323
325
resolution. binding = Some ( binding) ;
324
326
}
@@ -330,7 +332,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
330
332
&& key. ns == MacroNS
331
333
&& nonglob_binding. expansion != LocalExpnId :: ROOT
332
334
{
333
- resolution. binding = Some ( this . ambiguity (
335
+ resolution. binding = Some ( self . ambiguity (
334
336
AmbiguityKind :: GlobVsExpanded ,
335
337
nonglob_binding,
336
338
glob_binding,
@@ -342,66 +344,40 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
342
344
if let Some ( old_binding) = resolution. shadowed_glob {
343
345
assert ! ( old_binding. is_glob_import( ) ) ;
344
346
if glob_binding. res ( ) != old_binding. res ( ) {
345
- resolution. shadowed_glob = Some ( this . ambiguity (
347
+ resolution. shadowed_glob = Some ( self . ambiguity (
346
348
AmbiguityKind :: GlobVsGlob ,
347
349
old_binding,
348
350
glob_binding,
349
351
) ) ;
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 ) {
351
353
resolution. shadowed_glob = Some ( glob_binding) ;
352
354
}
353
355
} else {
354
356
resolution. shadowed_glob = Some ( glob_binding) ;
355
357
}
356
358
}
357
359
( false , false ) => {
358
- return Err ( old_binding) ;
360
+ t = Err ( old_binding) ;
359
361
}
360
362
}
361
- } else {
362
- resolution. binding = Some ( binding) ;
363
363
}
364
+ } else {
365
+ resolution. binding = Some ( binding) ;
366
+ } ;
364
367
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
- {
387
368
// Ensure that `resolution` isn't borrowed when defining in the module's glob importers,
388
369
// 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
+ } ,
403
377
} ;
404
378
379
+ drop ( resolution) ;
380
+
405
381
// Define `binding` in `module`s glob importers.
406
382
for import in module. glob_importers . borrow_mut ( ) . iter ( ) {
407
383
let mut ident = key. ident ;
@@ -420,6 +396,18 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
420
396
t
421
397
}
422
398
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
+
423
411
// Define a dummy resolution containing a `Res::Err` as a placeholder for a failed
424
412
// or indeterminate resolution, also mark such failed imports as used to avoid duplicate diagnostics.
425
413
fn import_dummy_binding ( & mut self , import : & ' a Import < ' a > , is_indeterminate : bool ) {
@@ -769,9 +757,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
769
757
. emit ( ) ;
770
758
}
771
759
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) ) ;
775
762
}
776
763
}
777
764
}
0 commit comments