]> git.proxmox.com Git - rustc.git/blame - src/librustc/infer/equate.rs
New upstream version 1.32.0+dfsg1
[rustc.git] / src / librustc / infer / equate.rs
CommitLineData
1a4d82fc
JJ
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
cc61c64b 11use super::combine::{CombineFields, RelationDir};
c34b1796 12use super::{Subtype};
cc61c64b
XL
13
14use hir::def_id::DefId;
c34b1796 15
54a0048b
SL
16use ty::{self, Ty, TyCtxt};
17use ty::TyVar;
cc61c64b
XL
18use ty::subst::Substs;
19use ty::relate::{self, Relate, RelateResult, TypeRelation};
1a4d82fc 20
b039eaaf 21/// Ensures `a` is made equal to `b`. Returns `a` on success.
5bcae85e
SL
22pub struct Equate<'combine, 'infcx: 'combine, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> {
23 fields: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>,
24 a_is_expected: bool,
1a4d82fc
JJ
25}
26
5bcae85e
SL
27impl<'combine, 'infcx, 'gcx, 'tcx> Equate<'combine, 'infcx, 'gcx, 'tcx> {
28 pub fn new(fields: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>, a_is_expected: bool)
29 -> Equate<'combine, 'infcx, 'gcx, 'tcx>
30 {
31 Equate { fields: fields, a_is_expected: a_is_expected }
54a0048b 32 }
c34b1796 33}
1a4d82fc 34
5bcae85e
SL
35impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
36 for Equate<'combine, 'infcx, 'gcx, 'tcx>
37{
c34b1796 38 fn tag(&self) -> &'static str { "Equate" }
1a4d82fc 39
5bcae85e 40 fn tcx(&self) -> TyCtxt<'infcx, 'gcx, 'tcx> { self.fields.tcx() }
1a4d82fc 41
5bcae85e 42 fn a_is_expected(&self) -> bool { self.a_is_expected }
1a4d82fc 43
cc61c64b
XL
44 fn relate_item_substs(&mut self,
45 _item_def_id: DefId,
46 a_subst: &'tcx Substs<'tcx>,
47 b_subst: &'tcx Substs<'tcx>)
48 -> RelateResult<'tcx, &'tcx Substs<'tcx>>
49 {
50 // NB: Once we are equating types, we don't care about
51 // variance, so don't try to lookup the variance here. This
52 // also avoids some cycles (e.g. #41849) since looking up
53 // variance requires computing types which can require
54 // performing trait matching (which then performs equality
55 // unification).
56
57 relate::relate_substs(self, None, a_subst, b_subst)
58 }
59
a7813a04
XL
60 fn relate_with_variance<T: Relate<'tcx>>(&mut self,
61 _: ty::Variance,
62 a: &T,
63 b: &T)
64 -> RelateResult<'tcx, T>
1a4d82fc 65 {
c34b1796 66 self.relate(a, b)
1a4d82fc
JJ
67 }
68
c34b1796 69 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
62682a34
SL
70 debug!("{}.tys({:?}, {:?})", self.tag(),
71 a, b);
1a4d82fc
JJ
72 if a == b { return Ok(a); }
73
74 let infcx = self.fields.infcx;
54a0048b
SL
75 let a = infcx.type_variables.borrow_mut().replace_if_possible(a);
76 let b = infcx.type_variables.borrow_mut().replace_if_possible(b);
13cf67c4
XL
77
78 debug!("{}.tys: replacements ({:?}, {:?})", self.tag(), a, b);
79
1a4d82fc 80 match (&a.sty, &b.sty) {
b7449926 81 (&ty::Infer(TyVar(a_id)), &ty::Infer(TyVar(b_id))) => {
cc61c64b 82 infcx.type_variables.borrow_mut().equate(a_id, b_id);
1a4d82fc
JJ
83 }
84
b7449926 85 (&ty::Infer(TyVar(a_id)), _) => {
cc61c64b 86 self.fields.instantiate(b, RelationDir::EqTo, a_id, self.a_is_expected)?;
1a4d82fc
JJ
87 }
88
b7449926 89 (_, &ty::Infer(TyVar(b_id))) => {
cc61c64b 90 self.fields.instantiate(a, RelationDir::EqTo, b_id, self.a_is_expected)?;
1a4d82fc
JJ
91 }
92
93 _ => {
a7813a04 94 self.fields.infcx.super_combine_tys(self, a, b)?;
1a4d82fc
JJ
95 }
96 }
0bf4aa26
XL
97
98 Ok(a)
1a4d82fc
JJ
99 }
100
7cac9316
XL
101 fn regions(&mut self, a: ty::Region<'tcx>, b: ty::Region<'tcx>)
102 -> RelateResult<'tcx, ty::Region<'tcx>> {
62682a34 103 debug!("{}.regions({:?}, {:?})",
c34b1796 104 self.tag(),
62682a34
SL
105 a,
106 b);
c34b1796 107 let origin = Subtype(self.fields.trace.clone());
abe05a73
XL
108 self.fields.infcx.borrow_region_constraints()
109 .make_eqregion(origin, a, b);
c34b1796
AL
110 Ok(a)
111 }
112
113 fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
114 -> RelateResult<'tcx, ty::Binder<T>>
a7813a04 115 where T: Relate<'tcx>
1a4d82fc 116 {
5bcae85e
SL
117 self.fields.higher_ranked_sub(a, b, self.a_is_expected)?;
118 self.fields.higher_ranked_sub(b, a, self.a_is_expected)
1a4d82fc
JJ
119 }
120}