@@ -38,6 +38,12 @@ impl From<UsePrelude> for bool {
38
38
}
39
39
}
40
40
41
+ #[ derive( Debug , PartialEq ) ]
42
+ enum Shadowing {
43
+ Restricted ,
44
+ Unrestricted ,
45
+ }
46
+
41
47
impl < ' ra , ' tcx > Resolver < ' ra , ' tcx > {
42
48
/// A generic scope visitor.
43
49
/// Visits scopes in order to resolve some identifier in them or perform other actions.
@@ -311,13 +317,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
311
317
312
318
// Walk backwards up the ribs in scope.
313
319
let mut module = self . graph_root ;
314
- for i in ( 0 .. ribs. len ( ) ) . rev ( ) {
315
- debug ! ( "walk rib\n {:?}" , ribs [ i ] . bindings) ;
320
+ for ( i , rib ) in ribs. iter ( ) . enumerate ( ) . rev ( ) {
321
+ debug ! ( "walk rib\n {:?}" , rib . bindings) ;
316
322
// Use the rib kind to determine whether we are resolving parameters
317
323
// (macro 2.0 hygiene) or local variables (`macro_rules` hygiene).
318
- let rib_ident = if ribs[ i] . kind . contains_params ( ) { normalized_ident } else { ident } ;
319
- if let Some ( ( original_rib_ident_def, res) ) = ribs[ i] . bindings . get_key_value ( & rib_ident)
320
- {
324
+ let rib_ident = if rib. kind . contains_params ( ) { normalized_ident } else { ident } ;
325
+ if let Some ( ( original_rib_ident_def, res) ) = rib. bindings . get_key_value ( & rib_ident) {
321
326
// The ident resolves to a type parameter or local variable.
322
327
return Some ( LexicalScopeBinding :: Res ( self . validate_res_from_ribs (
323
328
i,
@@ -329,7 +334,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
329
334
) ) ) ;
330
335
}
331
336
332
- module = match ribs [ i ] . kind {
337
+ module = match rib . kind {
333
338
RibKind :: Module ( module) => module,
334
339
RibKind :: MacroDefinition ( def) if def == self . macro_def ( ident. span . ctxt ( ) ) => {
335
340
// If an invocation of this macro created `ident`, give up on `ident`
@@ -350,6 +355,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
350
355
ident,
351
356
ns,
352
357
parent_scope,
358
+ Shadowing :: Unrestricted ,
353
359
finalize. map ( |finalize| Finalize { used : Used :: Scope , ..finalize } ) ,
354
360
ignore_binding,
355
361
None ,
@@ -494,7 +500,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
494
500
Scope :: CrateRoot => {
495
501
let root_ident = Ident :: new ( kw:: PathRoot , ident. span ) ;
496
502
let root_module = this. resolve_crate_root ( root_ident) ;
497
- let binding = this. resolve_ident_in_module_ext (
503
+ let binding = this. resolve_ident_in_module (
498
504
ModuleOrUniformRoot :: Module ( root_module) ,
499
505
ident,
500
506
ns,
@@ -516,12 +522,16 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
516
522
}
517
523
Scope :: Module ( module, derive_fallback_lint_id) => {
518
524
let adjusted_parent_scope = & ParentScope { module, ..* parent_scope } ;
519
- let binding = this. resolve_ident_in_module_unadjusted_ext (
525
+ let binding = this. resolve_ident_in_module_unadjusted (
520
526
ModuleOrUniformRoot :: Module ( module) ,
521
527
ident,
522
528
ns,
523
529
adjusted_parent_scope,
524
- !matches ! ( scope_set, ScopeSet :: Late ( ..) ) ,
530
+ if matches ! ( scope_set, ScopeSet :: Late ( ..) ) {
531
+ Shadowing :: Unrestricted
532
+ } else {
533
+ Shadowing :: Restricted
534
+ } ,
525
535
finalize. map ( |finalize| Finalize { used : Used :: Scope , ..finalize } ) ,
526
536
ignore_binding,
527
537
ignore_import,
@@ -590,6 +600,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
590
600
ident,
591
601
ns,
592
602
parent_scope,
603
+ Shadowing :: Unrestricted ,
593
604
None ,
594
605
ignore_binding,
595
606
ignore_import,
@@ -748,35 +759,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
748
759
parent_scope : & ParentScope < ' ra > ,
749
760
ignore_import : Option < Import < ' ra > > ,
750
761
) -> Result < NameBinding < ' ra > , Determinacy > {
751
- self . resolve_ident_in_module_ext ( module, ident, ns, parent_scope, None , None , ignore_import)
762
+ self . resolve_ident_in_module ( module, ident, ns, parent_scope, None , None , ignore_import)
752
763
. map_err ( |( determinacy, _) | determinacy)
753
764
}
754
765
755
766
#[ instrument( level = "debug" , skip( self ) ) ]
756
767
pub ( crate ) fn resolve_ident_in_module (
757
- & mut self ,
758
- module : ModuleOrUniformRoot < ' ra > ,
759
- ident : Ident ,
760
- ns : Namespace ,
761
- parent_scope : & ParentScope < ' ra > ,
762
- finalize : Option < Finalize > ,
763
- ignore_binding : Option < NameBinding < ' ra > > ,
764
- ignore_import : Option < Import < ' ra > > ,
765
- ) -> Result < NameBinding < ' ra > , Determinacy > {
766
- self . resolve_ident_in_module_ext (
767
- module,
768
- ident,
769
- ns,
770
- parent_scope,
771
- finalize,
772
- ignore_binding,
773
- ignore_import,
774
- )
775
- . map_err ( |( determinacy, _) | determinacy)
776
- }
777
-
778
- #[ instrument( level = "debug" , skip( self ) ) ]
779
- fn resolve_ident_in_module_ext (
780
768
& mut self ,
781
769
module : ModuleOrUniformRoot < ' ra > ,
782
770
mut ident : Ident ,
@@ -803,52 +791,28 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
803
791
// No adjustments
804
792
}
805
793
}
806
- self . resolve_ident_in_module_unadjusted_ext (
794
+ self . resolve_ident_in_module_unadjusted (
807
795
module,
808
796
ident,
809
797
ns,
810
798
adjusted_parent_scope,
811
- false ,
799
+ Shadowing :: Unrestricted ,
812
800
finalize,
813
801
ignore_binding,
814
802
ignore_import,
815
803
)
816
804
}
817
805
818
- #[ instrument( level = "debug" , skip( self ) ) ]
819
- fn resolve_ident_in_module_unadjusted (
820
- & mut self ,
821
- module : ModuleOrUniformRoot < ' ra > ,
822
- ident : Ident ,
823
- ns : Namespace ,
824
- parent_scope : & ParentScope < ' ra > ,
825
- finalize : Option < Finalize > ,
826
- ignore_binding : Option < NameBinding < ' ra > > ,
827
- ignore_import : Option < Import < ' ra > > ,
828
- ) -> Result < NameBinding < ' ra > , Determinacy > {
829
- self . resolve_ident_in_module_unadjusted_ext (
830
- module,
831
- ident,
832
- ns,
833
- parent_scope,
834
- false ,
835
- finalize,
836
- ignore_binding,
837
- ignore_import,
838
- )
839
- . map_err ( |( determinacy, _) | determinacy)
840
- }
841
-
842
806
/// Attempts to resolve `ident` in namespaces `ns` of `module`.
843
807
/// Invariant: if `finalize` is `Some`, expansion and import resolution must be complete.
844
808
#[ instrument( level = "debug" , skip( self ) ) ]
845
- fn resolve_ident_in_module_unadjusted_ext (
809
+ fn resolve_ident_in_module_unadjusted (
846
810
& mut self ,
847
811
module : ModuleOrUniformRoot < ' ra > ,
848
812
ident : Ident ,
849
813
ns : Namespace ,
850
814
parent_scope : & ParentScope < ' ra > ,
851
- restricted_shadowing : bool ,
815
+ shadowing : Shadowing ,
852
816
finalize : Option < Finalize > ,
853
817
// This binding should be ignored during in-module resolution, so that we don't get
854
818
// "self-confirming" import resolutions during import validation and checking.
@@ -858,7 +822,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
858
822
let module = match module {
859
823
ModuleOrUniformRoot :: Module ( module) => module,
860
824
ModuleOrUniformRoot :: CrateRootAndExternPrelude => {
861
- assert ! ( !restricted_shadowing ) ;
825
+ assert_eq ! ( shadowing , Shadowing :: Unrestricted ) ;
862
826
let binding = self . early_resolve_ident_in_lexical_scope (
863
827
ident,
864
828
ScopeSet :: AbsolutePath ( ns) ,
@@ -871,7 +835,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
871
835
return binding. map_err ( |determinacy| ( determinacy, Weak :: No ) ) ;
872
836
}
873
837
ModuleOrUniformRoot :: ExternPrelude => {
874
- assert ! ( !restricted_shadowing ) ;
838
+ assert_eq ! ( shadowing , Shadowing :: Unrestricted ) ;
875
839
return if ns != TypeNS {
876
840
Err ( ( Determined , Weak :: No ) )
877
841
} else if let Some ( binding) = self . extern_prelude_get ( ident, finalize. is_some ( ) ) {
@@ -884,7 +848,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
884
848
} ;
885
849
}
886
850
ModuleOrUniformRoot :: CurrentScope => {
887
- assert ! ( !restricted_shadowing ) ;
851
+ assert_eq ! ( shadowing , Shadowing :: Unrestricted ) ;
888
852
if ns == TypeNS {
889
853
if ident. name == kw:: Crate || ident. name == kw:: DollarCrate {
890
854
let module = self . resolve_crate_root ( ident) ;
@@ -943,7 +907,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
943
907
944
908
// Forbid expanded shadowing to avoid time travel.
945
909
if let Some ( shadowed_glob) = resolution. shadowed_glob
946
- && restricted_shadowing
910
+ && shadowing == Shadowing :: Restricted
947
911
&& binding. expansion != LocalExpnId :: ROOT
948
912
&& binding. res ( ) != shadowed_glob. res ( )
949
913
{
@@ -958,7 +922,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
958
922
} ) ;
959
923
}
960
924
961
- if !restricted_shadowing
925
+ if shadowing == Shadowing :: Unrestricted
962
926
&& binding. expansion != LocalExpnId :: ROOT
963
927
&& let NameBindingKind :: Import { import, .. } = binding. kind
964
928
&& matches ! ( import. kind, ImportKind :: MacroExport )
@@ -1047,13 +1011,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
1047
1011
ignore_binding,
1048
1012
ignore_import,
1049
1013
) {
1050
- Err ( Determined ) => continue ,
1014
+ Err ( ( Determined , _ ) ) => continue ,
1051
1015
Ok ( binding)
1052
1016
if !self . is_accessible_from ( binding. vis , single_import. parent_scope . module ) =>
1053
1017
{
1054
1018
continue ;
1055
1019
}
1056
- Ok ( _) | Err ( Undetermined ) => return Err ( ( Undetermined , Weak :: No ) ) ,
1020
+ Ok ( _) | Err ( ( Undetermined , _ ) ) => return Err ( ( Undetermined , Weak :: No ) ) ,
1057
1021
}
1058
1022
}
1059
1023
@@ -1070,7 +1034,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
1070
1034
// and prohibit access to macro-expanded `macro_export` macros instead (unless restricted
1071
1035
// shadowing is enabled, see `macro_expanded_macro_export_errors`).
1072
1036
if let Some ( binding) = binding {
1073
- if binding. determined ( ) || ns == MacroNS || restricted_shadowing {
1037
+ if binding. determined ( ) || ns == MacroNS || shadowing == Shadowing :: Restricted {
1074
1038
return check_usable ( self , binding) ;
1075
1039
} else {
1076
1040
return Err ( ( Undetermined , Weak :: No ) ) ;
@@ -1122,19 +1086,20 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
1122
1086
ident,
1123
1087
ns,
1124
1088
adjusted_parent_scope,
1089
+ Shadowing :: Unrestricted ,
1125
1090
None ,
1126
1091
ignore_binding,
1127
1092
ignore_import,
1128
1093
) ;
1129
1094
1130
1095
match result {
1131
- Err ( Determined ) => continue ,
1096
+ Err ( ( Determined , _ ) ) => continue ,
1132
1097
Ok ( binding)
1133
1098
if !self . is_accessible_from ( binding. vis , glob_import. parent_scope . module ) =>
1134
1099
{
1135
1100
continue ;
1136
1101
}
1137
- Ok ( _) | Err ( Undetermined ) => return Err ( ( Undetermined , Weak :: Yes ) ) ,
1102
+ Ok ( _) | Err ( ( Undetermined , _ ) ) => return Err ( ( Undetermined , Weak :: Yes ) ) ,
1138
1103
}
1139
1104
}
1140
1105
@@ -1200,7 +1165,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
1200
1165
// Still doesn't deal with upvars
1201
1166
if let Some ( span) = finalize {
1202
1167
let ( span, resolution_error) = match item {
1203
- None if rib_ident. as_str ( ) == "self" => ( span, LowercaseSelf ) ,
1168
+ None if rib_ident. name == kw:: SelfLower => {
1169
+ ( span, LowercaseSelf )
1170
+ }
1204
1171
None => {
1205
1172
// If we have a `let name = expr;`, we have the span for
1206
1173
// `name` and use that to see if it is followed by a type
@@ -1563,6 +1530,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
1563
1530
ignore_binding,
1564
1531
ignore_import,
1565
1532
)
1533
+ . map_err ( |( determinacy, _) | determinacy)
1566
1534
} else if let Some ( ribs) = ribs
1567
1535
&& let Some ( TypeNS | ValueNS ) = opt_ns
1568
1536
{
0 commit comments