Skip to content

Commit

Permalink
Make FunctionArgumentObligation also use the "no allocation for mis…
Browse files Browse the repository at this point in the history
…c" trick
  • Loading branch information
oli-obk committed May 10, 2022
1 parent 1b51e1a commit 213c174
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
22 changes: 16 additions & 6 deletions compiler/rustc_middle/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,9 @@ impl<'tcx> ObligationCause<'tcx> {

pub fn map_code(
&mut self,
f: impl FnOnce(Lrc<ObligationCauseCode<'tcx>>) -> Lrc<ObligationCauseCode<'tcx>>,
f: impl FnOnce(InternedObligationCauseCode<'tcx>) -> Lrc<ObligationCauseCode<'tcx>>,
) {
self.code = Some(f(match self.code.take() {
Some(code) => code,
None => Lrc::new(MISC_OBLIGATION_CAUSE_CODE),
}));
self.code = Some(f(InternedObligationCauseCode { code: self.code.take() }));
}

pub fn derived_cause(
Expand Down Expand Up @@ -206,6 +203,19 @@ pub struct UnifyReceiverContext<'tcx> {
pub substs: SubstsRef<'tcx>,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift)]
pub struct InternedObligationCauseCode<'tcx> {
code: Option<Lrc<ObligationCauseCode<'tcx>>>,
}

impl<'tcx> std::ops::Deref for InternedObligationCauseCode<'tcx> {
type Target = ObligationCauseCode<'tcx>;

fn deref(&self) -> &Self::Target {
self.code.as_deref().unwrap_or(&MISC_OBLIGATION_CAUSE_CODE)
}
}

#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift)]
pub enum ObligationCauseCode<'tcx> {
/// Not well classified or should be obvious from the span.
Expand Down Expand Up @@ -293,7 +303,7 @@ pub enum ObligationCauseCode<'tcx> {
/// The node of the function call.
call_hir_id: hir::HirId,
/// The obligation introduced by this argument.
parent_code: Lrc<ObligationCauseCode<'tcx>>,
parent_code: InternedObligationCauseCode<'tcx>,
},

/// Error derived when matching traits/impls; see ObligationCause for more details
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1652,7 +1652,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
debug!("maybe_note_obligation_cause_for_async_await: code={:?}", code);
match code {
ObligationCauseCode::FunctionArgumentObligation { parent_code, .. } => {
next_code = Some(parent_code.as_ref());
next_code = Some(parent_code);
}
ObligationCauseCode::ImplDerivedObligation(cause) => {
let ty = cause.derived.parent_trait_pred.skip_binder().self_ty();
Expand Down

0 comments on commit 213c174

Please sign in to comment.