Add cycle errors to ScrubbedTraitError to remove a couple more calls to new_with_diagnostics

This commit is contained in:
Michael Goulet 2024-06-02 18:36:11 -04:00
parent 27f5eccd1f
commit 1e72c7f536
14 changed files with 42 additions and 64 deletions

View File

@ -287,7 +287,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
ocx.infcx.at(&ObligationCause::dummy_with_span(self.span), self.param_env), ocx.infcx.at(&ObligationCause::dummy_with_span(self.span), self.param_env),
ty, ty,
) )
.map_err(|_: Vec<ScrubbedTraitError>| NoSolution) .map_err(|_: Vec<ScrubbedTraitError<'tcx>>| NoSolution)
}, },
"normalize type outlives obligation", "normalize type outlives obligation",
) )

View File

@ -319,7 +319,8 @@ fn orphan_check<'tcx>(
} }
let ty = if infcx.next_trait_solver() { let ty = if infcx.next_trait_solver() {
let mut fulfill_cx = <dyn traits::TraitEngine<'tcx, ScrubbedTraitError>>::new(&infcx); let mut fulfill_cx =
<dyn traits::TraitEngine<'tcx, ScrubbedTraitError<'tcx>>>::new(&infcx);
infcx infcx
.at(&cause, ty::ParamEnv::empty()) .at(&cause, ty::ParamEnv::empty())
.structurally_normalize(ty, &mut *fulfill_cx) .structurally_normalize(ty, &mut *fulfill_cx)

View File

@ -1,9 +1,5 @@
use crate::solve::NextSolverError;
use crate::traits::query::evaluate_obligation::InferCtxtExt as _; use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
use crate::traits::{ use crate::traits::{self, Obligation, ObligationCause, ObligationCtxt, SelectionContext};
self, FromSolverError, Obligation, ObligationCause, ObligationCtxt, OldSolverError,
SelectionContext,
};
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_hir::lang_items::LangItem; use rustc_hir::lang_items::LangItem;
@ -124,21 +120,19 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
/// bound for the closure and in part because it is convenient to /// bound for the closure and in part because it is convenient to
/// have `'tcx` be free on this function so that we can talk about /// have `'tcx` be free on this function so that we can talk about
/// `K: TypeFoldable<TyCtxt<'tcx>>`.) /// `K: TypeFoldable<TyCtxt<'tcx>>`.)
fn enter_canonical_trait_query<K, R, E>( fn enter_canonical_trait_query<K, R>(
self, self,
canonical_key: &Canonical<'tcx, K>, canonical_key: &Canonical<'tcx, K>,
operation: impl FnOnce(&ObligationCtxt<'_, 'tcx, E>, K) -> Result<R, NoSolution>, operation: impl FnOnce(&ObligationCtxt<'_, 'tcx>, K) -> Result<R, NoSolution>,
) -> Result<CanonicalQueryResponse<'tcx, R>, NoSolution> ) -> Result<CanonicalQueryResponse<'tcx, R>, NoSolution>
where where
K: TypeFoldable<TyCtxt<'tcx>>, K: TypeFoldable<TyCtxt<'tcx>>,
R: Debug + TypeFoldable<TyCtxt<'tcx>>, R: Debug + TypeFoldable<TyCtxt<'tcx>>,
Canonical<'tcx, QueryResponse<'tcx, R>>: ArenaAllocatable<'tcx>, Canonical<'tcx, QueryResponse<'tcx, R>>: ArenaAllocatable<'tcx>,
E: FromSolverError<'tcx, NextSolverError<'tcx>>
+ FromSolverError<'tcx, OldSolverError<'tcx>>,
{ {
let (infcx, key, canonical_inference_vars) = let (infcx, key, canonical_inference_vars) =
self.build_with_canonical(DUMMY_SP, canonical_key); self.build_with_canonical(DUMMY_SP, canonical_key);
let ocx = ObligationCtxt::new_generic(&infcx); let ocx = ObligationCtxt::new(&infcx);
let value = operation(&ocx, key)?; let value = operation(&ocx, key)?;
ocx.make_canonicalized_query_response(canonical_inference_vars, value) ocx.make_canonicalized_query_response(canonical_inference_vars, value)
} }

View File

@ -28,7 +28,7 @@ impl<'tcx> InferCtxt<'tcx> {
), ),
ty, ty,
) )
.map_err(|_: Vec<ScrubbedTraitError>| NoSolution) .map_err(|_: Vec<ScrubbedTraitError<'tcx>>| NoSolution)
} else { } else {
Ok(ty) Ok(ty)
} }

View File

@ -225,7 +225,7 @@ impl<'tcx> FromSolverError<'tcx, NextSolverError<'tcx>> for FulfillmentError<'tc
} }
} }
impl<'tcx> FromSolverError<'tcx, NextSolverError<'tcx>> for ScrubbedTraitError { impl<'tcx> FromSolverError<'tcx, NextSolverError<'tcx>> for ScrubbedTraitError<'tcx> {
fn from_solver_error(_infcx: &InferCtxt<'tcx>, error: NextSolverError<'tcx>) -> Self { fn from_solver_error(_infcx: &InferCtxt<'tcx>, error: NextSolverError<'tcx>) -> Self {
match error { match error {
NextSolverError::TrueError(_) => ScrubbedTraitError::TrueError, NextSolverError::TrueError(_) => ScrubbedTraitError::TrueError,

View File

@ -253,7 +253,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for DeeplyNormalizeForDiagnosticsFolder<'_,
ty, ty,
vec![None; ty.outer_exclusive_binder().as_usize()], vec![None; ty.outer_exclusive_binder().as_usize()],
) )
.unwrap_or_else(|_: Vec<ScrubbedTraitError>| ty.super_fold_with(self)) .unwrap_or_else(|_: Vec<ScrubbedTraitError<'tcx>>| ty.super_fold_with(self))
} }
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> { fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
@ -262,6 +262,6 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for DeeplyNormalizeForDiagnosticsFolder<'_,
ct, ct,
vec![None; ct.outer_exclusive_binder().as_usize()], vec![None; ct.outer_exclusive_binder().as_usize()],
) )
.unwrap_or_else(|_: Vec<ScrubbedTraitError>| ct.super_fold_with(self)) .unwrap_or_else(|_: Vec<ScrubbedTraitError<'tcx>>| ct.super_fold_with(self))
} }
} }

View File

@ -54,7 +54,7 @@ impl<
/// Used if you want to have pleasant experience when dealing /// Used if you want to have pleasant experience when dealing
/// with obligations outside of hir or mir typeck. /// with obligations outside of hir or mir typeck.
pub struct ObligationCtxt<'a, 'tcx, E = ScrubbedTraitError> { pub struct ObligationCtxt<'a, 'tcx, E = ScrubbedTraitError<'tcx>> {
pub infcx: &'a InferCtxt<'tcx>, pub infcx: &'a InferCtxt<'tcx>,
engine: RefCell<Box<dyn TraitEngine<'tcx, E>>>, engine: RefCell<Box<dyn TraitEngine<'tcx, E>>>,
} }
@ -65,21 +65,12 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx, FulfillmentError<'tcx>> {
} }
} }
impl<'a, 'tcx> ObligationCtxt<'a, 'tcx, ScrubbedTraitError> { impl<'a, 'tcx> ObligationCtxt<'a, 'tcx, ScrubbedTraitError<'tcx>> {
pub fn new(infcx: &'a InferCtxt<'tcx>) -> Self { pub fn new(infcx: &'a InferCtxt<'tcx>) -> Self {
Self { infcx, engine: RefCell::new(<dyn TraitEngine<'tcx, _>>::new(infcx)) } Self { infcx, engine: RefCell::new(<dyn TraitEngine<'tcx, _>>::new(infcx)) }
} }
} }
impl<'a, 'tcx, E> ObligationCtxt<'a, 'tcx, E>
where
E: FromSolverError<'tcx, NextSolverError<'tcx>> + FromSolverError<'tcx, OldSolverError<'tcx>>,
{
pub fn new_generic(infcx: &'a InferCtxt<'tcx>) -> Self {
Self { infcx, engine: RefCell::new(<dyn TraitEngine<'tcx, _>>::new(infcx)) }
}
}
impl<'a, 'tcx, E> ObligationCtxt<'a, 'tcx, E> impl<'a, 'tcx, E> ObligationCtxt<'a, 'tcx, E>
where where
E: FulfillmentErrorLike<'tcx>, E: FulfillmentErrorLike<'tcx>,

View File

@ -856,16 +856,15 @@ impl<'tcx> FromSolverError<'tcx, OldSolverError<'tcx>> for FulfillmentError<'tcx
} }
} }
impl<'tcx> FromSolverError<'tcx, OldSolverError<'tcx>> for ScrubbedTraitError { impl<'tcx> FromSolverError<'tcx, OldSolverError<'tcx>> for ScrubbedTraitError<'tcx> {
fn from_solver_error(_infcx: &InferCtxt<'tcx>, error: OldSolverError<'tcx>) -> Self { fn from_solver_error(_infcx: &InferCtxt<'tcx>, error: OldSolverError<'tcx>) -> Self {
match error.error { match error.error {
FulfillmentErrorCode::Select(_) FulfillmentErrorCode::Select(_)
| FulfillmentErrorCode::Project(_) | FulfillmentErrorCode::Project(_)
| FulfillmentErrorCode::Subtype(_, _) | FulfillmentErrorCode::Subtype(_, _)
| FulfillmentErrorCode::ConstEquate(_, _) => ScrubbedTraitError::TrueError, | FulfillmentErrorCode::ConstEquate(_, _) => ScrubbedTraitError::TrueError,
FulfillmentErrorCode::Cycle(_) | FulfillmentErrorCode::Ambiguity { overflow: _ } => { FulfillmentErrorCode::Ambiguity { overflow: _ } => ScrubbedTraitError::Ambiguity,
ScrubbedTraitError::Ambiguity FulfillmentErrorCode::Cycle(cycle) => ScrubbedTraitError::Cycle(cycle),
}
} }
} }
} }

View File

@ -77,24 +77,26 @@ pub use rustc_infer::traits::*;
/// error itself (except for if it's an ambiguity or true error). /// error itself (except for if it's an ambiguity or true error).
/// ///
/// use [`ObligationCtxt::new_with_diagnostics`] to get a [`FulfillmentError`]. /// use [`ObligationCtxt::new_with_diagnostics`] to get a [`FulfillmentError`].
#[derive(Copy, Clone, Debug)] #[derive(Clone, Debug)]
pub enum ScrubbedTraitError { pub enum ScrubbedTraitError<'tcx> {
/// A real error. This goal definitely does not hold. /// A real error. This goal definitely does not hold.
TrueError, TrueError,
/// An ambiguity. This goal may hold if further inference is done. /// An ambiguity. This goal may hold if further inference is done.
Ambiguity, Ambiguity,
/// An old-solver-style cycle error, which will fatal.
Cycle(Vec<PredicateObligation<'tcx>>),
} }
impl ScrubbedTraitError { impl<'tcx> ScrubbedTraitError<'tcx> {
fn is_true_error(&self) -> bool { fn is_true_error(&self) -> bool {
match self { match self {
ScrubbedTraitError::TrueError => true, ScrubbedTraitError::TrueError => true,
ScrubbedTraitError::Ambiguity => false, ScrubbedTraitError::Ambiguity | ScrubbedTraitError::Cycle(_) => false,
} }
} }
} }
impl<'tcx> FulfillmentErrorLike<'tcx> for ScrubbedTraitError { impl<'tcx> FulfillmentErrorLike<'tcx> for ScrubbedTraitError<'tcx> {
fn is_true_error(&self) -> bool { fn is_true_error(&self) -> bool {
self.is_true_error() self.is_true_error()
} }

View File

@ -76,7 +76,7 @@ impl<'cx, 'tcx> At<'cx, 'tcx> {
}; };
if self.infcx.next_trait_solver() { if self.infcx.next_trait_solver() {
match crate::solve::deeply_normalize_with_skipped_universes::<_, ScrubbedTraitError>( match crate::solve::deeply_normalize_with_skipped_universes::<_, ScrubbedTraitError<'tcx>>(
self, value, universes, self, value, universes,
) { ) {
Ok(value) => return Ok(Normalized { value, obligations: vec![] }), Ok(value) => return Ok(Normalized { value, obligations: vec![] }),

View File

@ -267,7 +267,7 @@ pub fn compute_implied_outlives_bounds_compat_inner<'tcx>(
ocx.infcx.at(&ObligationCause::dummy(), param_env), ocx.infcx.at(&ObligationCause::dummy(), param_env),
ty_a, ty_a,
) )
.map_err(|_errs: Vec<ScrubbedTraitError>| NoSolution)?; .map_err(|_errs: Vec<ScrubbedTraitError<'tcx>>| NoSolution)?;
} }
let mut components = smallvec![]; let mut components = smallvec![];
push_outlives_components(tcx, ty_a, &mut components); push_outlives_components(tcx, ty_a, &mut components);

View File

@ -9,8 +9,8 @@ use rustc_middle::traits::CodegenObligationError;
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt}; use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt; use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt;
use rustc_trait_selection::traits::{ use rustc_trait_selection::traits::{
FulfillmentErrorCode, ImplSource, Obligation, ObligationCause, ObligationCtxt, ImplSource, Obligation, ObligationCause, ObligationCtxt, ScrubbedTraitError, SelectionContext,
SelectionContext, Unimplemented, Unimplemented,
}; };
use tracing::debug; use tracing::debug;
@ -51,7 +51,7 @@ pub fn codegen_select_candidate<'tcx>(
// all nested obligations. This is because they can inform the // all nested obligations. This is because they can inform the
// inference of the impl's type parameters. // inference of the impl's type parameters.
// FIXME(-Znext-solver): Doesn't need diagnostics if new solver. // FIXME(-Znext-solver): Doesn't need diagnostics if new solver.
let ocx = ObligationCtxt::new_with_diagnostics(&infcx); let ocx = ObligationCtxt::new(&infcx);
let impl_source = selection.map(|obligation| { let impl_source = selection.map(|obligation| {
ocx.register_obligation(obligation); ocx.register_obligation(obligation);
}); });
@ -65,7 +65,7 @@ pub fn codegen_select_candidate<'tcx>(
// Cycle errors are the only post-monomorphization errors possible; emit them now so // Cycle errors are the only post-monomorphization errors possible; emit them now so
// `rustc_ty_utils::resolve_associated_item` doesn't return `None` post-monomorphization. // `rustc_ty_utils::resolve_associated_item` doesn't return `None` post-monomorphization.
for err in errors { for err in errors {
if let FulfillmentErrorCode::Cycle(cycle) = err.code { if let ScrubbedTraitError::Cycle(cycle) = err {
infcx.err_ctxt().report_overflow_obligation_cycle(&cycle); infcx.err_ctxt().report_overflow_obligation_cycle(&cycle);
} }
} }

View File

@ -7,9 +7,7 @@ use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt;
use rustc_trait_selection::traits::query::{ use rustc_trait_selection::traits::query::{
normalize::NormalizationResult, CanonicalAliasGoal, NoSolution, normalize::NormalizationResult, CanonicalAliasGoal, NoSolution,
}; };
use rustc_trait_selection::traits::{ use rustc_trait_selection::traits::{self, ObligationCause, ScrubbedTraitError, SelectionContext};
self, FulfillmentError, FulfillmentErrorCode, ObligationCause, ObligationCtxt, SelectionContext,
};
use tracing::debug; use tracing::debug;
pub(crate) fn provide(p: &mut Providers) { pub(crate) fn provide(p: &mut Providers) {
@ -29,8 +27,7 @@ fn normalize_canonicalized_projection_ty<'tcx>(
tcx.infer_ctxt().enter_canonical_trait_query( tcx.infer_ctxt().enter_canonical_trait_query(
&goal, &goal,
|ocx: &ObligationCtxt<'_, 'tcx, FulfillmentError<'tcx>>, |ocx, ParamEnvAnd { param_env, value: goal }| {
ParamEnvAnd { param_env, value: goal }| {
debug_assert!(!ocx.infcx.next_trait_solver()); debug_assert!(!ocx.infcx.next_trait_solver());
let selcx = &mut SelectionContext::new(ocx.infcx); let selcx = &mut SelectionContext::new(ocx.infcx);
let cause = ObligationCause::dummy(); let cause = ObligationCause::dummy();
@ -50,7 +47,7 @@ fn normalize_canonicalized_projection_ty<'tcx>(
// that impl vars are constrained by the signature, for example). // that impl vars are constrained by the signature, for example).
if !tcx.sess.opts.actually_rustdoc { if !tcx.sess.opts.actually_rustdoc {
for error in &errors { for error in &errors {
if let FulfillmentErrorCode::Cycle(cycle) = &error.code { if let ScrubbedTraitError::Cycle(cycle) = &error {
ocx.infcx.err_ctxt().report_overflow_obligation_cycle(cycle); ocx.infcx.err_ctxt().report_overflow_obligation_cycle(cycle);
} }
} }
@ -74,7 +71,7 @@ fn normalize_canonicalized_weak_ty<'tcx>(
tcx.infer_ctxt().enter_canonical_trait_query( tcx.infer_ctxt().enter_canonical_trait_query(
&goal, &goal,
|ocx: &ObligationCtxt<'_, 'tcx>, ParamEnvAnd { param_env, value: goal }| { |ocx, ParamEnvAnd { param_env, value: goal }| {
let obligations = tcx.predicates_of(goal.def_id).instantiate_own(tcx, goal.args).map( let obligations = tcx.predicates_of(goal.def_id).instantiate_own(tcx, goal.args).map(
|(predicate, span)| { |(predicate, span)| {
traits::Obligation::new( traits::Obligation::new(
@ -100,7 +97,7 @@ fn normalize_canonicalized_inherent_projection_ty<'tcx>(
tcx.infer_ctxt().enter_canonical_trait_query( tcx.infer_ctxt().enter_canonical_trait_query(
&goal, &goal,
|ocx: &ObligationCtxt<'_, 'tcx>, ParamEnvAnd { param_env, value: goal }| { |ocx, ParamEnvAnd { param_env, value: goal }| {
let selcx = &mut SelectionContext::new(ocx.infcx); let selcx = &mut SelectionContext::new(ocx.infcx);
let cause = ObligationCause::dummy(); let cause = ObligationCause::dummy();
let mut obligations = vec![]; let mut obligations = vec![];

View File

@ -43,13 +43,10 @@ fn type_op_eq<'tcx>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Eq<'tcx>>>, canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Eq<'tcx>>>,
) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> { ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
tcx.infer_ctxt().enter_canonical_trait_query( tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |ocx, key| {
&canonicalized, let (param_env, Eq { a, b }) = key.into_parts();
|ocx: &ObligationCtxt<'_, 'tcx>, key| { Ok(ocx.eq(&ObligationCause::dummy(), param_env, a, b)?)
let (param_env, Eq { a, b }) = key.into_parts(); })
Ok(ocx.eq(&ObligationCause::dummy(), param_env, a, b)?)
},
)
} }
fn type_op_normalize<'tcx, T>( fn type_op_normalize<'tcx, T>(
@ -98,13 +95,10 @@ fn type_op_subtype<'tcx>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Subtype<'tcx>>>, canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Subtype<'tcx>>>,
) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> { ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> {
tcx.infer_ctxt().enter_canonical_trait_query( tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |ocx, key| {
&canonicalized, let (param_env, Subtype { sub, sup }) = key.into_parts();
|ocx: &ObligationCtxt<'_, 'tcx>, key| { Ok(ocx.sup(&ObligationCause::dummy(), param_env, sup, sub)?)
let (param_env, Subtype { sub, sup }) = key.into_parts(); })
Ok(ocx.sup(&ObligationCause::dummy(), param_env, sup, sub)?)
},
)
} }
fn type_op_prove_predicate<'tcx>( fn type_op_prove_predicate<'tcx>(