]> git.proxmox.com Git - rustc.git/blob - src/librustc/infer/equate.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / librustc / infer / equate.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 super::combine::{self, CombineFields};
12 use super::higher_ranked::HigherRankedRelations;
13 use super::{Subtype};
14 use super::type_variable::{EqTo};
15
16 use ty::{self, Ty, TyCtxt};
17 use ty::TyVar;
18 use ty::relate::{Relate, RelateResult, TypeRelation};
19 use traits::PredicateObligations;
20
21 /// Ensures `a` is made equal to `b`. Returns `a` on success.
22 pub struct Equate<'a, 'tcx: 'a> {
23 fields: CombineFields<'a, 'tcx>
24 }
25
26 impl<'a, 'tcx> Equate<'a, 'tcx> {
27 pub fn new(fields: CombineFields<'a, 'tcx>) -> Equate<'a, 'tcx> {
28 Equate { fields: fields }
29 }
30
31 pub fn obligations(self) -> PredicateObligations<'tcx> {
32 self.fields.obligations
33 }
34 }
35
36 impl<'a, 'tcx> TypeRelation<'a,'tcx> for Equate<'a, 'tcx> {
37 fn tag(&self) -> &'static str { "Equate" }
38
39 fn tcx(&self) -> &'a TyCtxt<'tcx> { self.fields.tcx() }
40
41 fn a_is_expected(&self) -> bool { self.fields.a_is_expected }
42
43 fn relate_with_variance<T:Relate<'a,'tcx>>(&mut self,
44 _: ty::Variance,
45 a: &T,
46 b: &T)
47 -> RelateResult<'tcx, T>
48 {
49 self.relate(a, b)
50 }
51
52 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
53 debug!("{}.tys({:?}, {:?})", self.tag(),
54 a, b);
55 if a == b { return Ok(a); }
56
57 let infcx = self.fields.infcx;
58 let a = infcx.type_variables.borrow_mut().replace_if_possible(a);
59 let b = infcx.type_variables.borrow_mut().replace_if_possible(b);
60 match (&a.sty, &b.sty) {
61 (&ty::TyInfer(TyVar(a_id)), &ty::TyInfer(TyVar(b_id))) => {
62 infcx.type_variables.borrow_mut().relate_vars(a_id, EqTo, b_id);
63 Ok(a)
64 }
65
66 (&ty::TyInfer(TyVar(a_id)), _) => {
67 self.fields.instantiate(b, EqTo, a_id)?;
68 Ok(a)
69 }
70
71 (_, &ty::TyInfer(TyVar(b_id))) => {
72 self.fields.instantiate(a, EqTo, b_id)?;
73 Ok(a)
74 }
75
76 _ => {
77 combine::super_combine_tys(self.fields.infcx, self, a, b)?;
78 Ok(a)
79 }
80 }
81 }
82
83 fn regions(&mut self, a: ty::Region, b: ty::Region) -> RelateResult<'tcx, ty::Region> {
84 debug!("{}.regions({:?}, {:?})",
85 self.tag(),
86 a,
87 b);
88 let origin = Subtype(self.fields.trace.clone());
89 self.fields.infcx.region_vars.make_eqregion(origin, a, b);
90 Ok(a)
91 }
92
93 fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
94 -> RelateResult<'tcx, ty::Binder<T>>
95 where T: Relate<'a, 'tcx>
96 {
97 self.fields.higher_ranked_sub(a, b)?;
98 self.fields.higher_ranked_sub(b, a)
99 }
100 }