Skip to content

Commit 735e9c0

Browse files
ericmarkmartinspastorino
authored andcommitted
stable types for predicates
1 parent 544dea6 commit 735e9c0

File tree

2 files changed

+269
-11
lines changed

2 files changed

+269
-11
lines changed

compiler/rustc_smir/src/rustc_smir/mod.rs

Lines changed: 191 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ impl<'tcx> Stable<'tcx> for mir::Rvalue<'tcx> {
200200
stable_mir::mir::Rvalue::Repeat(op.stable(tables), len)
201201
}
202202
Ref(region, kind, place) => stable_mir::mir::Rvalue::Ref(
203-
opaque(region),
203+
region.stable(tables),
204204
kind.stable(tables),
205205
place.stable(tables),
206206
),
@@ -842,12 +842,9 @@ impl<'tcx> Stable<'tcx> for ty::GenericArgKind<'tcx> {
842842
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
843843
use stable_mir::ty::GenericArgKind;
844844
match self {
845-
ty::GenericArgKind::Lifetime(region) => GenericArgKind::Lifetime(opaque(region)),
845+
ty::GenericArgKind::Lifetime(region) => GenericArgKind::Lifetime(region.stable(tables)),
846846
ty::GenericArgKind::Type(ty) => GenericArgKind::Type(tables.intern_ty(*ty)),
847-
ty::GenericArgKind::Const(cnst) => {
848-
let cnst = ConstantKind::from_const(*cnst, tables.tcx);
849-
GenericArgKind::Const(stable_mir::ty::Const { literal: cnst.stable(tables) })
850-
}
847+
ty::GenericArgKind::Const(cnst) => GenericArgKind::Const(cnst.stable(tables)),
851848
}
852849
}
853850
}
@@ -1053,16 +1050,14 @@ impl<'tcx> Stable<'tcx> for Ty<'tcx> {
10531050
}
10541051
ty::Str => TyKind::RigidTy(RigidTy::Str),
10551052
ty::Array(ty, constant) => {
1056-
let cnst = ConstantKind::from_const(*constant, tables.tcx);
1057-
let cnst = stable_mir::ty::Const { literal: cnst.stable(tables) };
1058-
TyKind::RigidTy(RigidTy::Array(tables.intern_ty(*ty), cnst))
1053+
TyKind::RigidTy(RigidTy::Array(tables.intern_ty(*ty), constant.stable(tables)))
10591054
}
10601055
ty::Slice(ty) => TyKind::RigidTy(RigidTy::Slice(tables.intern_ty(*ty))),
10611056
ty::RawPtr(ty::TypeAndMut { ty, mutbl }) => {
10621057
TyKind::RigidTy(RigidTy::RawPtr(tables.intern_ty(*ty), mutbl.stable(tables)))
10631058
}
10641059
ty::Ref(region, ty, mutbl) => TyKind::RigidTy(RigidTy::Ref(
1065-
opaque(region),
1060+
region.stable(tables),
10661061
tables.intern_ty(*ty),
10671062
mutbl.stable(tables),
10681063
)),
@@ -1077,7 +1072,7 @@ impl<'tcx> Stable<'tcx> for Ty<'tcx> {
10771072
.iter()
10781073
.map(|existential_predicate| existential_predicate.stable(tables))
10791074
.collect(),
1080-
opaque(region),
1075+
region.stable(tables),
10811076
dyn_kind.stable(tables),
10821077
))
10831078
}
@@ -1112,6 +1107,15 @@ impl<'tcx> Stable<'tcx> for Ty<'tcx> {
11121107
}
11131108
}
11141109

1110+
impl<'tcx> Stable<'tcx> for ty::Const<'tcx> {
1111+
type T = stable_mir::ty::Const;
1112+
1113+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1114+
let cnst = ConstantKind::from_const(*self, tables.tcx);
1115+
stable_mir::ty::Const { literal: cnst.stable(tables) }
1116+
}
1117+
}
1118+
11151119
impl<'tcx> Stable<'tcx> for ty::ParamTy {
11161120
type T = stable_mir::ty::ParamTy;
11171121
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
@@ -1276,3 +1280,179 @@ impl<'tcx> Stable<'tcx> for rustc_middle::ty::GenericParamDef {
12761280
}
12771281
}
12781282
}
1283+
1284+
impl<'tcx> Stable<'tcx> for ty::PredicateKind<'tcx> {
1285+
type T = stable_mir::ty::PredicateKind;
1286+
1287+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1288+
use ty::PredicateKind;
1289+
match self {
1290+
PredicateKind::Clause(clause_kind) => {
1291+
stable_mir::ty::PredicateKind::Clause(clause_kind.stable(tables))
1292+
}
1293+
PredicateKind::ObjectSafe(did) => {
1294+
stable_mir::ty::PredicateKind::ObjectSafe(tables.trait_def(*did))
1295+
}
1296+
PredicateKind::ClosureKind(did, generic_args, closure_kind) => {
1297+
stable_mir::ty::PredicateKind::ClosureKind(
1298+
tables.closure_def(*did),
1299+
generic_args.stable(tables),
1300+
closure_kind.stable(tables),
1301+
)
1302+
}
1303+
PredicateKind::Subtype(subtype_predicate) => {
1304+
stable_mir::ty::PredicateKind::SubType(subtype_predicate.stable(tables))
1305+
}
1306+
PredicateKind::Coerce(coerce_predicate) => {
1307+
stable_mir::ty::PredicateKind::Coerce(coerce_predicate.stable(tables))
1308+
}
1309+
PredicateKind::ConstEquate(a, b) => {
1310+
stable_mir::ty::PredicateKind::ConstEquate(a.stable(tables), b.stable(tables))
1311+
}
1312+
PredicateKind::Ambiguous => stable_mir::ty::PredicateKind::Ambiguous,
1313+
PredicateKind::AliasRelate(a, b, alias_relation_direction) => {
1314+
stable_mir::ty::PredicateKind::AliasRelate(
1315+
a.unpack().stable(tables),
1316+
b.unpack().stable(tables),
1317+
alias_relation_direction.stable(tables),
1318+
)
1319+
}
1320+
}
1321+
}
1322+
}
1323+
1324+
impl<'tcx> Stable<'tcx> for ty::ClauseKind<'tcx> {
1325+
type T = stable_mir::ty::ClauseKind;
1326+
1327+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1328+
use ty::ClauseKind::*;
1329+
match *self {
1330+
Trait(trait_object) => stable_mir::ty::ClauseKind::Trait(trait_object.stable(tables)),
1331+
RegionOutlives(region_outlives) => {
1332+
stable_mir::ty::ClauseKind::RegionOutlives(region_outlives.stable(tables))
1333+
}
1334+
TypeOutlives(type_outlives) => {
1335+
let ty::OutlivesPredicate::<_, _>(a, b) = type_outlives;
1336+
stable_mir::ty::ClauseKind::TypeOutlives(stable_mir::ty::OutlivesPredicate(
1337+
tables.intern_ty(a),
1338+
b.stable(tables),
1339+
))
1340+
}
1341+
Projection(projection_predicate) => {
1342+
stable_mir::ty::ClauseKind::Projection(projection_predicate.stable(tables))
1343+
}
1344+
ConstArgHasType(const_, ty) => stable_mir::ty::ClauseKind::ConstArgHasType(
1345+
const_.stable(tables),
1346+
tables.intern_ty(ty),
1347+
),
1348+
WellFormed(generic_arg) => {
1349+
stable_mir::ty::ClauseKind::WellFormed(generic_arg.unpack().stable(tables))
1350+
}
1351+
ConstEvaluatable(const_) => {
1352+
stable_mir::ty::ClauseKind::ConstEvaluatable(const_.stable(tables))
1353+
}
1354+
}
1355+
}
1356+
}
1357+
1358+
impl<'tcx> Stable<'tcx> for ty::ClosureKind {
1359+
type T = stable_mir::ty::ClosureKind;
1360+
1361+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
1362+
use ty::ClosureKind::*;
1363+
match self {
1364+
Fn => stable_mir::ty::ClosureKind::Fn,
1365+
FnMut => stable_mir::ty::ClosureKind::FnMut,
1366+
FnOnce => stable_mir::ty::ClosureKind::FnOnce,
1367+
}
1368+
}
1369+
}
1370+
1371+
impl<'tcx> Stable<'tcx> for ty::SubtypePredicate<'tcx> {
1372+
type T = stable_mir::ty::SubtypePredicate;
1373+
1374+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1375+
let ty::SubtypePredicate { a, b, a_is_expected: _ } = self;
1376+
stable_mir::ty::SubtypePredicate { a: tables.intern_ty(*a), b: tables.intern_ty(*b) }
1377+
}
1378+
}
1379+
1380+
impl<'tcx> Stable<'tcx> for ty::CoercePredicate<'tcx> {
1381+
type T = stable_mir::ty::CoercePredicate;
1382+
1383+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1384+
let ty::CoercePredicate { a, b } = self;
1385+
stable_mir::ty::CoercePredicate { a: tables.intern_ty(*a), b: tables.intern_ty(*b) }
1386+
}
1387+
}
1388+
1389+
impl<'tcx> Stable<'tcx> for ty::AliasRelationDirection {
1390+
type T = stable_mir::ty::AliasRelationDirection;
1391+
1392+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
1393+
use ty::AliasRelationDirection::*;
1394+
match self {
1395+
Equate => stable_mir::ty::AliasRelationDirection::Equate,
1396+
Subtype => stable_mir::ty::AliasRelationDirection::Subtype,
1397+
}
1398+
}
1399+
}
1400+
1401+
impl<'tcx> Stable<'tcx> for ty::TraitPredicate<'tcx> {
1402+
type T = stable_mir::ty::TraitPredicate;
1403+
1404+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1405+
let ty::TraitPredicate { trait_ref, polarity } = self;
1406+
stable_mir::ty::TraitPredicate {
1407+
trait_ref: trait_ref.stable(tables),
1408+
polarity: polarity.stable(tables),
1409+
}
1410+
}
1411+
}
1412+
1413+
impl<'tcx, A, B, U, V> Stable<'tcx> for ty::OutlivesPredicate<A, B>
1414+
where
1415+
A: Stable<'tcx, T = U>,
1416+
B: Stable<'tcx, T = V>,
1417+
{
1418+
type T = stable_mir::ty::OutlivesPredicate<U, V>;
1419+
1420+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1421+
let ty::OutlivesPredicate(a, b) = self;
1422+
stable_mir::ty::OutlivesPredicate(a.stable(tables), b.stable(tables))
1423+
}
1424+
}
1425+
1426+
impl<'tcx> Stable<'tcx> for ty::ProjectionPredicate<'tcx> {
1427+
type T = stable_mir::ty::ProjectionPredicate;
1428+
1429+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1430+
let ty::ProjectionPredicate { projection_ty, term } = self;
1431+
stable_mir::ty::ProjectionPredicate {
1432+
projection_ty: projection_ty.stable(tables),
1433+
term: term.unpack().stable(tables),
1434+
}
1435+
}
1436+
}
1437+
1438+
impl<'tcx> Stable<'tcx> for ty::ImplPolarity {
1439+
type T = stable_mir::ty::ImplPolarity;
1440+
1441+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
1442+
use ty::ImplPolarity::*;
1443+
match self {
1444+
Positive => stable_mir::ty::ImplPolarity::Positive,
1445+
Negative => stable_mir::ty::ImplPolarity::Negative,
1446+
Reservation => stable_mir::ty::ImplPolarity::Reservation,
1447+
}
1448+
}
1449+
}
1450+
1451+
impl<'tcx> Stable<'tcx> for ty::Region<'tcx> {
1452+
type T = stable_mir::ty::Region;
1453+
1454+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
1455+
// FIXME: add a real implementation of stable regions
1456+
opaque(self)
1457+
}
1458+
}

compiler/rustc_smir/src/stable_mir/ty.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,3 +497,81 @@ pub struct GenericParamDef {
497497
pub pure_wrt_drop: bool,
498498
pub kind: GenericParamDefKind,
499499
}
500+
501+
pub struct GenericPredicates {
502+
pub parent: Option<DefId>,
503+
pub predicates: Vec<PredicateKind>,
504+
}
505+
506+
#[derive(Clone, Debug)]
507+
pub enum PredicateKind {
508+
Clause(ClauseKind),
509+
ObjectSafe(TraitDef),
510+
ClosureKind(ClosureDef, GenericArgs, ClosureKind),
511+
SubType(SubtypePredicate),
512+
Coerce(CoercePredicate),
513+
ConstEquate(Const, Const),
514+
Ambiguous,
515+
AliasRelate(TermKind, TermKind, AliasRelationDirection),
516+
}
517+
518+
#[derive(Clone, Debug)]
519+
pub enum ClauseKind {
520+
Trait(TraitPredicate),
521+
RegionOutlives(RegionOutlivesPredicate),
522+
TypeOutlives(TypeOutlivesPredicate),
523+
Projection(ProjectionPredicate),
524+
ConstArgHasType(Const, Ty),
525+
WellFormed(GenericArgKind),
526+
ConstEvaluatable(Const),
527+
}
528+
529+
#[derive(Clone, Debug)]
530+
pub enum ClosureKind {
531+
Fn,
532+
FnMut,
533+
FnOnce,
534+
}
535+
536+
#[derive(Clone, Debug)]
537+
pub struct SubtypePredicate {
538+
pub a: Ty,
539+
pub b: Ty,
540+
}
541+
542+
#[derive(Clone, Debug)]
543+
pub struct CoercePredicate {
544+
pub a: Ty,
545+
pub b: Ty,
546+
}
547+
548+
#[derive(Clone, Debug)]
549+
pub enum AliasRelationDirection {
550+
Equate,
551+
Subtype,
552+
}
553+
554+
#[derive(Clone, Debug)]
555+
pub struct TraitPredicate {
556+
pub trait_ref: TraitRef,
557+
pub polarity: ImplPolarity,
558+
}
559+
560+
#[derive(Clone, Debug)]
561+
pub struct OutlivesPredicate<A, B>(pub A, pub B);
562+
563+
pub type RegionOutlivesPredicate = OutlivesPredicate<Region, Region>;
564+
pub type TypeOutlivesPredicate = OutlivesPredicate<Ty, Region>;
565+
566+
#[derive(Clone, Debug)]
567+
pub struct ProjectionPredicate {
568+
pub projection_ty: AliasTy,
569+
pub term: TermKind,
570+
}
571+
572+
#[derive(Clone, Debug)]
573+
pub enum ImplPolarity {
574+
Positive,
575+
Negative,
576+
Reservation,
577+
}

0 commit comments

Comments
 (0)