Skip to content

Commit 0b8d817

Browse files
committed
Auto merge of #51433 - scalexm:finish-rules, r=nikomatsakis
[chalkify] Small refactoring and WF/FromEnv rules for types r? @nikomatsakis
2 parents ec60dd8 + 796ded2 commit 0b8d817

File tree

8 files changed

+335
-139
lines changed

8 files changed

+335
-139
lines changed

src/librustc/ich/impls_ty.rs

+35-10
Original file line numberDiff line numberDiff line change
@@ -1373,16 +1373,46 @@ impl_stable_hash_for!(enum infer::canonical::Certainty {
13731373
Proven, Ambiguous
13741374
});
13751375

1376-
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::WhereClauseAtom<'tcx> {
1376+
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::WhereClause<'tcx> {
13771377
fn hash_stable<W: StableHasherResult>(&self,
13781378
hcx: &mut StableHashingContext<'a>,
13791379
hasher: &mut StableHasher<W>) {
1380-
use traits::WhereClauseAtom::*;
1380+
use traits::WhereClause::*;
13811381

13821382
mem::discriminant(self).hash_stable(hcx, hasher);
13831383
match self {
13841384
Implemented(trait_ref) => trait_ref.hash_stable(hcx, hasher),
13851385
ProjectionEq(projection) => projection.hash_stable(hcx, hasher),
1386+
TypeOutlives(ty_outlives) => ty_outlives.hash_stable(hcx, hasher),
1387+
RegionOutlives(region_outlives) => region_outlives.hash_stable(hcx, hasher),
1388+
}
1389+
}
1390+
}
1391+
1392+
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::WellFormed<'tcx> {
1393+
fn hash_stable<W: StableHasherResult>(&self,
1394+
hcx: &mut StableHashingContext<'a>,
1395+
hasher: &mut StableHasher<W>) {
1396+
use traits::WellFormed::*;
1397+
1398+
mem::discriminant(self).hash_stable(hcx, hasher);
1399+
match self {
1400+
Trait(trait_ref) => trait_ref.hash_stable(hcx, hasher),
1401+
Ty(ty) => ty.hash_stable(hcx, hasher),
1402+
}
1403+
}
1404+
}
1405+
1406+
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::FromEnv<'tcx> {
1407+
fn hash_stable<W: StableHasherResult>(&self,
1408+
hcx: &mut StableHashingContext<'a>,
1409+
hasher: &mut StableHasher<W>) {
1410+
use traits::FromEnv::*;
1411+
1412+
mem::discriminant(self).hash_stable(hcx, hasher);
1413+
match self {
1414+
Trait(trait_ref) => trait_ref.hash_stable(hcx, hasher),
1415+
Ty(ty) => ty.hash_stable(hcx, hasher),
13861416
}
13871417
}
13881418
}
@@ -1395,15 +1425,10 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::DomainGoal<'tcx>
13951425

13961426
mem::discriminant(self).hash_stable(hcx, hasher);
13971427
match self {
1398-
Holds(where_clause) |
1399-
WellFormed(where_clause) |
1400-
FromEnv(where_clause) => where_clause.hash_stable(hcx, hasher),
1401-
1402-
WellFormedTy(ty) => ty.hash_stable(hcx, hasher),
1428+
Holds(wc) => wc.hash_stable(hcx, hasher),
1429+
WellFormed(wf) => wf.hash_stable(hcx, hasher),
1430+
FromEnv(from_env) => from_env.hash_stable(hcx, hasher),
14031431
Normalize(projection) => projection.hash_stable(hcx, hasher),
1404-
FromEnvTy(ty) => ty.hash_stable(hcx, hasher),
1405-
RegionOutlives(predicate) => predicate.hash_stable(hcx, hasher),
1406-
TypeOutlives(predicate) => predicate.hash_stable(hcx, hasher),
14071432
}
14081433
}
14091434
}

src/librustc/traits/mod.rs

+29-17
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

+73-30
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

+25-17
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)