]> git.lizzy.rs Git - rust.git/blob - src/librustc/infer/sub.rs
Auto merge of #61484 - nnethercote:avoid-more-hygiene-lookups, r=petrochenkov
[rust.git] / src / librustc / infer / sub.rs
1 use super::SubregionOrigin;
2 use super::combine::{CombineFields, RelationDir, const_unification_error};
3
4 use crate::traits::Obligation;
5 use crate::ty::{self, Ty, TyCtxt, InferConst};
6 use crate::ty::TyVar;
7 use crate::ty::fold::TypeFoldable;
8 use crate::ty::relate::{Cause, Relate, RelateResult, TypeRelation};
9 use crate::infer::unify_key::replace_if_possible;
10 use crate::mir::interpret::ConstValue;
11 use std::mem;
12
13 /// Ensures `a` is made a subtype of `b`. Returns `a` on success.
14 pub struct Sub<'combine, 'infcx: 'combine, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> {
15     fields: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>,
16     a_is_expected: bool,
17 }
18
19 impl<'combine, 'infcx, 'gcx, 'tcx> Sub<'combine, 'infcx, 'gcx, 'tcx> {
20     pub fn new(f: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>, a_is_expected: bool)
21         -> Sub<'combine, 'infcx, 'gcx, 'tcx>
22     {
23         Sub { fields: f, a_is_expected: a_is_expected }
24     }
25
26     fn with_expected_switched<R, F: FnOnce(&mut Self) -> R>(&mut self, f: F) -> R {
27         self.a_is_expected = !self.a_is_expected;
28         let result = f(self);
29         self.a_is_expected = !self.a_is_expected;
30         result
31     }
32 }
33
34 impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
35     for Sub<'combine, 'infcx, 'gcx, 'tcx>
36 {
37     fn tag(&self) -> &'static str { "Sub" }
38     fn tcx(&self) -> TyCtxt<'infcx, 'gcx, 'tcx> { self.fields.infcx.tcx }
39     fn a_is_expected(&self) -> bool { self.a_is_expected }
40
41     fn with_cause<F,R>(&mut self, cause: Cause, f: F) -> R
42         where F: FnOnce(&mut Self) -> R
43     {
44         debug!("sub with_cause={:?}", cause);
45         let old_cause = mem::replace(&mut self.fields.cause, Some(cause));
46         let r = f(self);
47         debug!("sub old_cause={:?}", old_cause);
48         self.fields.cause = old_cause;
49         r
50     }
51
52     fn relate_with_variance<T: Relate<'tcx>>(&mut self,
53                                              variance: ty::Variance,
54                                              a: &T,
55                                              b: &T)
56                                              -> RelateResult<'tcx, T>
57     {
58         match variance {
59             ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b),
60             ty::Covariant => self.relate(a, b),
61             ty::Bivariant => Ok(a.clone()),
62             ty::Contravariant => self.with_expected_switched(|this| { this.relate(b, a) }),
63         }
64     }
65
66     fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
67         debug!("{}.tys({:?}, {:?})", self.tag(), a, b);
68
69         if a == b { return Ok(a); }
70
71         let infcx = self.fields.infcx;
72         let a = infcx.type_variables.borrow_mut().replace_if_possible(a);
73         let b = infcx.type_variables.borrow_mut().replace_if_possible(b);
74         match (&a.sty, &b.sty) {
75             (&ty::Infer(TyVar(a_vid)), &ty::Infer(TyVar(b_vid))) => {
76                 // Shouldn't have any LBR here, so we can safely put
77                 // this under a binder below without fear of accidental
78                 // capture.
79                 assert!(!a.has_escaping_bound_vars());
80                 assert!(!b.has_escaping_bound_vars());
81
82                 // can't make progress on `A <: B` if both A and B are
83                 // type variables, so record an obligation. We also
84                 // have to record in the `type_variables` tracker that
85                 // the two variables are equal modulo subtyping, which
86                 // is important to the occurs check later on.
87                 infcx.type_variables.borrow_mut().sub(a_vid, b_vid);
88                 self.fields.obligations.push(
89                     Obligation::new(
90                         self.fields.trace.cause.clone(),
91                         self.fields.param_env,
92                         ty::Predicate::Subtype(
93                             ty::Binder::dummy(ty::SubtypePredicate {
94                                 a_is_expected: self.a_is_expected,
95                                 a,
96                                 b,
97                             }))));
98
99                 Ok(a)
100             }
101             (&ty::Infer(TyVar(a_id)), _) => {
102                 self.fields
103                     .instantiate(b, RelationDir::SupertypeOf, a_id, !self.a_is_expected)?;
104                 Ok(a)
105             }
106             (_, &ty::Infer(TyVar(b_id))) => {
107                 self.fields.instantiate(a, RelationDir::SubtypeOf, b_id, self.a_is_expected)?;
108                 Ok(a)
109             }
110
111             (&ty::Error, _) | (_, &ty::Error) => {
112                 infcx.set_tainted_by_errors();
113                 Ok(self.tcx().types.err)
114             }
115
116             _ => {
117                 self.fields.infcx.super_combine_tys(self, a, b)?;
118                 Ok(a)
119             }
120         }
121     }
122
123     fn regions(&mut self, a: ty::Region<'tcx>, b: ty::Region<'tcx>)
124                -> RelateResult<'tcx, ty::Region<'tcx>> {
125         debug!("{}.regions({:?}, {:?}) self.cause={:?}",
126                self.tag(), a, b, self.fields.cause);
127
128         // FIXME -- we have more fine-grained information available
129         // from the "cause" field, we could perhaps give more tailored
130         // error messages.
131         let origin = SubregionOrigin::Subtype(self.fields.trace.clone());
132         self.fields.infcx.borrow_region_constraints()
133                          .make_subregion(origin, a, b);
134
135         Ok(a)
136     }
137
138     fn consts(
139         &mut self,
140         a: &'tcx ty::Const<'tcx>,
141         b: &'tcx ty::Const<'tcx>,
142     ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
143         debug!("{}.consts({:?}, {:?})", self.tag(), a, b);
144         if a == b { return Ok(a); }
145
146         let infcx = self.fields.infcx;
147         let a = replace_if_possible(infcx.const_unification_table.borrow_mut(), a);
148         let b = replace_if_possible(infcx.const_unification_table.borrow_mut(), b);
149
150         // Consts can only be equal or unequal to each other: there's no subtyping
151         // relation, so we're just going to perform equating here instead.
152         let a_is_expected = self.a_is_expected();
153         match (a.val, b.val) {
154             (ConstValue::Infer(InferConst::Var(a_vid)),
155                 ConstValue::Infer(InferConst::Var(b_vid))) => {
156                 infcx.const_unification_table
157                     .borrow_mut()
158                     .unify_var_var(a_vid, b_vid)
159                     .map_err(|e| const_unification_error(a_is_expected, e))?;
160                 return Ok(a);
161             }
162
163             (ConstValue::Infer(InferConst::Var(a_id)), _) => {
164                 self.fields.infcx.unify_const_variable(a_is_expected, a_id, b)?;
165                 return Ok(a);
166             }
167
168             (_, ConstValue::Infer(InferConst::Var(b_id))) => {
169                 self.fields.infcx.unify_const_variable(!a_is_expected, b_id, a)?;
170                 return Ok(a);
171             }
172
173             _ => {}
174         }
175
176         self.fields.infcx.super_combine_consts(self, a, b)?;
177         Ok(a)
178     }
179
180     fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
181                   -> RelateResult<'tcx, ty::Binder<T>>
182         where T: Relate<'tcx>
183     {
184         self.fields.higher_ranked_sub(a, b, self.a_is_expected)
185     }
186 }