]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/variance/xform.rs
Auto merge of #31684 - tmiasko:alternate-stack, r=alexcrichton
[rust.git] / src / librustc_typeck / variance / xform.rs
1 // Copyright 2013 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.
4 //
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.
10
11 use middle::ty;
12
13 pub trait Xform {
14     fn xform(self, v: Self) -> Self;
15 }
16
17 impl Xform for ty::Variance {
18     fn xform(self, v: ty::Variance) -> ty::Variance {
19         // "Variance transformation", Figure 1 of The Paper
20         match (self, v) {
21             // Figure 1, column 1.
22             (ty::Covariant, ty::Covariant) => ty::Covariant,
23             (ty::Covariant, ty::Contravariant) => ty::Contravariant,
24             (ty::Covariant, ty::Invariant) => ty::Invariant,
25             (ty::Covariant, ty::Bivariant) => ty::Bivariant,
26
27             // Figure 1, column 2.
28             (ty::Contravariant, ty::Covariant) => ty::Contravariant,
29             (ty::Contravariant, ty::Contravariant) => ty::Covariant,
30             (ty::Contravariant, ty::Invariant) => ty::Invariant,
31             (ty::Contravariant, ty::Bivariant) => ty::Bivariant,
32
33             // Figure 1, column 3.
34             (ty::Invariant, _) => ty::Invariant,
35
36             // Figure 1, column 4.
37             (ty::Bivariant, _) => ty::Bivariant,
38         }
39     }
40 }
41
42 pub fn glb(v1: ty::Variance, v2: ty::Variance) -> ty::Variance {
43     // Greatest lower bound of the variance lattice as
44     // defined in The Paper:
45     //
46     //       *
47     //    -     +
48     //       o
49     match (v1, v2) {
50         (ty::Invariant, _) | (_, ty::Invariant) => ty::Invariant,
51
52         (ty::Covariant, ty::Contravariant) => ty::Invariant,
53         (ty::Contravariant, ty::Covariant) => ty::Invariant,
54
55         (ty::Covariant, ty::Covariant) => ty::Covariant,
56
57         (ty::Contravariant, ty::Contravariant) => ty::Contravariant,
58
59         (x, ty::Bivariant) | (ty::Bivariant, x) => x,
60     }
61 }