@@ -97,9 +97,7 @@ pub struct ObligationCause<'tcx> {
9797 /// information.
9898 pub body_id : hir:: HirId ,
9999
100- /// `None` for `MISC_OBLIGATION_CAUSE_CODE` (a common case, occurs ~60% of
101- /// the time). `Some` otherwise.
102- code : Option < Lrc < ObligationCauseCode < ' tcx > > > ,
100+ code : InternedObligationCauseCode < ' tcx > ,
103101}
104102
105103// This custom hash function speeds up hashing for `Obligation` deduplication
@@ -123,11 +121,7 @@ impl<'tcx> ObligationCause<'tcx> {
123121 body_id : hir:: HirId ,
124122 code : ObligationCauseCode < ' tcx > ,
125123 ) -> ObligationCause < ' tcx > {
126- ObligationCause {
127- span,
128- body_id,
129- code : if code == MISC_OBLIGATION_CAUSE_CODE { None } else { Some ( Lrc :: new ( code) ) } ,
130- }
124+ ObligationCause { span, body_id, code : code. into ( ) }
131125 }
132126
133127 pub fn misc ( span : Span , body_id : hir:: HirId ) -> ObligationCause < ' tcx > {
@@ -136,11 +130,11 @@ impl<'tcx> ObligationCause<'tcx> {
136130
137131 #[ inline( always) ]
138132 pub fn dummy ( ) -> ObligationCause < ' tcx > {
139- ObligationCause { span : DUMMY_SP , body_id : hir :: CRATE_HIR_ID , code : None }
133+ ObligationCause :: dummy_with_span ( DUMMY_SP )
140134 }
141135
142136 pub fn dummy_with_span ( span : Span ) -> ObligationCause < ' tcx > {
143- ObligationCause { span, body_id : hir:: CRATE_HIR_ID , code : None }
137+ ObligationCause { span, body_id : hir:: CRATE_HIR_ID , code : Default :: default ( ) }
144138 }
145139
146140 pub fn span ( & self , tcx : TyCtxt < ' tcx > ) -> Span {
@@ -160,14 +154,14 @@ impl<'tcx> ObligationCause<'tcx> {
160154
161155 #[ inline]
162156 pub fn code ( & self ) -> & ObligationCauseCode < ' tcx > {
163- self . code . as_deref ( ) . unwrap_or ( & MISC_OBLIGATION_CAUSE_CODE )
157+ & self . code
164158 }
165159
166160 pub fn map_code (
167161 & mut self ,
168- f : impl FnOnce ( InternedObligationCauseCode < ' tcx > ) -> Lrc < ObligationCauseCode < ' tcx > > ,
162+ f : impl FnOnce ( InternedObligationCauseCode < ' tcx > ) -> ObligationCauseCode < ' tcx > ,
169163 ) {
170- self . code = Some ( f ( InternedObligationCauseCode { code : self . code . take ( ) } ) ) ;
164+ self . code = f ( std :: mem :: take ( & mut self . code ) ) . into ( ) ;
171165 }
172166
173167 pub fn derived_cause (
@@ -188,10 +182,8 @@ impl<'tcx> ObligationCause<'tcx> {
188182 // NOTE(flaper87): As of now, it keeps track of the whole error
189183 // chain. Ideally, we should have a way to configure this either
190184 // by using -Z verbose or just a CLI argument.
191- self . code = Some (
192- variant ( DerivedObligationCause { parent_trait_pred, parent_code : self . code . take ( ) } )
193- . into ( ) ,
194- ) ;
185+ self . code =
186+ variant ( DerivedObligationCause { parent_trait_pred, parent_code : self . code } ) . into ( ) ;
195187 self
196188 }
197189}
@@ -203,11 +195,19 @@ pub struct UnifyReceiverContext<'tcx> {
203195 pub substs : SubstsRef < ' tcx > ,
204196}
205197
206- #[ derive( Clone , Debug , PartialEq , Eq , Hash , Lift ) ]
198+ #[ derive( Clone , Debug , PartialEq , Eq , Hash , Lift , Default ) ]
207199pub struct InternedObligationCauseCode < ' tcx > {
200+ /// `None` for `MISC_OBLIGATION_CAUSE_CODE` (a common case, occurs ~60% of
201+ /// the time). `Some` otherwise.
208202 code : Option < Lrc < ObligationCauseCode < ' tcx > > > ,
209203}
210204
205+ impl < ' tcx > From < ObligationCauseCode < ' tcx > > for InternedObligationCauseCode < ' tcx > {
206+ fn from ( code : ObligationCauseCode < ' tcx > ) -> Self {
207+ Self { code : if code == MISC_OBLIGATION_CAUSE_CODE { None } else { Some ( Lrc :: new ( code) ) } }
208+ }
209+ }
210+
211211impl < ' tcx > std:: ops:: Deref for InternedObligationCauseCode < ' tcx > {
212212 type Target = ObligationCauseCode < ' tcx > ;
213213
@@ -454,7 +454,7 @@ impl<'tcx> ObligationCauseCode<'tcx> {
454454 BuiltinDerivedObligation ( derived)
455455 | DerivedObligation ( derived)
456456 | ImplDerivedObligation ( box ImplDerivedObligationCause { derived, .. } ) => {
457- Some ( ( derived. parent_code ( ) , Some ( derived. parent_trait_pred ) ) )
457+ Some ( ( & derived. parent_code , Some ( derived. parent_trait_pred ) ) )
458458 }
459459 _ => None ,
460460 }
@@ -508,15 +508,7 @@ pub struct DerivedObligationCause<'tcx> {
508508 pub parent_trait_pred : ty:: PolyTraitPredicate < ' tcx > ,
509509
510510 /// The parent trait had this cause.
511- parent_code : Option < Lrc < ObligationCauseCode < ' tcx > > > ,
512- }
513-
514- impl < ' tcx > DerivedObligationCause < ' tcx > {
515- /// Get a reference to the derived obligation cause's parent code.
516- #[ must_use]
517- pub fn parent_code ( & self ) -> & ObligationCauseCode < ' tcx > {
518- self . parent_code . as_deref ( ) . unwrap_or ( & MISC_OBLIGATION_CAUSE_CODE )
519- }
511+ pub parent_code : InternedObligationCauseCode < ' tcx > ,
520512}
521513
522514#[ derive( Clone , Debug , TypeFoldable , Lift ) ]
0 commit comments