Skip to content

Commit 0347b32

Browse files
committed
Refactor the chalkify lowering process
1 parent 6eafab0 commit 0347b32

File tree

5 files changed

+234
-138
lines changed

5 files changed

+234
-138
lines changed

src/librustc/ich/impls_ty.rs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,16 +1382,46 @@ impl_stable_hash_for!(enum infer::canonical::Certainty {
13821382
Proven, Ambiguous
13831383
});
13841384

1385-
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::WhereClauseAtom<'tcx> {
1385+
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::WhereClause<'tcx> {
13861386
fn hash_stable<W: StableHasherResult>(&self,
13871387
hcx: &mut StableHashingContext<'a>,
13881388
hasher: &mut StableHasher<W>) {
1389-
use traits::WhereClauseAtom::*;
1389+
use traits::WhereClause::*;
13901390

13911391
mem::discriminant(self).hash_stable(hcx, hasher);
13921392
match self {
13931393
Implemented(trait_ref) => trait_ref.hash_stable(hcx, hasher),
13941394
ProjectionEq(projection) => projection.hash_stable(hcx, hasher),
1395+
TypeOutlives(ty_outlives) => ty_outlives.hash_stable(hcx, hasher),
1396+
RegionOutlives(region_outlives) => region_outlives.hash_stable(hcx, hasher),
1397+
}
1398+
}
1399+
}
1400+
1401+
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::WellFormed<'tcx> {
1402+
fn hash_stable<W: StableHasherResult>(&self,
1403+
hcx: &mut StableHashingContext<'a>,
1404+
hasher: &mut StableHasher<W>) {
1405+
use traits::WellFormed::*;
1406+
1407+
mem::discriminant(self).hash_stable(hcx, hasher);
1408+
match self {
1409+
Trait(trait_ref) => trait_ref.hash_stable(hcx, hasher),
1410+
Ty(ty) => ty.hash_stable(hcx, hasher),
1411+
}
1412+
}
1413+
}
1414+
1415+
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::FromEnv<'tcx> {
1416+
fn hash_stable<W: StableHasherResult>(&self,
1417+
hcx: &mut StableHashingContext<'a>,
1418+
hasher: &mut StableHasher<W>) {
1419+
use traits::FromEnv::*;
1420+
1421+
mem::discriminant(self).hash_stable(hcx, hasher);
1422+
match self {
1423+
Trait(trait_ref) => trait_ref.hash_stable(hcx, hasher),
1424+
Ty(ty) => ty.hash_stable(hcx, hasher),
13951425
}
13961426
}
13971427
}
@@ -1404,15 +1434,10 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::DomainGoal<'tcx>
14041434

14051435
mem::discriminant(self).hash_stable(hcx, hasher);
14061436
match self {
1407-
Holds(where_clause) |
1408-
WellFormed(where_clause) |
1409-
FromEnv(where_clause) => where_clause.hash_stable(hcx, hasher),
1410-
1411-
WellFormedTy(ty) => ty.hash_stable(hcx, hasher),
1437+
Holds(wc) => wc.hash_stable(hcx, hasher),
1438+
WellFormed(wf) => wf.hash_stable(hcx, hasher),
1439+
FromEnv(from_env) => from_env.hash_stable(hcx, hasher),
14121440
Normalize(projection) => projection.hash_stable(hcx, hasher),
1413-
FromEnvTy(ty) => ty.hash_stable(hcx, hasher),
1414-
RegionOutlives(predicate) => predicate.hash_stable(hcx, hasher),
1415-
TypeOutlives(predicate) => predicate.hash_stable(hcx, hasher),
14161441
}
14171442
}
14181443
}

src/librustc/traits/mod.rs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -269,29 +269,41 @@ pub type PredicateObligations<'tcx> = Vec<PredicateObligation<'tcx>>;
269269
pub type TraitObligations<'tcx> = Vec<TraitObligation<'tcx>>;
270270

271271
/// The following types:
272-
/// * `WhereClauseAtom`
272+
/// * `WhereClause`
273+
/// * `WellFormed`
274+
/// * `FromEnv`
273275
/// * `DomainGoal`
274276
/// * `Goal`
275277
/// * `Clause`
276278
/// are used for representing the trait system in the form of
277279
/// logic programming clauses. They are part of the interface
278280
/// for the chalk SLG solver.
279281
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
280-
pub enum WhereClauseAtom<'tcx> {
282+
pub enum WhereClause<'tcx> {
281283
Implemented(ty::TraitPredicate<'tcx>),
282284
ProjectionEq(ty::ProjectionPredicate<'tcx>),
285+
RegionOutlives(ty::RegionOutlivesPredicate<'tcx>),
286+
TypeOutlives(ty::TypeOutlivesPredicate<'tcx>),
287+
}
288+
289+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
290+
pub enum WellFormed<'tcx> {
291+
Trait(ty::TraitPredicate<'tcx>),
292+
Ty(Ty<'tcx>),
293+
}
294+
295+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
296+
pub enum FromEnv<'tcx> {
297+
Trait(ty::TraitPredicate<'tcx>),
298+
Ty(Ty<'tcx>),
283299
}
284300

285301
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
286302
pub enum DomainGoal<'tcx> {
287-
Holds(WhereClauseAtom<'tcx>),
288-
WellFormed(WhereClauseAtom<'tcx>),
289-
FromEnv(WhereClauseAtom<'tcx>),
290-
WellFormedTy(Ty<'tcx>),
303+
Holds(WhereClause<'tcx>),
304+
WellFormed(WellFormed<'tcx>),
305+
FromEnv(FromEnv<'tcx>),
291306
Normalize(ty::ProjectionPredicate<'tcx>),
292-
FromEnvTy(Ty<'tcx>),
293-
RegionOutlives(ty::RegionOutlivesPredicate<'tcx>),
294-
TypeOutlives(ty::TypeOutlivesPredicate<'tcx>),
295307
}
296308

297309
pub type PolyDomainGoal<'tcx> = ty::Binder<DomainGoal<'tcx>>;
@@ -314,27 +326,27 @@ pub enum Goal<'tcx> {
314326

315327
pub type Goals<'tcx> = &'tcx Slice<Goal<'tcx>>;
316328

329+
impl<'tcx> DomainGoal<'tcx> {
330+
pub fn into_goal(self) -> Goal<'tcx> {
331+
Goal::DomainGoal(self)
332+
}
333+
}
334+
317335
impl<'tcx> Goal<'tcx> {
318336
pub fn from_poly_domain_goal<'a>(
319337
domain_goal: PolyDomainGoal<'tcx>,
320338
tcx: TyCtxt<'a, 'tcx, 'tcx>,
321339
) -> Goal<'tcx> {
322340
match domain_goal.no_late_bound_regions() {
323-
Some(p) => p.into(),
341+
Some(p) => p.into_goal(),
324342
None => Goal::Quantified(
325343
QuantifierKind::Universal,
326-
domain_goal.map_bound(|p| tcx.mk_goal(Goal::from(p)))
344+
domain_goal.map_bound(|p| tcx.mk_goal(p.into_goal()))
327345
),
328346
}
329347
}
330348
}
331349

332-
impl<'tcx> From<DomainGoal<'tcx>> for Goal<'tcx> {
333-
fn from(domain_goal: DomainGoal<'tcx>) -> Self {
334-
Goal::DomainGoal(domain_goal)
335-
}
336-
}
337-
338350
/// This matches the definition from Page 7 of "A Proof Procedure for the Logic of Hereditary
339351
/// Harrop Formulas".
340352
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]

src/librustc/traits/structural_impls.rs

Lines changed: 73 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -405,33 +405,50 @@ BraceStructTypeFoldableImpl! {
405405
} where T: TypeFoldable<'tcx>
406406
}
407407

408-
impl<'tcx> fmt::Display for traits::WhereClauseAtom<'tcx> {
408+
impl<'tcx> fmt::Display for traits::WhereClause<'tcx> {
409409
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
410-
use traits::WhereClauseAtom::*;
410+
use traits::WhereClause::*;
411411

412412
match self {
413413
Implemented(trait_ref) => write!(fmt, "Implemented({})", trait_ref),
414414
ProjectionEq(projection) => write!(fmt, "ProjectionEq({})", projection),
415+
RegionOutlives(predicate) => write!(fmt, "RegionOutlives({})", predicate),
416+
TypeOutlives(predicate) => write!(fmt, "TypeOutlives({})", predicate),
417+
}
418+
}
419+
}
420+
421+
impl<'tcx> fmt::Display for traits::WellFormed<'tcx> {
422+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
423+
use traits::WellFormed::*;
424+
425+
match self {
426+
Trait(trait_ref) => write!(fmt, "WellFormed({})", trait_ref),
427+
Ty(ty) => write!(fmt, "WellFormed({})", ty),
428+
}
429+
}
430+
}
431+
432+
impl<'tcx> fmt::Display for traits::FromEnv<'tcx> {
433+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
434+
use traits::FromEnv::*;
435+
436+
match self {
437+
Trait(trait_ref) => write!(fmt, "FromEnv({})", trait_ref),
438+
Ty(ty) => write!(fmt, "FromEnv({})", ty),
415439
}
416440
}
417441
}
418442

419443
impl<'tcx> fmt::Display for traits::DomainGoal<'tcx> {
420444
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
421445
use traits::DomainGoal::*;
422-
use traits::WhereClauseAtom::*;
423446

424447
match self {
425448
Holds(wc) => write!(fmt, "{}", wc),
426-
WellFormed(Implemented(trait_ref)) => write!(fmt, "WellFormed({})", trait_ref),
427-
WellFormed(ProjectionEq(projection)) => write!(fmt, "WellFormed({})", projection),
428-
FromEnv(Implemented(trait_ref)) => write!(fmt, "FromEnv({})", trait_ref),
429-
FromEnv(ProjectionEq(projection)) => write!(fmt, "FromEnv({})", projection),
430-
WellFormedTy(ty) => write!(fmt, "WellFormed({})", ty),
449+
WellFormed(wf) => write!(fmt, "{}", wf),
450+
FromEnv(from_env) => write!(fmt, "{}", from_env),
431451
Normalize(projection) => write!(fmt, "Normalize({})", projection),
432-
FromEnvTy(ty) => write!(fmt, "FromEnv({})", ty),
433-
RegionOutlives(predicate) => write!(fmt, "RegionOutlives({})", predicate),
434-
TypeOutlives(predicate) => write!(fmt, "TypeOutlives({})", predicate),
435452
}
436453
}
437454
}
@@ -506,44 +523,70 @@ impl<'tcx> fmt::Display for traits::Clause<'tcx> {
506523
}
507524

508525
EnumTypeFoldableImpl! {
509-
impl<'tcx> TypeFoldable<'tcx> for traits::WhereClauseAtom<'tcx> {
510-
(traits::WhereClauseAtom::Implemented)(trait_ref),
511-
(traits::WhereClauseAtom::ProjectionEq)(projection),
526+
impl<'tcx> TypeFoldable<'tcx> for traits::WhereClause<'tcx> {
527+
(traits::WhereClause::Implemented)(trait_ref),
528+
(traits::WhereClause::ProjectionEq)(projection),
529+
(traits::WhereClause::TypeOutlives)(ty_outlives),
530+
(traits::WhereClause::RegionOutlives)(region_outlives),
531+
}
532+
}
533+
534+
EnumLiftImpl! {
535+
impl<'a, 'tcx> Lift<'tcx> for traits::WhereClause<'a> {
536+
type Lifted = traits::WhereClause<'tcx>;
537+
(traits::WhereClause::Implemented)(trait_ref),
538+
(traits::WhereClause::ProjectionEq)(projection),
539+
(traits::WhereClause::TypeOutlives)(ty_outlives),
540+
(traits::WhereClause::RegionOutlives)(region_outlives),
541+
}
542+
}
543+
544+
EnumTypeFoldableImpl! {
545+
impl<'tcx> TypeFoldable<'tcx> for traits::WellFormed<'tcx> {
546+
(traits::WellFormed::Trait)(trait_ref),
547+
(traits::WellFormed::Ty)(ty),
548+
}
549+
}
550+
551+
EnumLiftImpl! {
552+
impl<'a, 'tcx> Lift<'tcx> for traits::WellFormed<'a> {
553+
type Lifted = traits::WellFormed<'tcx>;
554+
(traits::WellFormed::Trait)(trait_ref),
555+
(traits::WellFormed::Ty)(ty),
556+
}
557+
}
558+
559+
EnumTypeFoldableImpl! {
560+
impl<'tcx> TypeFoldable<'tcx> for traits::FromEnv<'tcx> {
561+
(traits::FromEnv::Trait)(trait_ref),
562+
(traits::FromEnv::Ty)(ty),
512563
}
513564
}
514565

515566
EnumLiftImpl! {
516-
impl<'a, 'tcx> Lift<'tcx> for traits::WhereClauseAtom<'a> {
517-
type Lifted = traits::WhereClauseAtom<'tcx>;
518-
(traits::WhereClauseAtom::Implemented)(trait_ref),
519-
(traits::WhereClauseAtom::ProjectionEq)(projection),
567+
impl<'a, 'tcx> Lift<'tcx> for traits::FromEnv<'a> {
568+
type Lifted = traits::FromEnv<'tcx>;
569+
(traits::FromEnv::Trait)(trait_ref),
570+
(traits::FromEnv::Ty)(ty),
520571
}
521572
}
522573

523574
EnumTypeFoldableImpl! {
524575
impl<'tcx> TypeFoldable<'tcx> for traits::DomainGoal<'tcx> {
525576
(traits::DomainGoal::Holds)(wc),
526-
(traits::DomainGoal::WellFormed)(wc),
527-
(traits::DomainGoal::FromEnv)(wc),
528-
(traits::DomainGoal::WellFormedTy)(ty),
577+
(traits::DomainGoal::WellFormed)(wf),
578+
(traits::DomainGoal::FromEnv)(from_env),
529579
(traits::DomainGoal::Normalize)(projection),
530-
(traits::DomainGoal::FromEnvTy)(ty),
531-
(traits::DomainGoal::RegionOutlives)(predicate),
532-
(traits::DomainGoal::TypeOutlives)(predicate),
533580
}
534581
}
535582

536583
EnumLiftImpl! {
537584
impl<'a, 'tcx> Lift<'tcx> for traits::DomainGoal<'a> {
538585
type Lifted = traits::DomainGoal<'tcx>;
539586
(traits::DomainGoal::Holds)(wc),
540-
(traits::DomainGoal::WellFormed)(wc),
541-
(traits::DomainGoal::FromEnv)(wc),
542-
(traits::DomainGoal::WellFormedTy)(ty),
587+
(traits::DomainGoal::WellFormed)(wf),
588+
(traits::DomainGoal::FromEnv)(from_env),
543589
(traits::DomainGoal::Normalize)(projection),
544-
(traits::DomainGoal::FromEnvTy)(ty),
545-
(traits::DomainGoal::RegionOutlives)(predicate),
546-
(traits::DomainGoal::TypeOutlives)(predicate),
547590
}
548591
}
549592

src/librustc_traits/chalk_context.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@ use rustc::infer::canonical::{
1414
Canonical, CanonicalVarValues, Canonicalize, QueryRegionConstraint, QueryResult,
1515
};
1616
use rustc::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime};
17-
use rustc::traits::{DomainGoal, ExClauseFold, ExClauseLift, Goal, ProgramClause, QuantifierKind};
17+
use rustc::traits::{
18+
WellFormed,
19+
FromEnv,
20+
DomainGoal,
21+
ExClauseFold,
22+
ExClauseLift,
23+
Goal,
24+
ProgramClause,
25+
QuantifierKind
26+
};
1827
use rustc::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
1928
use rustc::ty::subst::Kind;
2029
use rustc::ty::{self, TyCtxt};
@@ -314,43 +323,42 @@ impl context::UnificationOps<ChalkArenas<'gcx>, ChalkArenas<'tcx>>
314323
_environment: &ty::ParamEnv<'tcx>,
315324
goal: &DomainGoal<'tcx>,
316325
) -> Vec<ProgramClause<'tcx>> {
317-
use rustc::traits::DomainGoal::*;
318-
use rustc::traits::WhereClauseAtom::*;
326+
use rustc::traits::WhereClause::*;
319327

320328
match goal {
321-
Holds(Implemented(_trait_predicate)) => {
329+
DomainGoal::Holds(Implemented(_trait_predicate)) => {
322330
// These come from:
323331
//
324332
// - Trait definitions (implied bounds)
325333
// - Implementations of the trait itself
326334
panic!()
327335
}
328336

329-
Holds(ProjectionEq(_projection_predicate)) => {
337+
DomainGoal::Holds(ProjectionEq(_projection_predicate)) => {
330338
// These come from:
331339
panic!()
332340
}
333341

334-
WellFormed(Implemented(_trait_predicate)) => {
335-
// These come from -- the trait decl.
342+
DomainGoal::Holds(RegionOutlives(_region_outlives)) => {
336343
panic!()
337344
}
338345

339-
WellFormed(ProjectionEq(_projection_predicate)) => panic!(),
340-
341-
FromEnv(Implemented(_trait_predicate)) => panic!(),
342-
343-
FromEnv(ProjectionEq(_projection_predicate)) => panic!(),
346+
DomainGoal::Holds(TypeOutlives(_type_outlives)) => {
347+
panic!()
348+
}
344349

345-
WellFormedTy(_ty) => panic!(),
350+
DomainGoal::WellFormed(WellFormed::Trait(_trait_predicate)) => {
351+
// These come from -- the trait decl.
352+
panic!()
353+
}
346354

347-
FromEnvTy(_ty) => panic!(),
355+
DomainGoal::WellFormed(WellFormed::Ty(_ty)) => panic!(),
348356

349-
RegionOutlives(_region_outlives) => panic!(),
357+
DomainGoal::FromEnv(FromEnv::Trait(_trait_predicate)) => panic!(),
350358

351-
TypeOutlives(_type_outlives) => panic!(),
359+
DomainGoal::FromEnv(FromEnv::Ty(_ty)) => panic!(),
352360

353-
Normalize(_) => panic!(),
361+
DomainGoal::Normalize(_) => panic!(),
354362
}
355363
}
356364

0 commit comments

Comments
 (0)