Skip to content

Commit a1931d3

Browse files
committed
Categorize chalk clauses
1 parent e192608 commit a1931d3

File tree

5 files changed

+55
-8
lines changed

5 files changed

+55
-8
lines changed

src/librustc/ich/impls_ty.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1395,10 +1395,16 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::Goal<'tcx> {
13951395

13961396
impl_stable_hash_for!(
13971397
impl<'tcx> for struct traits::ProgramClause<'tcx> {
1398-
goal, hypotheses
1398+
goal, hypotheses, category
13991399
}
14001400
);
14011401

1402+
impl_stable_hash_for!(enum traits::ProgramClauseCategory {
1403+
ImpliedBound,
1404+
WellFormed,
1405+
Other,
1406+
});
1407+
14021408
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::Clause<'tcx> {
14031409
fn hash_stable<W: StableHasherResult>(&self,
14041410
hcx: &mut StableHashingContext<'a>,

src/librustc/traits/mod.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,8 @@ impl<'tcx> DomainGoal<'tcx> {
341341
pub fn into_program_clause(self) -> ProgramClause<'tcx> {
342342
ProgramClause {
343343
goal: self,
344-
hypotheses: &ty::List::empty(),
344+
hypotheses: ty::List::empty(),
345+
category: ProgramClauseCategory::Other,
345346
}
346347
}
347348
}
@@ -369,6 +370,15 @@ pub enum Clause<'tcx> {
369370
ForAll(ty::Binder<ProgramClause<'tcx>>),
370371
}
371372

373+
impl Clause<'tcx> {
374+
pub fn category(self) -> ProgramClauseCategory {
375+
match self {
376+
Clause::Implies(clause) => clause.category,
377+
Clause::ForAll(clause) => clause.skip_binder().category,
378+
}
379+
}
380+
}
381+
372382
/// Multiple clauses.
373383
pub type Clauses<'tcx> = &'tcx List<Clause<'tcx>>;
374384

@@ -385,6 +395,16 @@ pub struct ProgramClause<'tcx> {
385395

386396
/// ...if we can prove these hypotheses (there may be no hypotheses at all):
387397
pub hypotheses: Goals<'tcx>,
398+
399+
/// Useful for filtering clauses.
400+
pub category: ProgramClauseCategory,
401+
}
402+
403+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
404+
pub enum ProgramClauseCategory {
405+
ImpliedBound,
406+
WellFormed,
407+
Other,
388408
}
389409

390410
/// A set of clauses that we assume to be true.

src/librustc/traits/structural_impls.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ impl<'tcx> fmt::Display for traits::Goal<'tcx> {
496496

497497
impl<'tcx> fmt::Display for traits::ProgramClause<'tcx> {
498498
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
499-
let traits::ProgramClause { goal, hypotheses } = self;
499+
let traits::ProgramClause { goal, hypotheses, .. } = self;
500500
write!(fmt, "{}", goal)?;
501501
if !hypotheses.is_empty() {
502502
write!(fmt, " :- ")?;
@@ -647,10 +647,15 @@ impl<'tcx> TypeFoldable<'tcx> for traits::Goal<'tcx> {
647647
BraceStructTypeFoldableImpl! {
648648
impl<'tcx> TypeFoldable<'tcx> for traits::ProgramClause<'tcx> {
649649
goal,
650-
hypotheses
650+
hypotheses,
651+
category,
651652
}
652653
}
653654

655+
CloneTypeFoldableAndLiftImpls! {
656+
traits::ProgramClauseCategory,
657+
}
658+
654659
EnumTypeFoldableImpl! {
655660
impl<'tcx> TypeFoldable<'tcx> for traits::Clause<'tcx> {
656661
(traits::Clause::Implies)(clause),

src/librustc_traits/lowering/environment.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc::traits::{
1414
DomainGoal,
1515
FromEnv,
1616
ProgramClause,
17+
ProgramClauseCategory,
1718
Environment,
1819
};
1920
use rustc::ty::{self, TyCtxt, Ty};
@@ -39,6 +40,7 @@ impl ClauseVisitor<'set, 'a, 'tcx> {
3940
self.round.extend(
4041
self.tcx.program_clauses_for(data.item_def_id)
4142
.iter()
43+
.filter(|c| c.category() == ProgramClauseCategory::ImpliedBound)
4244
.cloned()
4345
);
4446
}
@@ -56,6 +58,7 @@ impl ClauseVisitor<'set, 'a, 'tcx> {
5658
self.round.extend(
5759
self.tcx.program_clauses_for(def.did)
5860
.iter()
61+
.filter(|c| c.category() == ProgramClauseCategory::ImpliedBound)
5962
.cloned()
6063
);
6164
}
@@ -68,6 +71,7 @@ impl ClauseVisitor<'set, 'a, 'tcx> {
6871
self.round.extend(
6972
self.tcx.program_clauses_for(def_id)
7073
.iter()
74+
.filter(|c| c.category() == ProgramClauseCategory::ImpliedBound)
7175
.cloned()
7276
);
7377
}
@@ -98,6 +102,7 @@ impl ClauseVisitor<'set, 'a, 'tcx> {
98102
self.round.extend(
99103
self.tcx.program_clauses_for(predicate.def_id())
100104
.iter()
105+
.filter(|c| c.category() == ProgramClauseCategory::ImpliedBound)
101106
.cloned()
102107
);
103108
}
@@ -176,7 +181,7 @@ crate fn environment<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> En
176181
// Compute the bounds on `Self` and the type parameters.
177182
let ty::InstantiatedPredicates { predicates } =
178183
tcx.predicates_of(def_id).instantiate_identity(tcx);
179-
184+
180185
let clauses = predicates.into_iter()
181186
.map(|predicate| predicate.lower())
182187
.map(|domain_goal| domain_goal.map_bound(|bound| bound.into_from_env_goal()))
@@ -185,7 +190,7 @@ crate fn environment<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> En
185190
// `ForAll` because each `domain_goal` is a `PolyDomainGoal` and
186191
// could bound lifetimes.
187192
.map(Clause::ForAll);
188-
193+
189194
let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
190195
let node = tcx.hir.get(node_id);
191196

@@ -243,7 +248,7 @@ crate fn environment<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> En
243248
.map(|domain_goal| domain_goal.into_program_clause())
244249
.map(Clause::Implies)
245250
);
246-
251+
247252
Environment {
248253
clauses: tcx.mk_clauses(clauses),
249254
}

src/librustc_traits/lowering/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use rustc::traits::{
2222
GoalKind,
2323
PolyDomainGoal,
2424
ProgramClause,
25+
ProgramClauseCategory,
2526
WellFormed,
2627
WhereClause,
2728
};
@@ -204,6 +205,7 @@ fn program_clauses_for_trait<'a, 'tcx>(
204205
let implemented_from_env = ProgramClause {
205206
goal: impl_trait,
206207
hypotheses,
208+
category: ProgramClauseCategory::ImpliedBound,
207209
};
208210

209211
let clauses = iter::once(Clause::ForAll(ty::Binder::dummy(implemented_from_env)));
@@ -231,6 +233,7 @@ fn program_clauses_for_trait<'a, 'tcx>(
231233
.map(|wc| wc.map_bound(|goal| ProgramClause {
232234
goal: goal.into_from_env_goal(),
233235
hypotheses,
236+
category: ProgramClauseCategory::ImpliedBound,
234237
}))
235238
.map(Clause::ForAll);
236239

@@ -257,6 +260,7 @@ fn program_clauses_for_trait<'a, 'tcx>(
257260
hypotheses: tcx.mk_goals(
258261
wf_conditions.map(|wc| tcx.mk_goal(GoalKind::from_poly_domain_goal(wc, tcx))),
259262
),
263+
category: ProgramClauseCategory::WellFormed,
260264
};
261265
let wf_clause = iter::once(Clause::ForAll(ty::Binder::dummy(wf_clause)));
262266

@@ -299,6 +303,7 @@ fn program_clauses_for_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId
299303
where_clauses
300304
.map(|wc| tcx.mk_goal(GoalKind::from_poly_domain_goal(wc, tcx))),
301305
),
306+
category: ProgramClauseCategory::Other,
302307
};
303308
tcx.mk_clauses(iter::once(Clause::ForAll(ty::Binder::dummy(clause))))
304309
}
@@ -335,6 +340,7 @@ pub fn program_clauses_for_type_def<'a, 'tcx>(
335340
.cloned()
336341
.map(|wc| tcx.mk_goal(GoalKind::from_poly_domain_goal(wc, tcx))),
337342
),
343+
category: ProgramClauseCategory::WellFormed,
338344
};
339345

340346
let well_formed_clause = iter::once(Clause::ForAll(ty::Binder::dummy(well_formed)));
@@ -360,6 +366,7 @@ pub fn program_clauses_for_type_def<'a, 'tcx>(
360366
.map(|wc| wc.map_bound(|goal| ProgramClause {
361367
goal: goal.into_from_env_goal(),
362368
hypotheses,
369+
category: ProgramClauseCategory::ImpliedBound,
363370
}))
364371

365372
.map(Clause::ForAll);
@@ -407,7 +414,8 @@ pub fn program_clauses_for_associated_type_def<'a, 'tcx>(
407414

408415
let projection_eq_clause = ProgramClause {
409416
goal: DomainGoal::Holds(projection_eq),
410-
hypotheses: &ty::List::empty(),
417+
hypotheses: ty::List::empty(),
418+
category: ProgramClauseCategory::Other,
411419
};
412420

413421
// Rule WellFormed-AssocTy
@@ -425,6 +433,7 @@ pub fn program_clauses_for_associated_type_def<'a, 'tcx>(
425433
let wf_clause = ProgramClause {
426434
goal: DomainGoal::WellFormed(WellFormed::Ty(placeholder_ty)),
427435
hypotheses: tcx.mk_goals(iter::once(hypothesis)),
436+
category: ProgramClauseCategory::Other,
428437
};
429438

430439
// Rule Implied-Trait-From-AssocTy
@@ -441,6 +450,7 @@ pub fn program_clauses_for_associated_type_def<'a, 'tcx>(
441450
let from_env_clause = ProgramClause {
442451
goal: DomainGoal::FromEnv(FromEnv::Trait(trait_predicate)),
443452
hypotheses: tcx.mk_goals(iter::once(hypothesis)),
453+
category: ProgramClauseCategory::ImpliedBound,
444454
};
445455

446456
let clauses = iter::once(projection_eq_clause)
@@ -506,6 +516,7 @@ pub fn program_clauses_for_associated_type_value<'a, 'tcx>(
506516
.into_iter()
507517
.map(|wc| tcx.mk_goal(GoalKind::from_poly_domain_goal(wc, tcx))),
508518
),
519+
category: ProgramClauseCategory::Other,
509520
};
510521
tcx.mk_clauses(iter::once(Clause::ForAll(ty::Binder::dummy(clause))))
511522
}

0 commit comments

Comments
 (0)