-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Don't immediately error for cycles during normalization #75494
Merged
bors
merged 1 commit into
rust-lang:master
from
matthewjasper:defer-recursive-projection-error
Aug 20, 2020
Merged
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -41,6 +41,8 @@ pub type ProjectionObligation<'tcx> = Obligation<'tcx, ty::ProjectionPredicate<' | |
|
||
pub type ProjectionTyObligation<'tcx> = Obligation<'tcx, ty::ProjectionTy<'tcx>>; | ||
|
||
pub(super) struct InProgress; | ||
|
||
/// When attempting to resolve `<T as TraitRef>::Name` ... | ||
#[derive(Debug)] | ||
pub enum ProjectionTyError<'tcx> { | ||
|
@@ -143,10 +145,26 @@ impl<'tcx> ProjectionTyCandidateSet<'tcx> { | |
/// | ||
/// If successful, this may result in additional obligations. Also returns | ||
/// the projection cache key used to track these additional obligations. | ||
pub fn poly_project_and_unify_type<'cx, 'tcx>( | ||
/// | ||
/// ## Returns | ||
/// | ||
/// - `Err(_)`: the projection can be normalized, but is not equal to the | ||
/// expected type. | ||
/// - `Ok(Err(InProgress))`: this is called recursively while normalizing | ||
/// the same projection. | ||
/// - `Ok(Ok(None))`: The projection cannot be normalized due to ambiguity | ||
/// (resolving some inference variables in the projection may fix this). | ||
/// - `Ok(Ok(Some(obligations)))`: The projection bound holds subject to | ||
/// the given obligations. If the projection cannot be normalized because | ||
/// the required trait bound doesn't hold this returned with `obligations` | ||
/// being a predicate that cannot be proven. | ||
pub(super) fn poly_project_and_unify_type<'cx, 'tcx>( | ||
selcx: &mut SelectionContext<'cx, 'tcx>, | ||
obligation: &PolyProjectionObligation<'tcx>, | ||
) -> Result<Option<Vec<PredicateObligation<'tcx>>>, MismatchedProjectionTypes<'tcx>> { | ||
) -> Result< | ||
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. Nit: this is a complex return type! I thnk the doc comment could be extended to explain what the various return values mean. |
||
Result<Option<Vec<PredicateObligation<'tcx>>>, InProgress>, | ||
MismatchedProjectionTypes<'tcx>, | ||
> { | ||
debug!("poly_project_and_unify_type(obligation={:?})", obligation); | ||
|
||
let infcx = selcx.infcx(); | ||
|
@@ -165,10 +183,15 @@ pub fn poly_project_and_unify_type<'cx, 'tcx>( | |
/// <T as Trait>::U == V | ||
/// | ||
/// If successful, this may result in additional obligations. | ||
/// | ||
/// See [poly_project_and_unify_type] for an explanation of the return value. | ||
fn project_and_unify_type<'cx, 'tcx>( | ||
selcx: &mut SelectionContext<'cx, 'tcx>, | ||
obligation: &ProjectionObligation<'tcx>, | ||
) -> Result<Option<Vec<PredicateObligation<'tcx>>>, MismatchedProjectionTypes<'tcx>> { | ||
) -> Result< | ||
Result<Option<Vec<PredicateObligation<'tcx>>>, InProgress>, | ||
MismatchedProjectionTypes<'tcx>, | ||
> { | ||
debug!("project_and_unify_type(obligation={:?})", obligation); | ||
|
||
let mut obligations = vec![]; | ||
|
@@ -180,8 +203,9 @@ fn project_and_unify_type<'cx, 'tcx>( | |
obligation.recursion_depth, | ||
&mut obligations, | ||
) { | ||
Some(n) => n, | ||
None => return Ok(None), | ||
Ok(Some(n)) => n, | ||
Ok(None) => return Ok(Ok(None)), | ||
Err(InProgress) => return Ok(Err(InProgress)), | ||
}; | ||
|
||
debug!( | ||
|
@@ -196,7 +220,7 @@ fn project_and_unify_type<'cx, 'tcx>( | |
{ | ||
Ok(InferOk { obligations: inferred_obligations, value: () }) => { | ||
obligations.extend(inferred_obligations); | ||
Ok(Some(obligations)) | ||
Ok(Ok(Some(obligations))) | ||
} | ||
Err(err) => { | ||
debug!("project_and_unify_type: equating types encountered error {:?}", err); | ||
|
@@ -419,6 +443,8 @@ pub fn normalize_projection_type<'a, 'b, 'tcx>( | |
depth, | ||
obligations, | ||
) | ||
.ok() | ||
.flatten() | ||
.unwrap_or_else(move || { | ||
// if we bottom out in ambiguity, create a type variable | ||
// and a deferred predicate to resolve this when more type | ||
|
@@ -455,7 +481,7 @@ fn opt_normalize_projection_type<'a, 'b, 'tcx>( | |
cause: ObligationCause<'tcx>, | ||
depth: usize, | ||
obligations: &mut Vec<PredicateObligation<'tcx>>, | ||
) -> Option<Ty<'tcx>> { | ||
) -> Result<Option<Ty<'tcx>>, InProgress> { | ||
let infcx = selcx.infcx(); | ||
|
||
let projection_ty = infcx.resolve_vars_if_possible(&projection_ty); | ||
|
@@ -487,7 +513,7 @@ fn opt_normalize_projection_type<'a, 'b, 'tcx>( | |
"opt_normalize_projection_type: \ | ||
found cache entry: ambiguous" | ||
); | ||
return None; | ||
return Ok(None); | ||
} | ||
Err(ProjectionCacheEntry::InProgress) => { | ||
// If while normalized A::B, we are asked to normalize | ||
|
@@ -502,24 +528,14 @@ fn opt_normalize_projection_type<'a, 'b, 'tcx>( | |
// to normalize `A::B`, we will want to check the | ||
// where-clauses in scope. So we will try to unify `A::B` | ||
// with `A::B`, which can trigger a recursive | ||
// normalization. In that case, I think we will want this code: | ||
// | ||
// ``` | ||
// let ty = selcx.tcx().mk_projection(projection_ty.item_def_id, | ||
// projection_ty.substs; | ||
// return Some(NormalizedTy { value: v, obligations: vec![] }); | ||
// ``` | ||
// normalization. | ||
|
||
debug!( | ||
"opt_normalize_projection_type: \ | ||
found cache entry: in-progress" | ||
); | ||
|
||
// But for now, let's classify this as an overflow: | ||
let recursion_limit = selcx.tcx().sess.recursion_limit(); | ||
let obligation = | ||
Obligation::with_depth(cause, recursion_limit.0, param_env, projection_ty); | ||
selcx.infcx().report_overflow_error(&obligation, false); | ||
return Err(InProgress); | ||
} | ||
Err(ProjectionCacheEntry::NormalizedTy(ty)) => { | ||
// This is the hottest path in this function. | ||
|
@@ -555,7 +571,7 @@ fn opt_normalize_projection_type<'a, 'b, 'tcx>( | |
cause, | ||
depth, | ||
)); | ||
return Some(ty.value); | ||
return Ok(Some(ty.value)); | ||
} | ||
Err(ProjectionCacheEntry::Error) => { | ||
debug!( | ||
|
@@ -564,7 +580,7 @@ fn opt_normalize_projection_type<'a, 'b, 'tcx>( | |
); | ||
let result = normalize_to_error(selcx, param_env, projection_ty, cause, depth); | ||
obligations.extend(result.obligations); | ||
return Some(result.value); | ||
return Ok(Some(result.value)); | ||
} | ||
} | ||
|
||
|
@@ -611,7 +627,7 @@ fn opt_normalize_projection_type<'a, 'b, 'tcx>( | |
let cache_value = prune_cache_value_obligations(infcx, &result); | ||
infcx.inner.borrow_mut().projection_cache().insert_ty(cache_key, cache_value); | ||
obligations.extend(result.obligations); | ||
Some(result.value) | ||
Ok(Some(result.value)) | ||
} | ||
Ok(ProjectedTy::NoProgress(projected_ty)) => { | ||
debug!( | ||
|
@@ -622,15 +638,15 @@ fn opt_normalize_projection_type<'a, 'b, 'tcx>( | |
let result = Normalized { value: projected_ty, obligations: vec![] }; | ||
infcx.inner.borrow_mut().projection_cache().insert_ty(cache_key, result.clone()); | ||
// No need to extend `obligations`. | ||
Some(result.value) | ||
Ok(Some(result.value)) | ||
} | ||
Err(ProjectionTyError::TooManyCandidates) => { | ||
debug!( | ||
"opt_normalize_projection_type: \ | ||
too many candidates" | ||
); | ||
infcx.inner.borrow_mut().projection_cache().ambiguous(cache_key); | ||
None | ||
Ok(None) | ||
} | ||
Err(ProjectionTyError::TraitSelectionError(_)) => { | ||
debug!("opt_normalize_projection_type: ERROR"); | ||
|
@@ -642,7 +658,7 @@ fn opt_normalize_projection_type<'a, 'b, 'tcx>( | |
infcx.inner.borrow_mut().projection_cache().error(cache_key); | ||
let result = normalize_to_error(selcx, param_env, projection_ty, cause, depth); | ||
obligations.extend(result.obligations); | ||
Some(result.value) | ||
Ok(Some(result.value)) | ||
} | ||
} | ||
} | ||
|
@@ -1116,11 +1132,11 @@ fn assemble_candidates_from_impls<'cx, 'tcx>( | |
} | ||
super::ImplSourceAutoImpl(..) | super::ImplSourceBuiltin(..) => { | ||
// These traits have no associated types. | ||
span_bug!( | ||
selcx.tcx().sess.delay_span_bug( | ||
obligation.cause.span, | ||
"Cannot project an associated type from `{:?}`", | ||
impl_source | ||
&format!("Cannot project an associated type from `{:?}`", impl_source), | ||
); | ||
return Err(()); | ||
} | ||
}; | ||
|
||
|
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
21 changes: 11 additions & 10 deletions
21
src/test/ui/associated-types/defaults-cyclic-fail-1.stderr
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 |
---|---|---|
@@ -1,33 +1,34 @@ | ||
error[E0275]: overflow evaluating the requirement `<() as Tr>::B` | ||
error[E0275]: overflow evaluating the requirement `<() as Tr>::B == _` | ||
--> $DIR/defaults-cyclic-fail-1.rs:10:6 | ||
| | ||
LL | impl Tr for () {} | ||
| ^^ | ||
|
||
error[E0275]: overflow evaluating the requirement `<bool as Tr>::B` | ||
error[E0271]: type mismatch resolving `<bool as Tr>::B == _` | ||
--> $DIR/defaults-cyclic-fail-1.rs:28:6 | ||
| | ||
LL | impl Tr for bool { | ||
| ^^ | ||
| ^^ cyclic type of infinite size | ||
|
||
error[E0275]: overflow evaluating the requirement `<usize as Tr>::B` | ||
error[E0271]: type mismatch resolving `<usize as Tr>::B == _` | ||
--> $DIR/defaults-cyclic-fail-1.rs:35:6 | ||
| | ||
LL | impl Tr for usize { | ||
| ^^ | ||
| ^^ cyclic type of infinite size | ||
|
||
error[E0275]: overflow evaluating the requirement `<bool as Tr>::B` | ||
error[E0271]: type mismatch resolving `<bool as Tr>::B == _` | ||
--> $DIR/defaults-cyclic-fail-1.rs:30:5 | ||
| | ||
LL | type A = Box<Self::B>; | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
| ^^^^^^^^^^^^^^^^^^^^^^ cyclic type of infinite size | ||
|
||
error[E0275]: overflow evaluating the requirement `<usize as Tr>::A` | ||
error[E0271]: type mismatch resolving `<usize as Tr>::A == _` | ||
--> $DIR/defaults-cyclic-fail-1.rs:37:5 | ||
| | ||
LL | type B = &'static Self::A; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ cyclic type of infinite size | ||
|
||
error: aborting due to 5 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0275`. | ||
Some errors have detailed explanations: E0271, E0275. | ||
For more information about an error, try `rustc --explain E0271`. |
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.
ok, I see, so we basically just push the project predicate back up to the caller... interesting...