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 4, 2023
1 parent 773e972 commit 5a0f412
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 37 deletions.
17 changes: 4 additions & 13 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,19 +349,10 @@ 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,
}
}

Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ use crate::traits::solve::{
};
use crate::ty::{
self, AdtDef, AdtDefData, AdtKind, Binder, Clause, Const, ConstData, GenericParamDefKind,
ImplPolarity, InferTy, List, ParamConst, ParamTy, PolyExistentialPredicate, PolyFnSig,
Predicate, PredicateKind, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind,
TyVid, TypeAndMut, Visibility,
ImplPolarity, List, ParamConst, ParamTy, PolyExistentialPredicate, PolyFnSig, Predicate,
PredicateKind, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyVid,
TypeAndMut, Visibility,
};
use crate::ty::{GenericArg, GenericArgs, GenericArgsRef};
use rustc_ast::{self as ast, attr};
Expand Down Expand Up @@ -96,7 +96,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
type ParamTy = ParamTy;
type BoundTy = ty::BoundTy;
type PlaceholderTy = ty::PlaceholderType;
type InferTy = InferTy;

type ErrorGuaranteed = ErrorGuaranteed;
type BoundExistentialPredicates = &'tcx List<PolyExistentialPredicate<'tcx>>;
Expand Down
19 changes: 14 additions & 5 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 @@ -288,9 +289,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 @@ -348,6 +356,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 359 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 361 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 Down
4 changes: 2 additions & 2 deletions compiler/rustc_type_ir/src/debug.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{InferCtxtLike, Interner, UniverseIndex};
use crate::{InferCtxtLike, Interner, TyVid, UniverseIndex};

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

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

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_type_ir/src/infcx.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{Interner, UniverseIndex};
use crate::{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,
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_type_ir/src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ pub trait Interner: Sized {
type ParamTy: Clone + Debug + Hash + Ord;
type BoundTy: Clone + Debug + Hash + Ord;
type PlaceholderTy: Clone + Debug + Hash + Ord + Placeholder<Self>;
type InferTy: Clone + DebugWithInfcx<Self> + Hash + Ord;

// Things stored inside of tys
type ErrorGuaranteed: Clone + Debug + Hash + Ord;
Expand Down
20 changes: 10 additions & 10 deletions compiler/rustc_type_ir/src/ty_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ pub enum TyKind<I: Interner> {
/// correctly deal with higher ranked types. Though unlike placeholders,
/// that universe is stored in the `InferCtxt` instead of directly
/// inside of the type.
Infer(I::InferTy),
Infer(InferTy),

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

impl<I: Interner<InferTy = InferTy>> DebugWithInfcx<I> for InferTy {
impl<I: Interner> DebugWithInfcx<I> for InferTy {
fn fmt<Infcx: InferCtxtLike<Interner = I>>(
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 5a0f412

Please sign in to comment.