1 use super::SubregionOrigin;
2 use super::combine::{CombineFields, RelationDir, const_unification_error};
4 use crate::traits::Obligation;
5 use crate::ty::{self, Ty, TyCtxt, InferConst};
7 use crate::ty::fold::TypeFoldable;
8 use crate::ty::relate::{Cause, Relate, RelateResult, TypeRelation};
9 use crate::mir::interpret::ConstValue;
12 /// Ensures `a` is made a subtype of `b`. Returns `a` on success.
13 pub struct Sub<'combine, 'infcx: 'combine, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> {
14 fields: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>,
18 impl<'combine, 'infcx, 'gcx, 'tcx> Sub<'combine, 'infcx, 'gcx, 'tcx> {
19 pub fn new(f: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>, a_is_expected: bool)
20 -> Sub<'combine, 'infcx, 'gcx, 'tcx>
22 Sub { fields: f, a_is_expected: a_is_expected }
25 fn with_expected_switched<R, F: FnOnce(&mut Self) -> R>(&mut self, f: F) -> R {
26 self.a_is_expected = !self.a_is_expected;
28 self.a_is_expected = !self.a_is_expected;
33 impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
34 for Sub<'combine, 'infcx, 'gcx, 'tcx>
36 fn tag(&self) -> &'static str { "Sub" }
37 fn tcx(&self) -> TyCtxt<'infcx, 'gcx, 'tcx> { self.fields.infcx.tcx }
38 fn a_is_expected(&self) -> bool { self.a_is_expected }
40 fn with_cause<F,R>(&mut self, cause: Cause, f: F) -> R
41 where F: FnOnce(&mut Self) -> R
43 debug!("sub with_cause={:?}", cause);
44 let old_cause = mem::replace(&mut self.fields.cause, Some(cause));
46 debug!("sub old_cause={:?}", old_cause);
47 self.fields.cause = old_cause;
51 fn relate_with_variance<T: Relate<'tcx>>(&mut self,
52 variance: ty::Variance,
55 -> RelateResult<'tcx, T>
58 ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b),
59 ty::Covariant => self.relate(a, b),
60 ty::Bivariant => Ok(a.clone()),
61 ty::Contravariant => self.with_expected_switched(|this| { this.relate(b, a) }),
65 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
66 debug!("{}.tys({:?}, {:?})", self.tag(), a, b);
68 if a == b { return Ok(a); }
70 let infcx = self.fields.infcx;
71 let a = infcx.type_variables.borrow_mut().replace_if_possible(a);
72 let b = infcx.type_variables.borrow_mut().replace_if_possible(b);
73 match (&a.sty, &b.sty) {
74 (&ty::Infer(TyVar(a_vid)), &ty::Infer(TyVar(b_vid))) => {
75 // Shouldn't have any LBR here, so we can safely put
76 // this under a binder below without fear of accidental
78 assert!(!a.has_escaping_bound_vars());
79 assert!(!b.has_escaping_bound_vars());
81 // can't make progress on `A <: B` if both A and B are
82 // type variables, so record an obligation. We also
83 // have to record in the `type_variables` tracker that
84 // the two variables are equal modulo subtyping, which
85 // is important to the occurs check later on.
86 infcx.type_variables.borrow_mut().sub(a_vid, b_vid);
87 self.fields.obligations.push(
89 self.fields.trace.cause.clone(),
90 self.fields.param_env,
91 ty::Predicate::Subtype(
92 ty::Binder::dummy(ty::SubtypePredicate {
93 a_is_expected: self.a_is_expected,
100 (&ty::Infer(TyVar(a_id)), _) => {
102 .instantiate(b, RelationDir::SupertypeOf, a_id, !self.a_is_expected)?;
105 (_, &ty::Infer(TyVar(b_id))) => {
106 self.fields.instantiate(a, RelationDir::SubtypeOf, b_id, self.a_is_expected)?;
110 (&ty::Error, _) | (_, &ty::Error) => {
111 infcx.set_tainted_by_errors();
112 Ok(self.tcx().types.err)
116 self.fields.infcx.super_combine_tys(self, a, b)?;
122 fn regions(&mut self, a: ty::Region<'tcx>, b: ty::Region<'tcx>)
123 -> RelateResult<'tcx, ty::Region<'tcx>> {
124 debug!("{}.regions({:?}, {:?}) self.cause={:?}",
125 self.tag(), a, b, self.fields.cause);
127 // FIXME -- we have more fine-grained information available
128 // from the "cause" field, we could perhaps give more tailored
130 let origin = SubregionOrigin::Subtype(self.fields.trace.clone());
131 self.fields.infcx.borrow_region_constraints()
132 .make_subregion(origin, a, b);
139 a: &'tcx ty::LazyConst<'tcx>,
140 b: &'tcx ty::LazyConst<'tcx>,
141 ) -> RelateResult<'tcx, &'tcx ty::LazyConst<'tcx>> {
142 debug!("{}.consts({:?}, {:?})", self.tag(), a, b);
143 if a == b { return Ok(a); }
145 let infcx = self.fields.infcx;
146 let a = infcx.const_unification_table.borrow_mut().replace_if_possible(a);
147 let b = infcx.const_unification_table.borrow_mut().replace_if_possible(b);
149 // Consts can only be equal or unequal to each other: there's no subtyping
150 // relation, so we're just going to perform equating here instead.
151 let a_is_expected = self.a_is_expected();
152 if let (&ty::LazyConst::Evaluated(a_eval), &ty::LazyConst::Evaluated(b_eval)) = (a, b) {
153 match (a_eval.val, b_eval.val) {
154 (ConstValue::Infer(InferConst::Var(a_vid)),
155 ConstValue::Infer(InferConst::Var(b_vid))) => {
156 infcx.const_unification_table
158 .unify_var_var(a_vid, b_vid)
159 .map_err(|e| const_unification_error(a_is_expected, e))?;
163 (ConstValue::Infer(InferConst::Var(a_id)), _) => {
164 self.fields.infcx.unify_const_variable(a_is_expected, a_id, b)?;
168 (_, ConstValue::Infer(InferConst::Var(b_id))) => {
169 self.fields.infcx.unify_const_variable(!a_is_expected, b_id, a)?;
177 self.fields.infcx.super_combine_consts(self, a, b)?;
181 fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
182 -> RelateResult<'tcx, ty::Binder<T>>
183 where T: Relate<'tcx>
185 self.fields.higher_ranked_sub(a, b, self.a_is_expected)