Skip to content

Commit e7bb224

Browse files
authored
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
2 parents 8886c31 + 1eedca8 commit e7bb224

File tree

8 files changed

+53
-35
lines changed

8 files changed

+53
-35
lines changed

compiler/rustc_middle/src/ty/consts.rs

+13
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,20 @@ impl<'tcx> Const<'tcx> {
175175
let reported = tcx.dcx().span_delayed_bug(span, msg);
176176
Const::new_error(tcx, reported, ty)
177177
}
178+
}
178179

180+
impl<'tcx> rustc_type_ir::new::Const<TyCtxt<'tcx>> for Const<'tcx> {
181+
fn new_anon_bound(
182+
tcx: TyCtxt<'tcx>,
183+
debruijn: ty::DebruijnIndex,
184+
var: ty::BoundVar,
185+
ty: Ty<'tcx>,
186+
) -> Self {
187+
Const::new_bound(tcx, debruijn, var, ty)
188+
}
189+
}
190+
191+
impl<'tcx> Const<'tcx> {
179192
/// Literals and const generic parameters are eagerly converted to a constant, everything else
180193
/// becomes `Unevaluated`.
181194
#[instrument(skip(tcx), level = "debug")]

compiler/rustc_middle/src/ty/context.rs

-21
Original file line numberDiff line numberDiff line change
@@ -130,27 +130,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
130130
fn mk_canonical_var_infos(self, infos: &[ty::CanonicalVarInfo<Self>]) -> Self::CanonicalVars {
131131
self.mk_canonical_var_infos(infos)
132132
}
133-
134-
fn mk_bound_ty(self, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self::Ty {
135-
Ty::new_bound(self, debruijn, ty::BoundTy { var, kind: ty::BoundTyKind::Anon })
136-
}
137-
138-
fn mk_bound_region(self, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self::Region {
139-
Region::new_bound(
140-
self,
141-
debruijn,
142-
ty::BoundRegion { var, kind: ty::BoundRegionKind::BrAnon },
143-
)
144-
}
145-
146-
fn mk_bound_const(
147-
self,
148-
debruijn: ty::DebruijnIndex,
149-
var: ty::BoundVar,
150-
ty: Self::Ty,
151-
) -> Self::Const {
152-
Const::new_bound(self, debruijn, var, ty)
153-
}
154133
}
155134

156135
type InternedSet<'tcx, T> = ShardedHashMap<InternedInSet<'tcx, T>, ()>;

compiler/rustc_middle/src/ty/region.rs

+6
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ impl<'tcx> Region<'tcx> {
136136
}
137137
}
138138

139+
impl<'tcx> rustc_type_ir::new::Region<TyCtxt<'tcx>> for Region<'tcx> {
140+
fn new_anon_bound(tcx: TyCtxt<'tcx>, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self {
141+
Region::new_bound(tcx, debruijn, ty::BoundRegion { var, kind: ty::BoundRegionKind::BrAnon })
142+
}
143+
}
144+
139145
/// Region utilities
140146
impl<'tcx> Region<'tcx> {
141147
pub fn kind(self) -> RegionKind<'tcx> {

compiler/rustc_middle/src/ty/sty.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,8 @@ impl From<BoundVar> for BoundTy {
14261426

14271427
/// Constructors for `Ty`
14281428
impl<'tcx> Ty<'tcx> {
1429-
// Avoid this in favour of more specific `new_*` methods, where possible.
1429+
/// Avoid using this in favour of more specific `new_*` methods, where possible.
1430+
/// The more specific methods will often optimize their creation.
14301431
#[allow(rustc::usage_of_ty_tykind)]
14311432
#[inline]
14321433
pub fn new(tcx: TyCtxt<'tcx>, st: TyKind<'tcx>) -> Ty<'tcx> {
@@ -1813,6 +1814,12 @@ impl<'tcx> Ty<'tcx> {
18131814
}
18141815
}
18151816

1817+
impl<'tcx> rustc_type_ir::new::Ty<TyCtxt<'tcx>> for Ty<'tcx> {
1818+
fn new_anon_bound(tcx: TyCtxt<'tcx>, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self {
1819+
Ty::new_bound(tcx, debruijn, ty::BoundTy { var, kind: ty::BoundTyKind::Anon })
1820+
}
1821+
}
1822+
18161823
/// Type utilities
18171824
impl<'tcx> Ty<'tcx> {
18181825
#[inline(always)]

compiler/rustc_next_trait_solver/src/canonicalizer.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::cmp::Ordering;
22

33
use rustc_type_ir::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
4+
use rustc_type_ir::new::{Const, Region, Ty};
45
use rustc_type_ir::visit::TypeVisitableExt;
56
use rustc_type_ir::{
67
self as ty, Canonical, CanonicalTyVarKind, CanonicalVarInfo, CanonicalVarKind, ConstTy,
@@ -293,7 +294,7 @@ impl<Infcx: InferCtxtLike<Interner = I>, I: Interner> TypeFolder<I>
293294
var
294295
});
295296

296-
self.interner().mk_bound_region(self.binder_index, var)
297+
Region::new_anon_bound(self.interner(), self.binder_index, var)
297298
}
298299

299300
fn fold_ty(&mut self, t: I::Ty) -> I::Ty
@@ -375,7 +376,7 @@ impl<Infcx: InferCtxtLike<Interner = I>, I: Interner> TypeFolder<I>
375376
}),
376377
);
377378

378-
self.interner().mk_bound_ty(self.binder_index, var)
379+
Ty::new_anon_bound(self.interner(), self.binder_index, var)
379380
}
380381

381382
fn fold_const(&mut self, c: I::Const) -> I::Const
@@ -435,6 +436,6 @@ impl<Infcx: InferCtxtLike<Interner = I>, I: Interner> TypeFolder<I>
435436
}),
436437
);
437438

438-
self.interner().mk_bound_const(self.binder_index, var, c.ty())
439+
Const::new_anon_bound(self.interner(), self.binder_index, var, c.ty())
439440
}
440441
}

compiler/rustc_type_ir/src/interner.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::hash::Hash;
44

55
use crate::visit::{Flags, TypeSuperVisitable, TypeVisitable};
66
use crate::{
7-
BoundVar, BoundVars, CanonicalVarInfo, ConstKind, DebruijnIndex, DebugWithInfcx, RegionKind,
8-
TyKind, UniverseIndex,
7+
new, BoundVar, BoundVars, CanonicalVarInfo, ConstKind, DebugWithInfcx, RegionKind, TyKind,
8+
UniverseIndex,
99
};
1010

1111
pub trait Interner: Sized {
@@ -34,7 +34,8 @@ pub trait Interner: Sized {
3434
+ Into<Self::GenericArg>
3535
+ IntoKind<Kind = TyKind<Self>>
3636
+ TypeSuperVisitable<Self>
37-
+ Flags;
37+
+ Flags
38+
+ new::Ty<Self>;
3839
type Tys: Copy + Debug + Hash + Ord + IntoIterator<Item = Self::Ty>;
3940
type AliasTy: Copy + DebugWithInfcx<Self> + Hash + Ord;
4041
type ParamTy: Copy + Debug + Hash + Ord;
@@ -56,7 +57,8 @@ pub trait Interner: Sized {
5657
+ IntoKind<Kind = ConstKind<Self>>
5758
+ ConstTy<Self>
5859
+ TypeSuperVisitable<Self>
59-
+ Flags;
60+
+ Flags
61+
+ new::Const<Self>;
6062
type AliasConst: Copy + DebugWithInfcx<Self> + Hash + Ord;
6163
type PlaceholderConst: Copy + Debug + Hash + Ord + PlaceholderLike;
6264
type ParamConst: Copy + Debug + Hash + Ord;
@@ -71,7 +73,8 @@ pub trait Interner: Sized {
7173
+ Ord
7274
+ Into<Self::GenericArg>
7375
+ IntoKind<Kind = RegionKind<Self>>
74-
+ Flags;
76+
+ Flags
77+
+ new::Region<Self>;
7578
type EarlyParamRegion: Copy + Debug + Hash + Ord;
7679
type LateParamRegion: Copy + Debug + Hash + Ord;
7780
type BoundRegion: Copy + Debug + Hash + Ord;
@@ -90,11 +93,6 @@ pub trait Interner: Sized {
9093
type ClosureKind: Copy + Debug + Hash + Eq;
9194

9295
fn mk_canonical_var_infos(self, infos: &[CanonicalVarInfo<Self>]) -> Self::CanonicalVars;
93-
94-
// FIXME: We should not have all these constructors on `Interner`, but as functions on some trait.
95-
fn mk_bound_ty(self, debruijn: DebruijnIndex, var: BoundVar) -> Self::Ty;
96-
fn mk_bound_region(self, debruijn: DebruijnIndex, var: BoundVar) -> Self::Region;
97-
fn mk_bound_const(self, debruijn: DebruijnIndex, var: BoundVar, ty: Self::Ty) -> Self::Const;
9896
}
9997

10098
/// Common capabilities of placeholder kinds

compiler/rustc_type_ir/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use std::sync::Arc as Lrc;
2424
#[cfg(feature = "nightly")]
2525
pub mod codec;
2626
pub mod fold;
27+
pub mod new;
2728
pub mod ty_info;
2829
pub mod ty_kind;
2930
pub mod visit;

compiler/rustc_type_ir/src/new.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use crate::{BoundVar, DebruijnIndex, Interner};
2+
3+
pub trait Ty<I: Interner<Ty = Self>> {
4+
fn new_anon_bound(interner: I, debruijn: DebruijnIndex, var: BoundVar) -> Self;
5+
}
6+
7+
pub trait Region<I: Interner<Region = Self>> {
8+
fn new_anon_bound(interner: I, debruijn: DebruijnIndex, var: BoundVar) -> Self;
9+
}
10+
11+
pub trait Const<I: Interner<Const = Self>> {
12+
fn new_anon_bound(interner: I, debruijn: DebruijnIndex, var: BoundVar, ty: I::Ty) -> Self;
13+
}

0 commit comments

Comments
 (0)