@@ -86,12 +86,7 @@ pub trait Delegate: Sized {
86
86
kind : PathKind ,
87
87
input : <Self :: Cx as Cx >:: Input ,
88
88
) -> <Self :: Cx as Cx >:: Result ;
89
- fn is_initial_provisional_result (
90
- cx : Self :: Cx ,
91
- kind : PathKind ,
92
- input : <Self :: Cx as Cx >:: Input ,
93
- result : <Self :: Cx as Cx >:: Result ,
94
- ) -> bool ;
89
+ fn is_initial_provisional_result ( result : <Self :: Cx as Cx >:: Result ) -> Option < PathKind > ;
95
90
fn on_stack_overflow ( cx : Self :: Cx , input : <Self :: Cx as Cx >:: Input ) -> <Self :: Cx as Cx >:: Result ;
96
91
fn on_fixpoint_overflow (
97
92
cx : Self :: Cx ,
@@ -215,6 +210,27 @@ impl HeadUsages {
215
210
let HeadUsages { inductive, unknown, coinductive, forced_ambiguity } = self ;
216
211
inductive == 0 && unknown == 0 && coinductive == 0 && forced_ambiguity == 0
217
212
}
213
+
214
+ fn is_single ( self , path_kind : PathKind ) -> bool {
215
+ match path_kind {
216
+ PathKind :: Inductive => matches ! (
217
+ self ,
218
+ HeadUsages { inductive: _, unknown: 0 , coinductive: 0 , forced_ambiguity: 0 } ,
219
+ ) ,
220
+ PathKind :: Unknown => matches ! (
221
+ self ,
222
+ HeadUsages { inductive: 0 , unknown: _, coinductive: 0 , forced_ambiguity: 0 } ,
223
+ ) ,
224
+ PathKind :: Coinductive => matches ! (
225
+ self ,
226
+ HeadUsages { inductive: 0 , unknown: 0 , coinductive: _, forced_ambiguity: 0 } ,
227
+ ) ,
228
+ PathKind :: ForcedAmbiguity => matches ! (
229
+ self ,
230
+ HeadUsages { inductive: 0 , unknown: 0 , coinductive: 0 , forced_ambiguity: _ } ,
231
+ ) ,
232
+ }
233
+ }
218
234
}
219
235
220
236
#[ derive( Debug , Default ) ]
@@ -888,7 +904,20 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D> {
888
904
!entries. is_empty ( )
889
905
} ) ;
890
906
}
907
+ }
908
+
909
+ /// We need to rebase provisional cache entries when popping one of their cycle
910
+ /// heads from the stack. This may not necessarily mean that we've actually
911
+ /// reached a fixpoint for that cycle head, which impacts the way we rebase
912
+ /// provisional cache entries.
913
+ enum RebaseReason {
914
+ NoCycleUsages ,
915
+ Ambiguity ,
916
+ Overflow ,
917
+ ReachedFixpoint ( Option < PathKind > ) ,
918
+ }
891
919
920
+ impl < D : Delegate < Cx = X > , X : Cx > SearchGraph < D , X > {
892
921
/// A necessary optimization to handle complex solver cycles. A provisional cache entry
893
922
/// relies on a set of cycle heads and the path towards these heads. When popping a cycle
894
923
/// head from the stack after we've finished computing it, we can't be sure that the
@@ -908,8 +937,9 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D> {
908
937
/// to me.
909
938
fn rebase_provisional_cache_entries (
910
939
& mut self ,
940
+ cx : X ,
911
941
stack_entry : & StackEntry < X > ,
912
- mut mutate_result : impl FnMut ( X :: Input , X :: Result ) -> X :: Result ,
942
+ rebase_reason : RebaseReason ,
913
943
) {
914
944
let popped_head_index = self . stack . next_index ( ) ;
915
945
#[ allow( rustc:: potential_query_instability) ]
@@ -927,6 +957,10 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D> {
927
957
return true ;
928
958
} ;
929
959
960
+ let Some ( new_highest_head_index) = heads. opt_highest_cycle_head_index ( ) else {
961
+ return false ;
962
+ } ;
963
+
930
964
// We're rebasing an entry `e` over a head `p`. This head
931
965
// has a number of own heads `h` it depends on.
932
966
//
@@ -941,6 +975,22 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D> {
941
975
heads. insert ( head_index, PathsToNested :: EMPTY , HeadUsages :: default ( ) ) ;
942
976
}
943
977
} else {
978
+ // We may need to mutate the result of our cycle head in case we did not reach
979
+ // a fixpoint.
980
+ * result = match rebase_reason {
981
+ RebaseReason :: NoCycleUsages => return false ,
982
+ RebaseReason :: Ambiguity => D :: propagate_ambiguity ( cx, input, * result) ,
983
+ RebaseReason :: Overflow => D :: on_fixpoint_overflow ( cx, input) ,
984
+ RebaseReason :: ReachedFixpoint ( None ) => * result,
985
+ RebaseReason :: ReachedFixpoint ( Some ( path_kind) ) => {
986
+ if popped_head. usages . is_single ( path_kind) {
987
+ * result
988
+ } else {
989
+ return false ;
990
+ }
991
+ }
992
+ } ;
993
+
944
994
// The entry `e` actually depends on the value of `p`. We need
945
995
// to make sure that the value of `p` wouldn't change even if we
946
996
// were to reevaluate it as a nested goal of `e` instead. For this
@@ -979,20 +1029,14 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D> {
979
1029
}
980
1030
}
981
1031
982
- let Some ( head_index) = heads. opt_highest_cycle_head_index ( ) else {
983
- return false ;
984
- } ;
985
-
986
1032
// We now care about the path from the next highest cycle head to the
987
1033
// provisional cache entry.
988
1034
* path_from_head = path_from_head. extend ( Self :: cycle_path_kind (
989
1035
& self . stack ,
990
1036
stack_entry. step_kind_from_parent ,
991
- head_index ,
1037
+ new_highest_head_index ,
992
1038
) ) ;
993
- // Mutate the result of the provisional cache entry in case we did
994
- // not reach a fixpoint.
995
- * result = mutate_result ( input, * result) ;
1039
+
996
1040
true
997
1041
} ) ;
998
1042
!entries. is_empty ( )
@@ -1209,33 +1253,19 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D> {
1209
1253
/// Whether we've reached a fixpoint when evaluating a cycle head.
1210
1254
fn reached_fixpoint (
1211
1255
& mut self ,
1212
- cx : X ,
1213
1256
stack_entry : & StackEntry < X > ,
1214
1257
usages : HeadUsages ,
1215
1258
result : X :: Result ,
1216
- ) -> bool {
1259
+ ) -> Result < Option < PathKind > , ( ) > {
1217
1260
let provisional_result = stack_entry. provisional_result ;
1218
- if usages. is_empty ( ) {
1219
- true
1220
- } else if let Some ( provisional_result) = provisional_result {
1221
- provisional_result == result
1261
+ if let Some ( provisional_result) = provisional_result {
1262
+ if provisional_result == result { Ok ( None ) } else { Err ( ( ) ) }
1263
+ } else if let Some ( path_kind) = D :: is_initial_provisional_result ( result)
1264
+ . filter ( |& path_kind| usages. is_single ( path_kind) )
1265
+ {
1266
+ Ok ( Some ( path_kind) )
1222
1267
} else {
1223
- let check = |k| D :: is_initial_provisional_result ( cx, k, stack_entry. input , result) ;
1224
- match usages {
1225
- HeadUsages { inductive : _, unknown : 0 , coinductive : 0 , forced_ambiguity : 0 } => {
1226
- check ( PathKind :: Inductive )
1227
- }
1228
- HeadUsages { inductive : 0 , unknown : _, coinductive : 0 , forced_ambiguity : 0 } => {
1229
- check ( PathKind :: Unknown )
1230
- }
1231
- HeadUsages { inductive : 0 , unknown : 0 , coinductive : _, forced_ambiguity : 0 } => {
1232
- check ( PathKind :: Coinductive )
1233
- }
1234
- HeadUsages { inductive : 0 , unknown : 0 , coinductive : 0 , forced_ambiguity : _ } => {
1235
- check ( PathKind :: ForcedAmbiguity )
1236
- }
1237
- _ => false ,
1238
- }
1268
+ Err ( ( ) )
1239
1269
}
1240
1270
}
1241
1271
@@ -1280,8 +1310,19 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D> {
1280
1310
// is equal to the provisional result of the previous iteration, or because
1281
1311
// this was only the head of either coinductive or inductive cycles, and the
1282
1312
// final result is equal to the initial response for that case.
1283
- if self . reached_fixpoint ( cx, & stack_entry, usages, result) {
1284
- self . rebase_provisional_cache_entries ( & stack_entry, |_, result| result) ;
1313
+ if let Ok ( fixpoint) = self . reached_fixpoint ( & stack_entry, usages, result) {
1314
+ self . rebase_provisional_cache_entries (
1315
+ cx,
1316
+ & stack_entry,
1317
+ RebaseReason :: ReachedFixpoint ( fixpoint) ,
1318
+ ) ;
1319
+ return EvaluationResult :: finalize ( stack_entry, encountered_overflow, result) ;
1320
+ } else if usages. is_empty ( ) {
1321
+ self . rebase_provisional_cache_entries (
1322
+ cx,
1323
+ & stack_entry,
1324
+ RebaseReason :: NoCycleUsages ,
1325
+ ) ;
1285
1326
return EvaluationResult :: finalize ( stack_entry, encountered_overflow, result) ;
1286
1327
}
1287
1328
@@ -1298,9 +1339,7 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D> {
1298
1339
// we also taint all provisional cache entries which depend on the
1299
1340
// current goal.
1300
1341
if D :: is_ambiguous_result ( result) {
1301
- self . rebase_provisional_cache_entries ( & stack_entry, |input, _| {
1302
- D :: propagate_ambiguity ( cx, input, result)
1303
- } ) ;
1342
+ self . rebase_provisional_cache_entries ( cx, & stack_entry, RebaseReason :: Ambiguity ) ;
1304
1343
return EvaluationResult :: finalize ( stack_entry, encountered_overflow, result) ;
1305
1344
} ;
1306
1345
@@ -1310,9 +1349,7 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D> {
1310
1349
if i >= D :: FIXPOINT_STEP_LIMIT {
1311
1350
debug ! ( "canonical cycle overflow" ) ;
1312
1351
let result = D :: on_fixpoint_overflow ( cx, input) ;
1313
- self . rebase_provisional_cache_entries ( & stack_entry, |input, _| {
1314
- D :: on_fixpoint_overflow ( cx, input)
1315
- } ) ;
1352
+ self . rebase_provisional_cache_entries ( cx, & stack_entry, RebaseReason :: Overflow ) ;
1316
1353
return EvaluationResult :: finalize ( stack_entry, encountered_overflow, result) ;
1317
1354
}
1318
1355
0 commit comments