2
2
3
3
pub ( super ) mod structural_traits;
4
4
5
+ use std:: ops:: ControlFlow ;
6
+
5
7
use derive_where:: derive_where;
6
8
use rustc_type_ir:: inherent:: * ;
7
9
use rustc_type_ir:: lang_items:: TraitSolverLangItem ;
8
10
use rustc_type_ir:: {
9
- self as ty, Interner , TypeFoldable , TypeVisitableExt as _, TypingMode , Upcast as _, elaborate,
11
+ self as ty, Interner , TypeFoldable , TypeSuperVisitable , TypeVisitable , TypeVisitableExt as _,
12
+ TypeVisitor , TypingMode , Upcast as _, elaborate,
10
13
} ;
11
14
use tracing:: { debug, instrument} ;
12
15
13
- use super :: has_only_region_constraints;
14
16
use super :: trait_goals:: TraitGoalProvenVia ;
17
+ use super :: { has_only_region_constraints, inspect} ;
15
18
use crate :: delegate:: SolverDelegate ;
16
19
use crate :: solve:: inspect:: ProbeKind ;
17
20
use crate :: solve:: {
49
52
50
53
fn trait_def_id ( self , cx : I ) -> I :: DefId ;
51
54
52
- /// Try equating an assumption predicate against a goal's predicate. If it
53
- /// holds, then execute the `then` callback, which should do any additional
54
- /// work, then produce a response (typically by executing
55
- /// [`EvalCtxt::evaluate_added_goals_and_make_canonical_response`]).
56
- fn probe_and_match_goal_against_assumption (
57
- ecx : & mut EvalCtxt < ' _ , D > ,
58
- source : CandidateSource < I > ,
59
- goal : Goal < I , Self > ,
60
- assumption : I :: Clause ,
61
- then : impl FnOnce ( & mut EvalCtxt < ' _ , D > ) -> QueryResult < I > ,
62
- ) -> Result < Candidate < I > , NoSolution > ;
63
-
64
55
/// Consider a clause, which consists of a "assumption" and some "requirements",
65
56
/// to satisfy a goal. If the requirements hold, then attempt to satisfy our
66
57
/// goal by equating it with the assumption.
@@ -119,6 +110,68 @@ where
119
110
alias_ty : ty:: AliasTy < I > ,
120
111
) -> Vec < Candidate < I > > ;
121
112
113
+ fn probe_and_consider_param_env_candidate (
114
+ ecx : & mut EvalCtxt < ' _ , D > ,
115
+ goal : Goal < I , Self > ,
116
+ assumption : I :: Clause ,
117
+ idx : usize ,
118
+ ) -> Result < Candidate < I > , NoSolution > {
119
+ Self :: fast_reject_assumption ( ecx, goal, assumption) ?;
120
+
121
+ ecx. probe ( |candidate : & Result < Candidate < I > , NoSolution > | match candidate {
122
+ Ok ( candidate) => inspect:: ProbeKind :: TraitCandidate {
123
+ source : candidate. source ,
124
+ result : Ok ( candidate. result ) ,
125
+ } ,
126
+ Err ( NoSolution ) => inspect:: ProbeKind :: TraitCandidate {
127
+ source : CandidateSource :: BuiltinImpl ( BuiltinImplSource :: Misc ) ,
128
+ result : Err ( NoSolution ) ,
129
+ } ,
130
+ } )
131
+ . enter ( |ecx| {
132
+ Self :: match_assumption ( ecx, goal, assumption) ?;
133
+ let source = ecx. characterize_param_env_assumption ( goal. param_env , assumption, idx) ?;
134
+ Ok ( Candidate {
135
+ source,
136
+ result : ecx. evaluate_added_goals_and_make_canonical_response ( Certainty :: Yes ) ?,
137
+ } )
138
+ } )
139
+ }
140
+
141
+ /// Try equating an assumption predicate against a goal's predicate. If it
142
+ /// holds, then execute the `then` callback, which should do any additional
143
+ /// work, then produce a response (typically by executing
144
+ /// [`EvalCtxt::evaluate_added_goals_and_make_canonical_response`]).
145
+ fn probe_and_match_goal_against_assumption (
146
+ ecx : & mut EvalCtxt < ' _ , D > ,
147
+ source : CandidateSource < I > ,
148
+ goal : Goal < I , Self > ,
149
+ assumption : I :: Clause ,
150
+ then : impl FnOnce ( & mut EvalCtxt < ' _ , D > ) -> QueryResult < I > ,
151
+ ) -> Result < Candidate < I > , NoSolution > {
152
+ Self :: fast_reject_assumption ( ecx, goal, assumption) ?;
153
+
154
+ ecx. probe_trait_candidate ( source) . enter ( |ecx| {
155
+ Self :: match_assumption ( ecx, goal, assumption) ?;
156
+ then ( ecx)
157
+ } )
158
+ }
159
+
160
+ /// Try to reject the assumption based off of simple heuristics, such as [`ty::ClauseKind`]
161
+ /// and [`I::DefId`].
162
+ fn fast_reject_assumption (
163
+ ecx : & mut EvalCtxt < ' _ , D > ,
164
+ goal : Goal < I , Self > ,
165
+ assumption : I :: Clause ,
166
+ ) -> Result < ( ) , NoSolution > ;
167
+
168
+ /// Relate the goal and assumption.
169
+ fn match_assumption (
170
+ ecx : & mut EvalCtxt < ' _ , D > ,
171
+ goal : Goal < I , Self > ,
172
+ assumption : I :: Clause ,
173
+ ) -> Result < ( ) , NoSolution > ;
174
+
122
175
fn consider_impl_candidate (
123
176
ecx : & mut EvalCtxt < ' _ , D > ,
124
177
goal : Goal < I , Self > ,
@@ -501,13 +554,7 @@ where
501
554
candidates : & mut Vec < Candidate < I > > ,
502
555
) {
503
556
for ( i, assumption) in goal. param_env . caller_bounds ( ) . iter ( ) . enumerate ( ) {
504
- candidates. extend ( G :: probe_and_consider_implied_clause (
505
- self ,
506
- CandidateSource :: ParamEnv ( i) ,
507
- goal,
508
- assumption,
509
- [ ] ,
510
- ) ) ;
557
+ candidates. extend ( G :: probe_and_consider_param_env_candidate ( self , goal, assumption, i) ) ;
511
558
}
512
559
}
513
560
@@ -887,11 +934,20 @@ where
887
934
// See `tests/ui/winnowing/norm-where-bound-gt-alias-bound.rs`.
888
935
let mut considered_candidates: Vec < _ > = if candidates_from_env_and_bounds
889
936
. iter ( )
890
- . any ( |c| matches ! ( c. source, CandidateSource :: ParamEnv ( _) ) )
891
- {
937
+ . any ( |c| {
938
+ matches ! (
939
+ c. source,
940
+ CandidateSource :: ParamEnv ( _) | CandidateSource :: GlobalParamEnv ( _)
941
+ )
942
+ } ) {
892
943
candidates_from_env_and_bounds
893
944
. into_iter ( )
894
- . filter ( |c| matches ! ( c. source, CandidateSource :: ParamEnv ( _) ) )
945
+ . filter ( |c| {
946
+ matches ! (
947
+ c. source,
948
+ CandidateSource :: ParamEnv ( _) | CandidateSource :: GlobalParamEnv ( _)
949
+ )
950
+ } )
895
951
. map ( |c| c. result )
896
952
. collect ( )
897
953
} else {
@@ -920,7 +976,12 @@ where
920
976
// (for example, and ideally only) when proving item bounds for an impl.
921
977
let candidates_from_env: Vec < _ > = candidates
922
978
. iter ( )
923
- . filter ( |c| matches ! ( c. source, CandidateSource :: ParamEnv ( _) ) )
979
+ . filter ( |c| {
980
+ matches ! (
981
+ c. source,
982
+ CandidateSource :: ParamEnv ( _) | CandidateSource :: GlobalParamEnv ( _)
983
+ )
984
+ } )
924
985
. map ( |c| c. result )
925
986
. collect ( ) ;
926
987
if let Some ( response) = self . try_merge_responses ( & candidates_from_env) {
@@ -943,4 +1004,77 @@ where
943
1004
}
944
1005
}
945
1006
}
1007
+
1008
+ fn characterize_param_env_assumption (
1009
+ & mut self ,
1010
+ param_env : I :: ParamEnv ,
1011
+ assumption : I :: Clause ,
1012
+ idx : usize ,
1013
+ ) -> Result < CandidateSource < I > , NoSolution > {
1014
+ // FIXME:
1015
+ if assumption. has_bound_vars ( ) {
1016
+ return Ok ( CandidateSource :: ParamEnv ( idx) ) ;
1017
+ }
1018
+
1019
+ match assumption. visit_with ( & mut FindParamInClause { ecx : self , param_env } ) {
1020
+ ControlFlow :: Break ( Err ( NoSolution ) ) => Err ( NoSolution ) ,
1021
+ ControlFlow :: Break ( Ok ( ( ) ) ) => Ok ( CandidateSource :: ParamEnv ( idx) ) ,
1022
+ ControlFlow :: Continue ( ( ) ) => Ok ( CandidateSource :: GlobalParamEnv ( idx) ) ,
1023
+ }
1024
+ }
1025
+ }
1026
+
1027
+ struct FindParamInClause < ' a , ' b , D : SolverDelegate < Interner = I > , I : Interner > {
1028
+ ecx : & ' a mut EvalCtxt < ' b , D > ,
1029
+ param_env : I :: ParamEnv ,
1030
+ }
1031
+
1032
+ impl < D , I > TypeVisitor < I > for FindParamInClause < ' _ , ' _ , D , I >
1033
+ where
1034
+ D : SolverDelegate < Interner = I > ,
1035
+ I : Interner ,
1036
+ {
1037
+ type Result = ControlFlow < Result < ( ) , NoSolution > > ;
1038
+
1039
+ fn visit_binder < T : TypeFoldable < I > > ( & mut self , t : & ty:: Binder < I , T > ) -> Self :: Result {
1040
+ self . ecx . enter_forall ( t. clone ( ) , |ecx, v| {
1041
+ v. visit_with ( & mut FindParamInClause { ecx, param_env : self . param_env } )
1042
+ } )
1043
+ }
1044
+
1045
+ fn visit_ty ( & mut self , ty : I :: Ty ) -> Self :: Result {
1046
+ let Ok ( ty) = self . ecx . structurally_normalize_ty ( self . param_env , ty) else {
1047
+ return ControlFlow :: Break ( Err ( NoSolution ) ) ;
1048
+ } ;
1049
+ let ty = self . ecx . eager_resolve ( ty) ;
1050
+
1051
+ if let ty:: Placeholder ( _) = ty. kind ( ) {
1052
+ ControlFlow :: Break ( Ok ( ( ) ) )
1053
+ } else {
1054
+ ty. super_visit_with ( self )
1055
+ }
1056
+ }
1057
+
1058
+ fn visit_const ( & mut self , ct : I :: Const ) -> Self :: Result {
1059
+ let Ok ( ct) = self . ecx . structurally_normalize_const ( self . param_env , ct) else {
1060
+ return ControlFlow :: Break ( Err ( NoSolution ) ) ;
1061
+ } ;
1062
+ let ct = self . ecx . eager_resolve ( ct) ;
1063
+
1064
+ if let ty:: ConstKind :: Placeholder ( _) = ct. kind ( ) {
1065
+ ControlFlow :: Break ( Ok ( ( ) ) )
1066
+ } else {
1067
+ ct. super_visit_with ( self )
1068
+ }
1069
+ }
1070
+
1071
+ fn visit_region ( & mut self , r : I :: Region ) -> Self :: Result {
1072
+ match r. kind ( ) {
1073
+ ty:: ReStatic | ty:: ReError ( _) => ControlFlow :: Continue ( ( ) ) ,
1074
+ ty:: ReVar ( _) | ty:: RePlaceholder ( _) => ControlFlow :: Break ( Ok ( ( ) ) ) ,
1075
+ ty:: ReErased | ty:: ReEarlyParam ( _) | ty:: ReLateParam ( _) | ty:: ReBound ( ..) => {
1076
+ unreachable ! ( )
1077
+ }
1078
+ }
1079
+ }
946
1080
}
0 commit comments