9
9
// except according to those terms.
10
10
11
11
use { AmbiguityError , AmbiguityKind , AmbiguityErrorMisc } ;
12
- use { CrateLint , Resolver , ResolutionError , is_known_tool, resolve_error} ;
12
+ use { CrateLint , DeterminacyExt , Resolver , ResolutionError , is_known_tool, resolve_error} ;
13
13
use { Module , ModuleKind , NameBinding , NameBindingKind , PathResult , ToNameBinding } ;
14
14
use ModuleOrUniformRoot ;
15
15
use Namespace :: { self , * } ;
@@ -54,8 +54,8 @@ pub struct InvocationData<'a> {
54
54
crate parent_legacy_scope : Cell < LegacyScope < ' a > > ,
55
55
/// Legacy scope *produced* by expanding this macro invocation,
56
56
/// includes all the macro_rules items, other invocations, etc generated by it.
57
- /// Set to the parent scope if the macro is not expanded yet (as if the macro produced nothing) .
58
- crate output_legacy_scope : Cell < LegacyScope < ' a > > ,
57
+ /// `None` if the macro is not expanded yet.
58
+ crate output_legacy_scope : Cell < Option < LegacyScope < ' a > > > ,
59
59
}
60
60
61
61
impl < ' a > InvocationData < ' a > {
@@ -64,7 +64,7 @@ impl<'a> InvocationData<'a> {
64
64
module : Cell :: new ( graph_root) ,
65
65
def_index : CRATE_DEF_INDEX ,
66
66
parent_legacy_scope : Cell :: new ( LegacyScope :: Empty ) ,
67
- output_legacy_scope : Cell :: new ( LegacyScope :: Empty ) ,
67
+ output_legacy_scope : Cell :: new ( Some ( LegacyScope :: Empty ) ) ,
68
68
}
69
69
}
70
70
}
@@ -110,7 +110,7 @@ pub struct ParentScope<'a> {
110
110
// Macro namespace is separated into two sub-namespaces, one for bang macros and
111
111
// one for attribute-like macros (attributes, derives).
112
112
// We ignore resolutions from one sub-namespace when searching names in scope for another.
113
- fn sub_namespace_mismatch ( requirement : Option < MacroKind > , candidate : Option < MacroKind > ) -> bool {
113
+ fn sub_namespace_match ( candidate : Option < MacroKind > , requirement : Option < MacroKind > ) -> bool {
114
114
#[ derive( PartialEq ) ]
115
115
enum SubNS { Bang , AttrLike }
116
116
let sub_ns = |kind| match kind {
@@ -121,7 +121,7 @@ fn sub_namespace_mismatch(requirement: Option<MacroKind>, candidate: Option<Macr
121
121
let requirement = requirement. and_then ( |kind| sub_ns ( kind) ) ;
122
122
let candidate = candidate. and_then ( |kind| sub_ns ( kind) ) ;
123
123
// "No specific sub-namespace" means "matches anything" for both requirements and candidates.
124
- candidate. is_some ( ) && requirement. is_some ( ) && candidate ! = requirement
124
+ candidate. is_none ( ) || requirement. is_none ( ) || candidate = = requirement
125
125
}
126
126
127
127
impl < ' a , ' crateloader : ' a > base:: Resolver for Resolver < ' a , ' crateloader > {
@@ -136,7 +136,7 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
136
136
module : Cell :: new ( module) ,
137
137
def_index : module. def_id ( ) . unwrap ( ) . index ,
138
138
parent_legacy_scope : Cell :: new ( LegacyScope :: Empty ) ,
139
- output_legacy_scope : Cell :: new ( LegacyScope :: Empty ) ,
139
+ output_legacy_scope : Cell :: new ( Some ( LegacyScope :: Empty ) ) ,
140
140
} ) ) ;
141
141
mark
142
142
}
@@ -212,7 +212,7 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
212
212
expansion : mark,
213
213
} ;
214
214
fragment. visit_with ( & mut visitor) ;
215
- invocation. output_legacy_scope . set ( visitor. current_legacy_scope ) ;
215
+ invocation. output_legacy_scope . set ( Some ( visitor. current_legacy_scope ) ) ;
216
216
}
217
217
218
218
fn add_builtin ( & mut self , ident : ast:: Ident , ext : Lrc < SyntaxExtension > ) {
@@ -620,38 +620,50 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
620
620
let mut innermost_result: Option < ( & NameBinding , Flags ) > = None ;
621
621
622
622
// Go through all the scopes and try to resolve the name.
623
- let mut where_to_resolve = WhereToResolve :: DeriveHelpers ;
623
+ let mut where_to_resolve = if ns == MacroNS {
624
+ WhereToResolve :: DeriveHelpers
625
+ } else {
626
+ WhereToResolve :: Module ( parent_scope. module )
627
+ } ;
624
628
let mut use_prelude = !parent_scope. module . no_implicit_prelude ;
629
+ let mut determinacy = Determinacy :: Determined ;
625
630
loop {
626
631
let result = match where_to_resolve {
627
632
WhereToResolve :: DeriveHelpers => {
628
633
let mut result = Err ( Determinacy :: Determined ) ;
629
634
for derive in & parent_scope. derives {
630
635
let parent_scope = ParentScope { derives : Vec :: new ( ) , ..* parent_scope } ;
631
- if let Ok ( ( _, ext) ) = self . resolve_macro_to_def ( derive, MacroKind :: Derive ,
632
- & parent_scope, force) {
633
- if let SyntaxExtension :: ProcMacroDerive ( _, helper_attrs, _) = & * ext {
634
- if helper_attrs. contains ( & ident. name ) {
635
- let binding =
636
- ( Def :: NonMacroAttr ( NonMacroAttrKind :: DeriveHelper ) ,
637
- ty:: Visibility :: Public , derive. span , Mark :: root ( ) )
638
- . to_name_binding ( self . arenas ) ;
639
- result = Ok ( ( binding, Flags :: empty ( ) ) ) ;
640
- break ;
636
+ match self . resolve_macro_to_def ( derive, MacroKind :: Derive ,
637
+ & parent_scope, force) {
638
+ Ok ( ( _, ext) ) => {
639
+ if let SyntaxExtension :: ProcMacroDerive ( _, helpers, _) = & * ext {
640
+ if helpers. contains ( & ident. name ) {
641
+ let binding =
642
+ ( Def :: NonMacroAttr ( NonMacroAttrKind :: DeriveHelper ) ,
643
+ ty:: Visibility :: Public , derive. span , Mark :: root ( ) )
644
+ . to_name_binding ( self . arenas ) ;
645
+ result = Ok ( ( binding, Flags :: empty ( ) ) ) ;
646
+ break ;
647
+ }
641
648
}
642
649
}
650
+ Err ( Determinacy :: Determined ) => { }
651
+ Err ( Determinacy :: Undetermined ) =>
652
+ result = Err ( Determinacy :: Undetermined ) ,
643
653
}
644
654
}
645
655
result
646
656
}
647
657
WhereToResolve :: MacroRules ( legacy_scope) => match legacy_scope {
648
658
LegacyScope :: Binding ( legacy_binding) if ident == legacy_binding. ident =>
649
659
Ok ( ( legacy_binding. binding , Flags :: MACRO_RULES ) ) ,
660
+ LegacyScope :: Invocation ( invoc) if invoc. output_legacy_scope . get ( ) . is_none ( ) =>
661
+ Err ( Determinacy :: Undetermined ) ,
650
662
_ => Err ( Determinacy :: Determined ) ,
651
663
}
652
664
WhereToResolve :: Module ( module) => {
653
665
let orig_current_module = mem:: replace ( & mut self . current_module , module) ;
654
- let binding = self . resolve_ident_in_module_unadjusted (
666
+ let binding = self . resolve_ident_in_module_unadjusted_ext (
655
667
ModuleOrUniformRoot :: Module ( module) ,
656
668
ident,
657
669
ns,
@@ -660,21 +672,33 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
660
672
path_span,
661
673
) ;
662
674
self . current_module = orig_current_module;
663
- let misc_flags = if module. is_normal ( ) {
664
- Flags :: MISC_SUGGEST_SELF
665
- } else {
666
- Flags :: empty ( )
667
- } ;
668
- binding. map ( |binding| ( binding, Flags :: MODULE | misc_flags) )
675
+ match binding {
676
+ Ok ( binding) => {
677
+ let misc_flags = if module. is_normal ( ) {
678
+ Flags :: MISC_SUGGEST_SELF
679
+ } else {
680
+ Flags :: empty ( )
681
+ } ;
682
+ Ok ( ( binding, Flags :: MODULE | misc_flags) )
683
+ }
684
+ Err ( DeterminacyExt :: Undetermined ) =>
685
+ return Err ( Determinacy :: determined ( force) ) ,
686
+ Err ( DeterminacyExt :: WeakUndetermined ) => Err ( Determinacy :: Undetermined ) ,
687
+ Err ( DeterminacyExt :: Determined ) => Err ( Determinacy :: Determined ) ,
688
+ }
669
689
}
670
690
WhereToResolve :: MacroUsePrelude => {
671
- let mut result = Err ( Determinacy :: Determined ) ;
672
691
if use_prelude || self . session . rust_2015 ( ) {
673
- if let Some ( binding) = self . macro_use_prelude . get ( & ident. name ) . cloned ( ) {
674
- result = Ok ( ( binding, Flags :: PRELUDE | Flags :: MISC_FROM_PRELUDE ) ) ;
692
+ match self . macro_use_prelude . get ( & ident. name ) . cloned ( ) {
693
+ Some ( binding) =>
694
+ Ok ( ( binding, Flags :: PRELUDE | Flags :: MISC_FROM_PRELUDE ) ) ,
695
+ None => Err ( Determinacy :: determined (
696
+ self . graph_root . unresolved_invocations . borrow ( ) . is_empty ( )
697
+ ) )
675
698
}
699
+ } else {
700
+ Err ( Determinacy :: Determined )
676
701
}
677
- result
678
702
}
679
703
WhereToResolve :: BuiltinMacros => {
680
704
match self . builtin_macros . get ( & ident. name ) . cloned ( ) {
@@ -705,14 +729,17 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
705
729
}
706
730
}
707
731
WhereToResolve :: ExternPrelude => {
708
- let mut result = Err ( Determinacy :: Determined ) ;
709
732
if use_prelude {
710
- if let Some ( binding) = self . extern_prelude_get ( ident, !record_used,
711
- innermost_result. is_some ( ) ) {
712
- result = Ok ( ( binding, Flags :: PRELUDE ) ) ;
733
+ match self . extern_prelude_get ( ident, !record_used,
734
+ innermost_result. is_some ( ) ) {
735
+ Some ( binding) => Ok ( ( binding, Flags :: PRELUDE ) ) ,
736
+ None => Err ( Determinacy :: determined (
737
+ self . graph_root . unresolved_invocations . borrow ( ) . is_empty ( )
738
+ ) ) ,
713
739
}
740
+ } else {
741
+ Err ( Determinacy :: Determined )
714
742
}
715
- result
716
743
}
717
744
WhereToResolve :: ToolPrelude => {
718
745
if use_prelude && is_known_tool ( ident. name ) {
@@ -732,7 +759,6 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
732
759
ident,
733
760
ns,
734
761
false ,
735
- false ,
736
762
path_span,
737
763
) {
738
764
result = Ok ( ( binding, Flags :: PRELUDE | Flags :: MISC_FROM_PRELUDE ) ) ;
@@ -753,54 +779,8 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
753
779
}
754
780
} ;
755
781
756
- macro_rules! continue_search { ( ) => {
757
- where_to_resolve = match where_to_resolve {
758
- WhereToResolve :: DeriveHelpers =>
759
- WhereToResolve :: MacroRules ( parent_scope. legacy) ,
760
- WhereToResolve :: MacroRules ( legacy_scope) => match legacy_scope {
761
- LegacyScope :: Binding ( binding) =>
762
- WhereToResolve :: MacroRules ( binding. parent_legacy_scope) ,
763
- LegacyScope :: Invocation ( invocation) =>
764
- WhereToResolve :: MacroRules ( invocation. output_legacy_scope. get( ) ) ,
765
- LegacyScope :: Empty => WhereToResolve :: Module ( parent_scope. module) ,
766
- LegacyScope :: Uninitialized => unreachable!( ) ,
767
- }
768
- WhereToResolve :: Module ( module) => {
769
- match self . hygienic_lexical_parent( module, & mut ident. span) {
770
- Some ( parent_module) => WhereToResolve :: Module ( parent_module) ,
771
- None => {
772
- use_prelude = !module. no_implicit_prelude;
773
- match ns {
774
- TypeNS => WhereToResolve :: ExternPrelude ,
775
- ValueNS => WhereToResolve :: StdLibPrelude ,
776
- MacroNS => WhereToResolve :: MacroUsePrelude ,
777
- }
778
- }
779
- }
780
- }
781
- WhereToResolve :: MacroUsePrelude => WhereToResolve :: BuiltinMacros ,
782
- WhereToResolve :: BuiltinMacros => WhereToResolve :: BuiltinAttrs ,
783
- WhereToResolve :: BuiltinAttrs => WhereToResolve :: LegacyPluginHelpers ,
784
- WhereToResolve :: LegacyPluginHelpers => break , // nowhere else to search
785
- WhereToResolve :: ExternPrelude => WhereToResolve :: ToolPrelude ,
786
- WhereToResolve :: ToolPrelude => WhereToResolve :: StdLibPrelude ,
787
- WhereToResolve :: StdLibPrelude => match ns {
788
- TypeNS => WhereToResolve :: BuiltinTypes ,
789
- ValueNS => break , // nowhere else to search
790
- MacroNS => unreachable!( ) ,
791
- }
792
- WhereToResolve :: BuiltinTypes => break , // nowhere else to search
793
- } ;
794
-
795
- continue ;
796
- } }
797
-
798
782
match result {
799
- Ok ( ( binding, flags) ) => {
800
- if sub_namespace_mismatch ( macro_kind, binding. macro_kind ( ) ) {
801
- continue_search ! ( ) ;
802
- }
803
-
783
+ Ok ( ( binding, flags) ) if sub_namespace_match ( binding. macro_kind ( ) , macro_kind) => {
804
784
if !record_used {
805
785
return Ok ( binding) ;
806
786
}
@@ -861,22 +841,60 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
861
841
// Found the first solution.
862
842
innermost_result = Some ( ( binding, flags) ) ;
863
843
}
864
-
865
- continue_search ! ( ) ;
866
- } ,
867
- Err ( Determinacy :: Determined ) => {
868
- continue_search ! ( ) ;
869
844
}
870
- Err ( Determinacy :: Undetermined ) => return Err ( Determinacy :: determined ( force) ) ,
845
+ Ok ( ..) | Err ( Determinacy :: Determined ) => { }
846
+ Err ( Determinacy :: Undetermined ) => determinacy = Determinacy :: Undetermined
871
847
}
848
+
849
+ where_to_resolve = match where_to_resolve {
850
+ WhereToResolve :: DeriveHelpers =>
851
+ WhereToResolve :: MacroRules ( parent_scope. legacy ) ,
852
+ WhereToResolve :: MacroRules ( legacy_scope) => match legacy_scope {
853
+ LegacyScope :: Binding ( binding) => WhereToResolve :: MacroRules (
854
+ binding. parent_legacy_scope
855
+ ) ,
856
+ LegacyScope :: Invocation ( invoc) => WhereToResolve :: MacroRules (
857
+ invoc. output_legacy_scope . get ( ) . unwrap_or ( invoc. parent_legacy_scope . get ( ) )
858
+ ) ,
859
+ LegacyScope :: Empty => WhereToResolve :: Module ( parent_scope. module ) ,
860
+ LegacyScope :: Uninitialized => unreachable ! ( ) ,
861
+ }
862
+ WhereToResolve :: Module ( module) => {
863
+ match self . hygienic_lexical_parent ( module, & mut ident. span ) {
864
+ Some ( parent_module) => WhereToResolve :: Module ( parent_module) ,
865
+ None => {
866
+ use_prelude = !module. no_implicit_prelude ;
867
+ match ns {
868
+ TypeNS => WhereToResolve :: ExternPrelude ,
869
+ ValueNS => WhereToResolve :: StdLibPrelude ,
870
+ MacroNS => WhereToResolve :: MacroUsePrelude ,
871
+ }
872
+ }
873
+ }
874
+ }
875
+ WhereToResolve :: MacroUsePrelude => WhereToResolve :: BuiltinMacros ,
876
+ WhereToResolve :: BuiltinMacros => WhereToResolve :: BuiltinAttrs ,
877
+ WhereToResolve :: BuiltinAttrs => WhereToResolve :: LegacyPluginHelpers ,
878
+ WhereToResolve :: LegacyPluginHelpers => break , // nowhere else to search
879
+ WhereToResolve :: ExternPrelude => WhereToResolve :: ToolPrelude ,
880
+ WhereToResolve :: ToolPrelude => WhereToResolve :: StdLibPrelude ,
881
+ WhereToResolve :: StdLibPrelude => match ns {
882
+ TypeNS => WhereToResolve :: BuiltinTypes ,
883
+ ValueNS => break , // nowhere else to search
884
+ MacroNS => unreachable ! ( ) ,
885
+ }
886
+ WhereToResolve :: BuiltinTypes => break , // nowhere else to search
887
+ } ;
888
+
889
+ continue ;
872
890
}
873
891
874
892
// The first found solution was the only one, return it.
875
893
if let Some ( ( binding, ..) ) = innermost_result {
876
894
return Ok ( binding) ;
877
895
}
878
896
879
- let determinacy = Determinacy :: determined ( force) ;
897
+ let determinacy = Determinacy :: determined ( determinacy == Determinacy :: Determined || force) ;
880
898
if determinacy == Determinacy :: Determined && macro_kind == Some ( MacroKind :: Attr ) {
881
899
// For single-segment attributes interpret determinate "no resolution" as a custom
882
900
// attribute. (Lexical resolution implies the first segment and attr kind should imply
@@ -1020,7 +1038,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
1020
1038
def_index : invoc. def_index ,
1021
1039
module : Cell :: new ( graph_root) ,
1022
1040
parent_legacy_scope : Cell :: new ( LegacyScope :: Uninitialized ) ,
1023
- output_legacy_scope : Cell :: new ( LegacyScope :: Uninitialized ) ,
1041
+ output_legacy_scope : Cell :: new ( None ) ,
1024
1042
} )
1025
1043
} ) ;
1026
1044
} ;
0 commit comments