10 ProgramClauseCategory,
14 use rustc::hir::def_id::DefId;
15 use super::ChalkInferenceContext;
18 use self::primitive::*;
21 fn assemble_clauses_from_impls<'tcx>(
22 tcx: ty::TyCtxt<'_, '_, 'tcx>,
24 clauses: &mut Vec<Clause<'tcx>>
26 tcx.for_each_impl(trait_def_id, |impl_def_id| {
28 tcx.program_clauses_for(impl_def_id)
35 fn assemble_clauses_from_assoc_ty_values<'tcx>(
36 tcx: ty::TyCtxt<'_, '_, 'tcx>,
38 clauses: &mut Vec<Clause<'tcx>>
40 tcx.for_each_impl(trait_def_id, |impl_def_id| {
41 for def_id in tcx.associated_item_def_ids(impl_def_id).iter() {
43 tcx.program_clauses_for(*def_id)
51 impl ChalkInferenceContext<'cx, 'gcx, 'tcx> {
52 pub(super) fn program_clauses_impl(
54 environment: &Environment<'tcx>,
55 goal: &DomainGoal<'tcx>,
56 ) -> Vec<Clause<'tcx>> {
57 use rustc::traits::WhereClause::*;
58 use rustc::infer::canonical::OriginalQueryValues;
60 let goal = self.infcx.resolve_type_vars_if_possible(goal);
62 debug!("program_clauses(goal = {:?})", goal);
64 let mut clauses = match goal {
65 DomainGoal::Holds(Implemented(trait_predicate)) => {
67 // * implementations of the trait itself (rule `Implemented-From-Impl`)
68 // * the trait decl (rule `Implemented-From-Env`)
70 let mut clauses = vec![];
72 assemble_clauses_from_impls(
74 trait_predicate.def_id(),
78 if Some(trait_predicate.def_id()) == self.infcx.tcx.lang_items().sized_trait() {
79 assemble_builtin_sized_impls(
81 trait_predicate.def_id(),
82 trait_predicate.self_ty(),
87 if Some(trait_predicate.def_id()) == self.infcx.tcx.lang_items().unsize_trait() {
88 let source = trait_predicate.self_ty();
89 let target = trait_predicate.trait_ref.substs.type_at(1);
90 assemble_builtin_unsize_impls(
92 trait_predicate.def_id(),
99 // FIXME: we need to add special rules for other builtin impls:
100 // * `Copy` / `Clone`
102 // * `FnOnce` / `FnMut` / `Fn`
106 // Rule `Implemented-From-Env` will be computed from the environment.
110 DomainGoal::Holds(ProjectionEq(projection_predicate)) => {
112 // * the assoc type definition (rule `ProjectionEq-Placeholder`)
113 // * normalization of the assoc ty values (rule `ProjectionEq-Normalize`)
114 // * implied bounds from trait definitions (rule `Implied-Bound-From-Trait`)
115 // * implied bounds from type definitions (rule `Implied-Bound-From-Type`)
117 let clauses = self.infcx.tcx.program_clauses_for(
118 projection_predicate.projection_ty.item_def_id
121 // only select `ProjectionEq-Placeholder` and `ProjectionEq-Normalize`
122 .filter(|clause| clause.category() == ProgramClauseCategory::Other)
125 .collect::<Vec<_>>();
127 // Rules `Implied-Bound-From-Trait` and `Implied-Bound-From-Type` will be computed
128 // from the environment.
132 // For outlive requirements, just assume they hold. `ResolventOps::resolvent_clause`
133 // will register them as actual region constraints later.
134 DomainGoal::Holds(RegionOutlives(..)) | DomainGoal::Holds(TypeOutlives(..)) => {
135 vec![Clause::Implies(ProgramClause {
137 hypotheses: ty::List::empty(),
138 category: ProgramClauseCategory::Other,
142 DomainGoal::WellFormed(WellFormed::Trait(trait_predicate)) => {
143 // These come from -- the trait decl (rule `WellFormed-TraitRef`).
144 self.infcx.tcx.program_clauses_for(trait_predicate.def_id())
147 // only select `WellFormed-TraitRef`
148 .filter(|clause| clause.category() == ProgramClauseCategory::WellFormed)
154 DomainGoal::WellFormed(WellFormed::Ty(ty)) => {
156 // * the associated type definition if `ty` refers to an unnormalized
157 // associated type (rule `WellFormed-AssocTy`)
158 // * custom rules for built-in types
159 // * the type definition otherwise (rule `WellFormed-Type`)
160 let clauses = match ty.sty {
161 ty::Projection(data) => {
162 self.infcx.tcx.program_clauses_for(data.item_def_id)
165 // These types are always WF.
173 ty::Placeholder(..) |
176 let wf_clause = ProgramClause {
178 hypotheses: ty::List::empty(),
179 category: ProgramClauseCategory::WellFormed,
181 let wf_clause = Clause::Implies(wf_clause);
183 self.infcx.tcx.mk_clauses(iter::once(wf_clause))
186 // Always WF (recall that we do not check for parameters to be WF).
187 ty::RawPtr(ptr) => wf_clause_for_raw_ptr(self.infcx.tcx, ptr.mutbl),
189 // Always WF (recall that we do not check for parameters to be WF).
190 ty::FnPtr(fn_ptr) => {
191 let fn_ptr = fn_ptr.skip_binder();
192 wf_clause_for_fn_ptr(
194 fn_ptr.inputs_and_output.len(),
201 // WF if inner type is `Sized`.
202 ty::Slice(..) => wf_clause_for_slice(self.infcx.tcx),
204 // WF if inner type is `Sized`.
205 ty::Array(_, length) => wf_clause_for_array(self.infcx.tcx, length),
207 // WF if all types but the last one are `Sized`.
208 ty::Tuple(types) => wf_clause_for_tuple(
213 // WF if `sub_ty` outlives `region`.
214 ty::Ref(_, _, mutbl) => wf_clause_for_ref(self.infcx.tcx, mutbl),
216 ty::FnDef(def_id, ..) => wf_clause_for_fn_def(self.infcx.tcx, def_id),
219 // FIXME: no rules yet for trait objects
223 ty::Adt(def, ..) => {
224 self.infcx.tcx.program_clauses_for(def.did)
227 // FIXME: these are probably wrong
228 ty::Foreign(def_id) |
229 ty::Closure(def_id, ..) |
230 ty::Generator(def_id, ..) |
231 ty::Opaque(def_id, ..) => {
232 self.infcx.tcx.program_clauses_for(def_id)
235 // Artificially trigger an ambiguity.
237 let tcx = self.infcx.tcx;
238 let types = [tcx.types.i32, tcx.types.u32, tcx.types.f32, tcx.types.f64];
239 let clauses = types.iter()
241 .map(|ty| ProgramClause {
242 goal: DomainGoal::WellFormed(WellFormed::Ty(ty)),
243 hypotheses: ty::List::empty(),
244 category: ProgramClauseCategory::WellFormed,
246 .map(|clause| Clause::Implies(clause));
247 tcx.mk_clauses(clauses)
250 ty::GeneratorWitness(..) |
251 ty::UnnormalizedProjection(..) |
253 bug!("unexpected type {:?}", ty)
258 .filter(|clause| clause.category() == ProgramClauseCategory::WellFormed)
263 DomainGoal::FromEnv(FromEnv::Trait(..)) => {
265 // * implied bounds from trait definitions (rule `Implied-Bound-From-Trait`)
266 // * implied bounds from type definitions (rule `Implied-Bound-From-Type`)
267 // * implied bounds from assoc type defs (rules `Implied-Trait-From-AssocTy`,
268 // `Implied-Bound-From-AssocTy` and `Implied-WC-From-AssocTy`)
270 // All of these rules are computed in the environment.
274 DomainGoal::FromEnv(FromEnv::Ty(..)) => {
275 // There are no `FromEnv::Ty(..) :- ...` rules (this predicate only
276 // comes from the environment).
280 DomainGoal::Normalize(projection_predicate) => {
281 // These come from -- assoc ty values (rule `Normalize-From-Impl`).
282 let mut clauses = vec![];
284 assemble_clauses_from_assoc_ty_values(
286 projection_predicate.projection_ty.trait_ref(self.infcx.tcx).def_id,
294 debug!("program_clauses: clauses = {:?}", clauses);
295 debug!("program_clauses: adding clauses from environment = {:?}", environment);
297 let mut _orig_query_values = OriginalQueryValues::default();
298 let canonical_environment = self.infcx.canonicalize_query(
300 &mut _orig_query_values
302 let env_clauses = self.infcx.tcx.program_clauses_for_env(canonical_environment);
304 debug!("program_clauses: env_clauses = {:?}", env_clauses);
306 clauses.extend(env_clauses.into_iter().cloned());
307 clauses.extend(environment.clauses.iter().cloned());