1
Fork 0

Add a helper function for a common piece of code

This commit is contained in:
Oli Scherer 2022-05-10 11:03:52 +00:00
parent 05a62c5527
commit 5b5b549580
3 changed files with 32 additions and 68 deletions

View file

@ -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.