-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rustc: include ParamEnv in global trait select/eval cache keys. #66963
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -204,7 +204,10 @@ struct TraitObligationStack<'prev, 'tcx> { | |
#[derive(Clone, Default)] | ||
pub struct SelectionCache<'tcx> { | ||
hashmap: Lock< | ||
FxHashMap<ty::TraitRef<'tcx>, WithDepNode<SelectionResult<'tcx, SelectionCandidate<'tcx>>>>, | ||
FxHashMap< | ||
ty::ParamEnvAnd<'tcx, ty::TraitRef<'tcx>>, | ||
WithDepNode<SelectionResult<'tcx, SelectionCandidate<'tcx>>>, | ||
>, | ||
>, | ||
} | ||
|
||
|
@@ -490,7 +493,9 @@ impl<'tcx> From<OverflowError> for SelectionError<'tcx> { | |
|
||
#[derive(Clone, Default)] | ||
pub struct EvaluationCache<'tcx> { | ||
hashmap: Lock<FxHashMap<ty::PolyTraitRef<'tcx>, WithDepNode<EvaluationResult>>>, | ||
hashmap: Lock< | ||
FxHashMap<ty::ParamEnvAnd<'tcx, ty::PolyTraitRef<'tcx>>, WithDepNode<EvaluationResult>>, | ||
>, | ||
} | ||
|
||
impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { | ||
|
@@ -1143,15 +1148,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { | |
let tcx = self.tcx(); | ||
if self.can_use_global_caches(param_env) { | ||
let cache = tcx.evaluation_cache.hashmap.borrow(); | ||
if let Some(cached) = cache.get(&trait_ref) { | ||
if let Some(cached) = cache.get(¶m_env.and(trait_ref)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, so, in this case the only purpose is to capture the reveal mode, correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I can change to be literally that if e.g. it's a perf problem. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't expect that to have a perf impact, but might be worth including in a comment somewhere. |
||
return Some(cached.get(tcx)); | ||
} | ||
} | ||
self.infcx | ||
.evaluation_cache | ||
.hashmap | ||
.borrow() | ||
.get(&trait_ref) | ||
.get(¶m_env.and(trait_ref)) | ||
.map(|v| v.get(tcx)) | ||
} | ||
|
||
|
@@ -1182,7 +1187,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { | |
.evaluation_cache | ||
.hashmap | ||
.borrow_mut() | ||
.insert(trait_ref, WithDepNode::new(dep_node, result)); | ||
.insert(param_env.and(trait_ref), WithDepNode::new(dep_node, result)); | ||
return; | ||
} | ||
} | ||
|
@@ -1195,7 +1200,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { | |
.evaluation_cache | ||
.hashmap | ||
.borrow_mut() | ||
.insert(trait_ref, WithDepNode::new(dep_node, result)); | ||
.insert(param_env.and(trait_ref), WithDepNode::new(dep_node, result)); | ||
} | ||
|
||
/// For various reasons, it's possible for a subobligation | ||
|
@@ -1602,15 +1607,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { | |
let trait_ref = &cache_fresh_trait_pred.skip_binder().trait_ref; | ||
if self.can_use_global_caches(param_env) { | ||
let cache = tcx.selection_cache.hashmap.borrow(); | ||
if let Some(cached) = cache.get(&trait_ref) { | ||
if let Some(cached) = cache.get(¶m_env.and(*trait_ref)) { | ||
return Some(cached.get(tcx)); | ||
} | ||
} | ||
self.infcx | ||
.selection_cache | ||
.hashmap | ||
.borrow() | ||
.get(trait_ref) | ||
.get(¶m_env.and(*trait_ref)) | ||
.map(|v| v.get(tcx)) | ||
} | ||
|
||
|
@@ -1671,7 +1676,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { | |
tcx.selection_cache | ||
.hashmap | ||
.borrow_mut() | ||
.insert(trait_ref, WithDepNode::new(dep_node, candidate)); | ||
.insert(param_env.and(trait_ref), WithDepNode::new(dep_node, candidate)); | ||
return; | ||
} | ||
} | ||
|
@@ -1685,7 +1690,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { | |
.selection_cache | ||
.hashmap | ||
.borrow_mut() | ||
.insert(trait_ref, WithDepNode::new(dep_node, candidate)); | ||
.insert(param_env.and(trait_ref), WithDepNode::new(dep_node, candidate)); | ||
} | ||
|
||
fn assemble_candidates<'o>( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
error[E0277]: the trait bound `T: TraitWithAssoc` is not satisfied | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Huh, this error is definitely not great. Do you understand what's going on here? (I don't, at least not yet) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is because |
||
--> $DIR/bound_reduction2.rs:10:1 | ||
| | ||
LL | type Foo<V> = impl Trait<V>; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `TraitWithAssoc` is not implemented for `T` | ||
... | ||
LL | fn foo_desugared<T: TraitWithAssoc>(_: T) -> Foo<T::Assoc> { | ||
| -- help: consider further restricting this bound: `T: TraitWithAssoc +` | ||
|
||
error: defining opaque type use does not fully define opaque type: generic parameter `V` is specified as concrete type `<T as TraitWithAssoc>::Assoc` | ||
--> $DIR/bound_reduction2.rs:17:1 | ||
--> $DIR/bound_reduction2.rs:18:1 | ||
| | ||
LL | / fn foo_desugared<T: TraitWithAssoc>(_: T) -> Foo<T::Assoc> { | ||
LL | | () | ||
|
@@ -12,5 +21,6 @@ error: could not find defining uses | |
LL | type Foo<V> = impl Trait<V>; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be clear, did you see this debug assertion fail?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, in a run-pass specialization test, as I assume this can happen inside a temporary inference context inside a normalization query.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This little bit of code is making me nervous. I'm wondering if we might get into some scenario where we have an unresolved inference variable and so we opt not to reveal the item. I think this would mean we normalize to (e.g.)
<T as Foo<?X>>::Bar
. But then later the inference variable?X
gets resolved in such a way that we could have revealed the value. This could lead to an inconsistency that causes some ICEs or something.I think I would feel better if we flagged cases that involve inference variables as 'ambiguous'. I have to remember how that is done in this code, I can check if it has any impact.
Still, I could probably be persuaded to land it as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty sure not revealing is "ambiguous", to the extent that projection would be.
Doesn't ambiguity result in not making progress, in general?
Wouldn't that same situation apply to non-specialized impls when e.g. there's a
Foo<A>
impl and aFoo<B>
one and so?X
being resolved later to one of them would allow revealingBar
?I don't mind ICEs caused indirectly by this, considering this was even more ICE-y before, but also keep in mind this requires specialization, which is still unstable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, looks like the assert was added by... me, in #35091, and it assumed that only monomorphic codegen would use the
Reveal::All
codepath (I was curious and dug through the history).