@@ -19,7 +19,7 @@ use crate::{
19
19
AmbiguityError , AmbiguityErrorMisc , AmbiguityKind , BindingKey , CmResolver , Determinacy ,
20
20
Finalize , ImportKind , LexicalScopeBinding , Module , ModuleKind , ModuleOrUniformRoot ,
21
21
NameBinding , NameBindingKind , ParentScope , PathResult , PrivacyError , Res , ResolutionError ,
22
- Resolver , Scope , ScopeSet , Segment , Used , Weak , errors,
22
+ Resolver , Scope , ScopeSet , Segment , Stage , Used , Weak , errors,
23
23
} ;
24
24
25
25
#[ derive( Copy , Clone ) ]
@@ -49,6 +49,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
49
49
scope_set : ScopeSet < ' ra > ,
50
50
parent_scope : & ParentScope < ' ra > ,
51
51
ctxt : SyntaxContext ,
52
+ derive_fallback_lint_id : Option < NodeId > ,
52
53
mut visitor : impl FnMut (
53
54
& mut CmResolver < ' r , ' ra , ' tcx > ,
54
55
Scope < ' ra > ,
@@ -99,15 +100,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
99
100
100
101
let rust_2015 = ctxt. edition ( ) . is_rust_2015 ( ) ;
101
102
let ( ns, macro_kind) = match scope_set {
102
- ScopeSet :: All ( ns)
103
- | ScopeSet :: ModuleAndExternPrelude ( ns, _)
104
- | ScopeSet :: Late ( ns, ..) => ( ns, None ) ,
103
+ ScopeSet :: All ( ns) | ScopeSet :: ModuleAndExternPrelude ( ns, _) => ( ns, None ) ,
105
104
ScopeSet :: ExternPrelude => ( TypeNS , None ) ,
106
105
ScopeSet :: Macro ( macro_kind) => ( MacroNS , Some ( macro_kind) ) ,
107
106
} ;
108
107
let module = match scope_set {
109
108
// Start with the specified module.
110
- ScopeSet :: Late ( _ , module , _ ) | ScopeSet :: ModuleAndExternPrelude ( _, module) => module,
109
+ ScopeSet :: ModuleAndExternPrelude ( _, module) => module,
111
110
// Jump out of trait or enum modules, they do not act as scopes.
112
111
_ => parent_scope. module . nearest_item_scope ( ) ,
113
112
} ;
@@ -193,10 +192,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
193
192
} ,
194
193
Scope :: Module ( module, prev_lint_id) => {
195
194
use_prelude = !module. no_implicit_prelude ;
196
- let derive_fallback_lint_id = match scope_set {
197
- ScopeSet :: Late ( .., lint_id) => lint_id,
198
- _ => None ,
199
- } ;
200
195
match self . hygienic_lexical_parent ( module, & mut ctxt, derive_fallback_lint_id) {
201
196
Some ( ( parent_module, lint_id) ) => {
202
197
Scope :: Module ( parent_module, lint_id. or ( prev_lint_id) )
@@ -349,11 +344,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
349
344
return Some ( LexicalScopeBinding :: Item ( binding) ) ;
350
345
} else if let RibKind :: Module ( module) = rib. kind {
351
346
// Encountered a module item, abandon ribs and look into that module and preludes.
347
+ let parent_scope = & ParentScope { module, ..* parent_scope } ;
348
+ let finalize = finalize. map ( |f| Finalize { stage : Stage :: Late , ..f } ) ;
352
349
return self
353
350
. cm ( )
354
- . early_resolve_ident_in_lexical_scope (
351
+ . resolve_ident_in_scope_set (
355
352
orig_ident,
356
- ScopeSet :: Late ( ns, module , finalize . map ( |finalize| finalize . node_id ) ) ,
353
+ ScopeSet :: All ( ns) ,
357
354
parent_scope,
358
355
finalize,
359
356
finalize. is_some ( ) ,
@@ -376,13 +373,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
376
373
unreachable ! ( )
377
374
}
378
375
379
- /// Resolve an identifier in lexical scope.
380
- /// This is a variation of `fn resolve_ident_in_lexical_scope` that can be run during
381
- /// expansion and import resolution (perhaps they can be merged in the future).
382
- /// The function is used for resolving initial segments of macro paths (e.g., `foo` in
383
- /// `foo::bar!();` or `foo!();`) and also for import paths on 2018 edition.
376
+ /// Resolve an identifier in the specified set of scopes.
384
377
#[ instrument( level = "debug" , skip( self ) ) ]
385
- pub ( crate ) fn early_resolve_ident_in_lexical_scope < ' r > (
378
+ pub ( crate ) fn resolve_ident_in_scope_set < ' r > (
386
379
self : CmResolver < ' r , ' ra , ' tcx > ,
387
380
orig_ident : Ident ,
388
381
scope_set : ScopeSet < ' ra > ,
@@ -411,9 +404,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
411
404
}
412
405
413
406
let ( ns, macro_kind) = match scope_set {
414
- ScopeSet :: All ( ns)
415
- | ScopeSet :: ModuleAndExternPrelude ( ns, _)
416
- | ScopeSet :: Late ( ns, ..) => ( ns, None ) ,
407
+ ScopeSet :: All ( ns) | ScopeSet :: ModuleAndExternPrelude ( ns, _) => ( ns, None ) ,
417
408
ScopeSet :: ExternPrelude => ( TypeNS , None ) ,
418
409
ScopeSet :: Macro ( macro_kind) => ( MacroNS , Some ( macro_kind) ) ,
419
410
} ;
@@ -437,10 +428,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
437
428
}
438
429
439
430
// Go through all the scopes and try to resolve the name.
431
+ let derive_fallback_lint_id = match finalize {
432
+ Some ( Finalize { node_id, stage : Stage :: Late , .. } ) => Some ( node_id) ,
433
+ _ => None ,
434
+ } ;
440
435
let break_result = self . visit_scopes (
441
436
scope_set,
442
437
parent_scope,
443
438
orig_ident. span . ctxt ( ) ,
439
+ derive_fallback_lint_id,
444
440
|this, scope, use_prelude, ctxt| {
445
441
let ident = Ident :: new ( orig_ident. name , orig_ident. span . with_ctxt ( ctxt) ) ;
446
442
let result = match scope {
@@ -510,11 +506,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
510
506
ident,
511
507
ns,
512
508
adjusted_parent_scope,
513
- if matches ! ( scope_set, ScopeSet :: Late ( ..) ) {
514
- Shadowing :: Unrestricted
515
- } else {
516
- Shadowing :: Restricted
517
- } ,
509
+ Shadowing :: Restricted ,
518
510
adjusted_finalize,
519
511
ignore_binding,
520
512
ignore_import,
@@ -643,7 +635,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
643
635
return None ;
644
636
}
645
637
646
- if finalize. is_none ( ) || matches ! ( scope_set, ScopeSet :: Late ( ..) ) {
638
+ // Below we report various ambiguity errors.
639
+ // We do not need to report them if we are either in speculative resolution,
640
+ // or in late resolution when everything is already imported and expanded
641
+ // and no ambiguities exist.
642
+ if matches ! ( finalize, None | Some ( Finalize { stage: Stage :: Late , .. } ) ) {
647
643
return Some ( Ok ( binding) ) ;
648
644
}
649
645
@@ -811,7 +807,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
811
807
ModuleOrUniformRoot :: Module ( module) => module,
812
808
ModuleOrUniformRoot :: ModuleAndExternPrelude ( module) => {
813
809
assert_eq ! ( shadowing, Shadowing :: Unrestricted ) ;
814
- let binding = self . early_resolve_ident_in_lexical_scope (
810
+ let binding = self . resolve_ident_in_scope_set (
815
811
ident,
816
812
ScopeSet :: ModuleAndExternPrelude ( ns, module) ,
817
813
parent_scope,
@@ -827,7 +823,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
827
823
return if ns != TypeNS {
828
824
Err ( ( Determined , Weak :: No ) )
829
825
} else {
830
- let binding = self . early_resolve_ident_in_lexical_scope (
826
+ let binding = self . resolve_ident_in_scope_set (
831
827
ident,
832
828
ScopeSet :: ExternPrelude ,
833
829
parent_scope,
@@ -852,7 +848,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
852
848
}
853
849
}
854
850
855
- let binding = self . early_resolve_ident_in_lexical_scope (
851
+ let binding = self . resolve_ident_in_scope_set (
856
852
ident,
857
853
ScopeSet :: All ( ns) ,
858
854
parent_scope,
@@ -945,7 +941,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
945
941
// Now we are in situation when new item/import can appear only from a glob or a macro
946
942
// expansion. With restricted shadowing names from globs and macro expansions cannot
947
943
// shadow names from outer scopes, so we can freely fallback from module search to search
948
- // in outer scopes. For `early_resolve_ident_in_lexical_scope ` to continue search in outer
944
+ // in outer scopes. For `resolve_ident_in_scope_set ` to continue search in outer
949
945
// scopes we return `Undetermined` with `Weak::Yes`.
950
946
951
947
// Check if one of unexpanded macros can still define the name,
@@ -1040,6 +1036,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
1040
1036
// Forbid expanded shadowing to avoid time travel.
1041
1037
if let Some ( shadowed_glob) = shadowed_glob
1042
1038
&& shadowing == Shadowing :: Restricted
1039
+ && finalize. stage == Stage :: Early
1043
1040
&& binding. expansion != LocalExpnId :: ROOT
1044
1041
&& binding. res ( ) != shadowed_glob. res ( )
1045
1042
{
@@ -1635,7 +1632,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
1635
1632
_ => Err ( Determinacy :: determined ( finalize. is_some ( ) ) ) ,
1636
1633
}
1637
1634
} else {
1638
- self . reborrow ( ) . early_resolve_ident_in_lexical_scope (
1635
+ self . reborrow ( ) . resolve_ident_in_scope_set (
1639
1636
ident,
1640
1637
ScopeSet :: All ( ns) ,
1641
1638
parent_scope,
0 commit comments