1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use mir::interpret::{GlobalId, ErrorHandled};
13 use ty::{self, Ty, TypeFoldable, ToPolyTraitRef, ToPredicate};
14 use ty::error::ExpectedFound;
15 use rustc_data_structures::obligation_forest::{DoCompleted, Error, ForestObligation};
16 use rustc_data_structures::obligation_forest::{ObligationForest, ObligationProcessor};
17 use rustc_data_structures::obligation_forest::{ProcessResult};
18 use std::marker::PhantomData;
19 use hir::def_id::DefId;
21 use super::CodeAmbiguity;
22 use super::CodeProjectionError;
23 use super::CodeSelectionError;
24 use super::engine::{TraitEngine, TraitEngineExt};
25 use super::{FulfillmentError, FulfillmentErrorCode};
26 use super::{ObligationCause, PredicateObligation, Obligation};
28 use super::select::SelectionContext;
29 use super::{Unimplemented, ConstEvalFailure};
31 impl<'tcx> ForestObligation for PendingPredicateObligation<'tcx> {
32 type Predicate = ty::Predicate<'tcx>;
34 fn as_predicate(&self) -> &Self::Predicate { &self.obligation.predicate }
37 /// The fulfillment context is used to drive trait resolution. It
38 /// consists of a list of obligations that must be (eventually)
39 /// satisfied. The job is to track which are satisfied, which yielded
40 /// errors, and which are still pending. At any point, users can call
41 /// `select_where_possible`, and the fulfillment context will try to do
42 /// selection, retaining only those obligations that remain
43 /// ambiguous. This may be helpful in pushing type inference
44 /// along. Once all type inference constraints have been generated, the
45 /// method `select_all_or_error` can be used to report any remaining
46 /// ambiguous cases as errors.
47 pub struct FulfillmentContext<'tcx> {
48 // A list of all obligations that have been registered with this
49 // fulfillment context.
50 predicates: ObligationForest<PendingPredicateObligation<'tcx>>,
51 // Should this fulfillment context register type-lives-for-region
52 // obligations on its parent infcx? In some cases, region
53 // obligations are either already known to hold (normalization) or
54 // hopefully verifed elsewhere (type-impls-bound), and therefore
55 // should not be checked.
57 // Note that if we are normalizing a type that we already
58 // know is well-formed, there should be no harm setting this
59 // to true - all the region variables should be determinable
60 // using the RFC 447 rules, which don't depend on
61 // type-lives-for-region constraints, and because the type
62 // is well-formed, the constraints should hold.
63 register_region_obligations: bool,
66 #[derive(Clone, Debug)]
67 pub struct PendingPredicateObligation<'tcx> {
68 pub obligation: PredicateObligation<'tcx>,
69 pub stalled_on: Vec<Ty<'tcx>>,
72 impl<'a, 'gcx, 'tcx> FulfillmentContext<'tcx> {
73 /// Creates a new fulfillment context.
74 pub fn new() -> FulfillmentContext<'tcx> {
76 predicates: ObligationForest::new(),
77 register_region_obligations: true
81 pub fn new_ignoring_regions() -> FulfillmentContext<'tcx> {
83 predicates: ObligationForest::new(),
84 register_region_obligations: false
88 /// Attempts to select obligations using `selcx`.
89 fn select(&mut self, selcx: &mut SelectionContext<'a, 'gcx, 'tcx>)
90 -> Result<(), Vec<FulfillmentError<'tcx>>> {
91 debug!("select(obligation-forest-size={})", self.predicates.len());
93 let mut errors = Vec::new();
96 debug!("select: starting another iteration");
98 // Process pending obligations.
99 let outcome = self.predicates.process_obligations(&mut FulfillProcessor {
101 register_region_obligations: self.register_region_obligations
103 debug!("select: outcome={:#?}", outcome);
105 // FIXME: if we kept the original cache key, we could mark projection
106 // obligations as complete for the projection cache here.
109 outcome.errors.into_iter()
110 .map(|e| to_fulfillment_error(e)));
112 // If nothing new was added, no need to keep looping.
118 debug!("select({} predicates remaining, {} errors) done",
119 self.predicates.len(), errors.len());
121 if errors.is_empty() {
129 impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
130 /// "Normalize" a projection type `<SomeType as SomeTrait>::X` by
131 /// creating a fresh type variable `$0` as well as a projection
132 /// predicate `<SomeType as SomeTrait>::X == $0`. When the
133 /// inference engine runs, it will attempt to find an impl of
134 /// `SomeTrait` or a where clause that lets us unify `$0` with
135 /// something concrete. If this fails, we'll unify `$0` with
136 /// `projection_ty` again.
137 fn normalize_projection_type<'a, 'gcx>(&mut self,
138 infcx: &InferCtxt<'a, 'gcx, 'tcx>,
139 param_env: ty::ParamEnv<'tcx>,
140 projection_ty: ty::ProjectionTy<'tcx>,
141 cause: ObligationCause<'tcx>)
144 debug!("normalize_projection_type(projection_ty={:?})",
147 debug_assert!(!projection_ty.has_escaping_bound_vars());
149 // FIXME(#20304) -- cache
151 let mut selcx = SelectionContext::new(infcx);
152 let mut obligations = vec![];
153 let normalized_ty = project::normalize_projection_type(&mut selcx,
159 self.register_predicate_obligations(infcx, obligations);
161 debug!("normalize_projection_type: result={:?}", normalized_ty);
166 /// Requires that `ty` must implement the trait with `def_id` in
167 /// the given environment. This trait must not have any type
168 /// parameters (except for `Self`).
169 fn register_bound<'a, 'gcx>(&mut self,
170 infcx: &InferCtxt<'a, 'gcx, 'tcx>,
171 param_env: ty::ParamEnv<'tcx>,
174 cause: ObligationCause<'tcx>)
176 let trait_ref = ty::TraitRef {
178 substs: infcx.tcx.mk_substs_trait(ty, &[]),
180 self.register_predicate_obligation(infcx, Obligation {
184 predicate: trait_ref.to_predicate()
188 fn register_predicate_obligation<'a, 'gcx>(&mut self,
189 infcx: &InferCtxt<'a, 'gcx, 'tcx>,
190 obligation: PredicateObligation<'tcx>)
192 // this helps to reduce duplicate errors, as well as making
193 // debug output much nicer to read and so on.
194 let obligation = infcx.resolve_type_vars_if_possible(&obligation);
196 debug!("register_predicate_obligation(obligation={:?})", obligation);
198 assert!(!infcx.is_in_snapshot());
200 self.predicates.register_obligation(PendingPredicateObligation {
206 fn select_all_or_error<'a, 'gcx>(&mut self,
207 infcx: &InferCtxt<'a, 'gcx, 'tcx>)
208 -> Result<(),Vec<FulfillmentError<'tcx>>>
210 self.select_where_possible(infcx)?;
213 self.predicates.to_errors(CodeAmbiguity)
215 .map(|e| to_fulfillment_error(e))
217 if errors.is_empty() {
224 fn select_where_possible<'a, 'gcx>(&mut self,
225 infcx: &InferCtxt<'a, 'gcx, 'tcx>)
226 -> Result<(),Vec<FulfillmentError<'tcx>>>
228 let mut selcx = SelectionContext::new(infcx);
229 self.select(&mut selcx)
232 fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>> {
233 self.predicates.map_pending_obligations(|o| o.obligation.clone())
237 struct FulfillProcessor<'a, 'b: 'a, 'gcx: 'tcx, 'tcx: 'b> {
238 selcx: &'a mut SelectionContext<'b, 'gcx, 'tcx>,
239 register_region_obligations: bool
242 fn mk_pending(os: Vec<PredicateObligation<'tcx>>) -> Vec<PendingPredicateObligation<'tcx>> {
243 os.into_iter().map(|o| PendingPredicateObligation {
249 impl<'a, 'b, 'gcx, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'gcx, 'tcx> {
250 type Obligation = PendingPredicateObligation<'tcx>;
251 type Error = FulfillmentErrorCode<'tcx>;
253 /// Processes a predicate obligation and returns either:
254 /// - `Changed(v)` if the predicate is true, presuming that `v` are also true
255 /// - `Unchanged` if we don't have enough info to be sure
256 /// - `Error(e)` if the predicate does not hold
258 /// This is always inlined, despite its size, because it has a single
259 /// callsite and it is called *very* frequently.
261 fn process_obligation(&mut self,
262 pending_obligation: &mut Self::Obligation)
263 -> ProcessResult<Self::Obligation, Self::Error>
265 // if we were stalled on some unresolved variables, first check
266 // whether any of them have been resolved; if not, don't bother
267 // doing more work yet
268 if !pending_obligation.stalled_on.is_empty() {
269 if pending_obligation.stalled_on.iter().all(|&ty| {
270 // Use the force-inlined variant of shallow_resolve() because this code is hot.
271 let resolved_ty = self.selcx.infcx().inlined_shallow_resolve(&ty);
272 resolved_ty == ty // nothing changed here
274 debug!("process_predicate: pending obligation {:?} still stalled on {:?}",
276 .resolve_type_vars_if_possible(&pending_obligation.obligation),
277 pending_obligation.stalled_on);
278 return ProcessResult::Unchanged;
280 pending_obligation.stalled_on = vec![];
283 let obligation = &mut pending_obligation.obligation;
285 if obligation.predicate.has_infer_types() {
286 obligation.predicate =
287 self.selcx.infcx().resolve_type_vars_if_possible(&obligation.predicate);
290 match obligation.predicate {
291 ty::Predicate::Trait(ref data) => {
292 let trait_obligation = obligation.with(data.clone());
294 if data.is_global() {
295 // no type variables present, can use evaluation for better caching.
296 // FIXME: consider caching errors too.
297 if self.selcx.infcx().predicate_must_hold(&obligation) {
298 debug!("selecting trait `{:?}` at depth {} evaluated to holds",
299 data, obligation.recursion_depth);
300 return ProcessResult::Changed(vec![])
304 match self.selcx.select(&trait_obligation) {
305 Ok(Some(vtable)) => {
306 debug!("selecting trait `{:?}` at depth {} yielded Ok(Some)",
307 data, obligation.recursion_depth);
308 ProcessResult::Changed(mk_pending(vtable.nested_obligations()))
311 debug!("selecting trait `{:?}` at depth {} yielded Ok(None)",
312 data, obligation.recursion_depth);
314 // This is a bit subtle: for the most part, the
315 // only reason we can fail to make progress on
316 // trait selection is because we don't have enough
317 // information about the types in the trait. One
318 // exception is that we sometimes haven't decided
319 // what kind of closure a closure is. *But*, in
320 // that case, it turns out, the type of the
321 // closure will also change, because the closure
322 // also includes references to its upvars as part
323 // of its type, and those types are resolved at
326 // FIXME(#32286) logic seems false if no upvars
327 pending_obligation.stalled_on =
328 trait_ref_type_vars(self.selcx, data.to_poly_trait_ref());
330 debug!("process_predicate: pending obligation {:?} now stalled on {:?}",
331 self.selcx.infcx().resolve_type_vars_if_possible(obligation),
332 pending_obligation.stalled_on);
334 ProcessResult::Unchanged
336 Err(selection_err) => {
337 info!("selecting trait `{:?}` at depth {} yielded Err",
338 data, obligation.recursion_depth);
340 ProcessResult::Error(CodeSelectionError(selection_err))
345 ty::Predicate::RegionOutlives(ref binder) => {
346 match self.selcx.infcx().region_outlives_predicate(&obligation.cause, binder) {
347 Ok(()) => ProcessResult::Changed(vec![]),
348 Err(_) => ProcessResult::Error(CodeSelectionError(Unimplemented)),
352 ty::Predicate::TypeOutlives(ref binder) => {
353 // Check if there are higher-ranked vars.
354 match binder.no_bound_vars() {
355 // If there are, inspect the underlying type further.
357 // Convert from `Binder<OutlivesPredicate<Ty, Region>>` to `Binder<Ty>`.
358 let binder = binder.map_bound_ref(|pred| pred.0);
360 // Check if the type has any bound vars.
361 match binder.no_bound_vars() {
362 // If so, this obligation is an error (for now). Eventually we should be
363 // able to support additional cases here, like `for<'a> &'a str: 'a`.
364 // NOTE: this is duplicate-implemented between here and fulfillment.
366 ProcessResult::Error(CodeSelectionError(Unimplemented))
368 // Otherwise, we have something of the form
369 // `for<'a> T: 'a where 'a not in T`, which we can treat as
372 let r_static = self.selcx.tcx().types.re_static;
373 if self.register_region_obligations {
374 self.selcx.infcx().register_region_obligation_with_cause(
380 ProcessResult::Changed(vec![])
384 // If there aren't, register the obligation.
385 Some(ty::OutlivesPredicate(t_a, r_b)) => {
386 if self.register_region_obligations {
387 self.selcx.infcx().register_region_obligation_with_cause(
393 ProcessResult::Changed(vec![])
398 ty::Predicate::Projection(ref data) => {
399 let project_obligation = obligation.with(data.clone());
400 match project::poly_project_and_unify_type(self.selcx, &project_obligation) {
402 let tcx = self.selcx.tcx();
403 pending_obligation.stalled_on =
404 trait_ref_type_vars(self.selcx, data.to_poly_trait_ref(tcx));
405 ProcessResult::Unchanged
407 Ok(Some(os)) => ProcessResult::Changed(mk_pending(os)),
408 Err(e) => ProcessResult::Error(CodeProjectionError(e))
412 ty::Predicate::ObjectSafe(trait_def_id) => {
413 if !self.selcx.tcx().is_object_safe(trait_def_id) {
414 ProcessResult::Error(CodeSelectionError(Unimplemented))
416 ProcessResult::Changed(vec![])
420 ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind) => {
421 match self.selcx.infcx().closure_kind(closure_def_id, closure_substs) {
422 Some(closure_kind) => {
423 if closure_kind.extends(kind) {
424 ProcessResult::Changed(vec![])
426 ProcessResult::Error(CodeSelectionError(Unimplemented))
430 ProcessResult::Unchanged
435 ty::Predicate::WellFormed(ty) => {
436 match ty::wf::obligations(self.selcx.infcx(),
437 obligation.param_env,
438 obligation.cause.body_id,
439 ty, obligation.cause.span) {
441 pending_obligation.stalled_on = vec![ty];
442 ProcessResult::Unchanged
444 Some(os) => ProcessResult::Changed(mk_pending(os))
448 ty::Predicate::Subtype(ref subtype) => {
449 match self.selcx.infcx().subtype_predicate(&obligation.cause,
450 obligation.param_env,
453 // None means that both are unresolved.
454 pending_obligation.stalled_on = vec![subtype.skip_binder().a,
455 subtype.skip_binder().b];
456 ProcessResult::Unchanged
459 ProcessResult::Changed(mk_pending(ok.obligations))
462 let expected_found = ExpectedFound::new(subtype.skip_binder().a_is_expected,
463 subtype.skip_binder().a,
464 subtype.skip_binder().b);
465 ProcessResult::Error(
466 FulfillmentErrorCode::CodeSubtypeError(expected_found, err))
471 ty::Predicate::ConstEvaluatable(def_id, substs) => {
472 match self.selcx.tcx().lift_to_global(&obligation.param_env) {
474 ProcessResult::Unchanged
477 match self.selcx.tcx().lift_to_global(&substs) {
479 let instance = ty::Instance::resolve(
480 self.selcx.tcx().global_tcx(),
485 if let Some(instance) = instance {
490 match self.selcx.tcx().at(obligation.cause.span)
491 .const_eval(param_env.and(cid)) {
492 Ok(_) => ProcessResult::Changed(vec![]),
493 Err(err) => ProcessResult::Error(
494 CodeSelectionError(ConstEvalFailure(err)))
497 ProcessResult::Error(CodeSelectionError(
498 ConstEvalFailure(ErrorHandled::TooGeneric)
503 pending_obligation.stalled_on = substs.types().collect();
504 ProcessResult::Unchanged
513 fn process_backedge<'c, I>(&mut self, cycle: I,
514 _marker: PhantomData<&'c PendingPredicateObligation<'tcx>>)
515 where I: Clone + Iterator<Item=&'c PendingPredicateObligation<'tcx>>,
517 if self.selcx.coinductive_match(cycle.clone().map(|s| s.obligation.predicate)) {
518 debug!("process_child_obligations: coinductive match");
520 let cycle: Vec<_> = cycle.map(|c| c.obligation.clone()).collect();
521 self.selcx.infcx().report_overflow_error_cycle(&cycle);
526 /// Return the set of type variables contained in a trait ref
527 fn trait_ref_type_vars<'a, 'gcx, 'tcx>(selcx: &mut SelectionContext<'a, 'gcx, 'tcx>,
528 t: ty::PolyTraitRef<'tcx>) -> Vec<Ty<'tcx>>
530 t.skip_binder() // ok b/c this check doesn't care about regions
532 .map(|t| selcx.infcx().resolve_type_vars_if_possible(&t))
533 .filter(|t| t.has_infer_types())
534 .flat_map(|t| t.walk())
535 .filter(|t| match t.sty { ty::Infer(_) => true, _ => false })
539 fn to_fulfillment_error<'tcx>(
540 error: Error<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>>)
541 -> FulfillmentError<'tcx>
543 let obligation = error.backtrace.into_iter().next().unwrap().obligation;
544 FulfillmentError::new(obligation, error.error)