1 // Copyright 2012 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.
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.
11 use super::SubregionOrigin;
12 use super::combine::{CombineFields, RelationDir};
14 use traits::Obligation;
15 use ty::{self, Ty, TyCtxt};
17 use ty::fold::TypeFoldable;
18 use ty::relate::{Cause, Relate, RelateResult, TypeRelation};
21 /// Ensures `a` is made a subtype of `b`. Returns `a` on success.
22 pub struct Sub<'combine, 'infcx: 'combine, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> {
23 fields: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>,
27 impl<'combine, 'infcx, 'gcx, 'tcx> Sub<'combine, 'infcx, 'gcx, 'tcx> {
28 pub fn new(f: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>, a_is_expected: bool)
29 -> Sub<'combine, 'infcx, 'gcx, 'tcx>
31 Sub { fields: f, a_is_expected: a_is_expected }
34 fn with_expected_switched<R, F: FnOnce(&mut Self) -> R>(&mut self, f: F) -> R {
35 self.a_is_expected = !self.a_is_expected;
37 self.a_is_expected = !self.a_is_expected;
42 impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
43 for Sub<'combine, 'infcx, 'gcx, 'tcx>
45 fn tag(&self) -> &'static str { "Sub" }
46 fn tcx(&self) -> TyCtxt<'infcx, 'gcx, 'tcx> { self.fields.infcx.tcx }
47 fn a_is_expected(&self) -> bool { self.a_is_expected }
49 fn with_cause<F,R>(&mut self, cause: Cause, f: F) -> R
50 where F: FnOnce(&mut Self) -> R
52 debug!("sub with_cause={:?}", cause);
53 let old_cause = mem::replace(&mut self.fields.cause, Some(cause));
55 debug!("sub old_cause={:?}", old_cause);
56 self.fields.cause = old_cause;
60 fn relate_with_variance<T: Relate<'tcx>>(&mut self,
61 variance: ty::Variance,
64 -> RelateResult<'tcx, T>
67 ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b),
68 ty::Covariant => self.relate(a, b),
69 ty::Bivariant => Ok(a.clone()),
70 ty::Contravariant => self.with_expected_switched(|this| { this.relate(b, a) }),
74 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
75 debug!("{}.tys({:?}, {:?})", self.tag(), a, b);
77 if a == b { return Ok(a); }
79 let infcx = self.fields.infcx;
80 let a = infcx.type_variables.borrow_mut().replace_if_possible(a);
81 let b = infcx.type_variables.borrow_mut().replace_if_possible(b);
82 match (&a.sty, &b.sty) {
83 (&ty::Infer(TyVar(a_vid)), &ty::Infer(TyVar(b_vid))) => {
84 // Shouldn't have any LBR here, so we can safely put
85 // this under a binder below without fear of accidental
87 assert!(!a.has_escaping_regions());
88 assert!(!b.has_escaping_regions());
90 // can't make progress on `A <: B` if both A and B are
91 // type variables, so record an obligation. We also
92 // have to record in the `type_variables` tracker that
93 // the two variables are equal modulo subtyping, which
94 // is important to the occurs check later on.
95 infcx.type_variables.borrow_mut().sub(a_vid, b_vid);
96 self.fields.obligations.push(
98 self.fields.trace.cause.clone(),
99 self.fields.param_env,
100 ty::Predicate::Subtype(
101 ty::Binder::dummy(ty::SubtypePredicate {
102 a_is_expected: self.a_is_expected,
109 (&ty::Infer(TyVar(a_id)), _) => {
111 .instantiate(b, RelationDir::SupertypeOf, a_id, !self.a_is_expected)?;
114 (_, &ty::Infer(TyVar(b_id))) => {
115 self.fields.instantiate(a, RelationDir::SubtypeOf, b_id, self.a_is_expected)?;
119 (&ty::Error, _) | (_, &ty::Error) => {
120 infcx.set_tainted_by_errors();
121 Ok(self.tcx().types.err)
125 self.fields.infcx.super_combine_tys(self, a, b)?;
131 fn regions(&mut self, a: ty::Region<'tcx>, b: ty::Region<'tcx>)
132 -> RelateResult<'tcx, ty::Region<'tcx>> {
133 debug!("{}.regions({:?}, {:?}) self.cause={:?}",
134 self.tag(), a, b, self.fields.cause);
136 // FIXME -- we have more fine-grained information available
137 // from the "cause" field, we could perhaps give more tailored
139 let origin = SubregionOrigin::Subtype(self.fields.trace.clone());
140 self.fields.infcx.borrow_region_constraints()
141 .make_subregion(origin, a, b);
146 fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
147 -> RelateResult<'tcx, ty::Binder<T>>
148 where T: Relate<'tcx>
150 self.fields.higher_ranked_sub(a, b, self.a_is_expected)