Skip to content

Commit df5459a

Browse files
Fix canonicalizing different infer kinds
1 parent 3a5a1df commit df5459a

File tree

7 files changed

+52
-53
lines changed

7 files changed

+52
-53
lines changed

compiler/rustc_infer/src/infer/mod.rs

+8-23
Original file line numberDiff line numberDiff line change
@@ -349,32 +349,17 @@ impl<'tcx> ty::InferCtxtLike for InferCtxt<'tcx> {
349349
self.tcx
350350
}
351351

352-
fn universe_of_ty(&self, ty: ty::InferTy) -> Option<ty::UniverseIndex> {
353-
use InferTy::*;
354-
match ty {
355-
// FIXME(BoxyUwU): this is kind of jank and means that printing unresolved
356-
// ty infers will give you the universe of the var it resolved to not the universe
357-
// it actually had. It also means that if you have a `?0.1` and infer it to `u8` then
358-
// try to print out `?0.1` it will just print `?0`.
359-
TyVar(ty_vid) => match self.probe_ty_var(ty_vid) {
360-
Err(universe) => Some(universe),
361-
Ok(_) => None,
362-
},
363-
IntVar(_) | FloatVar(_) => Some(ty::UniverseIndex::ROOT),
364-
FreshTy(_) | FreshIntTy(_) | FreshFloatTy(_) => None,
352+
fn universe_of_ty(&self, vid: TyVid) -> Option<ty::UniverseIndex> {
353+
match self.probe_ty_var(vid) {
354+
Err(universe) => Some(universe),
355+
Ok(_) => None,
365356
}
366357
}
367358

368-
fn universe_of_ct(&self, ct: ty::InferConst) -> Option<ty::UniverseIndex> {
369-
use ty::InferConst::*;
370-
match ct {
371-
// Same issue as with `universe_of_ty`
372-
Var(ct_vid) => match self.probe_const_var(ct_vid) {
373-
Err(universe) => Some(universe),
374-
Ok(_) => None,
375-
},
376-
EffectVar(_) => Some(ty::UniverseIndex::ROOT),
377-
Fresh(_) => None,
359+
fn universe_of_ct(&self, ct: ConstVid) -> Option<ty::UniverseIndex> {
360+
match self.probe_const_var(ct) {
361+
Err(universe) => Some(universe),
362+
Ok(_) => None,
378363
}
379364
}
380365

compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
151151
debruijn: rustc_type_ir::DebruijnIndex,
152152
var: rustc_type_ir::BoundVar,
153153
) -> Self::Region {
154-
Region::new_late_bound(
154+
Region::new_bound(
155155
*self,
156156
debruijn,
157157
ty::BoundRegion { var, kind: ty::BoundRegionKind::BrAnon },

compiler/rustc_type_ir/src/canonicalizer.rs

+21-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use std::cmp::Ordering;
22

3+
use crate::canonical::*;
34
use crate::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
4-
use crate::{canonical::*, ConstTy, IntoKind, Placeholder};
55
use crate::{
6-
BoundVar, ConstKind, DebruijnIndex, InferCtxtLike, Interner, RegionKind, TyKind, UniverseIndex,
6+
BoundVar, ConstKind, ConstTy, DebruijnIndex, InferCtxtLike, InferTy, Interner, IntoKind,
7+
Placeholder, RegionKind, TyKind, UniverseIndex,
78
};
89

910
/// Whether we're canonicalizing a query input or the query response.
@@ -292,9 +293,16 @@ impl<Infcx: InferCtxtLike<Interner = I>, I: Interner> TypeFolder<I> for Canonica
292293
let Err(ui) = self.infcx.probe_ty_var(vid) else {
293294
panic!("ty var should have been resolved: {t}");
294295
}; */
295-
CanonicalVarKind::Ty(CanonicalTyVarKind::General(
296-
self.infcx.universe_of_ty(i).unwrap(),
297-
))
296+
match i {
297+
InferTy::TyVar(vid) => CanonicalVarKind::Ty(CanonicalTyVarKind::General(
298+
self.infcx.universe_of_ty(vid).unwrap(),
299+
)),
300+
InferTy::IntVar(_) => CanonicalVarKind::Ty(CanonicalTyVarKind::Int),
301+
InferTy::FloatVar(_) => CanonicalVarKind::Ty(CanonicalTyVarKind::Float),
302+
InferTy::FreshTy(_) | InferTy::FreshIntTy(_) | InferTy::FreshFloatTy(_) => {
303+
todo!()
304+
}
305+
}
298306
}
299307
TyKind::Placeholder(placeholder) => match self.canonicalize_mode {
300308
CanonicalizeMode::Input => CanonicalVarKind::PlaceholderTy(Placeholder::new(
@@ -352,6 +360,7 @@ impl<Infcx: InferCtxtLike<Interner = I>, I: Interner> TypeFolder<I> for Canonica
352360
I::Const: TypeSuperFoldable<I>,
353361
{
354362
let kind = match c.kind() {
363+
// TODO: This will not canonicalize effect vars until InferConst is uplifted.
355364
ConstKind::Infer(i) => {
356365
/* TODO: assert_eq!(
357366
self.infcx.root_const_var(vid),
@@ -362,7 +371,13 @@ impl<Infcx: InferCtxtLike<Interner = I>, I: Interner> TypeFolder<I> for Canonica
362371
panic!("const var should have been resolved");
363372
}; */
364373
// FIXME: we should fold this ty eventually
365-
CanonicalVarKind::Const(self.infcx.universe_of_ct(i).unwrap(), c.ty())
374+
match i {
375+
crate::InferConst::Var(vid) => {
376+
CanonicalVarKind::Const(self.infcx.universe_of_ct(vid).unwrap(), c.ty())
377+
}
378+
crate::InferConst::EffectVar(_) => CanonicalVarKind::Effect,
379+
crate::InferConst::Fresh(_) => todo!(),
380+
}
366381
}
367382
ConstKind::Placeholder(placeholder) => match self.canonicalize_mode {
368383
CanonicalizeMode::Input => CanonicalVarKind::PlaceholderConst(

compiler/rustc_type_ir/src/const_kind.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,15 @@ impl<I: Interner> DebugWithInfcx<I> for InferConst {
146146
this: WithInfcx<'_, Infcx, &Self>,
147147
f: &mut core::fmt::Formatter<'_>,
148148
) -> core::fmt::Result {
149-
match this.infcx.universe_of_ct(*this.data) {
150-
None => write!(f, "{:?}", this.data),
151-
Some(universe) => match *this.data {
152-
InferConst::Var(vid) => write!(f, "?{}_{}c", vid.index(), universe.index()),
153-
InferConst::EffectVar(vid) => write!(f, "?{}_{}e", vid.index(), universe.index()),
154-
InferConst::Fresh(_) => {
155-
unreachable!()
156-
}
149+
match *this.data {
150+
InferConst::Var(vid) => match this.infcx.universe_of_ct(vid) {
151+
None => write!(f, "{:?}", this.data),
152+
Some(universe) => write!(f, "?{}_{}c", vid.index(), universe.index()),
157153
},
154+
InferConst::EffectVar(vid) => write!(f, "?{}e", vid.index()),
155+
InferConst::Fresh(_) => {
156+
unreachable!()
157+
}
158158
}
159159
}
160160
}

compiler/rustc_type_ir/src/debug.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{InferConst, InferTy, Interner, UniverseIndex};
1+
use crate::{ConstVid, InferCtxtLike, Interner, TyVid, UniverseIndex};
22

33
use core::fmt;
44
use std::marker::PhantomData;
@@ -8,11 +8,11 @@ pub struct NoInfcx<I>(PhantomData<I>);
88
impl<I: Interner> InferCtxtLike for NoInfcx<I> {
99
type Interner = I;
1010

11-
fn universe_of_ty(&self, _ty: InferTy) -> Option<UniverseIndex> {
11+
fn universe_of_ty(&self, _ty: TyVid) -> Option<UniverseIndex> {
1212
None
1313
}
1414

15-
fn universe_of_ct(&self, _ct: InferConst) -> Option<UniverseIndex> {
15+
fn universe_of_ct(&self, _ct: ConstVid) -> Option<UniverseIndex> {
1616
None
1717
}
1818

compiler/rustc_type_ir/src/infcx.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
use crate::{Interner, UniverseIndex};
1+
use crate::{ConstVid, Interner, TyVid, UniverseIndex};
22

33
pub trait InferCtxtLike {
44
type Interner: Interner;
55

66
fn interner(&self) -> Self::Interner;
77

8-
fn universe_of_ty(&self, ty: <Self::Interner as Interner>::InferTy) -> Option<UniverseIndex>;
8+
fn universe_of_ty(&self, ty: TyVid) -> Option<UniverseIndex>;
99

1010
fn universe_of_lt(
1111
&self,
1212
lt: <Self::Interner as Interner>::InferRegion,
1313
) -> Option<UniverseIndex>;
1414

15-
fn universe_of_ct(&self, ct: <Self::Interner as Interner>::InferConst)
16-
-> Option<UniverseIndex>;
15+
fn universe_of_ct(&self, ct: ConstVid) -> Option<UniverseIndex>;
1716
}

compiler/rustc_type_ir/src/ty_kind.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -820,15 +820,15 @@ impl<I: Interner> DebugWithInfcx<I> for InferTy {
820820
this: WithInfcx<'_, Infcx, &Self>,
821821
f: &mut fmt::Formatter<'_>,
822822
) -> fmt::Result {
823-
use InferTy::*;
824-
match this.infcx.universe_of_ty(*this.data) {
825-
None => write!(f, "{:?}", this.data),
826-
Some(universe) => match *this.data {
827-
TyVar(ty_vid) => write!(f, "?{}_{}t", ty_vid.index(), universe.index()),
828-
IntVar(_) | FloatVar(_) | FreshTy(_) | FreshIntTy(_) | FreshFloatTy(_) => {
829-
unreachable!()
823+
match this.data {
824+
InferTy::TyVar(vid) => {
825+
if let Some(universe) = this.infcx.universe_of_ty(*vid) {
826+
write!(f, "?{}_{}t", vid.index(), universe.index())
827+
} else {
828+
write!(f, "{:?}", this.data)
830829
}
831-
},
830+
}
831+
_ => write!(f, "{:?}", this.data),
832832
}
833833
}
834834
}

0 commit comments

Comments
 (0)