Skip to content

Commit d8e01c0

Browse files
committed
Add ability to branch on whether DebugWithInfcx has an Infcx
1 parent 7c73595 commit d8e01c0

File tree

4 files changed

+60
-48
lines changed

4 files changed

+60
-48
lines changed

compiler/rustc_middle/src/ty/structural_impls.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,17 @@ impl fmt::Debug for ty::LateParamRegion {
8383
}
8484
}
8585

86+
impl<'tcx> fmt::Debug for Ty<'tcx> {
87+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88+
with_no_trimmed_paths!(self.kind().fmt(f))
89+
}
90+
}
8691
impl<'tcx> ty::DebugWithInfcx<TyCtxt<'tcx>> for Ty<'tcx> {
8792
fn fmt<Infcx: InferCtxtLike<Interner = TyCtxt<'tcx>>>(
8893
this: WithInfcx<'_, Infcx, &Self>,
8994
f: &mut core::fmt::Formatter<'_>,
9095
) -> core::fmt::Result {
91-
this.data.fmt(f)
92-
}
93-
}
94-
impl<'tcx> fmt::Debug for Ty<'tcx> {
95-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96-
with_no_trimmed_paths!(fmt::Debug::fmt(self.kind(), f))
96+
this.map(|ty| ty.kind()).fmt(f)
9797
}
9898
}
9999

@@ -264,9 +264,12 @@ impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for ty::RegionVid {
264264
this: WithInfcx<'_, Infcx, &Self>,
265265
f: &mut core::fmt::Formatter<'_>,
266266
) -> core::fmt::Result {
267-
match this.infcx.universe_of_lt(*this.data) {
268-
Some(universe) => write!(f, "'?{}_{}", this.data.index(), universe.index()),
267+
match this.infcx {
269268
None => write!(f, "{:?}", this.data),
269+
Some(infcx) => match infcx.universe_of_lt(*this.data) {
270+
Some(universe) => write!(f, "'?{}_{}", this.data.index(), universe.index()),
271+
None => write!(f, "'?{}_..", this.data.index()),
272+
},
270273
}
271274
}
272275
}

compiler/rustc_type_ir/src/const_kind.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,18 @@ impl<I: Interner> DebugWithInfcx<I> for InferConst {
180180
this: WithInfcx<'_, Infcx, &Self>,
181181
f: &mut core::fmt::Formatter<'_>,
182182
) -> core::fmt::Result {
183-
match *this.data {
184-
InferConst::Var(vid) => match this.infcx.universe_of_ct(vid) {
185-
None => write!(f, "{:?}", this.data),
186-
Some(universe) => write!(f, "?{}_{}c", vid.index(), universe.index()),
183+
match this.infcx {
184+
None => write!(f, "{:?}", this.data),
185+
Some(infcx) => match *this.data {
186+
InferConst::Var(vid) => match infcx.universe_of_ct(vid) {
187+
None => write!(f, "?{}_..c", vid.index()),
188+
Some(universe) => write!(f, "?{}_{}c", vid.index(), universe.index()),
189+
},
190+
InferConst::EffectVar(vid) => write!(f, "?{}e", vid.index()),
191+
InferConst::Fresh(_) => {
192+
unreachable!()
193+
}
187194
},
188-
InferConst::EffectVar(vid) => write!(f, "?{}e", vid.index()),
189-
InferConst::Fresh(_) => {
190-
unreachable!()
191-
}
192195
}
193196
}
194197
}

compiler/rustc_type_ir/src/debug.rs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,55 @@
11
use crate::{ConstVid, InferCtxtLike, Interner, TyVid, UniverseIndex};
22

33
use core::fmt;
4-
use std::marker::PhantomData;
5-
6-
pub struct NoInfcx<I>(PhantomData<I>);
74

5+
pub struct NoInfcx<I>(core::convert::Infallible, core::marker::PhantomData<I>);
86
impl<I: Interner> InferCtxtLike for NoInfcx<I> {
97
type Interner = I;
108

119
fn interner(&self) -> Self::Interner {
12-
unreachable!()
10+
match self.0 {}
1311
}
1412

1513
fn universe_of_ty(&self, _ty: TyVid) -> Option<UniverseIndex> {
16-
None
14+
match self.0 {}
1715
}
1816

19-
fn universe_of_lt(&self, _lt: I::InferRegion) -> Option<UniverseIndex> {
20-
None
17+
fn root_ty_var(&self, _vid: TyVid) -> TyVid {
18+
match self.0 {}
2119
}
2220

23-
fn universe_of_ct(&self, _ct: ConstVid) -> Option<UniverseIndex> {
24-
None
21+
fn probe_ty_var(&self, _vid: TyVid) -> Option<<Self::Interner as Interner>::Ty> {
22+
match self.0 {}
2523
}
2624

27-
fn root_ty_var(&self, vid: TyVid) -> TyVid {
28-
vid
25+
fn universe_of_lt(
26+
&self,
27+
_lt: <Self::Interner as Interner>::InferRegion,
28+
) -> Option<UniverseIndex> {
29+
match self.0 {}
2930
}
3031

31-
fn probe_ty_var(&self, _vid: TyVid) -> Option<I::Ty> {
32-
None
32+
fn opportunistic_resolve_lt_var(
33+
&self,
34+
_vid: <Self::Interner as Interner>::InferRegion,
35+
) -> Option<<Self::Interner as Interner>::Region> {
36+
match self.0 {}
3337
}
3438

35-
fn opportunistic_resolve_lt_var(&self, _vid: I::InferRegion) -> Option<I::Region> {
36-
None
39+
fn universe_of_ct(&self, _ct: ConstVid) -> Option<UniverseIndex> {
40+
match self.0 {}
3741
}
3842

39-
fn root_ct_var(&self, vid: ConstVid) -> ConstVid {
40-
vid
43+
fn root_ct_var(&self, _vid: ConstVid) -> ConstVid {
44+
match self.0 {}
4145
}
4246

43-
fn probe_ct_var(&self, _vid: ConstVid) -> Option<I::Const> {
44-
None
47+
fn probe_ct_var(&self, _vid: ConstVid) -> Option<<Self::Interner as Interner>::Const> {
48+
match self.0 {}
4549
}
4650

4751
fn defining_opaque_types(&self) -> <Self::Interner as Interner>::DefiningOpaqueTypes {
48-
Default::default()
52+
match self.0 {}
4953
}
5054
}
5155

@@ -96,7 +100,7 @@ impl<I: Interner, T: DebugWithInfcx<I>> DebugWithInfcx<I> for [T] {
96100

97101
pub struct WithInfcx<'a, Infcx: InferCtxtLike, T> {
98102
pub data: T,
99-
pub infcx: &'a Infcx,
103+
pub infcx: Option<&'a Infcx>,
100104
}
101105

102106
impl<Infcx: InferCtxtLike, T: Copy> Copy for WithInfcx<'_, Infcx, T> {}
@@ -109,13 +113,13 @@ impl<Infcx: InferCtxtLike, T: Clone> Clone for WithInfcx<'_, Infcx, T> {
109113

110114
impl<'a, I: Interner, T> WithInfcx<'a, NoInfcx<I>, T> {
111115
pub fn with_no_infcx(data: T) -> Self {
112-
Self { data, infcx: &NoInfcx(PhantomData) }
116+
Self { data, infcx: None }
113117
}
114118
}
115119

116120
impl<'a, Infcx: InferCtxtLike, T> WithInfcx<'a, Infcx, T> {
117121
pub fn new(data: T, infcx: &'a Infcx) -> Self {
118-
Self { data, infcx }
122+
Self { data, infcx: Some(infcx) }
119123
}
120124

121125
pub fn wrap<U>(self, u: U) -> WithInfcx<'a, Infcx, U> {

compiler/rustc_type_ir/src/ty_kind.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,6 @@ impl<I: Interner> DebugWithInfcx<I> for TyKind<I> {
415415
}
416416
}
417417

418-
// This is manually implemented because a derive would require `I: Debug`
419418
impl<I: Interner> fmt::Debug for TyKind<I> {
420419
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
421420
WithInfcx::with_no_infcx(self).fmt(f)
@@ -924,15 +923,18 @@ impl<I: Interner> DebugWithInfcx<I> for InferTy {
924923
this: WithInfcx<'_, Infcx, &Self>,
925924
f: &mut fmt::Formatter<'_>,
926925
) -> fmt::Result {
927-
match this.data {
928-
InferTy::TyVar(vid) => {
929-
if let Some(universe) = this.infcx.universe_of_ty(*vid) {
930-
write!(f, "?{}_{}t", vid.index(), universe.index())
931-
} else {
932-
write!(f, "{:?}", this.data)
926+
match this.infcx {
927+
None => write!(f, "{:?}", this.data),
928+
Some(infcx) => match this.data {
929+
InferTy::TyVar(vid) => {
930+
if let Some(universe) = infcx.universe_of_ty(*vid) {
931+
write!(f, "?{}_{}t", vid.index(), universe.index())
932+
} else {
933+
write!(f, "{}_..t", vid.index())
934+
}
933935
}
934-
}
935-
_ => write!(f, "{:?}", this.data),
936+
_ => write!(f, "{:?}", this.data),
937+
},
936938
}
937939
}
938940
}

0 commit comments

Comments
 (0)