Skip to content

Commit 824e9e4

Browse files
committedMay 10, 2022
Use InternedObligationCauseCode everywhere
1 parent 213c174 commit 824e9e4

File tree

4 files changed

+33
-42
lines changed

4 files changed

+33
-42
lines changed
 

‎compiler/rustc_middle/src/traits/mod.rs

+20-28
Original file line numberDiff line numberDiff line change
@@ -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)]
207199
pub 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+
211211
impl<'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)]

‎compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1868,7 +1868,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
18681868
match code {
18691869
ObligationCauseCode::BuiltinDerivedObligation(data) => {
18701870
let parent_trait_ref = self.resolve_vars_if_possible(data.parent_trait_pred);
1871-
match self.get_parent_trait_ref(data.parent_code()) {
1871+
match self.get_parent_trait_ref(&data.parent_code) {
18721872
Some(t) => Some(t),
18731873
None => {
18741874
let ty = parent_trait_ref.skip_binder().self_ty();

‎compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1683,7 +1683,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
16831683
_ => {}
16841684
}
16851685

1686-
next_code = Some(cause.derived.parent_code());
1686+
next_code = Some(&cause.derived.parent_code);
16871687
}
16881688
ObligationCauseCode::DerivedObligation(derived_obligation)
16891689
| ObligationCauseCode::BuiltinDerivedObligation(derived_obligation) => {
@@ -1715,7 +1715,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
17151715
_ => {}
17161716
}
17171717

1718-
next_code = Some(derived_obligation.parent_code());
1718+
next_code = Some(&derived_obligation.parent_code);
17191719
}
17201720
_ => break,
17211721
}
@@ -2365,7 +2365,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
23652365
let is_upvar_tys_infer_tuple = if !matches!(ty.kind(), ty::Tuple(..)) {
23662366
false
23672367
} else {
2368-
if let ObligationCauseCode::BuiltinDerivedObligation(data) = data.parent_code()
2368+
if let ObligationCauseCode::BuiltinDerivedObligation(data) = &*data.parent_code
23692369
{
23702370
let parent_trait_ref =
23712371
self.resolve_vars_if_possible(data.parent_trait_pred);
@@ -2392,14 +2392,14 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
23922392
obligated_types.push(ty);
23932393

23942394
let parent_predicate = parent_trait_ref.to_predicate(tcx);
2395-
if !self.is_recursive_obligation(obligated_types, data.parent_code()) {
2395+
if !self.is_recursive_obligation(obligated_types, &data.parent_code) {
23962396
// #74711: avoid a stack overflow
23972397
ensure_sufficient_stack(|| {
23982398
self.note_obligation_cause_code(
23992399
err,
24002400
&parent_predicate,
24012401
param_env,
2402-
data.parent_code(),
2402+
&data.parent_code,
24032403
obligated_types,
24042404
seen_requirements,
24052405
)
@@ -2410,7 +2410,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
24102410
err,
24112411
&parent_predicate,
24122412
param_env,
2413-
&cause_code.peel_derives(),
2413+
cause_code.peel_derives(),
24142414
obligated_types,
24152415
seen_requirements,
24162416
)
@@ -2461,7 +2461,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
24612461
// We don't want to point at the ADT saying "required because it appears within
24622462
// the type `X`", like we would otherwise do in test `supertrait-auto-trait.rs`.
24632463
while let ObligationCauseCode::BuiltinDerivedObligation(derived) =
2464-
data.parent_code()
2464+
&*data.parent_code
24652465
{
24662466
let child_trait_ref =
24672467
self.resolve_vars_if_possible(derived.parent_trait_pred);
@@ -2474,7 +2474,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
24742474
parent_trait_pred = child_trait_ref;
24752475
}
24762476
}
2477-
while let ObligationCauseCode::ImplDerivedObligation(child) = data.parent_code() {
2477+
while let ObligationCauseCode::ImplDerivedObligation(child) = &*data.parent_code {
24782478
// Skip redundant recursive obligation notes. See `ui/issue-20413.rs`.
24792479
let child_trait_pred =
24802480
self.resolve_vars_if_possible(child.derived.parent_trait_pred);
@@ -2505,7 +2505,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
25052505
err,
25062506
&parent_predicate,
25072507
param_env,
2508-
data.parent_code(),
2508+
&data.parent_code,
25092509
obligated_types,
25102510
seen_requirements,
25112511
)
@@ -2520,7 +2520,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
25202520
err,
25212521
&parent_predicate,
25222522
param_env,
2523-
data.parent_code(),
2523+
&data.parent_code,
25242524
obligated_types,
25252525
seen_requirements,
25262526
)

‎compiler/rustc_typeck/src/check/fn_ctxt/checks.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1606,9 +1606,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16061606
let mut result_code = code;
16071607
loop {
16081608
let parent = match code {
1609-
ObligationCauseCode::ImplDerivedObligation(c) => c.derived.parent_code(),
1609+
ObligationCauseCode::ImplDerivedObligation(c) => &c.derived.parent_code,
16101610
ObligationCauseCode::BuiltinDerivedObligation(c)
1611-
| ObligationCauseCode::DerivedObligation(c) => c.parent_code(),
1611+
| ObligationCauseCode::DerivedObligation(c) => &c.parent_code,
16121612
_ => break result_code,
16131613
};
16141614
(result_code, code) = (code, parent);
@@ -1670,7 +1670,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16701670
call_hir_id: expr.hir_id,
16711671
parent_code,
16721672
}
1673-
.into()
16741673
});
16751674
} else if error.obligation.cause.span == call_sp {
16761675
// Make function calls point at the callee, not the whole thing.

0 commit comments

Comments
 (0)
Please sign in to comment.