@@ -835,18 +835,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
835
835
trait_ref : ty:: PolyTraitRef < ' tcx > ,
836
836
) -> Option < EvaluationResult > {
837
837
let tcx = self . tcx ( ) ;
838
- if self . can_use_global_caches ( param_env) {
839
- let cache = tcx. evaluation_cache . hashmap . borrow ( ) ;
840
- if let Some ( cached) = cache. get ( & param_env. and ( trait_ref) ) {
841
- return Some ( cached. get ( tcx) ) ;
842
- }
843
- }
844
- self . infcx
845
- . evaluation_cache
846
- . hashmap
847
- . borrow ( )
848
- . get ( & param_env. and ( trait_ref) )
849
- . map ( |v| v. get ( tcx) )
838
+ let cache = if self . can_use_global_caches ( param_env) && !trait_ref. has_local_value ( ) {
839
+ & tcx. evaluation_cache
840
+ } else {
841
+ & self . infcx . evaluation_cache
842
+ } ;
843
+
844
+ cache. hashmap . borrow ( ) . get ( & param_env. and ( trait_ref) ) . map ( |v| v. get ( tcx) )
850
845
}
851
846
852
847
fn insert_evaluation_cache (
@@ -862,28 +857,22 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
862
857
return ;
863
858
}
864
859
865
- if self . can_use_global_caches ( param_env) {
866
- if !trait_ref. has_local_value ( ) {
867
- debug ! (
868
- "insert_evaluation_cache(trait_ref={:?}, candidate={:?}) global" ,
869
- trait_ref, result,
870
- ) ;
871
- // This may overwrite the cache with the same value
872
- // FIXME: Due to #50507 this overwrites the different values
873
- // This should be changed to use HashMapExt::insert_same
874
- // when that is fixed
875
- self . tcx ( )
876
- . evaluation_cache
877
- . hashmap
878
- . borrow_mut ( )
879
- . insert ( param_env. and ( trait_ref) , WithDepNode :: new ( dep_node, result) ) ;
880
- return ;
881
- }
882
- }
860
+ let cache = if self . can_use_global_caches ( param_env) && !trait_ref. has_local_value ( ) {
861
+ debug ! (
862
+ "insert_evaluation_cache(trait_ref={:?}, candidate={:?}) global" ,
863
+ trait_ref, result,
864
+ ) ;
865
+ // This may overwrite the cache with the same value
866
+ // FIXME: Due to #50507 this overwrites the different values
867
+ // This should be changed to use HashMapExt::insert_same
868
+ // when that is fixed
869
+ & self . tcx ( ) . evaluation_cache
870
+ } else {
871
+ debug ! ( "insert_evaluation_cache(trait_ref={:?}, candidate={:?})" , trait_ref, result, ) ;
872
+ & self . infcx . evaluation_cache
873
+ } ;
883
874
884
- debug ! ( "insert_evaluation_cache(trait_ref={:?}, candidate={:?})" , trait_ref, result, ) ;
885
- self . infcx
886
- . evaluation_cache
875
+ cache
887
876
. hashmap
888
877
. borrow_mut ( )
889
878
. insert ( param_env. and ( trait_ref) , WithDepNode :: new ( dep_node, result) ) ;
@@ -982,6 +971,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
982
971
cache_fresh_trait_pred,
983
972
dep_node,
984
973
candidate. clone ( ) ,
974
+ stack. obligation . cause . span ,
985
975
) ;
986
976
candidate
987
977
}
@@ -1250,18 +1240,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
1250
1240
) -> Option < SelectionResult < ' tcx , SelectionCandidate < ' tcx > > > {
1251
1241
let tcx = self . tcx ( ) ;
1252
1242
let trait_ref = & cache_fresh_trait_pred. skip_binder ( ) . trait_ref ;
1253
- if self . can_use_global_caches ( param_env) {
1254
- let cache = tcx. selection_cache . hashmap . borrow ( ) ;
1255
- if let Some ( cached) = cache. get ( & param_env. and ( * trait_ref) ) {
1256
- return Some ( cached. get ( tcx) ) ;
1257
- }
1258
- }
1259
- self . infcx
1260
- . selection_cache
1261
- . hashmap
1262
- . borrow ( )
1263
- . get ( & param_env. and ( * trait_ref) )
1264
- . map ( |v| v. get ( tcx) )
1243
+ let cache = if self . can_use_global_caches ( param_env) && !trait_ref. has_local_value ( ) {
1244
+ & tcx. selection_cache
1245
+ } else {
1246
+ & self . infcx . selection_cache
1247
+ } ;
1248
+
1249
+ cache. hashmap . borrow ( ) . get ( & param_env. and ( * trait_ref) ) . map ( |v| v. get ( tcx) )
1265
1250
}
1266
1251
1267
1252
/// Determines whether can we safely cache the result
@@ -1296,6 +1281,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
1296
1281
cache_fresh_trait_pred : ty:: PolyTraitPredicate < ' tcx > ,
1297
1282
dep_node : DepNodeIndex ,
1298
1283
candidate : SelectionResult < ' tcx , SelectionCandidate < ' tcx > > ,
1284
+ span : rustc_span:: Span ,
1299
1285
) {
1300
1286
let tcx = self . tcx ( ) ;
1301
1287
let trait_ref = cache_fresh_trait_pred. skip_binder ( ) . trait_ref ;
@@ -1309,31 +1295,35 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
1309
1295
return ;
1310
1296
}
1311
1297
1312
- if self . can_use_global_caches ( param_env) {
1313
- if let Err ( Overflow ) = candidate {
1314
- // Don't cache overflow globally; we only produce this in certain modes.
1315
- } else if !trait_ref. has_local_value ( ) {
1316
- if !candidate. has_local_value ( ) {
1317
- debug ! (
1318
- "insert_candidate_cache(trait_ref={:?}, candidate={:?}) global" ,
1319
- trait_ref, candidate,
1320
- ) ;
1321
- // This may overwrite the cache with the same value.
1322
- tcx. selection_cache
1323
- . hashmap
1324
- . borrow_mut ( )
1325
- . insert ( param_env. and ( trait_ref) , WithDepNode :: new ( dep_node, candidate) ) ;
1326
- return ;
1327
- }
1328
- }
1298
+ // HACK(eddyb) never cache overflow (this check used to be global-only).
1299
+ if let Err ( Overflow ) = candidate {
1300
+ return ;
1329
1301
}
1330
1302
1331
- debug ! (
1332
- "insert_candidate_cache(trait_ref={:?}, candidate={:?}) local" ,
1333
- trait_ref, candidate,
1334
- ) ;
1335
- self . infcx
1336
- . selection_cache
1303
+ let cache = if self . can_use_global_caches ( param_env) && !trait_ref. has_local_value ( ) {
1304
+ if candidate. has_local_value ( ) {
1305
+ span_bug ! (
1306
+ span,
1307
+ "selecting inference-free `{}` resulted in `{:?}`?!" ,
1308
+ trait_ref,
1309
+ candidate,
1310
+ ) ;
1311
+ }
1312
+ debug ! (
1313
+ "insert_candidate_cache(trait_ref={:?}, candidate={:?}) global" ,
1314
+ trait_ref, candidate,
1315
+ ) ;
1316
+ // This may overwrite the cache with the same value.
1317
+ & tcx. selection_cache
1318
+ } else {
1319
+ debug ! (
1320
+ "insert_candidate_cache(trait_ref={:?}, candidate={:?}) local" ,
1321
+ trait_ref, candidate,
1322
+ ) ;
1323
+ & self . infcx . selection_cache
1324
+ } ;
1325
+
1326
+ cache
1337
1327
. hashmap
1338
1328
. borrow_mut ( )
1339
1329
. insert ( param_env. and ( trait_ref) , WithDepNode :: new ( dep_node, candidate) ) ;
0 commit comments