Skip to content

Commit a6d4d2b

Browse files
committed
refactor
1 parent ff83ef0 commit a6d4d2b

File tree

1 file changed

+32
-35
lines changed

1 file changed

+32
-35
lines changed

src/librustc_traits/lowering.rs

+32-35
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ use rustc::hir::intravisit::{self, NestedVisitorMap, Visitor};
1313
use rustc::hir::map::definitions::DefPathData;
1414
use rustc::hir::{self, ImplPolarity};
1515
use rustc::traits::{
16-
Clause, Clauses, DomainGoal, Goal, PolyDomainGoal, ProgramClause, WhereClauseAtom,
16+
Clause, Clauses, DomainGoal, FromEnv, Goal, PolyDomainGoal, ProgramClause, WellFormed,
17+
WhereClause,
1718
};
18-
use rustc::ty::subst::Substs;
19+
use rustc::ty::query::Providers;
1920
use rustc::ty::{self, Slice, TyCtxt};
2021
use rustc_data_structures::fx::FxHashSet;
2122
use std::mem;
@@ -139,11 +140,13 @@ impl<'tcx> IntoFromEnvGoal for DomainGoal<'tcx> {
139140

140141
impl<'tcx> IntoWellFormedGoal for DomainGoal<'tcx> {
141142
fn into_wellformed_goal(self) -> DomainGoal<'tcx> {
142-
use self::DomainGoal::*;
143+
use self::WhereClause::*;
144+
143145
match self {
144-
Holds(wc_atom) => WellFormed(wc_atom),
145-
WellFormed(..) | FromEnv(..) | WellFormedTy(..) | FromEnvTy(..) | Normalize(..)
146-
| RegionOutlives(..) | TypeOutlives(..) => self,
146+
DomainGoal::Holds(Implemented(trait_ref)) => {
147+
DomainGoal::WellFormed(WellFormed::Trait(trait_ref))
148+
}
149+
other => other,
147150
}
148151
}
149152
}
@@ -256,6 +259,8 @@ fn program_clauses_for_trait<'a, 'tcx>(
256259

257260
let clauses = iter::once(Clause::ForAll(ty::Binder::dummy(implemented_from_env)));
258261

262+
let where_clauses = &tcx.predicates_defined_on(def_id).predicates;
263+
259264
// Rule Implied-Bound-From-Trait
260265
//
261266
// For each where clause WC:
@@ -266,7 +271,6 @@ fn program_clauses_for_trait<'a, 'tcx>(
266271
// ```
267272

268273
// `FromEnv(WC) :- FromEnv(Self: Trait<P1..Pn>)`, for each where clause WC
269-
let where_clauses = &tcx.predicates_defined_on(def_id).predicates;
270274
let implied_bound_clauses = where_clauses
271275
.into_iter()
272276
.map(|wc| wc.lower())
@@ -276,43 +280,36 @@ fn program_clauses_for_trait<'a, 'tcx>(
276280
goal: goal.into_from_env_goal(),
277281
hypotheses,
278282
}))
279-
.map(|wc| implied_bound_from_trait(tcx, trait_pred, wc));
280-
let wellformed_clauses = wellformed_from_bound(tcx, trait_pred, &where_clauses[1..]);
281-
tcx.mk_clauses(clauses.chain(implied_bound_clauses).chain(wellformed_clauses))
282-
}
283+
.map(Clause::ForAll);
283284

284-
fn wellformed_from_bound<'a, 'tcx>(
285-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
286-
trait_pred: ty::TraitPredicate<'tcx>,
287-
where_clauses: &[ty::Predicate<'tcx>],
288-
) -> iter::Once<Clause<'tcx>> {
289285
// Rule WellFormed-TraitRef
290286
//
291287
// For each where clause WC:
292288
// forall<Self, P1..Pn> {
293289
// WellFormed(Self: Trait<P1..Pn>) :- Implemented(Self: Trait<P1..Pn>) && WellFormed(WC)
294290
// }
295291

296-
// WellFormed(Self: Trait<P1..Pn>)
297-
let wellformed_trait = DomainGoal::WellFormed(WhereClauseAtom::Implemented(trait_pred));
298-
// Implemented(Self: Trait<P1..Pn>)
299-
let impl_trait = ty::Binder::dummy(DomainGoal::Holds(WhereClauseAtom::Implemented(trait_pred)));
300-
// WellFormed(WC)
301-
let wellformed_wc = where_clause
302-
.lower()
303-
.map_bound(|wc| wc.into_wellformed_goal());
304-
// Implemented(Self: Trait<P1..Pn>) && WellFormed(WC)
305-
let mut wcs = vec![impl_trait];
306-
wcs.extend(wellformed_wcs);
292+
let wellformed_clauses = where_clauses
293+
.into_iter()
294+
.map(|wc| wc.lower())
295+
// WellFormed(Self: Trait<P1..Pn>) :- Implemented(Self: Trait<P1..Pn>) && WellFormed(WC)
296+
.map(|wc| {
297+
wc.map_bound(|goal| ProgramClause {
298+
goal: goal.into_wellformed_goal(),
299+
hypotheses: tcx.mk_goals(
300+
where_clauses
301+
.into_iter()
302+
.map(|wc| Goal::from_poly_domain_goal(wc.lower(), tcx)),
303+
),
304+
})
305+
})
306+
.map(Clause::ForAll);
307307

308-
let clause = ProgramClause {
309-
goal: wellformed_trait,
310-
hypotheses: tcx.mk_goals(
311-
wcs.into_iter()
312-
.map(|wc| Goal::from_poly_domain_goal(wc, tcx)),
313-
),
314-
};
315-
iter::once(Clause::ForAll(ty::Binder::dummy(clause)))
308+
tcx.mk_clauses(
309+
clauses
310+
.chain(implied_bound_clauses)
311+
.chain(wellformed_clauses),
312+
)
316313
}
317314

318315
fn program_clauses_for_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Clauses<'tcx> {

0 commit comments

Comments
 (0)