From 1eedca8bdf0020b673957bfbb2c787583f474128 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Tue, 27 Feb 2024 20:22:13 +0000 Subject: [PATCH] Allow a way to add constructors for rustc_type_ir types --- compiler/rustc_middle/src/ty/consts.rs | 13 ++++++++++++ compiler/rustc_middle/src/ty/context.rs | 21 ------------------- compiler/rustc_middle/src/ty/region.rs | 6 ++++++ compiler/rustc_middle/src/ty/sty.rs | 9 +++++++- .../src/canonicalizer.rs | 7 ++++--- compiler/rustc_type_ir/src/interner.rs | 18 +++++++--------- compiler/rustc_type_ir/src/lib.rs | 1 + compiler/rustc_type_ir/src/new.rs | 13 ++++++++++++ 8 files changed, 53 insertions(+), 35 deletions(-) create mode 100644 compiler/rustc_type_ir/src/new.rs diff --git a/compiler/rustc_middle/src/ty/consts.rs b/compiler/rustc_middle/src/ty/consts.rs index 5e4d899f51768..5b62c0bf931af 100644 --- a/compiler/rustc_middle/src/ty/consts.rs +++ b/compiler/rustc_middle/src/ty/consts.rs @@ -175,7 +175,20 @@ impl<'tcx> Const<'tcx> { let reported = tcx.dcx().span_delayed_bug(span, msg); Const::new_error(tcx, reported, ty) } +} +impl<'tcx> rustc_type_ir::new::Const> for Const<'tcx> { + fn new_anon_bound( + tcx: TyCtxt<'tcx>, + debruijn: ty::DebruijnIndex, + var: ty::BoundVar, + ty: Ty<'tcx>, + ) -> Self { + Const::new_bound(tcx, debruijn, var, ty) + } +} + +impl<'tcx> Const<'tcx> { /// Literals and const generic parameters are eagerly converted to a constant, everything else /// becomes `Unevaluated`. #[instrument(skip(tcx), level = "debug")] diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index b2a7f5599b643..96b4797f4fa98 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -130,27 +130,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> { fn mk_canonical_var_infos(self, infos: &[ty::CanonicalVarInfo]) -> Self::CanonicalVars { self.mk_canonical_var_infos(infos) } - - fn mk_bound_ty(self, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self::Ty { - Ty::new_bound(self, debruijn, ty::BoundTy { var, kind: ty::BoundTyKind::Anon }) - } - - fn mk_bound_region(self, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self::Region { - Region::new_bound( - self, - debruijn, - ty::BoundRegion { var, kind: ty::BoundRegionKind::BrAnon }, - ) - } - - fn mk_bound_const( - self, - debruijn: ty::DebruijnIndex, - var: ty::BoundVar, - ty: Self::Ty, - ) -> Self::Const { - Const::new_bound(self, debruijn, var, ty) - } } type InternedSet<'tcx, T> = ShardedHashMap, ()>; diff --git a/compiler/rustc_middle/src/ty/region.rs b/compiler/rustc_middle/src/ty/region.rs index 51a4a9f411c02..7abc3cd283888 100644 --- a/compiler/rustc_middle/src/ty/region.rs +++ b/compiler/rustc_middle/src/ty/region.rs @@ -136,6 +136,12 @@ impl<'tcx> Region<'tcx> { } } +impl<'tcx> rustc_type_ir::new::Region> for Region<'tcx> { + fn new_anon_bound(tcx: TyCtxt<'tcx>, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self { + Region::new_bound(tcx, debruijn, ty::BoundRegion { var, kind: ty::BoundRegionKind::BrAnon }) + } +} + /// Region utilities impl<'tcx> Region<'tcx> { pub fn kind(self) -> RegionKind<'tcx> { diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 53690cc58111f..03c61803572c6 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -1426,7 +1426,8 @@ impl From for BoundTy { /// Constructors for `Ty` impl<'tcx> Ty<'tcx> { - // Avoid this in favour of more specific `new_*` methods, where possible. + /// Avoid using this in favour of more specific `new_*` methods, where possible. + /// The more specific methods will often optimize their creation. #[allow(rustc::usage_of_ty_tykind)] #[inline] pub fn new(tcx: TyCtxt<'tcx>, st: TyKind<'tcx>) -> Ty<'tcx> { @@ -1813,6 +1814,12 @@ impl<'tcx> Ty<'tcx> { } } +impl<'tcx> rustc_type_ir::new::Ty> for Ty<'tcx> { + fn new_anon_bound(tcx: TyCtxt<'tcx>, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self { + Ty::new_bound(tcx, debruijn, ty::BoundTy { var, kind: ty::BoundTyKind::Anon }) + } +} + /// Type utilities impl<'tcx> Ty<'tcx> { #[inline(always)] diff --git a/compiler/rustc_next_trait_solver/src/canonicalizer.rs b/compiler/rustc_next_trait_solver/src/canonicalizer.rs index 7b73e2aebf039..95b3006666202 100644 --- a/compiler/rustc_next_trait_solver/src/canonicalizer.rs +++ b/compiler/rustc_next_trait_solver/src/canonicalizer.rs @@ -1,6 +1,7 @@ use std::cmp::Ordering; use rustc_type_ir::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable}; +use rustc_type_ir::new::{Const, Region, Ty}; use rustc_type_ir::visit::TypeVisitableExt; use rustc_type_ir::{ self as ty, Canonical, CanonicalTyVarKind, CanonicalVarInfo, CanonicalVarKind, ConstTy, @@ -293,7 +294,7 @@ impl, I: Interner> TypeFolder var }); - self.interner().mk_bound_region(self.binder_index, var) + Region::new_anon_bound(self.interner(), self.binder_index, var) } fn fold_ty(&mut self, t: I::Ty) -> I::Ty @@ -375,7 +376,7 @@ impl, I: Interner> TypeFolder }), ); - self.interner().mk_bound_ty(self.binder_index, var) + Ty::new_anon_bound(self.interner(), self.binder_index, var) } fn fold_const(&mut self, c: I::Const) -> I::Const @@ -435,6 +436,6 @@ impl, I: Interner> TypeFolder }), ); - self.interner().mk_bound_const(self.binder_index, var, c.ty()) + Const::new_anon_bound(self.interner(), self.binder_index, var, c.ty()) } } diff --git a/compiler/rustc_type_ir/src/interner.rs b/compiler/rustc_type_ir/src/interner.rs index 7d2c42a6dbe91..373540de05e0e 100644 --- a/compiler/rustc_type_ir/src/interner.rs +++ b/compiler/rustc_type_ir/src/interner.rs @@ -4,8 +4,8 @@ use std::hash::Hash; use crate::visit::{Flags, TypeSuperVisitable, TypeVisitable}; use crate::{ - BoundVar, BoundVars, CanonicalVarInfo, ConstKind, DebruijnIndex, DebugWithInfcx, RegionKind, - TyKind, UniverseIndex, + new, BoundVar, BoundVars, CanonicalVarInfo, ConstKind, DebugWithInfcx, RegionKind, TyKind, + UniverseIndex, }; pub trait Interner: Sized { @@ -34,7 +34,8 @@ pub trait Interner: Sized { + Into + IntoKind> + TypeSuperVisitable - + Flags; + + Flags + + new::Ty; type Tys: Copy + Debug + Hash + Ord + IntoIterator; type AliasTy: Copy + DebugWithInfcx + Hash + Ord; type ParamTy: Copy + Debug + Hash + Ord; @@ -56,7 +57,8 @@ pub trait Interner: Sized { + IntoKind> + ConstTy + TypeSuperVisitable - + Flags; + + Flags + + new::Const; type AliasConst: Copy + DebugWithInfcx + Hash + Ord; type PlaceholderConst: Copy + Debug + Hash + Ord + PlaceholderLike; type ParamConst: Copy + Debug + Hash + Ord; @@ -71,7 +73,8 @@ pub trait Interner: Sized { + Ord + Into + IntoKind> - + Flags; + + Flags + + new::Region; type EarlyParamRegion: Copy + Debug + Hash + Ord; type LateParamRegion: Copy + Debug + Hash + Ord; type BoundRegion: Copy + Debug + Hash + Ord; @@ -90,11 +93,6 @@ pub trait Interner: Sized { type ClosureKind: Copy + Debug + Hash + Eq; fn mk_canonical_var_infos(self, infos: &[CanonicalVarInfo]) -> Self::CanonicalVars; - - // FIXME: We should not have all these constructors on `Interner`, but as functions on some trait. - fn mk_bound_ty(self, debruijn: DebruijnIndex, var: BoundVar) -> Self::Ty; - fn mk_bound_region(self, debruijn: DebruijnIndex, var: BoundVar) -> Self::Region; - fn mk_bound_const(self, debruijn: DebruijnIndex, var: BoundVar, ty: Self::Ty) -> Self::Const; } /// Common capabilities of placeholder kinds diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index 94ccbcbd8a570..01bf79bce1c64 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -24,6 +24,7 @@ use std::sync::Arc as Lrc; #[cfg(feature = "nightly")] pub mod codec; pub mod fold; +pub mod new; pub mod ty_info; pub mod ty_kind; pub mod visit; diff --git a/compiler/rustc_type_ir/src/new.rs b/compiler/rustc_type_ir/src/new.rs new file mode 100644 index 0000000000000..e7e695e59085a --- /dev/null +++ b/compiler/rustc_type_ir/src/new.rs @@ -0,0 +1,13 @@ +use crate::{BoundVar, DebruijnIndex, Interner}; + +pub trait Ty> { + fn new_anon_bound(interner: I, debruijn: DebruijnIndex, var: BoundVar) -> Self; +} + +pub trait Region> { + fn new_anon_bound(interner: I, debruijn: DebruijnIndex, var: BoundVar) -> Self; +} + +pub trait Const> { + fn new_anon_bound(interner: I, debruijn: DebruijnIndex, var: BoundVar, ty: I::Ty) -> Self; +}