]> git.lizzy.rs Git - rust.git/blob - src/librustc/infer/sub.rs
Remove Ty prefix from Ty{Adt|Array|Slice|RawPtr|Ref|FnDef|FnPtr|Dynamic|Closure|Gener...
[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::SubregionOrigin;
12 use super::combine::{CombineFields, RelationDir};
13
14 use traits::Obligation;
15 use ty::{self, Ty, TyCtxt};
16 use ty::TyVar;
17 use ty::fold::TypeFoldable;
18 use ty::relate::{Cause, Relate, RelateResult, TypeRelation};
19 use std::mem;
20
21 /// Ensures `a` is made a subtype of `b`. Returns `a` on success.
22 pub struct Sub<'combine, 'infcx: 'combine, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> {
23     fields: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>,
24     a_is_expected: bool,
25 }
26
27 impl<'combine, 'infcx, 'gcx, 'tcx> Sub<'combine, 'infcx, 'gcx, 'tcx> {
28     pub fn new(f: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>, a_is_expected: bool)
29         -> Sub<'combine, 'infcx, 'gcx, 'tcx>
30     {
31         Sub { fields: f, a_is_expected: a_is_expected }
32     }
33
34     fn with_expected_switched<R, F: FnOnce(&mut Self) -> R>(&mut self, f: F) -> R {
35         self.a_is_expected = !self.a_is_expected;
36         let result = f(self);
37         self.a_is_expected = !self.a_is_expected;
38         result
39     }
40 }
41
42 impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
43     for Sub<'combine, 'infcx, 'gcx, 'tcx>
44 {
45     fn tag(&self) -> &'static str { "Sub" }
46     fn tcx(&self) -> TyCtxt<'infcx, 'gcx, 'tcx> { self.fields.infcx.tcx }
47     fn a_is_expected(&self) -> bool { self.a_is_expected }
48
49     fn with_cause<F,R>(&mut self, cause: Cause, f: F) -> R
50         where F: FnOnce(&mut Self) -> R
51     {
52         debug!("sub with_cause={:?}", cause);
53         let old_cause = mem::replace(&mut self.fields.cause, Some(cause));
54         let r = f(self);
55         debug!("sub old_cause={:?}", old_cause);
56         self.fields.cause = old_cause;
57         r
58     }
59
60     fn relate_with_variance<T: Relate<'tcx>>(&mut self,
61                                              variance: ty::Variance,
62                                              a: &T,
63                                              b: &T)
64                                              -> RelateResult<'tcx, T>
65     {
66         match variance {
67             ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b),
68             ty::Covariant => self.relate(a, b),
69             ty::Bivariant => Ok(a.clone()),
70             ty::Contravariant => self.with_expected_switched(|this| { this.relate(b, a) }),
71         }
72     }
73
74     fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
75         debug!("{}.tys({:?}, {:?})", self.tag(), a, b);
76
77         if a == b { return Ok(a); }
78
79         let infcx = self.fields.infcx;
80         let a = infcx.type_variables.borrow_mut().replace_if_possible(a);
81         let b = infcx.type_variables.borrow_mut().replace_if_possible(b);
82         match (&a.sty, &b.sty) {
83             (&ty::Infer(TyVar(a_vid)), &ty::Infer(TyVar(b_vid))) => {
84                 // Shouldn't have any LBR here, so we can safely put
85                 // this under a binder below without fear of accidental
86                 // capture.
87                 assert!(!a.has_escaping_regions());
88                 assert!(!b.has_escaping_regions());
89
90                 // can't make progress on `A <: B` if both A and B are
91                 // type variables, so record an obligation. We also
92                 // have to record in the `type_variables` tracker that
93                 // the two variables are equal modulo subtyping, which
94                 // is important to the occurs check later on.
95                 infcx.type_variables.borrow_mut().sub(a_vid, b_vid);
96                 self.fields.obligations.push(
97                     Obligation::new(
98                         self.fields.trace.cause.clone(),
99                         self.fields.param_env,
100                         ty::Predicate::Subtype(
101                             ty::Binder::dummy(ty::SubtypePredicate {
102                                 a_is_expected: self.a_is_expected,
103                                 a,
104                                 b,
105                             }))));
106
107                 Ok(a)
108             }
109             (&ty::Infer(TyVar(a_id)), _) => {
110                 self.fields
111                     .instantiate(b, RelationDir::SupertypeOf, a_id, !self.a_is_expected)?;
112                 Ok(a)
113             }
114             (_, &ty::Infer(TyVar(b_id))) => {
115                 self.fields.instantiate(a, RelationDir::SubtypeOf, b_id, self.a_is_expected)?;
116                 Ok(a)
117             }
118
119             (&ty::Error, _) | (_, &ty::Error) => {
120                 infcx.set_tainted_by_errors();
121                 Ok(self.tcx().types.err)
122             }
123
124             _ => {
125                 self.fields.infcx.super_combine_tys(self, a, b)?;
126                 Ok(a)
127             }
128         }
129     }
130
131     fn regions(&mut self, a: ty::Region<'tcx>, b: ty::Region<'tcx>)
132                -> RelateResult<'tcx, ty::Region<'tcx>> {
133         debug!("{}.regions({:?}, {:?}) self.cause={:?}",
134                self.tag(), a, b, self.fields.cause);
135
136         // FIXME -- we have more fine-grained information available
137         // from the "cause" field, we could perhaps give more tailored
138         // error messages.
139         let origin = SubregionOrigin::Subtype(self.fields.trace.clone());
140         self.fields.infcx.borrow_region_constraints()
141                          .make_subregion(origin, a, b);
142
143         Ok(a)
144     }
145
146     fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
147                   -> RelateResult<'tcx, ty::Binder<T>>
148         where T: Relate<'tcx>
149     {
150         self.fields.higher_ranked_sub(a, b, self.a_is_expected)
151     }
152 }