10 use rustc::ty::{self, TyCtxt, Ty};
11 use rustc::hir::def_id::DefId;
12 use rustc_data_structures::fx::FxHashSet;
14 struct ClauseVisitor<'a, 'tcx> {
16 round: &'a mut FxHashSet<Clause<'tcx>>,
19 impl ClauseVisitor<'a, 'tcx> {
20 fn new(tcx: TyCtxt<'tcx>, round: &'a mut FxHashSet<Clause<'tcx>>) -> Self {
27 fn visit_ty(&mut self, ty: Ty<'tcx>) {
29 ty::Projection(data) => {
31 self.tcx.program_clauses_for(data.item_def_id)
33 .filter(|c| c.category() == ProgramClauseCategory::ImpliedBound)
39 // FIXME: trait object rules are not yet implemented
44 self.tcx.program_clauses_for(def.did)
46 .filter(|c| c.category() == ProgramClauseCategory::ImpliedBound)
52 ty::FnDef(def_id, ..) |
53 ty::Closure(def_id, ..) |
54 ty::Generator(def_id, ..) |
55 ty::Opaque(def_id, ..) => {
57 self.tcx.program_clauses_for(def_id)
59 .filter(|c| c.category() == ProgramClauseCategory::ImpliedBound)
82 ty::GeneratorWitness(..) |
83 ty::UnnormalizedProjection(..) |
85 bug!("unexpected type {:?}", ty);
90 fn visit_from_env(&mut self, from_env: FromEnv<'tcx>) {
92 FromEnv::Trait(predicate) => {
94 self.tcx.program_clauses_for(predicate.def_id())
96 .filter(|c| c.category() == ProgramClauseCategory::ImpliedBound)
101 FromEnv::Ty(ty) => self.visit_ty(ty),
105 fn visit_domain_goal(&mut self, domain_goal: DomainGoal<'tcx>) {
106 // The only domain goals we can find in an environment are:
107 // * `DomainGoal::Holds(..)`
108 // * `DomainGoal::FromEnv(..)`
109 // The former do not lead to any implied bounds. So we only need
110 // to visit the latter.
111 if let DomainGoal::FromEnv(from_env) = domain_goal {
112 self.visit_from_env(from_env);
116 fn visit_program_clause(&mut self, clause: ProgramClause<'tcx>) {
117 self.visit_domain_goal(clause.goal);
118 // No need to visit `clause.hypotheses`: they are always of the form
119 // `FromEnv(...)` and were visited at a previous round.
122 fn visit_clause(&mut self, clause: Clause<'tcx>) {
124 Clause::Implies(clause) => self.visit_program_clause(clause),
125 Clause::ForAll(clause) => self.visit_program_clause(*clause.skip_binder()),
130 crate fn program_clauses_for_env<'tcx>(
132 environment: Environment<'tcx>,
134 debug!("program_clauses_for_env(environment={:?})", environment);
136 let mut last_round = FxHashSet::default();
138 let mut visitor = ClauseVisitor::new(tcx, &mut last_round);
139 for &clause in environment.clauses {
140 visitor.visit_clause(clause);
144 let mut closure = last_round.clone();
145 let mut next_round = FxHashSet::default();
146 while !last_round.is_empty() {
147 let mut visitor = ClauseVisitor::new(tcx, &mut next_round);
148 for clause in last_round.drain() {
149 visitor.visit_clause(clause);
152 next_round.drain().filter(|&clause| closure.insert(clause))
156 debug!("program_clauses_for_env: closure = {:#?}", closure);
158 return tcx.mk_clauses(
163 crate fn environment<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Environment<'tcx> {
164 use super::{Lower, IntoFromEnvGoal};
165 use rustc::hir::{Node, TraitItemKind, ImplItemKind, ItemKind, ForeignItemKind};
167 debug!("environment(def_id = {:?})", def_id);
169 // The environment of an impl Trait type is its defining function's environment.
170 if let Some(parent) = ty::is_impl_trait_defn(tcx, def_id) {
171 return environment(tcx, parent);
174 // Compute the bounds on `Self` and the type parameters.
175 let ty::InstantiatedPredicates { predicates } = tcx.predicates_of(def_id)
176 .instantiate_identity(tcx);
178 let clauses = predicates.into_iter()
179 .map(|predicate| predicate.lower())
180 .map(|domain_goal| domain_goal.map_bound(|bound| bound.into_from_env_goal()))
181 .map(|domain_goal| domain_goal.map_bound(|bound| bound.into_program_clause()))
183 // `ForAll` because each `domain_goal` is a `PolyDomainGoal` and
184 // could bound lifetimes.
185 .map(Clause::ForAll);
187 let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
188 let node = tcx.hir().get_by_hir_id(hir_id);
197 let node_kind = match node {
198 Node::TraitItem(item) => match item.node {
199 TraitItemKind::Method(..) => NodeKind::Fn,
200 _ => NodeKind::Other,
203 Node::ImplItem(item) => match item.node {
204 ImplItemKind::Method(..) => NodeKind::Fn,
205 _ => NodeKind::Other,
208 Node::Item(item) => match item.node {
209 ItemKind::Impl(.., Some(..), _, _) => NodeKind::TraitImpl,
210 ItemKind::Impl(.., None, _, _) => NodeKind::InherentImpl,
211 ItemKind::Fn(..) => NodeKind::Fn,
212 _ => NodeKind::Other,
215 Node::ForeignItem(item) => match item.node {
216 ForeignItemKind::Fn(..) => NodeKind::Fn,
217 _ => NodeKind::Other,
221 _ => NodeKind::Other,
224 let mut input_tys = FxHashSet::default();
227 // In a trait impl, we assume that the header trait ref and all its
228 // constituents are well-formed.
229 NodeKind::TraitImpl => {
230 let trait_ref = tcx.impl_trait_ref(def_id)
231 .expect("not an impl");
234 trait_ref.input_types().flat_map(|ty| ty.walk())
238 // In an inherent impl, we assume that the receiver type and all its
239 // constituents are well-formed.
240 NodeKind::InherentImpl => {
241 let self_ty = tcx.type_of(def_id);
242 input_tys.extend(self_ty.walk());
245 // In an fn, we assume that the arguments and all their constituents are
248 let fn_sig = tcx.fn_sig(def_id);
249 let fn_sig = tcx.liberate_late_bound_regions(def_id, &fn_sig);
252 fn_sig.inputs().iter().flat_map(|ty| ty.walk())
256 NodeKind::Other => (),
259 let clauses = clauses.chain(
260 input_tys.into_iter()
261 .map(|ty| DomainGoal::FromEnv(FromEnv::Ty(ty)))
262 .map(|domain_goal| domain_goal.into_program_clause())
263 .map(Clause::Implies)
266 debug!("environment: clauses = {:?}", clauses);
269 clauses: tcx.mk_clauses(clauses),