Skip to content

Commit 1d1298e

Browse files
committed
rustc: include ParamEnv in global trait select/eval cache keys.
1 parent 4af3ee8 commit 1d1298e

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

src/librustc/traits/project.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1079,12 +1079,10 @@ fn assemble_candidates_from_impls<'cx, 'tcx>(
10791079
if !is_default {
10801080
true
10811081
} else if obligation.param_env.reveal == Reveal::All {
1082-
debug_assert!(!poly_trait_ref.needs_infer());
1083-
if !poly_trait_ref.needs_subst() {
1084-
true
1085-
} else {
1086-
false
1087-
}
1082+
// NOTE(eddyb) inference variables can resolve to parameters, so
1083+
// assume `poly_trait_ref` isn't monomorphic, if it contains any.
1084+
let poly_trait_ref = selcx.infcx().resolve_vars_if_possible(&poly_trait_ref);
1085+
!poly_trait_ref.needs_infer() && !poly_trait_ref.needs_subst()
10881086
} else {
10891087
false
10901088
}

src/librustc/traits/select.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,10 @@ struct TraitObligationStack<'prev, 'tcx> {
204204
#[derive(Clone, Default)]
205205
pub struct SelectionCache<'tcx> {
206206
hashmap: Lock<
207-
FxHashMap<ty::TraitRef<'tcx>, WithDepNode<SelectionResult<'tcx, SelectionCandidate<'tcx>>>>,
207+
FxHashMap<
208+
ty::ParamEnvAnd<'tcx, ty::TraitRef<'tcx>>,
209+
WithDepNode<SelectionResult<'tcx, SelectionCandidate<'tcx>>>,
210+
>,
208211
>,
209212
}
210213

@@ -490,7 +493,9 @@ impl<'tcx> From<OverflowError> for SelectionError<'tcx> {
490493

491494
#[derive(Clone, Default)]
492495
pub struct EvaluationCache<'tcx> {
493-
hashmap: Lock<FxHashMap<ty::PolyTraitRef<'tcx>, WithDepNode<EvaluationResult>>>,
496+
hashmap: Lock<
497+
FxHashMap<ty::ParamEnvAnd<'tcx, ty::PolyTraitRef<'tcx>>, WithDepNode<EvaluationResult>>,
498+
>,
494499
}
495500

496501
impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
@@ -1143,15 +1148,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11431148
let tcx = self.tcx();
11441149
if self.can_use_global_caches(param_env) {
11451150
let cache = tcx.evaluation_cache.hashmap.borrow();
1146-
if let Some(cached) = cache.get(&trait_ref) {
1151+
if let Some(cached) = cache.get(&param_env.and(trait_ref)) {
11471152
return Some(cached.get(tcx));
11481153
}
11491154
}
11501155
self.infcx
11511156
.evaluation_cache
11521157
.hashmap
11531158
.borrow()
1154-
.get(&trait_ref)
1159+
.get(&param_env.and(trait_ref))
11551160
.map(|v| v.get(tcx))
11561161
}
11571162

@@ -1182,7 +1187,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11821187
.evaluation_cache
11831188
.hashmap
11841189
.borrow_mut()
1185-
.insert(trait_ref, WithDepNode::new(dep_node, result));
1190+
.insert(param_env.and(trait_ref), WithDepNode::new(dep_node, result));
11861191
return;
11871192
}
11881193
}
@@ -1195,7 +1200,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11951200
.evaluation_cache
11961201
.hashmap
11971202
.borrow_mut()
1198-
.insert(trait_ref, WithDepNode::new(dep_node, result));
1203+
.insert(param_env.and(trait_ref), WithDepNode::new(dep_node, result));
11991204
}
12001205

12011206
/// For various reasons, it's possible for a subobligation
@@ -1602,15 +1607,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
16021607
let trait_ref = &cache_fresh_trait_pred.skip_binder().trait_ref;
16031608
if self.can_use_global_caches(param_env) {
16041609
let cache = tcx.selection_cache.hashmap.borrow();
1605-
if let Some(cached) = cache.get(&trait_ref) {
1610+
if let Some(cached) = cache.get(&param_env.and(*trait_ref)) {
16061611
return Some(cached.get(tcx));
16071612
}
16081613
}
16091614
self.infcx
16101615
.selection_cache
16111616
.hashmap
16121617
.borrow()
1613-
.get(trait_ref)
1618+
.get(&param_env.and(*trait_ref))
16141619
.map(|v| v.get(tcx))
16151620
}
16161621

@@ -1671,7 +1676,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
16711676
tcx.selection_cache
16721677
.hashmap
16731678
.borrow_mut()
1674-
.insert(trait_ref, WithDepNode::new(dep_node, candidate));
1679+
.insert(param_env.and(trait_ref), WithDepNode::new(dep_node, candidate));
16751680
return;
16761681
}
16771682
}
@@ -1685,7 +1690,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
16851690
.selection_cache
16861691
.hashmap
16871692
.borrow_mut()
1688-
.insert(trait_ref, WithDepNode::new(dep_node, candidate));
1693+
.insert(param_env.and(trait_ref), WithDepNode::new(dep_node, candidate));
16891694
}
16901695

16911696
fn assemble_candidates<'o>(

src/test/ui/type-alias-impl-trait/bound_reduction2.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ trait TraitWithAssoc {
99

1010
type Foo<V> = impl Trait<V>;
1111
//~^ ERROR could not find defining uses
12+
//~| ERROR the trait bound `T: TraitWithAssoc` is not satisfied
1213

1314
trait Trait<U> {}
1415

src/test/ui/type-alias-impl-trait/bound_reduction2.stderr

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
error[E0277]: the trait bound `T: TraitWithAssoc` is not satisfied
2+
--> $DIR/bound_reduction2.rs:10:1
3+
|
4+
LL | type Foo<V> = impl Trait<V>;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `TraitWithAssoc` is not implemented for `T`
6+
...
7+
LL | fn foo_desugared<T: TraitWithAssoc>(_: T) -> Foo<T::Assoc> {
8+
| -- help: consider further restricting this bound: `T: TraitWithAssoc +`
9+
110
error: defining opaque type use does not fully define opaque type: generic parameter `V` is specified as concrete type `<T as TraitWithAssoc>::Assoc`
2-
--> $DIR/bound_reduction2.rs:17:1
11+
--> $DIR/bound_reduction2.rs:18:1
312
|
413
LL | / fn foo_desugared<T: TraitWithAssoc>(_: T) -> Foo<T::Assoc> {
514
LL | | ()
@@ -12,5 +21,6 @@ error: could not find defining uses
1221
LL | type Foo<V> = impl Trait<V>;
1322
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1423

15-
error: aborting due to 2 previous errors
24+
error: aborting due to 3 previous errors
1625

26+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)