-
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
Don't over-constrain projections in generic method signatures #92728
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -19,7 +19,6 @@ use super::{Normalized, NormalizedTy, ProjectionCacheEntry, ProjectionCacheKey}; | |
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; | ||
use crate::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime}; | ||
use crate::traits::error_reporting::InferCtxtExt as _; | ||
use crate::traits::select::ProjectionMatchesProjection; | ||
use rustc_data_structures::sso::SsoHashSet; | ||
use rustc_data_structures::stack::ensure_sufficient_stack; | ||
use rustc_errors::ErrorGuaranteed; | ||
|
@@ -42,7 +41,7 @@ pub type ProjectionObligation<'tcx> = Obligation<'tcx, ty::ProjectionPredicate<' | |
|
||
pub type ProjectionTyObligation<'tcx> = Obligation<'tcx, ty::ProjectionTy<'tcx>>; | ||
|
||
pub(super) struct InProgress; | ||
pub struct InProgress; | ||
|
||
/// When attempting to resolve `<T as TraitRef>::Name` ... | ||
#[derive(Debug)] | ||
|
@@ -188,7 +187,7 @@ pub(super) fn poly_project_and_unify_type<'cx, 'tcx>( | |
/// 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>( | ||
pub fn project_and_unify_type<'cx, 'tcx>( | ||
selcx: &mut SelectionContext<'cx, 'tcx>, | ||
obligation: &ProjectionObligation<'tcx>, | ||
) -> Result< | ||
|
@@ -484,7 +483,7 @@ impl<'a, 'b, 'tcx> TypeFolder<'tcx> for AssocTypeNormalizer<'a, 'b, 'tcx> { | |
// there won't be bound vars there. | ||
|
||
let data = data.super_fold_with(self); | ||
let normalized_ty = if self.eager_inference_replacement { | ||
let normalized_ty = if self.selcx.normalization_mode.eager_inference_replacement { | ||
normalize_projection_type( | ||
self.selcx, | ||
self.param_env, | ||
|
@@ -914,7 +913,8 @@ fn opt_normalize_projection_type<'a, 'b, 'tcx>( | |
// Don't use the projection cache in intercrate mode - | ||
// the `infcx` may be re-used between intercrate in non-intercrate | ||
// mode, which could lead to using incorrect cache results. | ||
let use_cache = !selcx.is_intercrate(); | ||
let use_cache = | ||
!selcx.is_intercrate() && selcx.normalization_mode.allow_infer_constraint_during_projection; | ||
|
||
let projection_ty = infcx.resolve_vars_if_possible(projection_ty); | ||
let cache_key = ProjectionCacheKey::new(projection_ty); | ||
|
@@ -1168,7 +1168,7 @@ fn project<'cx, 'tcx>( | |
|
||
match candidates { | ||
ProjectionCandidateSet::Single(candidate) => { | ||
Ok(Projected::Progress(confirm_candidate(selcx, obligation, candidate))) | ||
Ok(confirm_candidate(selcx, obligation, candidate)) | ||
} | ||
ProjectionCandidateSet::None => Ok(Projected::NoProgress( | ||
// FIXME(associated_const_generics): this may need to change in the future? | ||
|
@@ -1243,7 +1243,7 @@ fn assemble_candidates_from_trait_def<'cx, 'tcx>( | |
ProjectionCandidate::TraitDef, | ||
bounds.iter(), | ||
true, | ||
); | ||
) | ||
} | ||
|
||
/// In the case of a trait object like | ||
|
@@ -1308,35 +1308,28 @@ fn assemble_candidates_from_predicates<'cx, 'tcx>( | |
let bound_predicate = predicate.kind(); | ||
if let ty::PredicateKind::Projection(data) = predicate.kind().skip_binder() { | ||
let data = bound_predicate.rebind(data); | ||
if data.projection_def_id() != obligation.predicate.item_def_id { | ||
continue; | ||
} | ||
let same_def_id = data.projection_def_id() == obligation.predicate.item_def_id; | ||
|
||
let is_match = infcx.probe(|_| { | ||
selcx.match_projection_projections( | ||
obligation, | ||
data, | ||
potentially_unnormalized_candidates, | ||
) | ||
}); | ||
let is_match = same_def_id | ||
&& infcx.probe(|_| { | ||
selcx.match_projection_projections( | ||
obligation, | ||
data, | ||
potentially_unnormalized_candidates, | ||
) | ||
}); | ||
|
||
match is_match { | ||
ProjectionMatchesProjection::Yes => { | ||
candidate_set.push_candidate(ctor(data)); | ||
|
||
if potentially_unnormalized_candidates | ||
&& !obligation.predicate.has_infer_types_or_consts() | ||
{ | ||
// HACK: Pick the first trait def candidate for a fully | ||
// inferred predicate. This is to allow duplicates that | ||
// differ only in normalization. | ||
return; | ||
} | ||
} | ||
ProjectionMatchesProjection::Ambiguous => { | ||
candidate_set.mark_ambiguous(); | ||
if is_match { | ||
candidate_set.push_candidate(ctor(data)); | ||
|
||
if potentially_unnormalized_candidates | ||
&& !obligation.predicate.has_infer_types_or_consts() | ||
{ | ||
// HACK: Pick the first trait def candidate for a fully | ||
// inferred predicate. This is to allow duplicates that | ||
// differ only in normalization. | ||
return; | ||
} | ||
ProjectionMatchesProjection::No => {} | ||
} | ||
} | ||
} | ||
|
@@ -1583,7 +1576,7 @@ fn confirm_candidate<'cx, 'tcx>( | |
selcx: &mut SelectionContext<'cx, 'tcx>, | ||
obligation: &ProjectionTyObligation<'tcx>, | ||
candidate: ProjectionCandidate<'tcx>, | ||
) -> Progress<'tcx> { | ||
) -> Projected<'tcx> { | ||
debug!(?obligation, ?candidate, "confirm_candidate"); | ||
let mut progress = match candidate { | ||
ProjectionCandidate::ParamEnv(poly_projection) | ||
|
@@ -1596,7 +1589,7 @@ fn confirm_candidate<'cx, 'tcx>( | |
} | ||
|
||
ProjectionCandidate::Select(impl_source) => { | ||
confirm_select_candidate(selcx, obligation, impl_source) | ||
Projected::Progress(confirm_select_candidate(selcx, obligation, impl_source)) | ||
} | ||
}; | ||
|
||
|
@@ -1605,9 +1598,11 @@ fn confirm_candidate<'cx, 'tcx>( | |
// with new region variables, we need to resolve them to existing variables | ||
// when possible for this to work. See `auto-trait-projection-recursion.rs` | ||
// for a case where this matters. | ||
if progress.term.has_infer_regions() { | ||
progress.term = | ||
progress.term.fold_with(&mut OpportunisticRegionResolver::new(selcx.infcx())); | ||
if let Projected::Progress(progress) = &mut progress { | ||
if progress.term.has_infer_regions() { | ||
progress.term = | ||
progress.term.fold_with(&mut OpportunisticRegionResolver::new(selcx.infcx())); | ||
} | ||
} | ||
progress | ||
} | ||
|
@@ -1688,9 +1683,12 @@ fn confirm_generator_candidate<'cx, 'tcx>( | |
} | ||
}); | ||
|
||
confirm_param_env_candidate(selcx, obligation, predicate, false) | ||
.with_addl_obligations(impl_source.nested) | ||
.with_addl_obligations(obligations) | ||
let progress = confirm_param_env_candidate(selcx, obligation, predicate, false); | ||
let progress = match progress { | ||
Projected::Progress(progress) => progress, | ||
Projected::NoProgress(_) => bug!(), | ||
}; | ||
progress.with_addl_obligations(impl_source.nested).with_addl_obligations(obligations) | ||
} | ||
|
||
fn confirm_discriminant_kind_candidate<'cx, 'tcx>( | ||
|
@@ -1715,7 +1713,12 @@ fn confirm_discriminant_kind_candidate<'cx, 'tcx>( | |
|
||
// We get here from `poly_project_and_unify_type` which replaces bound vars | ||
// with placeholders, so dummy is okay here. | ||
confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false) | ||
let progress = | ||
confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false); | ||
match progress { | ||
Projected::Progress(progress) => progress, | ||
Projected::NoProgress(_) => bug!(), | ||
} | ||
} | ||
|
||
fn confirm_pointee_candidate<'cx, 'tcx>( | ||
|
@@ -1746,8 +1749,12 @@ fn confirm_pointee_candidate<'cx, 'tcx>( | |
term: metadata_ty.into(), | ||
}; | ||
|
||
confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false) | ||
.with_addl_obligations(obligations) | ||
let progress = | ||
confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false); | ||
match progress { | ||
Projected::Progress(progress) => progress.with_addl_obligations(obligations), | ||
Projected::NoProgress(_) => bug!(), | ||
} | ||
} | ||
|
||
fn confirm_fn_pointer_candidate<'cx, 'tcx>( | ||
|
@@ -1819,15 +1826,19 @@ fn confirm_callable_candidate<'cx, 'tcx>( | |
term: ret_type.into(), | ||
}); | ||
|
||
confirm_param_env_candidate(selcx, obligation, predicate, true) | ||
let progress = confirm_param_env_candidate(selcx, obligation, predicate, true); | ||
match progress { | ||
Projected::Progress(progress) => progress, | ||
Projected::NoProgress(_) => bug!(), | ||
} | ||
} | ||
|
||
fn confirm_param_env_candidate<'cx, 'tcx>( | ||
selcx: &mut SelectionContext<'cx, 'tcx>, | ||
obligation: &ProjectionTyObligation<'tcx>, | ||
poly_cache_entry: ty::PolyProjectionPredicate<'tcx>, | ||
potentially_unnormalized_candidate: bool, | ||
) -> Progress<'tcx> { | ||
) -> Projected<'tcx> { | ||
let infcx = selcx.infcx(); | ||
let cause = &obligation.cause; | ||
let param_env = obligation.param_env; | ||
|
@@ -1868,23 +1879,42 @@ fn confirm_param_env_candidate<'cx, 'tcx>( | |
|
||
debug!(?cache_projection, ?obligation_projection); | ||
|
||
match infcx.at(cause, param_env).eq(cache_projection, obligation_projection) { | ||
Ok(InferOk { value: _, obligations }) => { | ||
nested_obligations.extend(obligations); | ||
assoc_ty_own_obligations(selcx, obligation, &mut nested_obligations); | ||
// FIXME(associated_const_equality): Handle consts here as well? Maybe this progress type should just take | ||
// a term instead. | ||
Progress { term: cache_entry.term, obligations: nested_obligations } | ||
} | ||
Err(e) => { | ||
let msg = format!( | ||
"Failed to unify obligation `{:?}` with poly_projection `{:?}`: {:?}", | ||
obligation, poly_cache_entry, e, | ||
); | ||
debug!("confirm_param_env_candidate: {}", msg); | ||
let err = infcx.tcx.ty_error_with_message(obligation.cause.span, &msg); | ||
Progress { term: err.into(), obligations: vec![] } | ||
match infcx.commit_if_ok(|snapshot| { | ||
let progress = match infcx.at(cause, param_env).eq(cache_projection, obligation_projection) | ||
{ | ||
Ok(InferOk { value: _, obligations }) => { | ||
nested_obligations.extend(obligations); | ||
assoc_ty_own_obligations(selcx, obligation, &mut nested_obligations); | ||
// FIXME(associated_const_equality): Handle consts here as well? Maybe this progress type should just take | ||
// a term instead. | ||
Progress { term: cache_entry.term, obligations: nested_obligations } | ||
} | ||
Err(e) => { | ||
let msg = format!( | ||
"Failed to unify obligation `{:?}` with poly_projection `{:?}`: {:?}", | ||
obligation, poly_cache_entry, e, | ||
); | ||
debug!("confirm_param_env_candidate: {}", msg); | ||
let err = infcx.tcx.ty_error_with_message(obligation.cause.span, &msg); | ||
Progress { term: err.into(), obligations: vec![] } | ||
} | ||
}; | ||
|
||
let any_instantiations = infcx.any_instantiations(&snapshot); | ||
|
||
if any_instantiations && !selcx.normalization_mode.allow_infer_constraint_during_projection | ||
Comment on lines
+1903
to
+1905
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. Now that I think about it, this could probably be done during candidate assembly. |
||
{ | ||
Err(ty::Term::Ty( | ||
infcx | ||
.tcx | ||
.mk_projection(obligation_projection.item_def_id, obligation_projection.substs), | ||
)) | ||
} else { | ||
Ok(progress) | ||
} | ||
}) { | ||
Ok(p) => Projected::Progress(p), | ||
Err(p) => Projected::NoProgress(p), | ||
} | ||
} | ||
|
||
|
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.
This is the same as to what is done in #90887, but puts the flag on
SelectionContext
instead ofAssocTypeNormalizer