Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 1b51e1a

Browse files
committed
Don't allocate misc obligation parents of derived obligations
1 parent 9ba6ddb commit 1b51e1a

File tree

1 file changed

+7
-6
lines changed
  • compiler/rustc_middle/src/traits

1 file changed

+7
-6
lines changed

compiler/rustc_middle/src/traits/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,10 @@ impl<'tcx> ObligationCause<'tcx> {
191191
// NOTE(flaper87): As of now, it keeps track of the whole error
192192
// chain. Ideally, we should have a way to configure this either
193193
// by using -Z verbose or just a CLI argument.
194-
self.map_code(|parent_code| {
195-
variant(DerivedObligationCause { parent_trait_pred, parent_code }).into()
196-
});
194+
self.code = Some(
195+
variant(DerivedObligationCause { parent_trait_pred, parent_code: self.code.take() })
196+
.into(),
197+
);
197198
self
198199
}
199200
}
@@ -443,7 +444,7 @@ impl<'tcx> ObligationCauseCode<'tcx> {
443444
BuiltinDerivedObligation(derived)
444445
| DerivedObligation(derived)
445446
| ImplDerivedObligation(box ImplDerivedObligationCause { derived, .. }) => {
446-
Some((&derived.parent_code, Some(derived.parent_trait_pred)))
447+
Some((derived.parent_code(), Some(derived.parent_trait_pred)))
447448
}
448449
_ => None,
449450
}
@@ -497,14 +498,14 @@ pub struct DerivedObligationCause<'tcx> {
497498
pub parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
498499

499500
/// The parent trait had this cause.
500-
parent_code: Lrc<ObligationCauseCode<'tcx>>,
501+
parent_code: Option<Lrc<ObligationCauseCode<'tcx>>>,
501502
}
502503

503504
impl<'tcx> DerivedObligationCause<'tcx> {
504505
/// Get a reference to the derived obligation cause's parent code.
505506
#[must_use]
506507
pub fn parent_code(&self) -> &ObligationCauseCode<'tcx> {
507-
self.parent_code.as_ref()
508+
self.parent_code.as_deref().unwrap_or(&MISC_OBLIGATION_CAUSE_CODE)
508509
}
509510
}
510511

0 commit comments

Comments
 (0)