Skip to content

Commit ea668d9

Browse files
committed
use enum to represent ObligationCause::dummy without allocating
1 parent af7fbec commit ea668d9

File tree

1 file changed

+9
-10
lines changed
  • src/librustc_middle/traits

1 file changed

+9
-10
lines changed

src/librustc_middle/traits/mod.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,12 @@ pub enum Reveal {
8989
/// only live for a short period of time.
9090
#[derive(Clone, PartialEq, Eq, Hash)]
9191
pub struct ObligationCause<'tcx> {
92-
data: Rc<ObligationCauseData<'tcx>>,
92+
/// `None` for `ObligationCause::dummy`, `Some` otherwise.
93+
data: Option<Rc<ObligationCauseData<'tcx>>>,
9394
}
9495

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 };
10098

10199
// Correctly format `ObligationCause::dummy`.
102100
impl<'tcx> fmt::Debug for ObligationCause<'tcx> {
@@ -108,8 +106,9 @@ impl<'tcx> fmt::Debug for ObligationCause<'tcx> {
108106
impl Deref for ObligationCause<'tcx> {
109107
type Target = ObligationCauseData<'tcx>;
110108

109+
#[inline(always)]
111110
fn deref(&self) -> &Self::Target {
112-
&self.data
111+
self.data.as_deref().unwrap_or(&DUMMY_OBLIGATION_CAUSE_DATA)
113112
}
114113
}
115114

@@ -135,7 +134,7 @@ impl<'tcx> ObligationCause<'tcx> {
135134
body_id: hir::HirId,
136135
code: ObligationCauseCode<'tcx>,
137136
) -> ObligationCause<'tcx> {
138-
ObligationCause { data: Rc::new(ObligationCauseData { span, body_id, code }) }
137+
ObligationCause { data: Some(Rc::new(ObligationCauseData { span, body_id, code })) }
139138
}
140139

141140
pub fn misc(span: Span, body_id: hir::HirId) -> ObligationCause<'tcx> {
@@ -148,11 +147,11 @@ impl<'tcx> ObligationCause<'tcx> {
148147

149148
#[inline(always)]
150149
pub fn dummy() -> ObligationCause<'tcx> {
151-
DUMMY_OBLIGATION_CAUSE.with(Clone::clone)
150+
ObligationCause { data: None }
152151
}
153152

154153
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)))
156155
}
157156

158157
pub fn span(&self, tcx: TyCtxt<'tcx>) -> Span {

0 commit comments

Comments
 (0)