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::infer::unify_key::replace_if_possible;
10 use crate::mir::interpret::ConstValue;
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>,
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>
23 Sub { fields: f, a_is_expected: a_is_expected }
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;
29 self.a_is_expected = !self.a_is_expected;
34 impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
35 for Sub<'combine, 'infcx, 'gcx, 'tcx>
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 }
41 fn with_cause<F,R>(&mut self, cause: Cause, f: F) -> R
42 where F: FnOnce(&mut Self) -> R
44 debug!("sub with_cause={:?}", cause);
45 let old_cause = mem::replace(&mut self.fields.cause, Some(cause));
47 debug!("sub old_cause={:?}", old_cause);
48 self.fields.cause = old_cause;
52 fn relate_with_variance<T: Relate<'tcx>>(&mut self,
53 variance: ty::Variance,
56 -> RelateResult<'tcx, T>
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) }),
66 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
67 debug!("{}.tys({:?}, {:?})", self.tag(), a, b);
69 if a == b { return Ok(a); }
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
79 assert!(!a.has_escaping_bound_vars());
80 assert!(!b.has_escaping_bound_vars());
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(
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,
101 (&ty::Infer(TyVar(a_id)), _) => {
103 .instantiate(b, RelationDir::SupertypeOf, a_id, !self.a_is_expected)?;
106 (_, &ty::Infer(TyVar(b_id))) => {
107 self.fields.instantiate(a, RelationDir::SubtypeOf, b_id, self.a_is_expected)?;
111 (&ty::Error, _) | (_, &ty::Error) => {
112 infcx.set_tainted_by_errors();
113 Ok(self.tcx().types.err)
117 self.fields.infcx.super_combine_tys(self, a, b)?;
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);
128 // FIXME -- we have more fine-grained information available
129 // from the "cause" field, we could perhaps give more tailored
131 let origin = SubregionOrigin::Subtype(self.fields.trace.clone());
132 self.fields.infcx.borrow_region_constraints()
133 .make_subregion(origin, a, b);
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); }
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);
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
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)?;
176 self.fields.infcx.super_combine_consts(self, a, b)?;
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>
184 self.fields.higher_ranked_sub(a, b, self.a_is_expected)