Skip to content

Commit

Permalink
Fix canonicalizing different infer kinds
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Nov 22, 2023
1 parent 3a5a1df commit df5459a
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 53 deletions.
31 changes: 8 additions & 23 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,32 +349,17 @@ impl<'tcx> ty::InferCtxtLike for InferCtxt<'tcx> {
self.tcx
}

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

fn universe_of_ct(&self, ct: ty::InferConst) -> Option<ty::UniverseIndex> {
use ty::InferConst::*;
match ct {
// Same issue as with `universe_of_ty`
Var(ct_vid) => match self.probe_const_var(ct_vid) {
Err(universe) => Some(universe),
Ok(_) => None,
},
EffectVar(_) => Some(ty::UniverseIndex::ROOT),
Fresh(_) => None,
fn universe_of_ct(&self, ct: ConstVid) -> Option<ty::UniverseIndex> {
match self.probe_const_var(ct) {
Err(universe) => Some(universe),
Ok(_) => None,
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
debruijn: rustc_type_ir::DebruijnIndex,
var: rustc_type_ir::BoundVar,
) -> Self::Region {
Region::new_late_bound(
Region::new_bound(
*self,
debruijn,
ty::BoundRegion { var, kind: ty::BoundRegionKind::BrAnon },
Expand Down
27 changes: 21 additions & 6 deletions compiler/rustc_type_ir/src/canonicalizer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::cmp::Ordering;

use crate::canonical::*;
use crate::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
use crate::{canonical::*, ConstTy, IntoKind, Placeholder};
use crate::{
BoundVar, ConstKind, DebruijnIndex, InferCtxtLike, Interner, RegionKind, TyKind, UniverseIndex,
BoundVar, ConstKind, ConstTy, DebruijnIndex, InferCtxtLike, InferTy, Interner, IntoKind,
Placeholder, RegionKind, TyKind, UniverseIndex,
};

/// Whether we're canonicalizing a query input or the query response.
Expand Down Expand Up @@ -292,9 +293,16 @@ impl<Infcx: InferCtxtLike<Interner = I>, I: Interner> TypeFolder<I> for Canonica
let Err(ui) = self.infcx.probe_ty_var(vid) else {
panic!("ty var should have been resolved: {t}");
}; */
CanonicalVarKind::Ty(CanonicalTyVarKind::General(
self.infcx.universe_of_ty(i).unwrap(),
))
match i {
InferTy::TyVar(vid) => CanonicalVarKind::Ty(CanonicalTyVarKind::General(
self.infcx.universe_of_ty(vid).unwrap(),
)),
InferTy::IntVar(_) => CanonicalVarKind::Ty(CanonicalTyVarKind::Int),
InferTy::FloatVar(_) => CanonicalVarKind::Ty(CanonicalTyVarKind::Float),
InferTy::FreshTy(_) | InferTy::FreshIntTy(_) | InferTy::FreshFloatTy(_) => {
todo!()
}
}
}
TyKind::Placeholder(placeholder) => match self.canonicalize_mode {
CanonicalizeMode::Input => CanonicalVarKind::PlaceholderTy(Placeholder::new(
Expand Down Expand Up @@ -352,6 +360,7 @@ impl<Infcx: InferCtxtLike<Interner = I>, I: Interner> TypeFolder<I> for Canonica
I::Const: TypeSuperFoldable<I>,
{
let kind = match c.kind() {
// TODO: This will not canonicalize effect vars until InferConst is uplifted.

Check failure on line 363 in compiler/rustc_type_ir/src/canonicalizer.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
ConstKind::Infer(i) => {
/* TODO: assert_eq!(

Check failure on line 365 in compiler/rustc_type_ir/src/canonicalizer.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
self.infcx.root_const_var(vid),
Expand All @@ -362,7 +371,13 @@ impl<Infcx: InferCtxtLike<Interner = I>, I: Interner> TypeFolder<I> for Canonica
panic!("const var should have been resolved");
}; */
// FIXME: we should fold this ty eventually
CanonicalVarKind::Const(self.infcx.universe_of_ct(i).unwrap(), c.ty())
match i {
crate::InferConst::Var(vid) => {
CanonicalVarKind::Const(self.infcx.universe_of_ct(vid).unwrap(), c.ty())
}
crate::InferConst::EffectVar(_) => CanonicalVarKind::Effect,
crate::InferConst::Fresh(_) => todo!(),
}
}
ConstKind::Placeholder(placeholder) => match self.canonicalize_mode {
CanonicalizeMode::Input => CanonicalVarKind::PlaceholderConst(
Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_type_ir/src/const_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ impl<I: Interner> DebugWithInfcx<I> for InferConst {
this: WithInfcx<'_, Infcx, &Self>,
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
match this.infcx.universe_of_ct(*this.data) {
None => write!(f, "{:?}", this.data),
Some(universe) => match *this.data {
InferConst::Var(vid) => write!(f, "?{}_{}c", vid.index(), universe.index()),
InferConst::EffectVar(vid) => write!(f, "?{}_{}e", vid.index(), universe.index()),
InferConst::Fresh(_) => {
unreachable!()
}
match *this.data {
InferConst::Var(vid) => match this.infcx.universe_of_ct(vid) {
None => write!(f, "{:?}", this.data),
Some(universe) => write!(f, "?{}_{}c", vid.index(), universe.index()),
},
InferConst::EffectVar(vid) => write!(f, "?{}e", vid.index()),
InferConst::Fresh(_) => {
unreachable!()
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_type_ir/src/debug.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{InferConst, InferTy, Interner, UniverseIndex};
use crate::{ConstVid, InferCtxtLike, Interner, TyVid, UniverseIndex};

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

fn universe_of_ty(&self, _ty: InferTy) -> Option<UniverseIndex> {
fn universe_of_ty(&self, _ty: TyVid) -> Option<UniverseIndex> {
None
}

fn universe_of_ct(&self, _ct: InferConst) -> Option<UniverseIndex> {
fn universe_of_ct(&self, _ct: ConstVid) -> Option<UniverseIndex> {
None
}

Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_type_ir/src/infcx.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use crate::{Interner, UniverseIndex};
use crate::{ConstVid, Interner, TyVid, UniverseIndex};

pub trait InferCtxtLike {
type Interner: Interner;

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

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

fn universe_of_lt(
&self,
lt: <Self::Interner as Interner>::InferRegion,
) -> Option<UniverseIndex>;

fn universe_of_ct(&self, ct: <Self::Interner as Interner>::InferConst)
-> Option<UniverseIndex>;
fn universe_of_ct(&self, ct: ConstVid) -> Option<UniverseIndex>;
}
16 changes: 8 additions & 8 deletions compiler/rustc_type_ir/src/ty_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -820,15 +820,15 @@ impl<I: Interner> DebugWithInfcx<I> for InferTy {
this: WithInfcx<'_, Infcx, &Self>,
f: &mut fmt::Formatter<'_>,
) -> fmt::Result {
use InferTy::*;
match this.infcx.universe_of_ty(*this.data) {
None => write!(f, "{:?}", this.data),
Some(universe) => match *this.data {
TyVar(ty_vid) => write!(f, "?{}_{}t", ty_vid.index(), universe.index()),
IntVar(_) | FloatVar(_) | FreshTy(_) | FreshIntTy(_) | FreshFloatTy(_) => {
unreachable!()
match this.data {
InferTy::TyVar(vid) => {
if let Some(universe) = this.infcx.universe_of_ty(*vid) {
write!(f, "?{}_{}t", vid.index(), universe.index())
} else {
write!(f, "{:?}", this.data)
}
},
}
_ => write!(f, "{:?}", this.data),
}
}
}

0 comments on commit df5459a

Please sign in to comment.