Skip to content

Commit 4474899

Browse files
Uplift NormalizesTo, CoercePredicate, and SubtypePredicate
1 parent afd37bd commit 4474899

File tree

10 files changed

+128
-87
lines changed

10 files changed

+128
-87
lines changed

compiler/rustc_errors/src/diagnostic_impls.rs

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

103-
104-
105103
impl<I: rustc_type_ir::Interner> IntoDiagArg for rustc_type_ir::ExistentialTraitRef<I> {
106104
fn into_diag_arg(self) -> DiagArgValue {
107105
self.to_string().into_diag_arg()

compiler/rustc_middle/src/ty/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,15 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
149149
fn mk_args(self, args: &[Self::GenericArg]) -> Self::GenericArgs {
150150
self.mk_args(args)
151151
}
152-
152+
153153
fn check_and_mk_args(
154154
self,
155155
def_id: DefId,
156156
args: impl IntoIterator<Item: Into<ty::GenericArg<'tcx>>>,
157157
) -> ty::GenericArgsRef<'tcx> {
158158
self.check_and_mk_args(def_id, args)
159159
}
160-
160+
161161
fn parent(self, def_id: Self::DefId) -> Self::DefId {
162162
self.parent(def_id)
163163
}

compiler/rustc_middle/src/ty/predicate.rs

+11-49
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@ use rustc_data_structures::intern::Interned;
33
use rustc_hir::def_id::DefId;
44
use rustc_macros::{HashStable, Lift, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
55
use rustc_type_ir::ClauseKind as IrClauseKind;
6+
use rustc_type_ir::CoercePredicate as IrCoercePredicate;
7+
use rustc_type_ir::ExistentialProjection as IrExistentialProjection;
8+
use rustc_type_ir::ExistentialTraitRef as IrExistentialTraitRef;
9+
use rustc_type_ir::NormalizesTo as IrNormalizesTo;
610
use rustc_type_ir::PredicateKind as IrPredicateKind;
11+
use rustc_type_ir::ProjectionPredicate as IrProjectionPredicate;
12+
use rustc_type_ir::SubtypePredicate as IrSubtypePredicate;
713
use rustc_type_ir::TraitPredicate as IrTraitPredicate;
814
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;
1215
use std::cmp::Ordering;
1316

1417
use crate::ty::{
15-
self, AliasTy, Binder, DebruijnIndex, DebugWithInfcx, EarlyBinder,
16-
PredicatePolarity, Term, Ty, TyCtxt, TypeFlags, WithCachedTypeInfo,
18+
self, Binder, DebruijnIndex, DebugWithInfcx, EarlyBinder, PredicatePolarity, Term, Ty, TyCtxt,
19+
TypeFlags, WithCachedTypeInfo,
1720
};
1821

1922
pub type TraitRef<'tcx> = IrTraitRef<TyCtxt<'tcx>>;
@@ -23,6 +26,9 @@ pub type ExistentialProjection<'tcx> = IrExistentialProjection<TyCtxt<'tcx>>;
2326
pub type TraitPredicate<'tcx> = IrTraitPredicate<TyCtxt<'tcx>>;
2427
pub type ClauseKind<'tcx> = IrClauseKind<TyCtxt<'tcx>>;
2528
pub type PredicateKind<'tcx> = IrPredicateKind<TyCtxt<'tcx>>;
29+
pub type NormalizesTo<'tcx> = IrNormalizesTo<TyCtxt<'tcx>>;
30+
pub type CoercePredicate<'tcx> = IrCoercePredicate<TyCtxt<'tcx>>;
31+
pub type SubtypePredicate<'tcx> = IrSubtypePredicate<TyCtxt<'tcx>>;
2632

2733
/// A statement that can be proven by a trait solver. This includes things that may
2834
/// show up in where clauses, such as trait predicates and projection predicates,
@@ -511,25 +517,8 @@ pub type TypeOutlivesPredicate<'tcx> = OutlivesPredicate<Ty<'tcx>, ty::Region<'t
511517
pub type PolyRegionOutlivesPredicate<'tcx> = ty::Binder<'tcx, RegionOutlivesPredicate<'tcx>>;
512518
pub type PolyTypeOutlivesPredicate<'tcx> = ty::Binder<'tcx, TypeOutlivesPredicate<'tcx>>;
513519

514-
/// Encodes that `a` must be a subtype of `b`. The `a_is_expected` flag indicates
515-
/// whether the `a` type is the type that we should label as "expected" when
516-
/// presenting user diagnostics.
517-
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, TyEncodable, TyDecodable)]
518-
#[derive(HashStable, TypeFoldable, TypeVisitable, Lift)]
519-
pub struct SubtypePredicate<'tcx> {
520-
pub a_is_expected: bool,
521-
pub a: Ty<'tcx>,
522-
pub b: Ty<'tcx>,
523-
}
524520
pub type PolySubtypePredicate<'tcx> = ty::Binder<'tcx, SubtypePredicate<'tcx>>;
525521

526-
/// Encodes that we have to coerce *from* the `a` type to the `b` type.
527-
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, TyEncodable, TyDecodable)]
528-
#[derive(HashStable, TypeFoldable, TypeVisitable, Lift)]
529-
pub struct CoercePredicate<'tcx> {
530-
pub a: Ty<'tcx>,
531-
pub b: Ty<'tcx>,
532-
}
533522
pub type PolyCoercePredicate<'tcx> = ty::Binder<'tcx, CoercePredicate<'tcx>>;
534523

535524
pub type PolyProjectionPredicate<'tcx> = Binder<'tcx, ProjectionPredicate<'tcx>>;
@@ -568,33 +557,6 @@ impl<'tcx> PolyProjectionPredicate<'tcx> {
568557
}
569558
}
570559

571-
/// Used by the new solver. Unlike a `ProjectionPredicate` this can only be
572-
/// proven by actually normalizing `alias`.
573-
#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
574-
#[derive(HashStable, TypeFoldable, TypeVisitable, Lift)]
575-
pub struct NormalizesTo<'tcx> {
576-
pub alias: AliasTy<'tcx>,
577-
pub term: Term<'tcx>,
578-
}
579-
580-
impl<'tcx> NormalizesTo<'tcx> {
581-
pub fn self_ty(self) -> Ty<'tcx> {
582-
self.alias.self_ty()
583-
}
584-
585-
pub fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> NormalizesTo<'tcx> {
586-
Self { alias: self.alias.with_self_ty(tcx, self_ty), ..self }
587-
}
588-
589-
pub fn trait_def_id(self, tcx: TyCtxt<'tcx>) -> DefId {
590-
self.alias.trait_def_id(tcx)
591-
}
592-
593-
pub fn def_id(self) -> DefId {
594-
self.alias.def_id
595-
}
596-
}
597-
598560
pub trait ToPolyTraitRef<'tcx> {
599561
fn to_poly_trait_ref(&self) -> PolyTraitRef<'tcx>;
600562
}

compiler/rustc_middle/src/ty/print/pretty.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -3105,6 +3105,24 @@ define_print! {
31053105
cx.reset_type_limit();
31063106
p!(print(self.term))
31073107
}
3108+
3109+
ty::SubtypePredicate<'tcx> {
3110+
p!(print(self.a), " <: ");
3111+
cx.reset_type_limit();
3112+
p!(print(self.b))
3113+
}
3114+
3115+
ty::CoercePredicate<'tcx> {
3116+
p!(print(self.a), " -> ");
3117+
cx.reset_type_limit();
3118+
p!(print(self.b))
3119+
}
3120+
3121+
ty::NormalizesTo<'tcx> {
3122+
p!(print(self.alias), " normalizes-to ");
3123+
cx.reset_type_limit();
3124+
p!(print(self.term))
3125+
}
31083126
}
31093127

31103128
define_print_and_forward_display! {
@@ -3180,24 +3198,6 @@ define_print_and_forward_display! {
31803198
p!(write("{}", self.name))
31813199
}
31823200

3183-
ty::SubtypePredicate<'tcx> {
3184-
p!(print(self.a), " <: ");
3185-
cx.reset_type_limit();
3186-
p!(print(self.b))
3187-
}
3188-
3189-
ty::CoercePredicate<'tcx> {
3190-
p!(print(self.a), " -> ");
3191-
cx.reset_type_limit();
3192-
p!(print(self.b))
3193-
}
3194-
3195-
ty::NormalizesTo<'tcx> {
3196-
p!(print(self.alias), " normalizes-to ");
3197-
cx.reset_type_limit();
3198-
p!(print(self.term))
3199-
}
3200-
32013201
ty::Term<'tcx> {
32023202
match self.unpack() {
32033203
ty::TermKind::Ty(ty) => p!(print(ty)),

compiler/rustc_middle/src/ty/structural_impls.rs

-6
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,6 @@ impl fmt::Debug for ty::ParamConst {
152152
}
153153
}
154154

155-
impl<'tcx> fmt::Debug for ty::NormalizesTo<'tcx> {
156-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157-
write!(f, "NormalizesTo({:?}, {:?})", self.alias, self.term)
158-
}
159-
}
160-
161155
impl<'tcx> fmt::Debug for ty::Predicate<'tcx> {
162156
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163157
write!(f, "{:?}", self.kind())

compiler/rustc_type_ir/src/inherent.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub trait BoundVars<I: Interner> {
9090
fn has_no_bound_vars(&self) -> bool;
9191
}
9292

93-
// TODO: Uplift `AliasTy`
93+
// FIXME: Uplift `AliasTy`
9494
pub trait AliasTy<I: Interner>: Copy + DebugWithInfcx<I> + Hash + Eq + Sized {
9595
fn new(
9696
interner: I,
@@ -107,5 +107,4 @@ pub trait AliasTy<I: Interner>: Copy + DebugWithInfcx<I> + Hash + Eq + Sized {
107107
fn self_ty(self) -> I::Ty;
108108

109109
fn with_self_ty(self, tcx: I, self_ty: I::Ty) -> Self;
110-
111110
}

compiler/rustc_type_ir/src/interner.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use crate::inherent::*;
66
use crate::ir_print::IrPrint;
77
use crate::visit::{Flags, TypeSuperVisitable, TypeVisitable};
88
use crate::{
9-
CanonicalVarInfo, DebugWithInfcx, ExistentialProjection, ExistentialTraitRef,
10-
ProjectionPredicate, TraitPredicate, TraitRef,
9+
CanonicalVarInfo, CoercePredicate, DebugWithInfcx, ExistentialProjection, ExistentialTraitRef,
10+
NormalizesTo, ProjectionPredicate, SubtypePredicate, TraitPredicate, TraitRef,
1111
};
1212

1313
pub trait Interner:
@@ -18,6 +18,9 @@ pub trait Interner:
1818
+ IrPrint<ExistentialTraitRef<Self>>
1919
+ IrPrint<ExistentialProjection<Self>>
2020
+ IrPrint<ProjectionPredicate<Self>>
21+
+ IrPrint<NormalizesTo<Self>>
22+
+ IrPrint<SubtypePredicate<Self>>
23+
+ IrPrint<CoercePredicate<Self>>
2124
{
2225
type DefId: Copy + Debug + Hash + Eq;
2326
type DefiningOpaqueTypes: Copy + Debug + Hash + Default + Eq + TypeVisitable<Self>;

compiler/rustc_type_ir/src/ir_print.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::fmt;
22

33
use crate::{
4-
ExistentialProjection, ExistentialTraitRef, Interner, ProjectionPredicate, TraitPredicate,
5-
TraitRef,
4+
CoercePredicate, ExistentialProjection, ExistentialTraitRef, Interner, NormalizesTo,
5+
ProjectionPredicate, SubtypePredicate, TraitPredicate, TraitRef,
66
};
77

88
pub trait IrPrint<T> {
@@ -39,7 +39,10 @@ define_display_via_print!(
3939
TraitPredicate,
4040
ExistentialTraitRef,
4141
ExistentialProjection,
42-
ProjectionPredicate
42+
ProjectionPredicate,
43+
NormalizesTo,
44+
SubtypePredicate,
45+
CoercePredicate,
4346
);
4447

4548
define_debug_via_print!(TraitRef, ExistentialTraitRef, ExistentialProjection);

compiler/rustc_type_ir/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ mod debug;
3737
mod flags;
3838
mod infcx;
3939
mod interner;
40+
mod predicate;
4041
mod predicate_kind;
4142
mod region_kind;
42-
mod predicate;
4343

4444
pub use canonical::*;
4545
#[cfg(feature = "nightly")]
@@ -49,9 +49,9 @@ pub use debug::{DebugWithInfcx, WithInfcx};
4949
pub use flags::*;
5050
pub use infcx::InferCtxtLike;
5151
pub use interner::*;
52+
pub use predicate::*;
5253
pub use predicate_kind::*;
5354
pub use region_kind::*;
54-
pub use predicate::*;
5555
pub use ty_info::*;
5656
pub use ty_kind::*;
5757
pub use AliasKind::*;

compiler/rustc_type_ir/src/predicate.rs

+83-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,11 @@ impl<I: Interner> ExistentialTraitRef<I> {
187187
// otherwise the escaping vars would be captured by the binder
188188
// debug_assert!(!self_ty.has_escaping_bound_vars());
189189

190-
TraitRef::new(interner, self.def_id, [self_ty.into()].into_iter().chain(self.args.into_iter()))
190+
TraitRef::new(
191+
interner,
192+
self.def_id,
193+
[self_ty.into()].into_iter().chain(self.args.into_iter()),
194+
)
191195
}
192196
}
193197

@@ -296,3 +300,81 @@ impl<I: Interner> fmt::Debug for ProjectionPredicate<I> {
296300
write!(f, "ProjectionPredicate({:?}, {:?})", self.projection_ty, self.term)
297301
}
298302
}
303+
304+
/// Used by the new solver. Unlike a `ProjectionPredicate` this can only be
305+
/// proven by actually normalizing `alias`.
306+
#[derive(derivative::Derivative)]
307+
#[derivative(
308+
Clone(bound = ""),
309+
Copy(bound = ""),
310+
Hash(bound = ""),
311+
PartialEq(bound = ""),
312+
Eq(bound = "")
313+
)]
314+
#[derive(TypeVisitable_Generic, TypeFoldable_Generic, Lift_Generic)]
315+
#[cfg_attr(feature = "nightly", derive(TyDecodable, TyEncodable, HashStable_NoContext))]
316+
pub struct NormalizesTo<I: Interner> {
317+
pub alias: I::AliasTy,
318+
pub term: I::Term,
319+
}
320+
321+
impl<I: Interner> NormalizesTo<I> {
322+
pub fn self_ty(self) -> I::Ty {
323+
self.alias.self_ty()
324+
}
325+
326+
pub fn with_self_ty(self, tcx: I, self_ty: I::Ty) -> NormalizesTo<I> {
327+
Self { alias: self.alias.with_self_ty(tcx, self_ty), ..self }
328+
}
329+
330+
pub fn trait_def_id(self, tcx: I) -> I::DefId {
331+
self.alias.trait_def_id(tcx)
332+
}
333+
334+
pub fn def_id(self) -> I::DefId {
335+
self.alias.def_id()
336+
}
337+
}
338+
339+
impl<I: Interner> fmt::Debug for NormalizesTo<I> {
340+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
341+
write!(f, "NormalizesTo({:?}, {:?})", self.alias, self.term)
342+
}
343+
}
344+
345+
/// Encodes that `a` must be a subtype of `b`. The `a_is_expected` flag indicates
346+
/// whether the `a` type is the type that we should label as "expected" when
347+
/// presenting user diagnostics.
348+
#[derive(derivative::Derivative)]
349+
#[derivative(
350+
Clone(bound = ""),
351+
Copy(bound = ""),
352+
Hash(bound = ""),
353+
PartialEq(bound = ""),
354+
Eq(bound = ""),
355+
Debug(bound = "")
356+
)]
357+
#[derive(TypeVisitable_Generic, TypeFoldable_Generic, Lift_Generic)]
358+
#[cfg_attr(feature = "nightly", derive(TyDecodable, TyEncodable, HashStable_NoContext))]
359+
pub struct SubtypePredicate<I: Interner> {
360+
pub a_is_expected: bool,
361+
pub a: I::Ty,
362+
pub b: I::Ty,
363+
}
364+
365+
/// Encodes that we have to coerce *from* the `a` type to the `b` type.
366+
#[derive(derivative::Derivative)]
367+
#[derivative(
368+
Clone(bound = ""),
369+
Copy(bound = ""),
370+
Hash(bound = ""),
371+
PartialEq(bound = ""),
372+
Eq(bound = ""),
373+
Debug(bound = "")
374+
)]
375+
#[derive(TypeVisitable_Generic, TypeFoldable_Generic, Lift_Generic)]
376+
#[cfg_attr(feature = "nightly", derive(TyDecodable, TyEncodable, HashStable_NoContext))]
377+
pub struct CoercePredicate<I: Interner> {
378+
pub a: I::Ty,
379+
pub b: I::Ty,
380+
}

0 commit comments

Comments
 (0)