@@ -19,7 +19,7 @@ use crate::{
1919 AmbiguityError , AmbiguityErrorMisc , AmbiguityKind , BindingKey , CmResolver , Determinacy ,
2020 Finalize , ImportKind , LexicalScopeBinding , Module , ModuleKind , ModuleOrUniformRoot ,
2121 NameBinding , NameBindingKind , ParentScope , PathResult , PrivacyError , Res , ResolutionError ,
22- Resolver , Scope , ScopeSet , Segment , Used , Weak , errors,
22+ Resolver , Scope , ScopeSet , Segment , Stage , Used , Weak , errors,
2323} ;
2424
2525#[ derive( Copy , Clone ) ]
@@ -49,6 +49,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
4949 scope_set : ScopeSet < ' ra > ,
5050 parent_scope : & ParentScope < ' ra > ,
5151 ctxt : SyntaxContext ,
52+ derive_fallback_lint_id : Option < NodeId > ,
5253 mut visitor : impl FnMut (
5354 & mut CmResolver < ' r , ' ra , ' tcx > ,
5455 Scope < ' ra > ,
@@ -99,15 +100,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
99100
100101 let rust_2015 = ctxt. edition ( ) . is_rust_2015 ( ) ;
101102 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 ) ,
105104 ScopeSet :: ExternPrelude => ( TypeNS , None ) ,
106105 ScopeSet :: Macro ( macro_kind) => ( MacroNS , Some ( macro_kind) ) ,
107106 } ;
108107 let module = match scope_set {
109108 // Start with the specified module.
110- ScopeSet :: Late ( _ , module , _ ) | ScopeSet :: ModuleAndExternPrelude ( _, module) => module,
109+ ScopeSet :: ModuleAndExternPrelude ( _, module) => module,
111110 // Jump out of trait or enum modules, they do not act as scopes.
112111 _ => parent_scope. module . nearest_item_scope ( ) ,
113112 } ;
@@ -193,10 +192,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
193192 } ,
194193 Scope :: Module ( module, prev_lint_id) => {
195194 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- } ;
200195 match self . hygienic_lexical_parent ( module, & mut ctxt, derive_fallback_lint_id) {
201196 Some ( ( parent_module, lint_id) ) => {
202197 Scope :: Module ( parent_module, lint_id. or ( prev_lint_id) )
@@ -349,11 +344,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
349344 return Some ( LexicalScopeBinding :: Item ( binding) ) ;
350345 } else if let RibKind :: Module ( module) = rib. kind {
351346 // 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 } ) ;
352349 return self
353350 . cm ( )
354- . early_resolve_ident_in_lexical_scope (
351+ . resolve_ident_in_scope_set (
355352 orig_ident,
356- ScopeSet :: Late ( ns, module , finalize . map ( |finalize| finalize . node_id ) ) ,
353+ ScopeSet :: All ( ns) ,
357354 parent_scope,
358355 finalize,
359356 finalize. is_some ( ) ,
@@ -376,13 +373,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
376373 unreachable ! ( )
377374 }
378375
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.
384377 #[ 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 > (
386379 self : CmResolver < ' r , ' ra , ' tcx > ,
387380 orig_ident : Ident ,
388381 scope_set : ScopeSet < ' ra > ,
@@ -411,9 +404,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
411404 }
412405
413406 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 ) ,
417408 ScopeSet :: ExternPrelude => ( TypeNS , None ) ,
418409 ScopeSet :: Macro ( macro_kind) => ( MacroNS , Some ( macro_kind) ) ,
419410 } ;
@@ -437,10 +428,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
437428 }
438429
439430 // 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+ } ;
440435 let break_result = self . visit_scopes (
441436 scope_set,
442437 parent_scope,
443438 orig_ident. span . ctxt ( ) ,
439+ derive_fallback_lint_id,
444440 |this, scope, use_prelude, ctxt| {
445441 let ident = Ident :: new ( orig_ident. name , orig_ident. span . with_ctxt ( ctxt) ) ;
446442 let result = match scope {
@@ -510,11 +506,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
510506 ident,
511507 ns,
512508 adjusted_parent_scope,
513- if matches ! ( scope_set, ScopeSet :: Late ( ..) ) {
514- Shadowing :: Unrestricted
515- } else {
516- Shadowing :: Restricted
517- } ,
509+ Shadowing :: Restricted ,
518510 adjusted_finalize,
519511 ignore_binding,
520512 ignore_import,
@@ -643,7 +635,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
643635 return None ;
644636 }
645637
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 , .. } ) ) {
647643 return Some ( Ok ( binding) ) ;
648644 }
649645
@@ -811,7 +807,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
811807 ModuleOrUniformRoot :: Module ( module) => module,
812808 ModuleOrUniformRoot :: ModuleAndExternPrelude ( module) => {
813809 assert_eq ! ( shadowing, Shadowing :: Unrestricted ) ;
814- let binding = self . early_resolve_ident_in_lexical_scope (
810+ let binding = self . resolve_ident_in_scope_set (
815811 ident,
816812 ScopeSet :: ModuleAndExternPrelude ( ns, module) ,
817813 parent_scope,
@@ -827,7 +823,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
827823 return if ns != TypeNS {
828824 Err ( ( Determined , Weak :: No ) )
829825 } else {
830- let binding = self . early_resolve_ident_in_lexical_scope (
826+ let binding = self . resolve_ident_in_scope_set (
831827 ident,
832828 ScopeSet :: ExternPrelude ,
833829 parent_scope,
@@ -852,7 +848,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
852848 }
853849 }
854850
855- let binding = self . early_resolve_ident_in_lexical_scope (
851+ let binding = self . resolve_ident_in_scope_set (
856852 ident,
857853 ScopeSet :: All ( ns) ,
858854 parent_scope,
@@ -945,7 +941,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
945941 // Now we are in situation when new item/import can appear only from a glob or a macro
946942 // expansion. With restricted shadowing names from globs and macro expansions cannot
947943 // 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
949945 // scopes we return `Undetermined` with `Weak::Yes`.
950946
951947 // Check if one of unexpanded macros can still define the name,
@@ -1040,6 +1036,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10401036 // Forbid expanded shadowing to avoid time travel.
10411037 if let Some ( shadowed_glob) = shadowed_glob
10421038 && shadowing == Shadowing :: Restricted
1039+ && finalize. stage == Stage :: Early
10431040 && binding. expansion != LocalExpnId :: ROOT
10441041 && binding. res ( ) != shadowed_glob. res ( )
10451042 {
@@ -1635,7 +1632,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16351632 _ => Err ( Determinacy :: determined ( finalize. is_some ( ) ) ) ,
16361633 }
16371634 } else {
1638- self . reborrow ( ) . early_resolve_ident_in_lexical_scope (
1635+ self . reborrow ( ) . resolve_ident_in_scope_set (
16391636 ident,
16401637 ScopeSet :: All ( ns) ,
16411638 parent_scope,
0 commit comments