Skip to content

Commit 0d4dca2

Browse files
Uplift ExistentialTraitRef, ExistentialProjection, ProjectionPredicate
1 parent 204cde4 commit 0d4dca2

File tree

12 files changed

+428
-325
lines changed

12 files changed

+428
-325
lines changed

Diff for: compiler/rustc_errors/src/diagnostic_impls.rs

+8
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ impl<I: rustc_type_ir::Interner> IntoDiagArg for rustc_type_ir::TraitRef<I> {
100100
}
101101
}
102102

103+
104+
105+
impl<I: rustc_type_ir::Interner> IntoDiagArg for rustc_type_ir::ExistentialTraitRef<I> {
106+
fn into_diag_arg(self) -> DiagArgValue {
107+
self.to_string().into_diag_arg()
108+
}
109+
}
110+
103111
into_diag_arg_for_number!(i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize);
104112

105113
impl IntoDiagArg for bool {

Diff for: compiler/rustc_middle/src/ty/context.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,17 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
9999
type CanonicalVars = CanonicalVarInfos<'tcx>;
100100

101101
type Ty = Ty<'tcx>;
102-
type Pat = Pattern<'tcx>;
103102
type Tys = &'tcx List<Ty<'tcx>>;
104103
type AliasTy = ty::AliasTy<'tcx>;
105104
type ParamTy = ParamTy;
106105
type BoundTy = ty::BoundTy;
107106
type PlaceholderTy = ty::PlaceholderType;
108-
109107
type ErrorGuaranteed = ErrorGuaranteed;
108+
110109
type BoundExistentialPredicates = &'tcx List<PolyExistentialPredicate<'tcx>>;
111110
type PolyFnSig = PolyFnSig<'tcx>;
112111
type AllocId = crate::mir::interpret::AllocId;
112+
type Pat = Pattern<'tcx>;
113113

114114
type Const = ty::Const<'tcx>;
115115
type AliasConst = ty::UnevaluatedConst<'tcx>;
@@ -121,8 +121,8 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
121121

122122
type Region = Region<'tcx>;
123123
type EarlyParamRegion = ty::EarlyParamRegion;
124-
type BoundRegion = ty::BoundRegion;
125124
type LateParamRegion = ty::LateParamRegion;
125+
type BoundRegion = ty::BoundRegion;
126126
type InferRegion = ty::RegionVid;
127127
type PlaceholderRegion = ty::PlaceholderRegion;
128128

@@ -146,13 +146,21 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
146146
self.generics_of(def_id)
147147
}
148148

149+
fn mk_args(self, args: &[Self::GenericArg]) -> Self::GenericArgs {
150+
self.mk_args(args)
151+
}
152+
149153
fn check_and_mk_args(
150154
self,
151155
def_id: DefId,
152156
args: impl IntoIterator<Item: Into<ty::GenericArg<'tcx>>>,
153157
) -> ty::GenericArgsRef<'tcx> {
154158
self.check_and_mk_args(def_id, args)
155159
}
160+
161+
fn parent(self, def_id: Self::DefId) -> Self::DefId {
162+
self.parent(def_id)
163+
}
156164
}
157165

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

Diff for: compiler/rustc_middle/src/ty/predicate.rs

+7-140
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
use rustc_data_structures::captures::Captures;
22
use rustc_data_structures::intern::Interned;
3-
use rustc_errors::{DiagArgValue, IntoDiagArg};
43
use rustc_hir::def_id::DefId;
54
use rustc_macros::{HashStable, Lift, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
65
use rustc_type_ir::ClauseKind as IrClauseKind;
76
use rustc_type_ir::PredicateKind as IrPredicateKind;
87
use rustc_type_ir::TraitPredicate as IrTraitPredicate;
98
use rustc_type_ir::TraitRef as IrTraitRef;
9+
use rustc_type_ir::ProjectionPredicate as IrProjectionPredicate;
10+
use rustc_type_ir::ExistentialTraitRef as IrExistentialTraitRef;
11+
use rustc_type_ir::ExistentialProjection as IrExistentialProjection;
1012
use std::cmp::Ordering;
1113

12-
use crate::ty::visit::TypeVisitableExt;
1314
use crate::ty::{
14-
self, AliasTy, Binder, DebruijnIndex, DebugWithInfcx, EarlyBinder, GenericArgsRef,
15+
self, AliasTy, Binder, DebruijnIndex, DebugWithInfcx, EarlyBinder,
1516
PredicatePolarity, Term, Ty, TyCtxt, TypeFlags, WithCachedTypeInfo,
1617
};
1718

1819
pub type TraitRef<'tcx> = IrTraitRef<TyCtxt<'tcx>>;
20+
pub type ProjectionPredicate<'tcx> = IrProjectionPredicate<TyCtxt<'tcx>>;
21+
pub type ExistentialTraitRef<'tcx> = IrExistentialTraitRef<TyCtxt<'tcx>>;
22+
pub type ExistentialProjection<'tcx> = IrExistentialProjection<TyCtxt<'tcx>>;
1923
pub type TraitPredicate<'tcx> = IrTraitPredicate<TyCtxt<'tcx>>;
2024
pub type ClauseKind<'tcx> = IrClauseKind<TyCtxt<'tcx>>;
2125
pub type PredicateKind<'tcx> = IrPredicateKind<TyCtxt<'tcx>>;
@@ -342,52 +346,6 @@ impl<'tcx> PolyTraitRef<'tcx> {
342346
}
343347
}
344348

345-
/// An existential reference to a trait, where `Self` is erased.
346-
/// For example, the trait object `Trait<'a, 'b, X, Y>` is:
347-
/// ```ignore (illustrative)
348-
/// exists T. T: Trait<'a, 'b, X, Y>
349-
/// ```
350-
/// The generic parameters don't include the erased `Self`, only trait
351-
/// type and lifetime parameters (`[X, Y]` and `['a, 'b]` above).
352-
#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
353-
#[derive(HashStable, TypeFoldable, TypeVisitable, Lift)]
354-
pub struct ExistentialTraitRef<'tcx> {
355-
pub def_id: DefId,
356-
pub args: GenericArgsRef<'tcx>,
357-
}
358-
359-
impl<'tcx> ExistentialTraitRef<'tcx> {
360-
pub fn erase_self_ty(
361-
tcx: TyCtxt<'tcx>,
362-
trait_ref: ty::TraitRef<'tcx>,
363-
) -> ty::ExistentialTraitRef<'tcx> {
364-
// Assert there is a Self.
365-
trait_ref.args.type_at(0);
366-
367-
ty::ExistentialTraitRef {
368-
def_id: trait_ref.def_id,
369-
args: tcx.mk_args(&trait_ref.args[1..]),
370-
}
371-
}
372-
373-
/// Object types don't have a self type specified. Therefore, when
374-
/// we convert the principal trait-ref into a normal trait-ref,
375-
/// you must give *some* self type. A common choice is `mk_err()`
376-
/// or some placeholder type.
377-
pub fn with_self_ty(&self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> ty::TraitRef<'tcx> {
378-
// otherwise the escaping vars would be captured by the binder
379-
// debug_assert!(!self_ty.has_escaping_bound_vars());
380-
381-
ty::TraitRef::new(tcx, self.def_id, [self_ty.into()].into_iter().chain(self.args.iter()))
382-
}
383-
}
384-
385-
impl<'tcx> IntoDiagArg for ExistentialTraitRef<'tcx> {
386-
fn into_diag_arg(self) -> DiagArgValue {
387-
self.to_string().into_diag_arg()
388-
}
389-
}
390-
391349
pub type PolyExistentialTraitRef<'tcx> = ty::Binder<'tcx, ExistentialTraitRef<'tcx>>;
392350

393351
impl<'tcx> PolyExistentialTraitRef<'tcx> {
@@ -404,62 +362,8 @@ impl<'tcx> PolyExistentialTraitRef<'tcx> {
404362
}
405363
}
406364

407-
/// A `ProjectionPredicate` for an `ExistentialTraitRef`.
408-
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, TyEncodable, TyDecodable)]
409-
#[derive(HashStable, TypeFoldable, TypeVisitable, Lift)]
410-
pub struct ExistentialProjection<'tcx> {
411-
pub def_id: DefId,
412-
pub args: GenericArgsRef<'tcx>,
413-
pub term: Term<'tcx>,
414-
}
415-
416365
pub type PolyExistentialProjection<'tcx> = ty::Binder<'tcx, ExistentialProjection<'tcx>>;
417366

418-
impl<'tcx> ExistentialProjection<'tcx> {
419-
/// Extracts the underlying existential trait reference from this projection.
420-
/// For example, if this is a projection of `exists T. <T as Iterator>::Item == X`,
421-
/// then this function would return an `exists T. T: Iterator` existential trait
422-
/// reference.
423-
pub fn trait_ref(&self, tcx: TyCtxt<'tcx>) -> ty::ExistentialTraitRef<'tcx> {
424-
let def_id = tcx.parent(self.def_id);
425-
let args_count = tcx.generics_of(def_id).count() - 1;
426-
let args = tcx.mk_args(&self.args[..args_count]);
427-
ty::ExistentialTraitRef { def_id, args }
428-
}
429-
430-
pub fn with_self_ty(
431-
&self,
432-
tcx: TyCtxt<'tcx>,
433-
self_ty: Ty<'tcx>,
434-
) -> ty::ProjectionPredicate<'tcx> {
435-
// otherwise the escaping regions would be captured by the binders
436-
debug_assert!(!self_ty.has_escaping_bound_vars());
437-
438-
ty::ProjectionPredicate {
439-
projection_ty: AliasTy::new(
440-
tcx,
441-
self.def_id,
442-
[self_ty.into()].into_iter().chain(self.args),
443-
),
444-
term: self.term,
445-
}
446-
}
447-
448-
pub fn erase_self_ty(
449-
tcx: TyCtxt<'tcx>,
450-
projection_predicate: ty::ProjectionPredicate<'tcx>,
451-
) -> Self {
452-
// Assert there is a Self.
453-
projection_predicate.projection_ty.args.type_at(0);
454-
455-
Self {
456-
def_id: projection_predicate.projection_ty.def_id,
457-
args: tcx.mk_args(&projection_predicate.projection_ty.args[1..]),
458-
term: projection_predicate.term,
459-
}
460-
}
461-
}
462-
463367
impl<'tcx> PolyExistentialProjection<'tcx> {
464368
pub fn with_self_ty(
465369
&self,
@@ -628,43 +532,6 @@ pub struct CoercePredicate<'tcx> {
628532
}
629533
pub type PolyCoercePredicate<'tcx> = ty::Binder<'tcx, CoercePredicate<'tcx>>;
630534

631-
/// This kind of predicate has no *direct* correspondent in the
632-
/// syntax, but it roughly corresponds to the syntactic forms:
633-
///
634-
/// 1. `T: TraitRef<..., Item = Type>`
635-
/// 2. `<T as TraitRef<...>>::Item == Type` (NYI)
636-
///
637-
/// In particular, form #1 is "desugared" to the combination of a
638-
/// normal trait predicate (`T: TraitRef<...>`) and one of these
639-
/// predicates. Form #2 is a broader form in that it also permits
640-
/// equality between arbitrary types. Processing an instance of
641-
/// Form #2 eventually yields one of these `ProjectionPredicate`
642-
/// instances to normalize the LHS.
643-
#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
644-
#[derive(HashStable, TypeFoldable, TypeVisitable, Lift)]
645-
pub struct ProjectionPredicate<'tcx> {
646-
pub projection_ty: AliasTy<'tcx>,
647-
pub term: Term<'tcx>,
648-
}
649-
650-
impl<'tcx> ProjectionPredicate<'tcx> {
651-
pub fn self_ty(self) -> Ty<'tcx> {
652-
self.projection_ty.self_ty()
653-
}
654-
655-
pub fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> ProjectionPredicate<'tcx> {
656-
Self { projection_ty: self.projection_ty.with_self_ty(tcx, self_ty), ..self }
657-
}
658-
659-
pub fn trait_def_id(self, tcx: TyCtxt<'tcx>) -> DefId {
660-
self.projection_ty.trait_def_id(tcx)
661-
}
662-
663-
pub fn def_id(self) -> DefId {
664-
self.projection_ty.def_id
665-
}
666-
}
667-
668535
pub type PolyProjectionPredicate<'tcx> = Binder<'tcx, ProjectionPredicate<'tcx>>;
669536

670537
impl<'tcx> PolyProjectionPredicate<'tcx> {

Diff for: compiler/rustc_middle/src/ty/print/pretty.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -3087,14 +3087,6 @@ define_print! {
30873087
ty::PredicateKind::AliasRelate(t1, t2, dir) => p!(print(t1), write(" {} ", dir), print(t2)),
30883088
}
30893089
}
3090-
}
3091-
3092-
define_print_and_forward_display! {
3093-
(self, cx):
3094-
3095-
&'tcx ty::List<Ty<'tcx>> {
3096-
p!("{{", comma_sep(self.iter()), "}}")
3097-
}
30983090

30993091
ty::ExistentialTraitRef<'tcx> {
31003092
// Use a type that can't appear in defaults of type parameters.
@@ -3108,6 +3100,20 @@ define_print_and_forward_display! {
31083100
p!(write("{} = ", name), print(self.term))
31093101
}
31103102

3103+
ty::ProjectionPredicate<'tcx> {
3104+
p!(print(self.projection_ty), " == ");
3105+
cx.reset_type_limit();
3106+
p!(print(self.term))
3107+
}
3108+
}
3109+
3110+
define_print_and_forward_display! {
3111+
(self, cx):
3112+
3113+
&'tcx ty::List<Ty<'tcx>> {
3114+
p!("{{", comma_sep(self.iter()), "}}")
3115+
}
3116+
31113117
ty::ExistentialPredicate<'tcx> {
31123118
match *self {
31133119
ty::ExistentialPredicate::Trait(x) => p!(print(x)),
@@ -3186,12 +3192,6 @@ define_print_and_forward_display! {
31863192
p!(print(self.b))
31873193
}
31883194

3189-
ty::ProjectionPredicate<'tcx> {
3190-
p!(print(self.projection_ty), " == ");
3191-
cx.reset_type_limit();
3192-
p!(print(self.term))
3193-
}
3194-
31953195
ty::NormalizesTo<'tcx> {
31963196
p!(print(self.alias), " normalizes-to ");
31973197
cx.reset_type_limit();

Diff for: compiler/rustc_middle/src/ty/structural_impls.rs

-12
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ impl fmt::Debug for ty::UpvarId {
5555
}
5656
}
5757

58-
impl<'tcx> fmt::Debug for ty::ExistentialTraitRef<'tcx> {
59-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60-
with_no_trimmed_paths!(fmt::Display::fmt(self, f))
61-
}
62-
}
63-
6458
impl<'tcx> fmt::Debug for ty::adjustment::Adjustment<'tcx> {
6559
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6660
write!(f, "{:?} -> {}", self.kind, self.target)
@@ -158,12 +152,6 @@ impl fmt::Debug for ty::ParamConst {
158152
}
159153
}
160154

161-
impl<'tcx> fmt::Debug for ty::ProjectionPredicate<'tcx> {
162-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163-
write!(f, "ProjectionPredicate({:?}, {:?})", self.projection_ty, self.term)
164-
}
165-
}
166-
167155
impl<'tcx> fmt::Debug for ty::NormalizesTo<'tcx> {
168156
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
169157
write!(f, "NormalizesTo({:?}, {:?})", self.alias, self.term)

Diff for: compiler/rustc_middle/src/ty/sty.rs

+30
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,36 @@ pub struct AliasTy<'tcx> {
11421142
_use_alias_ty_new_instead: (),
11431143
}
11441144

1145+
impl<'tcx> rustc_type_ir::inherent::AliasTy<TyCtxt<'tcx>> for AliasTy<'tcx> {
1146+
fn new(
1147+
interner: TyCtxt<'tcx>,
1148+
trait_def_id: DefId,
1149+
args: impl IntoIterator<Item: Into<ty::GenericArg<'tcx>>>,
1150+
) -> Self {
1151+
AliasTy::new(interner, trait_def_id, args)
1152+
}
1153+
1154+
fn def_id(self) -> DefId {
1155+
self.def_id
1156+
}
1157+
1158+
fn args(self) -> ty::GenericArgsRef<'tcx> {
1159+
self.args
1160+
}
1161+
1162+
fn trait_def_id(self, interner: TyCtxt<'tcx>) -> DefId {
1163+
self.trait_def_id(interner)
1164+
}
1165+
1166+
fn self_ty(self) -> Ty<'tcx> {
1167+
self.self_ty()
1168+
}
1169+
1170+
fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> Self {
1171+
self.with_self_ty(tcx, self_ty)
1172+
}
1173+
}
1174+
11451175
impl<'tcx> AliasTy<'tcx> {
11461176
pub fn new(
11471177
tcx: TyCtxt<'tcx>,

0 commit comments

Comments
 (0)