@@ -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
@@ -1298,6 +1283,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
1298
1283
cache_fresh_trait_pred : ty:: PolyTraitPredicate < ' tcx > ,
1299
1284
dep_node : DepNodeIndex ,
1300
1285
candidate : SelectionResult < ' tcx , SelectionCandidate < ' tcx > > ,
1286
+ span : rustc_span:: Span ,
1301
1287
) {
1302
1288
let tcx = self . tcx ( ) ;
1303
1289
let trait_ref = cache_fresh_trait_pred. skip_binder ( ) . trait_ref ;
@@ -1311,31 +1297,35 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
1311
1297
return ;
1312
1298
}
1313
1299
1314
- if self . can_use_global_caches ( param_env) {
1315
- if let Err ( Overflow ) = candidate {
1316
- // Don't cache overflow globally; we only produce this in certain modes.
1317
- } else if !trait_ref. has_local_value ( ) {
1318
- if !candidate. has_local_value ( ) {
1319
- debug ! (
1320
- "insert_candidate_cache(trait_ref={:?}, candidate={:?}) global" ,
1321
- trait_ref, candidate,
1322
- ) ;
1323
- // This may overwrite the cache with the same value.
1324
- tcx. selection_cache
1325
- . hashmap
1326
- . borrow_mut ( )
1327
- . insert ( param_env. and ( trait_ref) , WithDepNode :: new ( dep_node, candidate) ) ;
1328
- return ;
1329
- }
1330
- }
1300
+ // HACK(eddyb) never cache overflow (this check used to be global-only).
1301
+ if let Err ( Overflow ) = candidate {
1302
+ return ;
1331
1303
}
1332
1304
1333
- debug ! (
1334
- "insert_candidate_cache(trait_ref={:?}, candidate={:?}) local" ,
1335
- trait_ref, candidate,
1336
- ) ;
1337
- self . infcx
1338
- . selection_cache
1305
+ let cache = if self . can_use_global_caches ( param_env) && !trait_ref. has_local_value ( ) {
1306
+ if candidate. has_local_value ( ) {
1307
+ span_bug ! (
1308
+ span,
1309
+ "selecting inference-free `{}` resulted in `{:?}`?!" ,
1310
+ trait_ref,
1311
+ candidate,
1312
+ ) ;
1313
+ }
1314
+ debug ! (
1315
+ "insert_candidate_cache(trait_ref={:?}, candidate={:?}) global" ,
1316
+ trait_ref, candidate,
1317
+ ) ;
1318
+ // This may overwrite the cache with the same value.
1319
+ & tcx. selection_cache
1320
+ } else {
1321
+ debug ! (
1322
+ "insert_candidate_cache(trait_ref={:?}, candidate={:?}) local" ,
1323
+ trait_ref, candidate,
1324
+ ) ;
1325
+ & self . infcx . selection_cache
1326
+ } ;
1327
+
1328
+ cache
1339
1329
. hashmap
1340
1330
. borrow_mut ( )
1341
1331
. insert ( param_env. and ( trait_ref) , WithDepNode :: new ( dep_node, candidate) ) ;
0 commit comments