Skip to content

Commit 5a0f412

Browse files
Fix canonicalizing different infer kinds
1 parent 773e972 commit 5a0f412

File tree

7 files changed

+35
-37
lines changed

7 files changed

+35
-37
lines changed

compiler/rustc_infer/src/infer/mod.rs

+4-13
Original file line numberDiff line numberDiff line change
@@ -349,19 +349,10 @@ 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

compiler/rustc_middle/src/ty/context.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ use crate::traits::solve::{
2626
};
2727
use crate::ty::{
2828
self, AdtDef, AdtDefData, AdtKind, Binder, Clause, Const, ConstData, GenericParamDefKind,
29-
ImplPolarity, InferTy, List, ParamConst, ParamTy, PolyExistentialPredicate, PolyFnSig,
30-
Predicate, PredicateKind, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind,
31-
TyVid, TypeAndMut, Visibility,
29+
ImplPolarity, List, ParamConst, ParamTy, PolyExistentialPredicate, PolyFnSig, Predicate,
30+
PredicateKind, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyVid,
31+
TypeAndMut, Visibility,
3232
};
3333
use crate::ty::{GenericArg, GenericArgs, GenericArgsRef};
3434
use rustc_ast::{self as ast, attr};
@@ -96,7 +96,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
9696
type ParamTy = ParamTy;
9797
type BoundTy = ty::BoundTy;
9898
type PlaceholderTy = ty::PlaceholderType;
99-
type InferTy = InferTy;
10099

101100
type ErrorGuaranteed = ErrorGuaranteed;
102101
type BoundExistentialPredicates = &'tcx List<PolyExistentialPredicate<'tcx>>;

compiler/rustc_type_ir/src/canonicalizer.rs

+14-5
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.
@@ -288,9 +289,16 @@ impl<Infcx: InferCtxtLike<Interner = I>, I: Interner> TypeFolder<I> for Canonica
288289
let Err(ui) = self.infcx.probe_ty_var(vid) else {
289290
panic!("ty var should have been resolved: {t}");
290291
}; */
291-
CanonicalVarKind::Ty(CanonicalTyVarKind::General(
292-
self.infcx.universe_of_ty(i).unwrap(),
293-
))
292+
match i {
293+
InferTy::TyVar(vid) => CanonicalVarKind::Ty(CanonicalTyVarKind::General(
294+
self.infcx.universe_of_ty(vid).unwrap(),
295+
)),
296+
InferTy::IntVar(_) => CanonicalVarKind::Ty(CanonicalTyVarKind::Int),
297+
InferTy::FloatVar(_) => CanonicalVarKind::Ty(CanonicalTyVarKind::Float),
298+
InferTy::FreshTy(_) | InferTy::FreshIntTy(_) | InferTy::FreshFloatTy(_) => {
299+
todo!()
300+
}
301+
}
294302
}
295303
TyKind::Placeholder(placeholder) => match self.canonicalize_mode {
296304
CanonicalizeMode::Input => CanonicalVarKind::PlaceholderTy(Placeholder::new(
@@ -348,6 +356,7 @@ impl<Infcx: InferCtxtLike<Interner = I>, I: Interner> TypeFolder<I> for Canonica
348356
I::Const: TypeSuperFoldable<I>,
349357
{
350358
let kind = match c.kind() {
359+
// TODO: This will not canonicalize effect vars until InferConst is uplifted.
351360
ConstKind::Infer(i) => {
352361
/* TODO: assert_eq!(
353362
self.infcx.root_const_var(vid),

compiler/rustc_type_ir/src/debug.rs

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

33
use core::fmt;
44
use std::marker::PhantomData;
@@ -8,7 +8,7 @@ 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: <I as Interner>::InferTy) -> Option<UniverseIndex> {
11+
fn universe_of_ty(&self, _ty: TyVid) -> Option<UniverseIndex> {
1212
None
1313
}
1414

compiler/rustc_type_ir/src/infcx.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use crate::{Interner, UniverseIndex};
1+
use crate::{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,

compiler/rustc_type_ir/src/interner.rs

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ pub trait Interner: Sized {
3636
type ParamTy: Clone + Debug + Hash + Ord;
3737
type BoundTy: Clone + Debug + Hash + Ord;
3838
type PlaceholderTy: Clone + Debug + Hash + Ord + Placeholder<Self>;
39-
type InferTy: Clone + DebugWithInfcx<Self> + Hash + Ord;
4039

4140
// Things stored inside of tys
4241
type ErrorGuaranteed: Clone + Debug + Hash + Ord;

compiler/rustc_type_ir/src/ty_kind.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ pub enum TyKind<I: Interner> {
280280
/// correctly deal with higher ranked types. Though unlike placeholders,
281281
/// that universe is stored in the `InferCtxt` instead of directly
282282
/// inside of the type.
283-
Infer(I::InferTy),
283+
Infer(InferTy),
284284

285285
/// A placeholder for a type which could not be computed; this is
286286
/// propagated to avoid useless error messages.
@@ -797,20 +797,20 @@ impl fmt::Debug for InferTy {
797797
}
798798
}
799799

800-
impl<I: Interner<InferTy = InferTy>> DebugWithInfcx<I> for InferTy {
800+
impl<I: Interner> DebugWithInfcx<I> for InferTy {
801801
fn fmt<Infcx: InferCtxtLike<Interner = I>>(
802802
this: WithInfcx<'_, Infcx, &Self>,
803803
f: &mut fmt::Formatter<'_>,
804804
) -> fmt::Result {
805-
use InferTy::*;
806-
match this.infcx.universe_of_ty(*this.data) {
807-
None => write!(f, "{:?}", this.data),
808-
Some(universe) => match *this.data {
809-
TyVar(ty_vid) => write!(f, "?{}_{}t", ty_vid.index(), universe.index()),
810-
IntVar(_) | FloatVar(_) | FreshTy(_) | FreshIntTy(_) | FreshFloatTy(_) => {
811-
unreachable!()
805+
match this.data {
806+
InferTy::TyVar(vid) => {
807+
if let Some(universe) = this.infcx.universe_of_ty(*vid) {
808+
write!(f, "?{}_{}t", vid.index(), universe.index())
809+
} else {
810+
write!(f, "{:?}", this.data)
812811
}
813-
},
812+
}
813+
_ => write!(f, "{:?}", this.data),
814814
}
815815
}
816816
}

0 commit comments

Comments
 (0)