-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Fix stack overflow when finding blanket impls #56722
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6d54672
Fix stack overflow when finding blanket impls
Aaron1011 c55c312
Fix tidy errors
Aaron1011 54fd8ca
Remove extra recursion_depth tracking
Aaron1011 03cd934
Ensure that we properly increment obligation depth
Aaron1011 1f08280
More tidy fixes
Aaron1011 b1a8da6
Improve error generation, fixup recursion limits
Aaron1011 9a64d79
Cleanup code
Aaron1011 dadd7bb
Fix diagnostic error
Aaron1011 726bdec
Improve comment
Aaron1011 9b68dcd
Don't explicitly increment the depth for new trait predicates
Aaron1011 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,7 @@ use rustc_data_structures::bit_set::GrowableBitSet; | |
use rustc_data_structures::sync::Lock; | ||
use rustc_target::spec::abi::Abi; | ||
use std::cmp; | ||
use std::fmt; | ||
use std::fmt::{self, Display}; | ||
use std::iter; | ||
use std::rc::Rc; | ||
use util::nodemap::{FxHashMap, FxHashSet}; | ||
|
@@ -629,7 +629,8 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> { | |
obligation: &PredicateObligation<'tcx>, | ||
) -> Result<EvaluationResult, OverflowError> { | ||
self.evaluation_probe(|this| { | ||
this.evaluate_predicate_recursively(TraitObligationStackList::empty(), obligation) | ||
this.evaluate_predicate_recursively(TraitObligationStackList::empty(), | ||
obligation.clone()) | ||
}) | ||
} | ||
|
||
|
@@ -655,12 +656,12 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> { | |
predicates: I, | ||
) -> Result<EvaluationResult, OverflowError> | ||
where | ||
I: IntoIterator<Item = &'a PredicateObligation<'tcx>>, | ||
I: IntoIterator<Item = PredicateObligation<'tcx>>, | ||
'tcx: 'a, | ||
{ | ||
let mut result = EvaluatedToOk; | ||
for obligation in predicates { | ||
let eval = self.evaluate_predicate_recursively(stack, obligation)?; | ||
let eval = self.evaluate_predicate_recursively(stack, obligation.clone())?; | ||
debug!( | ||
"evaluate_predicate_recursively({:?}) = {:?}", | ||
obligation, eval | ||
|
@@ -679,14 +680,25 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> { | |
fn evaluate_predicate_recursively<'o>( | ||
&mut self, | ||
previous_stack: TraitObligationStackList<'o, 'tcx>, | ||
obligation: &PredicateObligation<'tcx>, | ||
obligation: PredicateObligation<'tcx>, | ||
) -> Result<EvaluationResult, OverflowError> { | ||
debug!("evaluate_predicate_recursively({:?})", obligation); | ||
debug!("evaluate_predicate_recursively(previous_stack={:?}, obligation={:?})", | ||
previous_stack.head(), obligation); | ||
|
||
// Previous_stack stores a TraitObligatiom, while 'obligation' is | ||
// a PredicateObligation. These are distinct types, so we can't | ||
// use any Option combinator method that would force them to be | ||
// the same | ||
match previous_stack.head() { | ||
Some(h) => self.check_recursion_limit(&obligation, h.obligation)?, | ||
None => self.check_recursion_limit(&obligation, &obligation)? | ||
} | ||
|
||
match obligation.predicate { | ||
ty::Predicate::Trait(ref t) => { | ||
debug_assert!(!t.has_escaping_bound_vars()); | ||
let obligation = obligation.with(t.clone()); | ||
let mut obligation = obligation.with(t.clone()); | ||
obligation.recursion_depth += 1; | ||
self.evaluate_trait_predicate_recursively(previous_stack, obligation) | ||
} | ||
|
||
|
@@ -695,8 +707,9 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> { | |
match self.infcx | ||
.subtype_predicate(&obligation.cause, obligation.param_env, p) | ||
{ | ||
Some(Ok(InferOk { obligations, .. })) => { | ||
self.evaluate_predicates_recursively(previous_stack, &obligations) | ||
Some(Ok(InferOk { mut obligations, .. })) => { | ||
self.add_depth(obligations.iter_mut(), obligation.recursion_depth); | ||
nikomatsakis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.evaluate_predicates_recursively(previous_stack,obligations.into_iter()) | ||
} | ||
Some(Err(_)) => Ok(EvaluatedToErr), | ||
None => Ok(EvaluatedToAmbig), | ||
|
@@ -710,8 +723,9 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> { | |
ty, | ||
obligation.cause.span, | ||
) { | ||
Some(obligations) => { | ||
self.evaluate_predicates_recursively(previous_stack, obligations.iter()) | ||
Some(mut obligations) => { | ||
self.add_depth(obligations.iter_mut(), obligation.recursion_depth); | ||
nikomatsakis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.evaluate_predicates_recursively(previous_stack, obligations.into_iter()) | ||
} | ||
None => Ok(EvaluatedToAmbig), | ||
}, | ||
|
@@ -733,10 +747,11 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> { | |
ty::Predicate::Projection(ref data) => { | ||
let project_obligation = obligation.with(data.clone()); | ||
match project::poly_project_and_unify_type(self, &project_obligation) { | ||
Ok(Some(subobligations)) => { | ||
Ok(Some(mut subobligations)) => { | ||
self.add_depth(subobligations.iter_mut(), obligation.recursion_depth); | ||
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. and here |
||
let result = self.evaluate_predicates_recursively( | ||
previous_stack, | ||
subobligations.iter(), | ||
subobligations.into_iter(), | ||
); | ||
if let Some(key) = | ||
ProjectionCacheKey::from_poly_projection_predicate(self, data) | ||
|
@@ -1005,7 +1020,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> { | |
match this.confirm_candidate(stack.obligation, candidate) { | ||
Ok(selection) => this.evaluate_predicates_recursively( | ||
stack.list(), | ||
selection.nested_obligations().iter(), | ||
selection.nested_obligations().into_iter() | ||
), | ||
Err(..) => Ok(EvaluatedToErr), | ||
} | ||
|
@@ -1080,6 +1095,45 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> { | |
.insert(trait_ref, WithDepNode::new(dep_node, result)); | ||
} | ||
|
||
// For various reasons, it's possible for a subobligation | ||
// to have a *lower* recursion_depth than the obligation used to create it. | ||
// Projection sub-obligations may be returned from the projection cache, | ||
// which results in obligations with an 'old' recursion_depth. | ||
// Additionally, methods like ty::wf::obligations and | ||
// InferCtxt.subtype_predicate produce subobligations without | ||
// taking in a 'parent' depth, causing the generated subobligations | ||
// to have a recursion_depth of 0 | ||
// | ||
// To ensure that obligation_depth never decreasees, we force all subobligations | ||
// to have at least the depth of the original obligation. | ||
fn add_depth<T: 'cx, I: Iterator<Item = &'cx mut Obligation<'tcx, T>>>(&self, it: I, | ||
min_depth: usize) { | ||
it.for_each(|o| o.recursion_depth = cmp::max(min_depth, o.recursion_depth) + 1); | ||
} | ||
|
||
// Check that the recursion limit has not been exceeded. | ||
// | ||
// The weird return type of this function allows it to be used with the 'try' (?) | ||
// operator within certain functions | ||
fn check_recursion_limit<T: Display + TypeFoldable<'tcx>, V: Display + TypeFoldable<'tcx>>( | ||
&self, | ||
obligation: &Obligation<'tcx, T>, | ||
error_obligation: &Obligation<'tcx, V> | ||
) -> Result<(), OverflowError> { | ||
let recursion_limit = *self.infcx.tcx.sess.recursion_limit.get(); | ||
if obligation.recursion_depth >= recursion_limit { | ||
match self.query_mode { | ||
TraitQueryMode::Standard => { | ||
self.infcx().report_overflow_error(error_obligation, true); | ||
} | ||
TraitQueryMode::Canonical => { | ||
return Err(OverflowError); | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
/////////////////////////////////////////////////////////////////////////// | ||
// CANDIDATE ASSEMBLY | ||
// | ||
|
@@ -1096,17 +1150,8 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> { | |
) -> SelectionResult<'tcx, SelectionCandidate<'tcx>> { | ||
// Watch out for overflow. This intentionally bypasses (and does | ||
// not update) the cache. | ||
let recursion_limit = *self.infcx.tcx.sess.recursion_limit.get(); | ||
if stack.obligation.recursion_depth >= recursion_limit { | ||
match self.query_mode { | ||
TraitQueryMode::Standard => { | ||
self.infcx().report_overflow_error(&stack.obligation, true); | ||
} | ||
TraitQueryMode::Canonical => { | ||
return Err(Overflow); | ||
} | ||
} | ||
} | ||
self.check_recursion_limit(&stack.obligation, &stack.obligation)?; | ||
|
||
|
||
// Check the cache. Note that we freshen the trait-ref | ||
// separately rather than using `stack.fresh_trait_ref` -- | ||
|
@@ -1774,7 +1819,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> { | |
self.evaluation_probe(|this| { | ||
match this.match_where_clause_trait_ref(stack.obligation, where_clause_trait_ref) { | ||
Ok(obligations) => { | ||
this.evaluate_predicates_recursively(stack.list(), obligations.iter()) | ||
this.evaluate_predicates_recursively(stack.list(), obligations.into_iter()) | ||
} | ||
Err(()) => Ok(EvaluatedToErr), | ||
} | ||
|
@@ -3800,6 +3845,10 @@ impl<'o, 'tcx> TraitObligationStackList<'o, 'tcx> { | |
fn with(r: &'o TraitObligationStack<'o, 'tcx>) -> TraitObligationStackList<'o, 'tcx> { | ||
TraitObligationStackList { head: Some(r) } | ||
} | ||
|
||
fn head(&self) -> Option<&'o TraitObligationStack<'o, 'tcx>> { | ||
self.head | ||
} | ||
} | ||
|
||
impl<'o, 'tcx> Iterator for TraitObligationStackList<'o, 'tcx> { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// This shouldn't cause a stack overflow when rustdoc is run | ||
|
||
use std::ops::Deref; | ||
use std::ops::DerefMut; | ||
|
||
pub trait SimpleTrait { | ||
type SimpleT; | ||
} | ||
|
||
impl<Inner: SimpleTrait, Outer: Deref<Target = Inner>> SimpleTrait for Outer { | ||
type SimpleT = Inner::SimpleT; | ||
} | ||
|
||
pub trait AnotherTrait { | ||
type AnotherT; | ||
} | ||
|
||
impl<T, Simple: SimpleTrait<SimpleT = Vec<T>>> AnotherTrait for Simple { | ||
type AnotherT = T; | ||
} | ||
|
||
pub struct Unrelated<Inner, UnrelatedT: DerefMut<Target = Vec<Inner>>>(UnrelatedT); | ||
|
||
impl<Inner, UnrelatedT: DerefMut<Target = Vec<Inner>>> Deref for Unrelated<Inner, UnrelatedT> { | ||
type Target = Vec<Inner>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
|
||
pub fn main() { } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 don't think we need to increase the recursion depth here -- this is just translating the predicate from one form to another.
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 believe it is necessary here. Consider the following call stack:
evaluate_predicate_recursively
evaluate_trait_predicate_recursively
evaluate_stack
evaluate_candidate
evaluate_predicates_recursively
evaluate_predicate_recursively
Here, we've recursed back to
evaluate_predicate_recursively
without ever incrementing the recursion depth. To ensure that repeatedly recursing through thety::Predicate::Trait
match arm doesn't lead to a stack overflow, we need to increment the depth here.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 think that there should be an increase in between steps 4 and 5. In particular, the "nested obligations" returned by
evaluate_candidate
ought to be at a recursion level one higher than the candidate that spawned them.