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