mirror of
https://github.com/rust-lang/rust.git
synced 2026-06-01 05:57:03 +03:00
Rollup merge of #121703 - compiler-errors:new, r=lcnr
Add a way to add constructors for `rustc_type_ir` types Introduces a module called `rustc_type_ir`, in which we can place traits which are named `Ty`/`Region`/`Const`/etc. which expose constructors for the `rustc_type_ir` types. This means we can construct things `Interner::Ty` with `Ty::new_x(...)`, which is needed to uplift the new trait solver into an interner-agnostic crate. These traits are placed into a *separate* module because they're only intended to be used in interner-agnostic code, and they should mirror the constructors that are provided by the inherent constructor methods in `rustc_middle`. Putting this up for vibe-check mostly. I haven't copied over any of the type constructors, except for one to create bound types for use in the canonicalizer. r? lcnr
This commit is contained in:
@@ -175,7 +175,20 @@ pub fn new_error_with_message<S: Into<MultiSpan>>(
|
||||
let reported = tcx.dcx().span_delayed_bug(span, msg);
|
||||
Const::new_error(tcx, reported, ty)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> rustc_type_ir::new::Const<TyCtxt<'tcx>> 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")]
|
||||
|
||||
@@ -130,27 +130,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
|
||||
fn mk_canonical_var_infos(self, infos: &[ty::CanonicalVarInfo<Self>]) -> 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<InternedInSet<'tcx, T>, ()>;
|
||||
|
||||
@@ -136,6 +136,12 @@ pub fn new_from_kind(tcx: TyCtxt<'tcx>, kind: RegionKind<'tcx>) -> Region<'tcx>
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> rustc_type_ir::new::Region<TyCtxt<'tcx>> 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> {
|
||||
|
||||
@@ -1426,7 +1426,8 @@ fn from(var: BoundVar) -> Self {
|
||||
|
||||
/// 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 @@ pub fn new_task_context(tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> rustc_type_ir::new::Ty<TyCtxt<'tcx>> 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)]
|
||||
|
||||
@@ -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 @@ fn fold_region(&mut self, r: I::Region) -> I::Region {
|
||||
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 @@ fn fold_ty(&mut self, t: I::Ty) -> I::Ty
|
||||
}),
|
||||
);
|
||||
|
||||
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 @@ fn fold_const(&mut self, c: I::Const) -> I::Const
|
||||
}),
|
||||
);
|
||||
|
||||
self.interner().mk_bound_const(self.binder_index, var, c.ty())
|
||||
Const::new_anon_bound(self.interner(), self.binder_index, var, c.ty())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
|
||||
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<Self::GenericArg>
|
||||
+ IntoKind<Kind = TyKind<Self>>
|
||||
+ TypeSuperVisitable<Self>
|
||||
+ Flags;
|
||||
+ Flags
|
||||
+ new::Ty<Self>;
|
||||
type Tys: Copy + Debug + Hash + Ord + IntoIterator<Item = Self::Ty>;
|
||||
type AliasTy: Copy + DebugWithInfcx<Self> + Hash + Ord;
|
||||
type ParamTy: Copy + Debug + Hash + Ord;
|
||||
@@ -56,7 +57,8 @@ pub trait Interner: Sized {
|
||||
+ IntoKind<Kind = ConstKind<Self>>
|
||||
+ ConstTy<Self>
|
||||
+ TypeSuperVisitable<Self>
|
||||
+ Flags;
|
||||
+ Flags
|
||||
+ new::Const<Self>;
|
||||
type AliasConst: Copy + DebugWithInfcx<Self> + 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<Self::GenericArg>
|
||||
+ IntoKind<Kind = RegionKind<Self>>
|
||||
+ Flags;
|
||||
+ Flags
|
||||
+ new::Region<Self>;
|
||||
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>]) -> 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
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#[cfg(feature = "nightly")]
|
||||
pub mod codec;
|
||||
pub mod fold;
|
||||
pub mod new;
|
||||
pub mod ty_info;
|
||||
pub mod ty_kind;
|
||||
pub mod visit;
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
use crate::{BoundVar, DebruijnIndex, Interner};
|
||||
|
||||
pub trait Ty<I: Interner<Ty = Self>> {
|
||||
fn new_anon_bound(interner: I, debruijn: DebruijnIndex, var: BoundVar) -> Self;
|
||||
}
|
||||
|
||||
pub trait Region<I: Interner<Region = Self>> {
|
||||
fn new_anon_bound(interner: I, debruijn: DebruijnIndex, var: BoundVar) -> Self;
|
||||
}
|
||||
|
||||
pub trait Const<I: Interner<Const = Self>> {
|
||||
fn new_anon_bound(interner: I, debruijn: DebruijnIndex, var: BoundVar, ty: I::Ty) -> Self;
|
||||
}
|
||||
Reference in New Issue
Block a user