Skip to content

Commit 213c174

Browse files
committed
Make FunctionArgumentObligation also use the "no allocation for misc" trick
1 parent 1b51e1a commit 213c174

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

compiler/rustc_middle/src/traits/mod.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,9 @@ impl<'tcx> ObligationCause<'tcx> {
165165

166166
pub fn map_code(
167167
&mut self,
168-
f: impl FnOnce(Lrc<ObligationCauseCode<'tcx>>) -> Lrc<ObligationCauseCode<'tcx>>,
168+
f: impl FnOnce(InternedObligationCauseCode<'tcx>) -> Lrc<ObligationCauseCode<'tcx>>,
169169
) {
170-
self.code = Some(f(match self.code.take() {
171-
Some(code) => code,
172-
None => Lrc::new(MISC_OBLIGATION_CAUSE_CODE),
173-
}));
170+
self.code = Some(f(InternedObligationCauseCode { code: self.code.take() }));
174171
}
175172

176173
pub fn derived_cause(
@@ -206,6 +203,19 @@ pub struct UnifyReceiverContext<'tcx> {
206203
pub substs: SubstsRef<'tcx>,
207204
}
208205

206+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift)]
207+
pub struct InternedObligationCauseCode<'tcx> {
208+
code: Option<Lrc<ObligationCauseCode<'tcx>>>,
209+
}
210+
211+
impl<'tcx> std::ops::Deref for InternedObligationCauseCode<'tcx> {
212+
type Target = ObligationCauseCode<'tcx>;
213+
214+
fn deref(&self) -> &Self::Target {
215+
self.code.as_deref().unwrap_or(&MISC_OBLIGATION_CAUSE_CODE)
216+
}
217+
}
218+
209219
#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift)]
210220
pub enum ObligationCauseCode<'tcx> {
211221
/// Not well classified or should be obvious from the span.
@@ -293,7 +303,7 @@ pub enum ObligationCauseCode<'tcx> {
293303
/// The node of the function call.
294304
call_hir_id: hir::HirId,
295305
/// The obligation introduced by this argument.
296-
parent_code: Lrc<ObligationCauseCode<'tcx>>,
306+
parent_code: InternedObligationCauseCode<'tcx>,
297307
},
298308

299309
/// Error derived when matching traits/impls; see ObligationCause for more details

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1652,7 +1652,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
16521652
debug!("maybe_note_obligation_cause_for_async_await: code={:?}", code);
16531653
match code {
16541654
ObligationCauseCode::FunctionArgumentObligation { parent_code, .. } => {
1655-
next_code = Some(parent_code.as_ref());
1655+
next_code = Some(parent_code);
16561656
}
16571657
ObligationCauseCode::ImplDerivedObligation(cause) => {
16581658
let ty = cause.derived.parent_trait_pred.skip_binder().self_ty();

0 commit comments

Comments
 (0)