]> git.lizzy.rs Git - rust.git/blob - src/librustc/traits/fulfill.rs
test: Ignore ui/target-feature-gate on sparc and sparc64
[rust.git] / src / librustc / traits / fulfill.rs
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.
4 //
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.
10
11 use infer::InferCtxt;
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;
20
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};
27 use super::project;
28 use super::select::SelectionContext;
29 use super::{Unimplemented, ConstEvalFailure};
30
31 impl<'tcx> ForestObligation for PendingPredicateObligation<'tcx> {
32     type Predicate = ty::Predicate<'tcx>;
33
34     fn as_predicate(&self) -> &Self::Predicate { &self.obligation.predicate }
35 }
36
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.
56     //
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,
64 }
65
66 #[derive(Clone, Debug)]
67 pub struct PendingPredicateObligation<'tcx> {
68     pub obligation: PredicateObligation<'tcx>,
69     pub stalled_on: Vec<Ty<'tcx>>,
70 }
71
72 impl<'a, 'gcx, 'tcx> FulfillmentContext<'tcx> {
73     /// Creates a new fulfillment context.
74     pub fn new() -> FulfillmentContext<'tcx> {
75         FulfillmentContext {
76             predicates: ObligationForest::new(),
77             register_region_obligations: true
78         }
79     }
80
81     pub fn new_ignoring_regions() -> FulfillmentContext<'tcx> {
82         FulfillmentContext {
83             predicates: ObligationForest::new(),
84             register_region_obligations: false
85         }
86     }
87
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());
92
93         let mut errors = Vec::new();
94
95         loop {
96             debug!("select: starting another iteration");
97
98             // Process pending obligations.
99             let outcome = self.predicates.process_obligations(&mut FulfillProcessor {
100                 selcx,
101                 register_region_obligations: self.register_region_obligations
102             }, DoCompleted::No);
103             debug!("select: outcome={:#?}", outcome);
104
105             // FIXME: if we kept the original cache key, we could mark projection
106             // obligations as complete for the projection cache here.
107
108             errors.extend(
109                 outcome.errors.into_iter()
110                               .map(|e| to_fulfillment_error(e)));
111
112             // If nothing new was added, no need to keep looping.
113             if outcome.stalled {
114                 break;
115             }
116         }
117
118         debug!("select({} predicates remaining, {} errors) done",
119                self.predicates.len(), errors.len());
120
121         if errors.is_empty() {
122             Ok(())
123         } else {
124             Err(errors)
125         }
126     }
127 }
128
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>)
142                                  -> Ty<'tcx>
143     {
144         debug!("normalize_projection_type(projection_ty={:?})",
145                projection_ty);
146
147         debug_assert!(!projection_ty.has_escaping_bound_vars());
148
149         // FIXME(#20304) -- cache
150
151         let mut selcx = SelectionContext::new(infcx);
152         let mut obligations = vec![];
153         let normalized_ty = project::normalize_projection_type(&mut selcx,
154                                                                param_env,
155                                                                projection_ty,
156                                                                cause,
157                                                                0,
158                                                                &mut obligations);
159         self.register_predicate_obligations(infcx, obligations);
160
161         debug!("normalize_projection_type: result={:?}", normalized_ty);
162
163         normalized_ty
164     }
165
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>,
172                       ty: Ty<'tcx>,
173                       def_id: DefId,
174                       cause: ObligationCause<'tcx>)
175     {
176         let trait_ref = ty::TraitRef {
177             def_id,
178             substs: infcx.tcx.mk_substs_trait(ty, &[]),
179         };
180         self.register_predicate_obligation(infcx, Obligation {
181             cause,
182             recursion_depth: 0,
183             param_env,
184             predicate: trait_ref.to_predicate()
185         });
186     }
187
188     fn register_predicate_obligation<'a, 'gcx>(&mut self,
189                                      infcx: &InferCtxt<'a, 'gcx, 'tcx>,
190                                      obligation: PredicateObligation<'tcx>)
191     {
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);
195
196         debug!("register_predicate_obligation(obligation={:?})", obligation);
197
198         assert!(!infcx.is_in_snapshot());
199
200         self.predicates.register_obligation(PendingPredicateObligation {
201             obligation,
202             stalled_on: vec![]
203         });
204     }
205
206     fn select_all_or_error<'a, 'gcx>(&mut self,
207                                      infcx: &InferCtxt<'a, 'gcx, 'tcx>)
208                                      -> Result<(),Vec<FulfillmentError<'tcx>>>
209     {
210         self.select_where_possible(infcx)?;
211
212         let errors: Vec<_> =
213             self.predicates.to_errors(CodeAmbiguity)
214                            .into_iter()
215                            .map(|e| to_fulfillment_error(e))
216                            .collect();
217         if errors.is_empty() {
218             Ok(())
219         } else {
220             Err(errors)
221         }
222     }
223
224     fn select_where_possible<'a, 'gcx>(&mut self,
225                              infcx: &InferCtxt<'a, 'gcx, 'tcx>)
226                              -> Result<(),Vec<FulfillmentError<'tcx>>>
227     {
228         let mut selcx = SelectionContext::new(infcx);
229         self.select(&mut selcx)
230     }
231
232     fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>> {
233         self.predicates.map_pending_obligations(|o| o.obligation.clone())
234     }
235 }
236
237 struct FulfillProcessor<'a, 'b: 'a, 'gcx: 'tcx, 'tcx: 'b> {
238     selcx: &'a mut SelectionContext<'b, 'gcx, 'tcx>,
239     register_region_obligations: bool
240 }
241
242 fn mk_pending(os: Vec<PredicateObligation<'tcx>>) -> Vec<PendingPredicateObligation<'tcx>> {
243     os.into_iter().map(|o| PendingPredicateObligation {
244         obligation: o,
245         stalled_on: vec![]
246     }).collect()
247 }
248
249 impl<'a, 'b, 'gcx, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'gcx, 'tcx> {
250     type Obligation = PendingPredicateObligation<'tcx>;
251     type Error = FulfillmentErrorCode<'tcx>;
252
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
257     ///
258     /// This is always inlined, despite its size, because it has a single
259     /// callsite and it is called *very* frequently.
260     #[inline(always)]
261     fn process_obligation(&mut self,
262                           pending_obligation: &mut Self::Obligation)
263                           -> ProcessResult<Self::Obligation, Self::Error>
264     {
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
273             }) {
274                 debug!("process_predicate: pending obligation {:?} still stalled on {:?}",
275                        self.selcx.infcx()
276                            .resolve_type_vars_if_possible(&pending_obligation.obligation),
277                        pending_obligation.stalled_on);
278                 return ProcessResult::Unchanged;
279             }
280             pending_obligation.stalled_on = vec![];
281         }
282
283         let obligation = &mut pending_obligation.obligation;
284
285         if obligation.predicate.has_infer_types() {
286             obligation.predicate =
287                 self.selcx.infcx().resolve_type_vars_if_possible(&obligation.predicate);
288         }
289
290         match obligation.predicate {
291             ty::Predicate::Trait(ref data) => {
292                 let trait_obligation = obligation.with(data.clone());
293
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![])
301                     }
302                 }
303
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()))
309                     }
310                     Ok(None) => {
311                         debug!("selecting trait `{:?}` at depth {} yielded Ok(None)",
312                                data, obligation.recursion_depth);
313
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
324                         // the same time.
325                         //
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());
329
330                         debug!("process_predicate: pending obligation {:?} now stalled on {:?}",
331                                self.selcx.infcx().resolve_type_vars_if_possible(obligation),
332                                pending_obligation.stalled_on);
333
334                         ProcessResult::Unchanged
335                     }
336                     Err(selection_err) => {
337                         info!("selecting trait `{:?}` at depth {} yielded Err",
338                               data, obligation.recursion_depth);
339
340                         ProcessResult::Error(CodeSelectionError(selection_err))
341                     }
342                 }
343             }
344
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)),
349                 }
350             }
351
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.
356                     None => {
357                         // Convert from `Binder<OutlivesPredicate<Ty, Region>>` to `Binder<Ty>`.
358                         let binder = binder.map_bound_ref(|pred| pred.0);
359
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.
365                             None => {
366                                 ProcessResult::Error(CodeSelectionError(Unimplemented))
367                             }
368                             // Otherwise, we have something of the form
369                             // `for<'a> T: 'a where 'a not in T`, which we can treat as
370                             // `T: 'static`.
371                             Some(t_a) => {
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(
375                                         t_a,
376                                         r_static,
377                                         &obligation.cause,
378                                     );
379                                 }
380                                 ProcessResult::Changed(vec![])
381                             }
382                         }
383                     }
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(
388                                 t_a,
389                                 r_b,
390                                 &obligation.cause,
391                             );
392                         }
393                         ProcessResult::Changed(vec![])
394                     }
395                 }
396             }
397
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) {
401                     Ok(None) => {
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
406                     }
407                     Ok(Some(os)) => ProcessResult::Changed(mk_pending(os)),
408                     Err(e) => ProcessResult::Error(CodeProjectionError(e))
409                 }
410             }
411
412             ty::Predicate::ObjectSafe(trait_def_id) => {
413                 if !self.selcx.tcx().is_object_safe(trait_def_id) {
414                     ProcessResult::Error(CodeSelectionError(Unimplemented))
415                 } else {
416                     ProcessResult::Changed(vec![])
417                 }
418             }
419
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![])
425                         } else {
426                             ProcessResult::Error(CodeSelectionError(Unimplemented))
427                         }
428                     }
429                     None => {
430                         ProcessResult::Unchanged
431                     }
432                 }
433             }
434
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) {
440                     None => {
441                         pending_obligation.stalled_on = vec![ty];
442                         ProcessResult::Unchanged
443                     }
444                     Some(os) => ProcessResult::Changed(mk_pending(os))
445                 }
446             }
447
448             ty::Predicate::Subtype(ref subtype) => {
449                 match self.selcx.infcx().subtype_predicate(&obligation.cause,
450                                                            obligation.param_env,
451                                                            subtype) {
452                     None => {
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
457                     }
458                     Some(Ok(ok)) => {
459                         ProcessResult::Changed(mk_pending(ok.obligations))
460                     }
461                     Some(Err(err)) => {
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))
467                     }
468                 }
469             }
470
471             ty::Predicate::ConstEvaluatable(def_id, substs) => {
472                 match self.selcx.tcx().lift_to_global(&obligation.param_env) {
473                     None => {
474                         ProcessResult::Unchanged
475                     }
476                     Some(param_env) => {
477                         match self.selcx.tcx().lift_to_global(&substs) {
478                             Some(substs) => {
479                                 let instance = ty::Instance::resolve(
480                                     self.selcx.tcx().global_tcx(),
481                                     param_env,
482                                     def_id,
483                                     substs,
484                                 );
485                                 if let Some(instance) = instance {
486                                     let cid = GlobalId {
487                                         instance,
488                                         promoted: None,
489                                     };
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)))
495                                     }
496                                 } else {
497                                     ProcessResult::Error(CodeSelectionError(
498                                         ConstEvalFailure(ErrorHandled::TooGeneric)
499                                     ))
500                                 }
501                             },
502                             None => {
503                                 pending_obligation.stalled_on = substs.types().collect();
504                                 ProcessResult::Unchanged
505                             }
506                         }
507                     }
508                 }
509             }
510         }
511     }
512
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>>,
516     {
517         if self.selcx.coinductive_match(cycle.clone().map(|s| s.obligation.predicate)) {
518             debug!("process_child_obligations: coinductive match");
519         } else {
520             let cycle: Vec<_> = cycle.map(|c| c.obligation.clone()).collect();
521             self.selcx.infcx().report_overflow_error_cycle(&cycle);
522         }
523     }
524 }
525
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>>
529 {
530     t.skip_binder() // ok b/c this check doesn't care about regions
531      .input_types()
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 })
536      .collect()
537 }
538
539 fn to_fulfillment_error<'tcx>(
540     error: Error<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>>)
541     -> FulfillmentError<'tcx>
542 {
543     let obligation = error.backtrace.into_iter().next().unwrap().obligation;
544     FulfillmentError::new(obligation, error.error)
545 }