@@ -835,18 +835,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
835835 trait_ref : ty:: PolyTraitRef < ' tcx > ,
836836 ) -> Option < EvaluationResult > {
837837 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) )
850845 }
851846
852847 fn insert_evaluation_cache (
@@ -862,28 +857,22 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
862857 return ;
863858 }
864859
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+ } ;
883874
884- debug ! ( "insert_evaluation_cache(trait_ref={:?}, candidate={:?})" , trait_ref, result, ) ;
885- self . infcx
886- . evaluation_cache
875+ cache
887876 . hashmap
888877 . borrow_mut ( )
889878 . insert ( param_env. and ( trait_ref) , WithDepNode :: new ( dep_node, result) ) ;
@@ -982,6 +971,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
982971 cache_fresh_trait_pred,
983972 dep_node,
984973 candidate. clone ( ) ,
974+ stack. obligation . cause . span ,
985975 ) ;
986976 candidate
987977 }
@@ -1250,18 +1240,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
12501240 ) -> Option < SelectionResult < ' tcx , SelectionCandidate < ' tcx > > > {
12511241 let tcx = self . tcx ( ) ;
12521242 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) )
12651250 }
12661251
12671252 /// Determines whether can we safely cache the result
@@ -1296,6 +1281,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
12961281 cache_fresh_trait_pred : ty:: PolyTraitPredicate < ' tcx > ,
12971282 dep_node : DepNodeIndex ,
12981283 candidate : SelectionResult < ' tcx , SelectionCandidate < ' tcx > > ,
1284+ span : rustc_span:: Span ,
12991285 ) {
13001286 let tcx = self . tcx ( ) ;
13011287 let trait_ref = cache_fresh_trait_pred. skip_binder ( ) . trait_ref ;
@@ -1309,31 +1295,35 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
13091295 return ;
13101296 }
13111297
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 ;
13291301 }
13301302
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
13371327 . hashmap
13381328 . borrow_mut ( )
13391329 . insert ( param_env. and ( trait_ref) , WithDepNode :: new ( dep_node, candidate) ) ;
0 commit comments