]> git.lizzy.rs Git - rust.git/blob - src/librustc/traits/fulfill.rs
Fix invalid associated type rendering in rustdoc
[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 dep_graph::DepGraph;
12 use infer::{InferCtxt, InferOk};
13 use ty::{self, Ty, TypeFoldable, ToPolyTraitRef, TyCtxt, ToPredicate};
14 use rustc_data_structures::obligation_forest::{ObligationForest, Error};
15 use rustc_data_structures::obligation_forest::{ForestObligation, ObligationProcessor};
16 use std::marker::PhantomData;
17 use syntax::ast;
18 use util::nodemap::{FxHashSet, NodeMap};
19 use hir::def_id::DefId;
20
21 use super::CodeAmbiguity;
22 use super::CodeProjectionError;
23 use super::CodeSelectionError;
24 use super::{FulfillmentError, FulfillmentErrorCode};
25 use super::{ObligationCause, PredicateObligation, Obligation};
26 use super::project;
27 use super::select::SelectionContext;
28 use super::Unimplemented;
29
30 impl<'tcx> ForestObligation for PendingPredicateObligation<'tcx> {
31     type Predicate = ty::Predicate<'tcx>;
32
33     fn as_predicate(&self) -> &Self::Predicate { &self.obligation.predicate }
34 }
35
36 pub struct GlobalFulfilledPredicates<'tcx> {
37     set: FxHashSet<ty::PolyTraitPredicate<'tcx>>,
38     dep_graph: DepGraph,
39 }
40
41 /// The fulfillment context is used to drive trait resolution.  It
42 /// consists of a list of obligations that must be (eventually)
43 /// satisfied. The job is to track which are satisfied, which yielded
44 /// errors, and which are still pending. At any point, users can call
45 /// `select_where_possible`, and the fulfilment context will try to do
46 /// selection, retaining only those obligations that remain
47 /// ambiguous. This may be helpful in pushing type inference
48 /// along. Once all type inference constraints have been generated, the
49 /// method `select_all_or_error` can be used to report any remaining
50 /// ambiguous cases as errors.
51
52 pub struct FulfillmentContext<'tcx> {
53     // A list of all obligations that have been registered with this
54     // fulfillment context.
55     predicates: ObligationForest<PendingPredicateObligation<'tcx>>,
56
57     // A set of constraints that regionck must validate. Each
58     // constraint has the form `T:'a`, meaning "some type `T` must
59     // outlive the lifetime 'a". These constraints derive from
60     // instantiated type parameters. So if you had a struct defined
61     // like
62     //
63     //     struct Foo<T:'static> { ... }
64     //
65     // then in some expression `let x = Foo { ... }` it will
66     // instantiate the type parameter `T` with a fresh type `$0`. At
67     // the same time, it will record a region obligation of
68     // `$0:'static`. This will get checked later by regionck. (We
69     // can't generally check these things right away because we have
70     // to wait until types are resolved.)
71     //
72     // These are stored in a map keyed to the id of the innermost
73     // enclosing fn body / static initializer expression. This is
74     // because the location where the obligation was incurred can be
75     // relevant with respect to which sublifetime assumptions are in
76     // place. The reason that we store under the fn-id, and not
77     // something more fine-grained, is so that it is easier for
78     // regionck to be sure that it has found *all* the region
79     // obligations (otherwise, it's easy to fail to walk to a
80     // particular node-id).
81     region_obligations: NodeMap<Vec<RegionObligation<'tcx>>>,
82 }
83
84 #[derive(Clone)]
85 pub struct RegionObligation<'tcx> {
86     pub sub_region: &'tcx ty::Region,
87     pub sup_type: Ty<'tcx>,
88     pub cause: ObligationCause<'tcx>,
89 }
90
91 #[derive(Clone, Debug)]
92 pub struct PendingPredicateObligation<'tcx> {
93     pub obligation: PredicateObligation<'tcx>,
94     pub stalled_on: Vec<Ty<'tcx>>,
95 }
96
97 impl<'a, 'gcx, 'tcx> FulfillmentContext<'tcx> {
98     /// Creates a new fulfillment context.
99     pub fn new() -> FulfillmentContext<'tcx> {
100         FulfillmentContext {
101             predicates: ObligationForest::new(),
102             region_obligations: NodeMap(),
103         }
104     }
105
106     /// "Normalize" a projection type `<SomeType as SomeTrait>::X` by
107     /// creating a fresh type variable `$0` as well as a projection
108     /// predicate `<SomeType as SomeTrait>::X == $0`. When the
109     /// inference engine runs, it will attempt to find an impl of
110     /// `SomeTrait` or a where clause that lets us unify `$0` with
111     /// something concrete. If this fails, we'll unify `$0` with
112     /// `projection_ty` again.
113     pub fn normalize_projection_type(&mut self,
114                                      infcx: &InferCtxt<'a, 'gcx, 'tcx>,
115                                      projection_ty: ty::ProjectionTy<'tcx>,
116                                      cause: ObligationCause<'tcx>)
117                                      -> Ty<'tcx>
118     {
119         debug!("normalize_projection_type(projection_ty={:?})",
120                projection_ty);
121
122         assert!(!projection_ty.has_escaping_regions());
123
124         // FIXME(#20304) -- cache
125
126         let mut selcx = SelectionContext::new(infcx);
127         let normalized = project::normalize_projection_type(&mut selcx, projection_ty, cause, 0);
128
129         for obligation in normalized.obligations {
130             self.register_predicate_obligation(infcx, obligation);
131         }
132
133         debug!("normalize_projection_type: result={:?}", normalized.value);
134
135         normalized.value
136     }
137
138     pub fn register_bound(&mut self,
139                           infcx: &InferCtxt<'a, 'gcx, 'tcx>,
140                           ty: Ty<'tcx>,
141                           def_id: DefId,
142                           cause: ObligationCause<'tcx>)
143     {
144         let trait_ref = ty::TraitRef {
145             def_id: def_id,
146             substs: infcx.tcx.mk_substs_trait(ty, &[]),
147         };
148         self.register_predicate_obligation(infcx, Obligation {
149             cause: cause,
150             recursion_depth: 0,
151             predicate: trait_ref.to_predicate()
152         });
153     }
154
155     pub fn register_region_obligation(&mut self,
156                                       t_a: Ty<'tcx>,
157                                       r_b: &'tcx ty::Region,
158                                       cause: ObligationCause<'tcx>)
159     {
160         register_region_obligation(t_a, r_b, cause, &mut self.region_obligations);
161     }
162
163     pub fn register_predicate_obligation(&mut self,
164                                          infcx: &InferCtxt<'a, 'gcx, 'tcx>,
165                                          obligation: PredicateObligation<'tcx>)
166     {
167         // this helps to reduce duplicate errors, as well as making
168         // debug output much nicer to read and so on.
169         let obligation = infcx.resolve_type_vars_if_possible(&obligation);
170
171         debug!("register_predicate_obligation(obligation={:?})", obligation);
172
173         infcx.obligations_in_snapshot.set(true);
174
175         if infcx.tcx.fulfilled_predicates.borrow().check_duplicate(&obligation.predicate) {
176             debug!("register_predicate_obligation: duplicate");
177             return
178         }
179
180         self.predicates.register_obligation(PendingPredicateObligation {
181             obligation: obligation,
182             stalled_on: vec![]
183         });
184     }
185
186     pub fn region_obligations(&self,
187                               body_id: ast::NodeId)
188                               -> &[RegionObligation<'tcx>]
189     {
190         match self.region_obligations.get(&body_id) {
191             None => Default::default(),
192             Some(vec) => vec,
193         }
194     }
195
196     pub fn select_all_or_error(&mut self,
197                                infcx: &InferCtxt<'a, 'gcx, 'tcx>)
198                                -> Result<(),Vec<FulfillmentError<'tcx>>>
199     {
200         self.select_where_possible(infcx)?;
201
202         let errors: Vec<_> =
203             self.predicates.to_errors(CodeAmbiguity)
204                            .into_iter()
205                            .map(|e| to_fulfillment_error(e))
206                            .collect();
207         if errors.is_empty() {
208             Ok(())
209         } else {
210             Err(errors)
211         }
212     }
213
214     pub fn select_where_possible(&mut self,
215                                  infcx: &InferCtxt<'a, 'gcx, 'tcx>)
216                                  -> Result<(),Vec<FulfillmentError<'tcx>>>
217     {
218         let mut selcx = SelectionContext::new(infcx);
219         self.select(&mut selcx)
220     }
221
222     pub fn pending_obligations(&self) -> Vec<PendingPredicateObligation<'tcx>> {
223         self.predicates.pending_obligations()
224     }
225
226     /// Attempts to select obligations using `selcx`. If `only_new_obligations` is true, then it
227     /// only attempts to select obligations that haven't been seen before.
228     fn select(&mut self, selcx: &mut SelectionContext<'a, 'gcx, 'tcx>)
229               -> Result<(),Vec<FulfillmentError<'tcx>>> {
230         debug!("select(obligation-forest-size={})", self.predicates.len());
231
232         let mut errors = Vec::new();
233
234         loop {
235             debug!("select: starting another iteration");
236
237             // Process pending obligations.
238             let outcome = self.predicates.process_obligations(&mut FulfillProcessor {
239                 selcx: selcx,
240                 region_obligations: &mut self.region_obligations,
241             });
242             debug!("select: outcome={:?}", outcome);
243
244             // these are obligations that were proven to be true.
245             for pending_obligation in outcome.completed {
246                 let predicate = &pending_obligation.obligation.predicate;
247                 selcx.tcx().fulfilled_predicates.borrow_mut()
248                            .add_if_global(selcx.tcx(), predicate);
249             }
250
251             errors.extend(
252                 outcome.errors.into_iter()
253                               .map(|e| to_fulfillment_error(e)));
254
255             // If nothing new was added, no need to keep looping.
256             if outcome.stalled {
257                 break;
258             }
259         }
260
261         debug!("select({} predicates remaining, {} errors) done",
262                self.predicates.len(), errors.len());
263
264         if errors.is_empty() {
265             Ok(())
266         } else {
267             Err(errors)
268         }
269     }
270 }
271
272 struct FulfillProcessor<'a, 'b: 'a, 'gcx: 'tcx, 'tcx: 'b> {
273     selcx: &'a mut SelectionContext<'b, 'gcx, 'tcx>,
274     region_obligations: &'a mut NodeMap<Vec<RegionObligation<'tcx>>>,
275 }
276
277 impl<'a, 'b, 'gcx, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'gcx, 'tcx> {
278     type Obligation = PendingPredicateObligation<'tcx>;
279     type Error = FulfillmentErrorCode<'tcx>;
280
281     fn process_obligation(&mut self,
282                           obligation: &mut Self::Obligation)
283                           -> Result<Option<Vec<Self::Obligation>>, Self::Error>
284     {
285         process_predicate(self.selcx,
286                           obligation,
287                           self.region_obligations)
288             .map(|os| os.map(|os| os.into_iter().map(|o| PendingPredicateObligation {
289                 obligation: o,
290                 stalled_on: vec![]
291             }).collect()))
292     }
293
294     fn process_backedge<'c, I>(&mut self, cycle: I,
295                                _marker: PhantomData<&'c PendingPredicateObligation<'tcx>>)
296         where I: Clone + Iterator<Item=&'c PendingPredicateObligation<'tcx>>,
297     {
298         if coinductive_match(self.selcx, cycle.clone()) {
299             debug!("process_child_obligations: coinductive match");
300         } else {
301             let cycle : Vec<_> = cycle.map(|c| c.obligation.clone()).collect();
302             self.selcx.infcx().report_overflow_error_cycle(&cycle);
303         }
304     }
305 }
306
307 /// Return the set of type variables contained in a trait ref
308 fn trait_ref_type_vars<'a, 'gcx, 'tcx>(selcx: &mut SelectionContext<'a, 'gcx, 'tcx>,
309                                        t: ty::PolyTraitRef<'tcx>) -> Vec<Ty<'tcx>>
310 {
311     t.skip_binder() // ok b/c this check doesn't care about regions
312      .input_types()
313      .map(|t| selcx.infcx().resolve_type_vars_if_possible(&t))
314      .filter(|t| t.has_infer_types())
315      .flat_map(|t| t.walk())
316      .filter(|t| match t.sty { ty::TyInfer(_) => true, _ => false })
317      .collect()
318 }
319
320 /// Processes a predicate obligation and returns either:
321 /// - `Ok(Some(v))` if the predicate is true, presuming that `v` are also true
322 /// - `Ok(None)` if we don't have enough info to be sure
323 /// - `Err` if the predicate does not hold
324 fn process_predicate<'a, 'gcx, 'tcx>(
325     selcx: &mut SelectionContext<'a, 'gcx, 'tcx>,
326     pending_obligation: &mut PendingPredicateObligation<'tcx>,
327     region_obligations: &mut NodeMap<Vec<RegionObligation<'tcx>>>)
328     -> Result<Option<Vec<PredicateObligation<'tcx>>>,
329               FulfillmentErrorCode<'tcx>>
330 {
331     // if we were stalled on some unresolved variables, first check
332     // whether any of them have been resolved; if not, don't bother
333     // doing more work yet
334     if !pending_obligation.stalled_on.is_empty() {
335         if pending_obligation.stalled_on.iter().all(|&ty| {
336             let resolved_ty = selcx.infcx().shallow_resolve(&ty);
337             resolved_ty == ty // nothing changed here
338         }) {
339             debug!("process_predicate: pending obligation {:?} still stalled on {:?}",
340                    selcx.infcx().resolve_type_vars_if_possible(&pending_obligation.obligation),
341                    pending_obligation.stalled_on);
342             return Ok(None);
343         }
344         pending_obligation.stalled_on = vec![];
345     }
346
347     let obligation = &mut pending_obligation.obligation;
348
349     if obligation.predicate.has_infer_types() {
350         obligation.predicate = selcx.infcx().resolve_type_vars_if_possible(&obligation.predicate);
351     }
352
353     match obligation.predicate {
354         ty::Predicate::Trait(ref data) => {
355             if selcx.tcx().fulfilled_predicates.borrow().check_duplicate_trait(data) {
356                 return Ok(Some(vec![]));
357             }
358
359             let trait_obligation = obligation.with(data.clone());
360             match selcx.select(&trait_obligation) {
361                 Ok(Some(vtable)) => {
362                     debug!("selecting trait `{:?}` at depth {} yielded Ok(Some)",
363                           data, obligation.recursion_depth);
364                     Ok(Some(vtable.nested_obligations()))
365                 }
366                 Ok(None) => {
367                     debug!("selecting trait `{:?}` at depth {} yielded Ok(None)",
368                           data, obligation.recursion_depth);
369
370                     // This is a bit subtle: for the most part, the
371                     // only reason we can fail to make progress on
372                     // trait selection is because we don't have enough
373                     // information about the types in the trait. One
374                     // exception is that we sometimes haven't decided
375                     // what kind of closure a closure is. *But*, in
376                     // that case, it turns out, the type of the
377                     // closure will also change, because the closure
378                     // also includes references to its upvars as part
379                     // of its type, and those types are resolved at
380                     // the same time.
381                     //
382                     // FIXME(#32286) logic seems false if no upvars
383                     pending_obligation.stalled_on =
384                         trait_ref_type_vars(selcx, data.to_poly_trait_ref());
385
386                     debug!("process_predicate: pending obligation {:?} now stalled on {:?}",
387                            selcx.infcx().resolve_type_vars_if_possible(obligation),
388                            pending_obligation.stalled_on);
389
390                     Ok(None)
391                 }
392                 Err(selection_err) => {
393                     info!("selecting trait `{:?}` at depth {} yielded Err",
394                           data, obligation.recursion_depth);
395
396                     Err(CodeSelectionError(selection_err))
397                 }
398             }
399         }
400
401         ty::Predicate::Equate(ref binder) => {
402             match selcx.infcx().equality_predicate(&obligation.cause, binder) {
403                 Ok(InferOk { obligations, value: () }) => {
404                     Ok(Some(obligations))
405                 },
406                 Err(_) => Err(CodeSelectionError(Unimplemented)),
407             }
408         }
409
410         ty::Predicate::RegionOutlives(ref binder) => {
411             match selcx.infcx().region_outlives_predicate(&obligation.cause, binder) {
412                 Ok(()) => Ok(Some(Vec::new())),
413                 Err(_) => Err(CodeSelectionError(Unimplemented)),
414             }
415         }
416
417         ty::Predicate::TypeOutlives(ref binder) => {
418             // Check if there are higher-ranked regions.
419             match selcx.tcx().no_late_bound_regions(binder) {
420                 // If there are, inspect the underlying type further.
421                 None => {
422                     // Convert from `Binder<OutlivesPredicate<Ty, Region>>` to `Binder<Ty>`.
423                     let binder = binder.map_bound_ref(|pred| pred.0);
424
425                     // Check if the type has any bound regions.
426                     match selcx.tcx().no_late_bound_regions(&binder) {
427                         // If so, this obligation is an error (for now). Eventually we should be
428                         // able to support additional cases here, like `for<'a> &'a str: 'a`.
429                         None => {
430                             Err(CodeSelectionError(Unimplemented))
431                         }
432                         // Otherwise, we have something of the form
433                         // `for<'a> T: 'a where 'a not in T`, which we can treat as `T: 'static`.
434                         Some(t_a) => {
435                             let r_static = selcx.tcx().mk_region(ty::ReStatic);
436                             register_region_obligation(t_a, r_static,
437                                                        obligation.cause.clone(),
438                                                        region_obligations);
439                             Ok(Some(vec![]))
440                         }
441                     }
442                 }
443                 // If there aren't, register the obligation.
444                 Some(ty::OutlivesPredicate(t_a, r_b)) => {
445                     register_region_obligation(t_a, r_b,
446                                                obligation.cause.clone(),
447                                                region_obligations);
448                     Ok(Some(vec![]))
449                 }
450             }
451         }
452
453         ty::Predicate::Projection(ref data) => {
454             let project_obligation = obligation.with(data.clone());
455             match project::poly_project_and_unify_type(selcx, &project_obligation) {
456                 Ok(None) => {
457                     pending_obligation.stalled_on =
458                         trait_ref_type_vars(selcx, data.to_poly_trait_ref());
459                     Ok(None)
460                 }
461                 Ok(v) => Ok(v),
462                 Err(e) => Err(CodeProjectionError(e))
463             }
464         }
465
466         ty::Predicate::ObjectSafe(trait_def_id) => {
467             if !selcx.tcx().is_object_safe(trait_def_id) {
468                 Err(CodeSelectionError(Unimplemented))
469             } else {
470                 Ok(Some(Vec::new()))
471             }
472         }
473
474         ty::Predicate::ClosureKind(closure_def_id, kind) => {
475             match selcx.infcx().closure_kind(closure_def_id) {
476                 Some(closure_kind) => {
477                     if closure_kind.extends(kind) {
478                         Ok(Some(vec![]))
479                     } else {
480                         Err(CodeSelectionError(Unimplemented))
481                     }
482                 }
483                 None => {
484                     Ok(None)
485                 }
486             }
487         }
488
489         ty::Predicate::WellFormed(ty) => {
490             match ty::wf::obligations(selcx.infcx(), obligation.cause.body_id,
491                                       ty, obligation.cause.span) {
492                 None => {
493                     pending_obligation.stalled_on = vec![ty];
494                     Ok(None)
495                 }
496                 s => Ok(s)
497             }
498         }
499     }
500 }
501
502 /// For defaulted traits, we use a co-inductive strategy to solve, so
503 /// that recursion is ok. This routine returns true if the top of the
504 /// stack (`cycle[0]`):
505 /// - is a defaulted trait, and
506 /// - it also appears in the backtrace at some position `X`; and,
507 /// - all the predicates at positions `X..` between `X` an the top are
508 ///   also defaulted traits.
509 fn coinductive_match<'a,'c,'gcx,'tcx,I>(selcx: &mut SelectionContext<'a,'gcx,'tcx>,
510                                         cycle: I) -> bool
511     where I: Iterator<Item=&'c PendingPredicateObligation<'tcx>>,
512           'tcx: 'c
513 {
514     let mut cycle = cycle;
515     cycle
516         .all(|bt_obligation| {
517             let result = coinductive_obligation(selcx, &bt_obligation.obligation);
518             debug!("coinductive_match: bt_obligation={:?} coinductive={}",
519                    bt_obligation, result);
520             result
521         })
522 }
523
524 fn coinductive_obligation<'a,'gcx,'tcx>(selcx: &SelectionContext<'a,'gcx,'tcx>,
525                                           obligation: &PredicateObligation<'tcx>)
526                                           -> bool {
527     match obligation.predicate {
528         ty::Predicate::Trait(ref data) => {
529             selcx.tcx().trait_has_default_impl(data.def_id())
530         }
531         _ => {
532             false
533         }
534     }
535 }
536
537 fn register_region_obligation<'tcx>(t_a: Ty<'tcx>,
538                                     r_b: &'tcx ty::Region,
539                                     cause: ObligationCause<'tcx>,
540                                     region_obligations: &mut NodeMap<Vec<RegionObligation<'tcx>>>)
541 {
542     let region_obligation = RegionObligation { sup_type: t_a,
543                                                sub_region: r_b,
544                                                cause: cause };
545
546     debug!("register_region_obligation({:?}, cause={:?})",
547            region_obligation, region_obligation.cause);
548
549     region_obligations.entry(region_obligation.cause.body_id)
550                       .or_insert(vec![])
551                       .push(region_obligation);
552
553 }
554
555 impl<'a, 'gcx, 'tcx> GlobalFulfilledPredicates<'gcx> {
556     pub fn new(dep_graph: DepGraph) -> GlobalFulfilledPredicates<'gcx> {
557         GlobalFulfilledPredicates {
558             set: FxHashSet(),
559             dep_graph: dep_graph,
560         }
561     }
562
563     pub fn check_duplicate(&self, key: &ty::Predicate<'tcx>) -> bool {
564         if let ty::Predicate::Trait(ref data) = *key {
565             self.check_duplicate_trait(data)
566         } else {
567             false
568         }
569     }
570
571     pub fn check_duplicate_trait(&self, data: &ty::PolyTraitPredicate<'tcx>) -> bool {
572         // For the global predicate registry, when we find a match, it
573         // may have been computed by some other task, so we want to
574         // add a read from the node corresponding to the predicate
575         // processing to make sure we get the transitive dependencies.
576         if self.set.contains(data) {
577             debug_assert!(data.is_global());
578             self.dep_graph.read(data.dep_node());
579             debug!("check_duplicate: global predicate `{:?}` already proved elsewhere", data);
580
581             true
582         } else {
583             false
584         }
585     }
586
587     fn add_if_global(&mut self, tcx: TyCtxt<'a, 'gcx, 'tcx>, key: &ty::Predicate<'tcx>) {
588         if let ty::Predicate::Trait(ref data) = *key {
589             // We only add things to the global predicate registry
590             // after the current task has proved them, and hence
591             // already has the required read edges, so we don't need
592             // to add any more edges here.
593             if data.is_global() {
594                 if let Some(data) = tcx.lift_to_global(data) {
595                     if self.set.insert(data.clone()) {
596                         debug!("add_if_global: global predicate `{:?}` added", data);
597                     }
598                 }
599             }
600         }
601     }
602 }
603
604 fn to_fulfillment_error<'tcx>(
605     error: Error<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>>)
606     -> FulfillmentError<'tcx>
607 {
608     let obligation = error.backtrace.into_iter().next().unwrap().obligation;
609     FulfillmentError::new(obligation, error.error)
610 }