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