@@ -1310,13 +1310,19 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
1310
1310
param_env : ty:: ParamEnv < ' tcx > ,
1311
1311
trait_pred : ty:: PolyTraitPredicate < ' tcx > ,
1312
1312
) -> Option < EvaluationResult > {
1313
- let tcx = self . tcx ( ) ;
1313
+ let infcx = self . infcx ;
1314
+ let tcx = infcx. tcx ;
1314
1315
if self . can_use_global_caches ( param_env, trait_pred) {
1315
- if let Some ( res) = tcx. evaluation_cache . get ( & ( param_env, trait_pred) , tcx) {
1316
- return Some ( res) ;
1316
+ let key = ( infcx. typing_env ( param_env) , trait_pred) ;
1317
+ if let Some ( res) = tcx. evaluation_cache . get ( & key, tcx) {
1318
+ Some ( res)
1319
+ } else {
1320
+ debug_assert_eq ! ( infcx. evaluation_cache. get( & ( param_env, trait_pred) , tcx) , None ) ;
1321
+ None
1317
1322
}
1323
+ } else {
1324
+ self . infcx . evaluation_cache . get ( & ( param_env, trait_pred) , tcx)
1318
1325
}
1319
- self . infcx . evaluation_cache . get ( & ( param_env, trait_pred) , tcx)
1320
1326
}
1321
1327
1322
1328
fn insert_evaluation_cache (
@@ -1332,18 +1338,21 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
1332
1338
return ;
1333
1339
}
1334
1340
1335
- if self . can_use_global_caches ( param_env, trait_pred) && !trait_pred. has_infer ( ) {
1341
+ let infcx = self . infcx ;
1342
+ let tcx = infcx. tcx ;
1343
+ if self . can_use_global_caches ( param_env, trait_pred) {
1336
1344
debug ! ( ?trait_pred, ?result, "insert_evaluation_cache global" ) ;
1337
1345
// This may overwrite the cache with the same value
1338
- // FIXME: Due to #50507 this overwrites the different values
1339
- // This should be changed to use HashMapExt::insert_same
1340
- // when that is fixed
1341
- self . tcx ( ) . evaluation_cache . insert ( ( param_env, trait_pred) , dep_node, result) ;
1346
+ tcx. evaluation_cache . insert (
1347
+ ( infcx. typing_env ( param_env) , trait_pred) ,
1348
+ dep_node,
1349
+ result,
1350
+ ) ;
1342
1351
return ;
1352
+ } else {
1353
+ debug ! ( ?trait_pred, ?result, "insert_evaluation_cache local" ) ;
1354
+ self . infcx . evaluation_cache . insert ( ( param_env, trait_pred) , dep_node, result) ;
1343
1355
}
1344
-
1345
- debug ! ( ?trait_pred, ?result, "insert_evaluation_cache" ) ;
1346
- self . infcx . evaluation_cache . insert ( ( param_env, trait_pred) , dep_node, result) ;
1347
1356
}
1348
1357
1349
1358
fn check_recursion_depth < T > (
@@ -1485,7 +1494,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
1485
1494
// If there are any inference variables in the `ParamEnv`, then we
1486
1495
// always use a cache local to this particular scope. Otherwise, we
1487
1496
// switch to a global cache.
1488
- if param_env. has_infer ( ) {
1497
+ if param_env. has_infer ( ) || pred . has_infer ( ) {
1489
1498
return false ;
1490
1499
}
1491
1500
@@ -1522,15 +1531,20 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
1522
1531
param_env : ty:: ParamEnv < ' tcx > ,
1523
1532
cache_fresh_trait_pred : ty:: PolyTraitPredicate < ' tcx > ,
1524
1533
) -> Option < SelectionResult < ' tcx , SelectionCandidate < ' tcx > > > {
1525
- let tcx = self . tcx ( ) ;
1534
+ let infcx = self . infcx ;
1535
+ let tcx = infcx. tcx ;
1526
1536
let pred = cache_fresh_trait_pred. skip_binder ( ) ;
1527
1537
1528
1538
if self . can_use_global_caches ( param_env, cache_fresh_trait_pred) {
1529
- if let Some ( res) = tcx. selection_cache . get ( & ( param_env, pred) , tcx) {
1530
- return Some ( res) ;
1539
+ if let Some ( res) = tcx. selection_cache . get ( & ( infcx. typing_env ( param_env) , pred) , tcx) {
1540
+ Some ( res)
1541
+ } else {
1542
+ debug_assert_eq ! ( infcx. selection_cache. get( & ( param_env, pred) , tcx) , None ) ;
1543
+ None
1531
1544
}
1545
+ } else {
1546
+ infcx. selection_cache . get ( & ( param_env, pred) , tcx)
1532
1547
}
1533
- self . infcx . selection_cache . get ( & ( param_env, pred) , tcx)
1534
1548
}
1535
1549
1536
1550
/// Determines whether can we safely cache the result
@@ -1567,7 +1581,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
1567
1581
dep_node : DepNodeIndex ,
1568
1582
candidate : SelectionResult < ' tcx , SelectionCandidate < ' tcx > > ,
1569
1583
) {
1570
- let tcx = self . tcx ( ) ;
1584
+ let infcx = self . infcx ;
1585
+ let tcx = infcx. tcx ;
1571
1586
let pred = cache_fresh_trait_pred. skip_binder ( ) ;
1572
1587
1573
1588
if !self . can_cache_candidate ( & candidate) {
@@ -1578,10 +1593,16 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
1578
1593
if self . can_use_global_caches ( param_env, cache_fresh_trait_pred) {
1579
1594
if let Err ( Overflow ( OverflowError :: Canonical ) ) = candidate {
1580
1595
// Don't cache overflow globally; we only produce this in certain modes.
1581
- } else if !pred . has_infer ( ) && !candidate . has_infer ( ) {
1596
+ } else {
1582
1597
debug ! ( ?pred, ?candidate, "insert_candidate_cache global" ) ;
1598
+ debug_assert ! ( !candidate. has_infer( ) ) ;
1599
+
1583
1600
// This may overwrite the cache with the same value.
1584
- tcx. selection_cache . insert ( ( param_env, pred) , dep_node, candidate) ;
1601
+ tcx. selection_cache . insert (
1602
+ ( infcx. typing_env ( param_env) , pred) ,
1603
+ dep_node,
1604
+ candidate,
1605
+ ) ;
1585
1606
return ;
1586
1607
}
1587
1608
}
0 commit comments