Skip to content

Commit b8c75d9

Browse files
committed
Implement Rule Implemented-From-Env
This extends the Chalk lowering pass with the "Implemented-From-Env" rule for generating program clauses from a trait definition as part of rust-lang#49177.
1 parent de9e665 commit b8c75d9

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/librustc_traits/lowering.rs

+32
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc::hir::{self, ImplPolarity};
1212
use rustc::hir::def_id::DefId;
1313
use rustc::hir::intravisit::{self, NestedVisitorMap, Visitor};
1414
use rustc::ty::{self, TyCtxt};
15+
use rustc::ty::subst::Substs;
1516
use rustc::traits::{QuantifierKind, Goal, DomainGoal, Clause, WhereClauseAtom};
1617
use syntax::ast;
1718
use rustc_data_structures::sync::Lrc;
@@ -104,13 +105,44 @@ crate fn program_clauses_for<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefI
104105
let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
105106
let item = tcx.hir.expect_item(node_id);
106107
match item.node {
108+
hir::ItemTrait(..) => program_clauses_for_trait(tcx, def_id),
107109
hir::ItemImpl(..) => program_clauses_for_impl(tcx, def_id),
108110

109111
// FIXME: other constructions e.g. traits, associated types...
110112
_ => Lrc::new(vec![]),
111113
}
112114
}
113115

116+
fn program_clauses_for_trait<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
117+
-> Lrc<Vec<Clause<'tcx>>>
118+
{
119+
// Rule Implemented-From-Env (see rustc guide)
120+
//
121+
// `trait Trait<P1..Pn> where WC { .. } // P0 == Self`
122+
//
123+
// ```
124+
// forall<Self, P1..Pn> {
125+
// Implemented(Self: Trait<P1..Pn>) :- FromEnv(Self: Trait<P1..Pn>)
126+
// }
127+
// ```
128+
129+
// `Self: Trait<P1..Pn>`
130+
let trait_pred = ty::TraitPredicate {
131+
trait_ref: ty::TraitRef {
132+
def_id,
133+
substs: Substs::identity_for_item(tcx, def_id)
134+
}
135+
};
136+
// `FromEnv(Self: Trait<P1..Pn>)`
137+
let from_env = Goal::DomainGoal(DomainGoal::FromEnv(trait_pred.lower()));
138+
// `Implemented(Self: Trait<P1..Pn>)`
139+
let impl_trait = DomainGoal::Holds(WhereClauseAtom::Implemented(trait_pred));
140+
141+
// `Implemented(Self: Trait<P1..Pn>) :- FromEnv(Self: Trait<P1..Pn>)`
142+
let clause = Clause::Implies(vec![from_env], impl_trait);
143+
Lrc::new(vec![clause])
144+
}
145+
114146
fn program_clauses_for_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
115147
-> Lrc<Vec<Clause<'tcx>>>
116148
{

0 commit comments

Comments
 (0)