Add a helper function for a common piece of code
This commit is contained in:
parent
05a62c5527
commit
5b5b549580
@ -427,25 +427,27 @@ pub struct ImplDerivedObligationCause<'tcx> {
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
impl ObligationCauseCode<'_> {
|
||||
impl<'tcx> ObligationCauseCode<'tcx> {
|
||||
// Return the base obligation, ignoring derived obligations.
|
||||
pub fn peel_derives(&self) -> &Self {
|
||||
let mut base_cause = self;
|
||||
loop {
|
||||
match base_cause {
|
||||
BuiltinDerivedObligation(DerivedObligationCause { parent_code, .. })
|
||||
| DerivedObligation(DerivedObligationCause { parent_code, .. })
|
||||
| FunctionArgumentObligation { parent_code, .. } => {
|
||||
base_cause = &parent_code;
|
||||
}
|
||||
ImplDerivedObligation(obligation_cause) => {
|
||||
base_cause = &*obligation_cause.derived.parent_code;
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
while let Some((parent_code, _)) = base_cause.parent() {
|
||||
base_cause = parent_code;
|
||||
}
|
||||
base_cause
|
||||
}
|
||||
|
||||
pub fn parent(&self) -> Option<(&Self, Option<ty::PolyTraitPredicate<'tcx>>)> {
|
||||
match self {
|
||||
FunctionArgumentObligation { parent_code, .. } => Some((parent_code, None)),
|
||||
BuiltinDerivedObligation(derived)
|
||||
| DerivedObligation(derived)
|
||||
| ImplDerivedObligation(box ImplDerivedObligationCause { derived, .. }) => {
|
||||
Some((&derived.parent_code, Some(derived.parent_trait_pred)))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// `ObligationCauseCode` is used a lot. Make sure it doesn't unintentionally get bigger.
|
||||
|
@ -2,11 +2,10 @@ pub mod on_unimplemented;
|
||||
pub mod suggestions;
|
||||
|
||||
use super::{
|
||||
DerivedObligationCause, EvaluationResult, FulfillmentContext, FulfillmentError,
|
||||
FulfillmentErrorCode, ImplDerivedObligationCause, MismatchedProjectionTypes, Obligation,
|
||||
ObligationCause, ObligationCauseCode, OnUnimplementedDirective, OnUnimplementedNote,
|
||||
OutputTypeParameterMismatch, Overflow, PredicateObligation, SelectionContext, SelectionError,
|
||||
TraitNotObjectSafe,
|
||||
EvaluationResult, FulfillmentContext, FulfillmentError, FulfillmentErrorCode,
|
||||
MismatchedProjectionTypes, Obligation, ObligationCause, ObligationCauseCode,
|
||||
OnUnimplementedDirective, OnUnimplementedNote, OutputTypeParameterMismatch, Overflow,
|
||||
PredicateObligation, SelectionContext, SelectionError, TraitNotObjectSafe,
|
||||
};
|
||||
|
||||
use crate::infer::error_reporting::{TyCategory, TypeAnnotationNeeded as ErrorCode};
|
||||
@ -684,32 +683,12 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
||||
let mut code = obligation.cause.code();
|
||||
let mut trait_pred = trait_predicate;
|
||||
let mut peeled = false;
|
||||
loop {
|
||||
match &*code {
|
||||
ObligationCauseCode::FunctionArgumentObligation {
|
||||
parent_code,
|
||||
..
|
||||
} => {
|
||||
code = &parent_code;
|
||||
}
|
||||
ObligationCauseCode::ImplDerivedObligation(
|
||||
box ImplDerivedObligationCause {
|
||||
derived,
|
||||
..
|
||||
},
|
||||
)
|
||||
| ObligationCauseCode::BuiltinDerivedObligation(
|
||||
derived,
|
||||
)
|
||||
| ObligationCauseCode::DerivedObligation(
|
||||
derived,
|
||||
) => {
|
||||
peeled = true;
|
||||
code = &derived.parent_code;
|
||||
trait_pred = derived.parent_trait_pred;
|
||||
}
|
||||
_ => break,
|
||||
};
|
||||
while let Some((parent_code, parent_trait_pred)) = code.parent() {
|
||||
code = parent_code;
|
||||
if let Some(parent_trait_pred) = parent_trait_pred {
|
||||
trait_pred = parent_trait_pred;
|
||||
peeled = true;
|
||||
}
|
||||
}
|
||||
let def_id = trait_pred.def_id();
|
||||
// Mention *all* the `impl`s for the *top most* obligation, the
|
||||
|
@ -1,6 +1,6 @@
|
||||
use super::{
|
||||
DerivedObligationCause, EvaluationResult, ImplDerivedObligationCause, Obligation,
|
||||
ObligationCause, ObligationCauseCode, PredicateObligation, SelectionContext,
|
||||
EvaluationResult, Obligation, ObligationCause, ObligationCauseCode, PredicateObligation,
|
||||
SelectionContext,
|
||||
};
|
||||
|
||||
use crate::autoderef::Autoderef;
|
||||
@ -623,28 +623,11 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
||||
let span = obligation.cause.span;
|
||||
let mut real_trait_pred = trait_pred;
|
||||
let mut code = obligation.cause.code();
|
||||
loop {
|
||||
match &code {
|
||||
ObligationCauseCode::FunctionArgumentObligation { parent_code, .. } => {
|
||||
code = &parent_code;
|
||||
}
|
||||
ObligationCauseCode::ImplDerivedObligation(box ImplDerivedObligationCause {
|
||||
derived: DerivedObligationCause { parent_code, parent_trait_pred },
|
||||
..
|
||||
})
|
||||
| ObligationCauseCode::BuiltinDerivedObligation(DerivedObligationCause {
|
||||
parent_code,
|
||||
parent_trait_pred,
|
||||
})
|
||||
| ObligationCauseCode::DerivedObligation(DerivedObligationCause {
|
||||
parent_code,
|
||||
parent_trait_pred,
|
||||
}) => {
|
||||
code = &parent_code;
|
||||
real_trait_pred = *parent_trait_pred;
|
||||
}
|
||||
_ => break,
|
||||
};
|
||||
while let Some((parent_code, parent_trait_pred)) = code.parent() {
|
||||
code = parent_code;
|
||||
if let Some(parent_trait_pred) = parent_trait_pred {
|
||||
real_trait_pred = parent_trait_pred;
|
||||
}
|
||||
let Some(real_ty) = real_trait_pred.self_ty().no_bound_vars() else {
|
||||
continue;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user