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