@@ -97,9 +97,7 @@ pub struct ObligationCause<'tcx> {
97
97
/// information.
98
98
pub body_id : hir:: HirId ,
99
99
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 > ,
103
101
}
104
102
105
103
// This custom hash function speeds up hashing for `Obligation` deduplication
@@ -123,11 +121,7 @@ impl<'tcx> ObligationCause<'tcx> {
123
121
body_id : hir:: HirId ,
124
122
code : ObligationCauseCode < ' tcx > ,
125
123
) -> 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 ( ) }
131
125
}
132
126
133
127
pub fn misc ( span : Span , body_id : hir:: HirId ) -> ObligationCause < ' tcx > {
@@ -136,11 +130,11 @@ impl<'tcx> ObligationCause<'tcx> {
136
130
137
131
#[ inline( always) ]
138
132
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 )
140
134
}
141
135
142
136
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 ( ) }
144
138
}
145
139
146
140
pub fn span ( & self , tcx : TyCtxt < ' tcx > ) -> Span {
@@ -160,14 +154,14 @@ impl<'tcx> ObligationCause<'tcx> {
160
154
161
155
#[ inline]
162
156
pub fn code ( & self ) -> & ObligationCauseCode < ' tcx > {
163
- self . code . as_deref ( ) . unwrap_or ( & MISC_OBLIGATION_CAUSE_CODE )
157
+ & self . code
164
158
}
165
159
166
160
pub fn map_code (
167
161
& mut self ,
168
- f : impl FnOnce ( InternedObligationCauseCode < ' tcx > ) -> Lrc < ObligationCauseCode < ' tcx > > ,
162
+ f : impl FnOnce ( InternedObligationCauseCode < ' tcx > ) -> ObligationCauseCode < ' tcx > ,
169
163
) {
170
- self . code = Some ( f ( InternedObligationCauseCode { code : self . code . take ( ) } ) ) ;
164
+ self . code = f ( std :: mem :: take ( & mut self . code ) ) . into ( ) ;
171
165
}
172
166
173
167
pub fn derived_cause (
@@ -188,10 +182,8 @@ impl<'tcx> ObligationCause<'tcx> {
188
182
// NOTE(flaper87): As of now, it keeps track of the whole error
189
183
// chain. Ideally, we should have a way to configure this either
190
184
// 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 ( ) ;
195
187
self
196
188
}
197
189
}
@@ -203,11 +195,19 @@ pub struct UnifyReceiverContext<'tcx> {
203
195
pub substs : SubstsRef < ' tcx > ,
204
196
}
205
197
206
- #[ derive( Clone , Debug , PartialEq , Eq , Hash , Lift ) ]
198
+ #[ derive( Clone , Debug , PartialEq , Eq , Hash , Lift , Default ) ]
207
199
pub struct InternedObligationCauseCode < ' tcx > {
200
+ /// `None` for `MISC_OBLIGATION_CAUSE_CODE` (a common case, occurs ~60% of
201
+ /// the time). `Some` otherwise.
208
202
code : Option < Lrc < ObligationCauseCode < ' tcx > > > ,
209
203
}
210
204
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
+
211
211
impl < ' tcx > std:: ops:: Deref for InternedObligationCauseCode < ' tcx > {
212
212
type Target = ObligationCauseCode < ' tcx > ;
213
213
@@ -454,7 +454,7 @@ impl<'tcx> ObligationCauseCode<'tcx> {
454
454
BuiltinDerivedObligation ( derived)
455
455
| DerivedObligation ( derived)
456
456
| ImplDerivedObligation ( box ImplDerivedObligationCause { derived, .. } ) => {
457
- Some ( ( derived. parent_code ( ) , Some ( derived. parent_trait_pred ) ) )
457
+ Some ( ( & derived. parent_code , Some ( derived. parent_trait_pred ) ) )
458
458
}
459
459
_ => None ,
460
460
}
@@ -508,15 +508,7 @@ pub struct DerivedObligationCause<'tcx> {
508
508
pub parent_trait_pred : ty:: PolyTraitPredicate < ' tcx > ,
509
509
510
510
/// 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 > ,
520
512
}
521
513
522
514
#[ derive( Clone , Debug , TypeFoldable , Lift ) ]
0 commit comments