@@ -89,14 +89,12 @@ pub enum Reveal {
89
89
/// only live for a short period of time.
90
90
#[ derive( Clone , PartialEq , Eq , Hash ) ]
91
91
pub struct ObligationCause < ' tcx > {
92
- data : Rc < ObligationCauseData < ' tcx > > ,
92
+ /// `None` for `ObligationCause::dummy`, `Some` otherwise.
93
+ data : Option < Rc < ObligationCauseData < ' tcx > > > ,
93
94
}
94
95
95
- // A dummy obligation. As the parralel compiler does not share `Obligation`s between
96
- // threads, we use a `thread_local` here so we can keep using an `Rc` inside of `ObligationCause`.
97
- thread_local ! {
98
- static DUMMY_OBLIGATION_CAUSE : ObligationCause <' static > = ObligationCause :: new( DUMMY_SP , hir:: CRATE_HIR_ID , MiscObligation ) ;
99
- }
96
+ const DUMMY_OBLIGATION_CAUSE_DATA : ObligationCauseData < ' static > =
97
+ ObligationCauseData { span : DUMMY_SP , body_id : hir:: CRATE_HIR_ID , code : MiscObligation } ;
100
98
101
99
// Correctly format `ObligationCause::dummy`.
102
100
impl < ' tcx > fmt:: Debug for ObligationCause < ' tcx > {
@@ -108,8 +106,9 @@ impl<'tcx> fmt::Debug for ObligationCause<'tcx> {
108
106
impl Deref for ObligationCause < ' tcx > {
109
107
type Target = ObligationCauseData < ' tcx > ;
110
108
109
+ #[ inline( always) ]
111
110
fn deref ( & self ) -> & Self :: Target {
112
- & self . data
111
+ self . data . as_deref ( ) . unwrap_or ( & DUMMY_OBLIGATION_CAUSE_DATA )
113
112
}
114
113
}
115
114
@@ -135,7 +134,7 @@ impl<'tcx> ObligationCause<'tcx> {
135
134
body_id : hir:: HirId ,
136
135
code : ObligationCauseCode < ' tcx > ,
137
136
) -> ObligationCause < ' tcx > {
138
- ObligationCause { data : Rc :: new ( ObligationCauseData { span, body_id, code } ) }
137
+ ObligationCause { data : Some ( Rc :: new ( ObligationCauseData { span, body_id, code } ) ) }
139
138
}
140
139
141
140
pub fn misc ( span : Span , body_id : hir:: HirId ) -> ObligationCause < ' tcx > {
@@ -148,11 +147,11 @@ impl<'tcx> ObligationCause<'tcx> {
148
147
149
148
#[ inline( always) ]
150
149
pub fn dummy ( ) -> ObligationCause < ' tcx > {
151
- DUMMY_OBLIGATION_CAUSE . with ( Clone :: clone )
150
+ ObligationCause { data : None }
152
151
}
153
152
154
153
pub fn make_mut ( & mut self ) -> & mut ObligationCauseData < ' tcx > {
155
- Rc :: make_mut ( & mut self . data )
154
+ Rc :: make_mut ( self . data . get_or_insert_with ( || Rc :: new ( DUMMY_OBLIGATION_CAUSE_DATA ) ) )
156
155
}
157
156
158
157
pub fn span ( & self , tcx : TyCtxt < ' tcx > ) -> Span {
0 commit comments