-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Gracefully handle overflow errors in impl rematching #122539
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
Changes from 1 commit
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 |
---|---|---|
@@ -1,11 +1,14 @@ | ||
//! Deeply normalize types using the old trait solver. | ||
use crate::traits::project::ProjectionNormalizationFailure; | ||
|
||
use super::error_reporting::OverflowCause; | ||
use super::error_reporting::TypeErrCtxtExt; | ||
use super::SelectionContext; | ||
use super::{project, with_replaced_escaping_bound_vars, BoundVarReplacer, PlaceholderReplacer}; | ||
use rustc_data_structures::stack::ensure_sufficient_stack; | ||
use rustc_infer::infer::at::At; | ||
use rustc_infer::infer::InferOk; | ||
use rustc_infer::traits::OverflowError; | ||
use rustc_infer::traits::PredicateObligation; | ||
use rustc_infer::traits::{FulfillmentError, Normalized, Obligation, TraitEngine}; | ||
use rustc_middle::traits::{ObligationCause, ObligationCauseCode, Reveal}; | ||
|
@@ -97,6 +100,27 @@ where | |
result | ||
} | ||
|
||
#[instrument(level = "info", skip(selcx, param_env, cause, obligations))] | ||
pub(crate) fn try_normalize_with_depth_to<'a, 'b, 'tcx, T>( | ||
selcx: &'a mut SelectionContext<'b, 'tcx>, | ||
param_env: ty::ParamEnv<'tcx>, | ||
cause: ObligationCause<'tcx>, | ||
depth: usize, | ||
value: T, | ||
obligations: &mut Vec<PredicateObligation<'tcx>>, | ||
) -> Result<T, OverflowError> | ||
where | ||
T: TypeFoldable<TyCtxt<'tcx>>, | ||
{ | ||
debug!(obligations.len = obligations.len()); | ||
let mut normalizer = AssocTypeNormalizer::new(selcx, param_env, cause, depth, obligations); | ||
let result = ensure_sufficient_stack(|| normalizer.fold(value)); | ||
debug!(?result, obligations.len = normalizer.obligations.len()); | ||
debug!(?normalizer.obligations,); | ||
normalizer.overflowed?; | ||
Ok(result) | ||
} | ||
|
||
pub(super) fn needs_normalization<'tcx, T: TypeVisitable<TyCtxt<'tcx>>>( | ||
value: &T, | ||
reveal: Reveal, | ||
|
@@ -121,6 +145,11 @@ struct AssocTypeNormalizer<'a, 'b, 'tcx> { | |
obligations: &'a mut Vec<PredicateObligation<'tcx>>, | ||
depth: usize, | ||
universes: Vec<Option<ty::UniverseIndex>>, | ||
/// Signals that there was an overflow during normalization somewhere. | ||
/// We use this side channel instead of a `FallibleTypeFolder`, because | ||
/// most code expects to get their value back on error, and we'd have to | ||
/// clone the original value before normalization to achieve this. | ||
overflowed: Result<(), OverflowError>, | ||
} | ||
|
||
impl<'a, 'b, 'tcx> AssocTypeNormalizer<'a, 'b, 'tcx> { | ||
|
@@ -132,7 +161,15 @@ impl<'a, 'b, 'tcx> AssocTypeNormalizer<'a, 'b, 'tcx> { | |
obligations: &'a mut Vec<PredicateObligation<'tcx>>, | ||
) -> AssocTypeNormalizer<'a, 'b, 'tcx> { | ||
debug_assert!(!selcx.infcx.next_trait_solver()); | ||
AssocTypeNormalizer { selcx, param_env, cause, obligations, depth, universes: vec![] } | ||
AssocTypeNormalizer { | ||
selcx, | ||
param_env, | ||
cause, | ||
obligations, | ||
depth, | ||
universes: vec![], | ||
overflowed: Ok(()), | ||
} | ||
} | ||
|
||
fn fold<T: TypeFoldable<TyCtxt<'tcx>>>(&mut self, value: T) -> T { | ||
|
@@ -243,14 +280,21 @@ impl<'a, 'b, 'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTypeNormalizer<'a, 'b, 'tcx | |
self.depth, | ||
self.obligations, | ||
); | ||
let normalized_ty = match normalized_ty { | ||
Ok(term) => term.ty().unwrap(), | ||
Err(oflo) => { | ||
self.overflowed = Err(oflo); | ||
return ty; | ||
} | ||
}; | ||
debug!( | ||
?self.depth, | ||
?ty, | ||
?normalized_ty, | ||
obligations.len = ?self.obligations.len(), | ||
"AssocTypeNormalizer: normalized type" | ||
); | ||
normalized_ty.ty().unwrap() | ||
normalized_ty | ||
} | ||
|
||
ty::Projection => { | ||
|
@@ -277,21 +321,24 @@ impl<'a, 'b, 'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTypeNormalizer<'a, 'b, 'tcx | |
self.cause.clone(), | ||
self.depth, | ||
self.obligations, | ||
) | ||
.ok() | ||
.flatten() | ||
.map(|term| term.ty().unwrap()) | ||
.map(|normalized_ty| { | ||
PlaceholderReplacer::replace_placeholders( | ||
); | ||
let normalized_ty = match normalized_ty { | ||
Ok(Some(term)) => PlaceholderReplacer::replace_placeholders( | ||
infcx, | ||
mapped_regions, | ||
mapped_types, | ||
mapped_consts, | ||
&self.universes, | ||
normalized_ty, | ||
) | ||
}) | ||
.unwrap_or_else(|| ty.super_fold_with(self)); | ||
term.ty().unwrap(), | ||
), | ||
Ok(None) | Err(ProjectionNormalizationFailure::InProgress) => { | ||
ty.super_fold_with(self) | ||
} | ||
Err(ProjectionNormalizationFailure::Overflow(oflo)) => { | ||
self.overflowed = Err(oflo); | ||
return ty; | ||
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. we don't even bother to recurse with |
||
} | ||
}; | ||
|
||
debug!( | ||
?self.depth, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -925,6 +925,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { | |
// since we don't actually use them. | ||
&mut vec![], | ||
) | ||
.ok()? | ||
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. if normalization overflowed, we will not do the migration. To combine both infallible overflowing normalization and this migration, some truly cursed shenanigans are necessary, if possible at all. |
||
.ty() | ||
.unwrap(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ use crate::solve::InferCtxtSelectExt; | |
use crate::traits::error_reporting::TypeErrCtxtExt; | ||
use crate::traits::normalize::normalize_with_depth; | ||
use crate::traits::normalize::normalize_with_depth_to; | ||
use crate::traits::normalize::try_normalize_with_depth_to; | ||
use crate::traits::project::ProjectAndUnifyResult; | ||
use crate::traits::project::ProjectionCacheKeyExt; | ||
use crate::traits::ProjectionCacheKey; | ||
|
@@ -41,11 +42,13 @@ use rustc_middle::dep_graph::DepNodeIndex; | |
use rustc_middle::mir::interpret::ErrorHandled; | ||
use rustc_middle::ty::_match::MatchAgainstFreshVars; | ||
use rustc_middle::ty::abstract_const::NotConstEvaluatable; | ||
use rustc_middle::ty::error::TypeError; | ||
use rustc_middle::ty::relate::TypeRelation; | ||
use rustc_middle::ty::GenericArgsRef; | ||
use rustc_middle::ty::{self, PolyProjectionPredicate, ToPredicate}; | ||
use rustc_middle::ty::{Ty, TyCtxt, TypeFoldable, TypeVisitableExt}; | ||
use rustc_span::symbol::sym; | ||
use rustc_span::ErrorGuaranteed; | ||
use rustc_span::Symbol; | ||
|
||
use std::cell::{Cell, RefCell}; | ||
|
@@ -2083,6 +2086,16 @@ impl<'tcx> SelectionContext<'_, 'tcx> { | |
} | ||
} | ||
|
||
#[derive(Debug)] | ||
enum MatchImplFailure<'tcx> { | ||
ReservationImpl, | ||
#[allow(dead_code)] | ||
TypeError(TypeError<'tcx>), | ||
#[allow(dead_code)] | ||
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. why |
||
Overflow(OverflowError), | ||
Error(ErrorGuaranteed), | ||
} | ||
|
||
impl<'tcx> SelectionContext<'_, 'tcx> { | ||
fn sized_conditions( | ||
&mut self, | ||
|
@@ -2435,9 +2448,11 @@ impl<'tcx> SelectionContext<'_, 'tcx> { | |
let impl_trait_header = self.tcx().impl_trait_header(impl_def_id).unwrap(); | ||
match self.match_impl(impl_def_id, impl_trait_header, obligation) { | ||
Ok(args) => args, | ||
Err(()) => { | ||
Err(err) => { | ||
let predicate = self.infcx.resolve_vars_if_possible(obligation.predicate); | ||
bug!("impl {impl_def_id:?} was matchable against {predicate:?} but now is not") | ||
bug!( | ||
"impl {impl_def_id:?} was matchable against {predicate:?} but now is not: {err:?}" | ||
) | ||
} | ||
} | ||
} | ||
|
@@ -2448,30 +2463,30 @@ impl<'tcx> SelectionContext<'_, 'tcx> { | |
impl_def_id: DefId, | ||
impl_trait_header: ty::ImplTraitHeader<'tcx>, | ||
obligation: &PolyTraitObligation<'tcx>, | ||
) -> Result<Normalized<'tcx, GenericArgsRef<'tcx>>, ()> { | ||
) -> Result<Normalized<'tcx, GenericArgsRef<'tcx>>, MatchImplFailure<'tcx>> { | ||
let placeholder_obligation = | ||
self.infcx.enter_forall_and_leak_universe(obligation.predicate); | ||
let placeholder_obligation_trait_ref = placeholder_obligation.trait_ref; | ||
|
||
let impl_args = self.infcx.fresh_args_for_item(obligation.cause.span, impl_def_id); | ||
|
||
let trait_ref = impl_trait_header.trait_ref.instantiate(self.tcx(), impl_args); | ||
if trait_ref.references_error() { | ||
return Err(()); | ||
} | ||
trait_ref.error_reported().map_err(MatchImplFailure::Error)?; | ||
|
||
debug!(?impl_trait_header); | ||
|
||
let Normalized { value: impl_trait_ref, obligations: mut nested_obligations } = | ||
ensure_sufficient_stack(|| { | ||
normalize_with_depth( | ||
self, | ||
obligation.param_env, | ||
obligation.cause.clone(), | ||
obligation.recursion_depth + 1, | ||
trait_ref, | ||
) | ||
}); | ||
let mut nested_obligations = vec![]; | ||
let impl_trait_ref = ensure_sufficient_stack(|| { | ||
try_normalize_with_depth_to( | ||
self, | ||
obligation.param_env, | ||
obligation.cause.clone(), | ||
obligation.recursion_depth + 1, | ||
trait_ref, | ||
&mut nested_obligations, | ||
) | ||
}) | ||
.map_err(MatchImplFailure::Overflow)?; | ||
|
||
debug!(?impl_trait_ref, ?placeholder_obligation_trait_ref); | ||
|
||
|
@@ -2485,14 +2500,12 @@ impl<'tcx> SelectionContext<'_, 'tcx> { | |
.infcx | ||
.at(&cause, obligation.param_env) | ||
.eq(DefineOpaqueTypes::No, placeholder_obligation_trait_ref, impl_trait_ref) | ||
.map_err(|e| { | ||
debug!("match_impl: failed eq_trait_refs due to `{}`", e.to_string(self.tcx())) | ||
})?; | ||
.map_err(MatchImplFailure::TypeError)?; | ||
nested_obligations.extend(obligations); | ||
|
||
if !self.is_intercrate() && impl_trait_header.polarity == ty::ImplPolarity::Reservation { | ||
debug!("reservation impls only apply in intercrate mode"); | ||
return Err(()); | ||
return Err(MatchImplFailure::ReservationImpl); | ||
} | ||
|
||
Ok(Normalized { value: impl_args, obligations: nested_obligations }) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,8 @@ fn normalize_canonicalized_projection_ty<'tcx>( | |
cause, | ||
0, | ||
&mut obligations, | ||
); | ||
) | ||
.map_err(|_| NoSolution)?; | ||
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. The overflow would have errored out below anyway, causing |
||
ocx.register_obligations(obligations); | ||
// #112047: With projections and opaques, we are able to create opaques that | ||
// are recursive (given some generic parameters of the opaque's type variables). | ||
|
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.
slight behaviour change, before this PR the
normalize_projection_type
would replace the entire projection with an inference variable and set up the correct obligations. I can replicate the previous behaviour, but afaict we're in a doomed normalization anyway.