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::combine
::{self, CombineFields}
;
12 use super::higher_ranked
::HigherRankedRelations
;
13 use super::SubregionOrigin
;
14 use super::type_variable
::{SubtypeOf, SupertypeOf}
;
16 use middle
::ty
::{self, Ty}
;
17 use middle
::ty
::TyVar
;
18 use middle
::ty
::relate
::{Cause, Relate, RelateResult, TypeRelation}
;
21 /// Ensures `a` is made a subtype of `b`. Returns `a` on success.
22 pub struct Sub
<'a
, 'tcx
: 'a
> {
23 fields
: CombineFields
<'a
, 'tcx
>,
26 impl<'a
, 'tcx
> Sub
<'a
, 'tcx
> {
27 pub fn new(f
: CombineFields
<'a
, 'tcx
>) -> Sub
<'a
, 'tcx
> {
32 impl<'a
, 'tcx
> TypeRelation
<'a
, 'tcx
> for Sub
<'a
, 'tcx
> {
33 fn tag(&self) -> &'
static str { "Sub" }
34 fn tcx(&self) -> &'a ty
::ctxt
<'tcx
> { self.fields.infcx.tcx }
35 fn a_is_expected(&self) -> bool { self.fields.a_is_expected }
37 fn with_cause
<F
,R
>(&mut self, cause
: Cause
, f
: F
) -> R
38 where F
: FnOnce(&mut Self) -> R
40 debug
!("sub with_cause={:?}", cause
);
41 let old_cause
= mem
::replace(&mut self.fields
.cause
, Some(cause
));
43 debug
!("sub old_cause={:?}", old_cause
);
44 self.fields
.cause
= old_cause
;
48 fn relate_with_variance
<T
:Relate
<'a
,'tcx
>>(&mut self,
49 variance
: ty
::Variance
,
52 -> RelateResult
<'tcx
, T
>
55 ty
::Invariant
=> self.fields
.equate().relate(a
, b
),
56 ty
::Covariant
=> self.relate(a
, b
),
57 ty
::Bivariant
=> self.fields
.bivariate().relate(a
, b
),
58 ty
::Contravariant
=> self.fields
.switch_expected().sub().relate(b
, a
),
62 fn tys(&mut self, a
: Ty
<'tcx
>, b
: Ty
<'tcx
>) -> RelateResult
<'tcx
, Ty
<'tcx
>> {
63 debug
!("{}.tys({:?}, {:?})", self.tag(), a
, b
);
65 if a
== b { return Ok(a); }
67 let infcx
= self.fields
.infcx
;
68 let a
= infcx
.type_variables
.borrow().replace_if_possible(a
);
69 let b
= infcx
.type_variables
.borrow().replace_if_possible(b
);
70 match (&a
.sty
, &b
.sty
) {
71 (&ty
::TyInfer(TyVar(a_id
)), &ty
::TyInfer(TyVar(b_id
))) => {
74 .relate_vars(a_id
, SubtypeOf
, b_id
);
77 (&ty
::TyInfer(TyVar(a_id
)), _
) => {
80 .instantiate(b
, SupertypeOf
, a_id
));
83 (_
, &ty
::TyInfer(TyVar(b_id
))) => {
84 try
!(self.fields
.instantiate(a
, SubtypeOf
, b_id
));
88 (&ty
::TyError
, _
) | (_
, &ty
::TyError
) => {
89 Ok(self.tcx().types
.err
)
93 try
!(combine
::super_combine_tys(self.fields
.infcx
, self, a
, b
));
99 fn regions(&mut self, a
: ty
::Region
, b
: ty
::Region
) -> RelateResult
<'tcx
, ty
::Region
> {
100 debug
!("{}.regions({:?}, {:?}) self.cause={:?}",
101 self.tag(), a
, b
, self.fields
.cause
);
102 // FIXME -- we have more fine-grained information available
103 // from the "cause" field, we could perhaps give more tailored
105 let origin
= SubregionOrigin
::Subtype(self.fields
.trace
.clone());
106 self.fields
.infcx
.region_vars
.make_subregion(origin
, a
, b
);
110 fn binders
<T
>(&mut self, a
: &ty
::Binder
<T
>, b
: &ty
::Binder
<T
>)
111 -> RelateResult
<'tcx
, ty
::Binder
<T
>>
112 where T
: Relate
<'a
,'tcx
>
114 self.fields
.higher_ranked_sub(a
, b
)