@@ -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 ) ]
210220pub 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
0 commit comments