]> git.proxmox.com Git - rustc.git/blob - src/librustc/infer/sub.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.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::{self, CombineFields};
12 use super::higher_ranked::HigherRankedRelations;
13 use super::SubregionOrigin;
14 use super::type_variable::{SubtypeOf, SupertypeOf};
15
16 use ty::{self, Ty, TyCtxt};
17 use ty::TyVar;
18 use ty::relate::{Cause, Relate, RelateResult, TypeRelation};
19 use traits::PredicateObligations;
20 use std::mem;
21
22 /// Ensures `a` is made a subtype of `b`. Returns `a` on success.
23 pub struct Sub<'a, 'tcx: 'a> {
24 fields: CombineFields<'a, 'tcx>,
25 }
26
27 impl<'a, 'tcx> Sub<'a, 'tcx> {
28 pub fn new(f: CombineFields<'a, 'tcx>) -> Sub<'a, 'tcx> {
29 Sub { fields: f }
30 }
31
32 pub fn obligations(self) -> PredicateObligations<'tcx> {
33 self.fields.obligations
34 }
35 }
36
37 impl<'a, 'tcx> TypeRelation<'a, 'tcx> for Sub<'a, 'tcx> {
38 fn tag(&self) -> &'static str { "Sub" }
39 fn tcx(&self) -> &'a TyCtxt<'tcx> { self.fields.infcx.tcx }
40 fn a_is_expected(&self) -> bool { self.fields.a_is_expected }
41
42 fn with_cause<F,R>(&mut self, cause: Cause, f: F) -> R
43 where F: FnOnce(&mut Self) -> R
44 {
45 debug!("sub with_cause={:?}", cause);
46 let old_cause = mem::replace(&mut self.fields.cause, Some(cause));
47 let r = f(self);
48 debug!("sub old_cause={:?}", old_cause);
49 self.fields.cause = old_cause;
50 r
51 }
52
53 fn relate_with_variance<T:Relate<'a,'tcx>>(&mut self,
54 variance: ty::Variance,
55 a: &T,
56 b: &T)
57 -> RelateResult<'tcx, T>
58 {
59 match variance {
60 ty::Invariant => self.fields.equate().relate(a, b),
61 ty::Covariant => self.relate(a, b),
62 ty::Bivariant => self.fields.bivariate().relate(a, b),
63 ty::Contravariant => self.fields.switch_expected().sub().relate(b, a),
64 }
65 }
66
67 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
68 debug!("{}.tys({:?}, {:?})", self.tag(), a, b);
69
70 if a == b { return Ok(a); }
71
72 let infcx = self.fields.infcx;
73 let a = infcx.type_variables.borrow_mut().replace_if_possible(a);
74 let b = infcx.type_variables.borrow_mut().replace_if_possible(b);
75 match (&a.sty, &b.sty) {
76 (&ty::TyInfer(TyVar(a_id)), &ty::TyInfer(TyVar(b_id))) => {
77 infcx.type_variables
78 .borrow_mut()
79 .relate_vars(a_id, SubtypeOf, b_id);
80 Ok(a)
81 }
82 (&ty::TyInfer(TyVar(a_id)), _) => {
83 self.fields
84 .switch_expected()
85 .instantiate(b, SupertypeOf, a_id)?;
86 Ok(a)
87 }
88 (_, &ty::TyInfer(TyVar(b_id))) => {
89 self.fields.instantiate(a, SubtypeOf, b_id)?;
90 Ok(a)
91 }
92
93 (&ty::TyError, _) | (_, &ty::TyError) => {
94 Ok(self.tcx().types.err)
95 }
96
97 _ => {
98 combine::super_combine_tys(self.fields.infcx, 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<'a,'tcx>
118 {
119 self.fields.higher_ranked_sub(a, b)
120 }
121 }