]> git.lizzy.rs Git - rust.git/blob - src/librustc/infer/sub.rs
Rename infcx lifetimes in inference relations
[rust.git] / src / librustc / infer / sub.rs
1 // Copyright 2012 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 super::combine::CombineFields;
12 use super::SubregionOrigin;
13 use super::type_variable::{SubtypeOf, SupertypeOf};
14
15 use ty::{self, Ty, TyCtxt};
16 use ty::TyVar;
17 use ty::relate::{Cause, Relate, RelateResult, TypeRelation};
18 use traits::PredicateObligations;
19 use std::mem;
20
21 /// Ensures `a` is made a subtype of `b`. Returns `a` on success.
22 pub struct Sub<'infcx, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> {
23     fields: CombineFields<'infcx, 'gcx, 'tcx>,
24 }
25
26 impl<'infcx, 'gcx, 'tcx> Sub<'infcx, 'gcx, 'tcx> {
27     pub fn new(f: CombineFields<'infcx, 'gcx, 'tcx>) -> Sub<'infcx, 'gcx, 'tcx> {
28         Sub { fields: f }
29     }
30
31     pub fn obligations(self) -> PredicateObligations<'tcx> {
32         self.fields.obligations
33     }
34 }
35
36 impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Sub<'infcx, 'gcx, 'tcx> {
37     fn tag(&self) -> &'static str { "Sub" }
38     fn tcx(&self) -> TyCtxt<'infcx, 'gcx, 'tcx> { self.fields.infcx.tcx }
39     fn a_is_expected(&self) -> bool { self.fields.a_is_expected }
40
41     fn with_cause<F,R>(&mut self, cause: Cause, f: F) -> R
42         where F: FnOnce(&mut Self) -> R
43     {
44         debug!("sub with_cause={:?}", cause);
45         let old_cause = mem::replace(&mut self.fields.cause, Some(cause));
46         let r = f(self);
47         debug!("sub old_cause={:?}", old_cause);
48         self.fields.cause = old_cause;
49         r
50     }
51
52     fn relate_with_variance<T: Relate<'tcx>>(&mut self,
53                                              variance: ty::Variance,
54                                              a: &T,
55                                              b: &T)
56                                              -> RelateResult<'tcx, T>
57     {
58         match variance {
59             ty::Invariant => self.fields.equate().relate(a, b),
60             ty::Covariant => self.relate(a, b),
61             ty::Bivariant => self.fields.bivariate().relate(a, b),
62             ty::Contravariant => self.fields.switch_expected().sub().relate(b, a),
63         }
64     }
65
66     fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
67         debug!("{}.tys({:?}, {:?})", self.tag(), a, b);
68
69         if a == b { return Ok(a); }
70
71         let infcx = self.fields.infcx;
72         let a = infcx.type_variables.borrow_mut().replace_if_possible(a);
73         let b = infcx.type_variables.borrow_mut().replace_if_possible(b);
74         match (&a.sty, &b.sty) {
75             (&ty::TyInfer(TyVar(a_id)), &ty::TyInfer(TyVar(b_id))) => {
76                 infcx.type_variables
77                     .borrow_mut()
78                     .relate_vars(a_id, SubtypeOf, b_id);
79                 Ok(a)
80             }
81             (&ty::TyInfer(TyVar(a_id)), _) => {
82                 self.fields
83                     .switch_expected()
84                     .instantiate(b, SupertypeOf, a_id)?;
85                 Ok(a)
86             }
87             (_, &ty::TyInfer(TyVar(b_id))) => {
88                 self.fields.instantiate(a, SubtypeOf, b_id)?;
89                 Ok(a)
90             }
91
92             (&ty::TyError, _) | (_, &ty::TyError) => {
93                 infcx.set_tainted_by_errors();
94                 Ok(self.tcx().types.err)
95             }
96
97             _ => {
98                 self.fields.infcx.super_combine_tys(self, a, b)?;
99                 Ok(a)
100             }
101         }
102     }
103
104     fn regions(&mut self, a: ty::Region, b: ty::Region) -> RelateResult<'tcx, ty::Region> {
105         debug!("{}.regions({:?}, {:?}) self.cause={:?}",
106                self.tag(), a, b, self.fields.cause);
107         // FIXME -- we have more fine-grained information available
108         // from the "cause" field, we could perhaps give more tailored
109         // error messages.
110         let origin = SubregionOrigin::Subtype(self.fields.trace.clone());
111         self.fields.infcx.region_vars.make_subregion(origin, a, b);
112         Ok(a)
113     }
114
115     fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
116                   -> RelateResult<'tcx, ty::Binder<T>>
117         where T: Relate<'tcx>
118     {
119         self.fields.higher_ranked_sub(a, b)
120     }
121 }