// GADTs and existential types (ADR 0041). pub type Expr { IntLit(Int) -> Expr , BoolLit(Bool) -> Expr , Add(Expr, Expr) -> Expr , If(Expr, Expr, Expr) -> Expr , Pair(Expr, Expr) -> Expr<(b, c)> } pub type Showable { Showable(b) -> Showable where Show } pub type Box { Box(b) -> Box } // Constructor schemes: their own variables, and their constraints. pub fn int_lit :: -> fn(Int) -> Expr { IntLit } //~ TYPE int_lit: fn() -> fn(Int) -> Expr pub fn eval :: e: Expr -> a { match e { IntLit(n) -> n , BoolLit(b) -> b , Add(x, y) -> eval x + eval y , If(c, t, f) -> if eval c { eval t } else { eval f } , Pair(x, y) -> (eval x, eval y) } } // Each arm knows its own index only. pub fn wrong :: e: Expr -> a { match e { IntLit(n) -> n , BoolLit(b) -> 1 //~ ERROR E0202 expected `a`, found `Int` , _ -> wrong e } } // A matched `IntLit` proves `a` is `Int`. pub fn cast :: e: Expr x: a -> Int { match e { IntLit(_) -> x, _ -> 0 } } // `Expr` has no `BoolLit` or `Pair`: no arm needed. pub fn ints :: e: Expr -> Int { match e { IntLit(n) -> n , Add(x, y) -> ints x + ints y , If(c, t, f) -> if eval c { ints t } else { ints f } } } pub fn missing :: e: Expr -> Int { match e //~ ERROR E0301 `Add(_, _)` is not covered { IntLit(n) -> n, If(c, t, f) -> 0 } } // Under `IntLit` in the first column, the second is an `Expr` too. pub fn both :: x: Expr y: Expr -> Int { match (x, y) { (IntLit(n), IntLit(m)) -> n + m , (IntLit(_), Add(_, _)) -> 1 , (IntLit(_), If(_, _, _)) -> 2 , (BoolLit(_), _) -> 3 , (Add(_, _), _) -> 4 , (If(_, _, _), _) -> 5 , (Pair(_, _), _) -> 6 } } pub fn impossible :: e: Expr -> Int { match e { BoolLit //~ WARN W0301 //~^ ERROR E0202 `BoolLit` cannot match a value of type `Expr` (b) -> 0 , _ -> 1 } } pub fn describe :: s: Showable -> String { match s { Showable(x) -> show x } } pub fn leak :: s: Showable -> Int { let r = match s { Showable(x) -> x //~ ERROR E0705 would escape its `match` arm } 0 } pub fn no_show :: b: Box -> String { match b { Box(x) -> show x //~ ERROR E0402 `b` is not known to implement `Show` } } pub fn unpack :: s: Showable -> Int { let Showable(_) = s //~ ERROR E0215 take it apart in a `match` arm 0 } pub fn alternative :: e: Expr -> Int { match e { IntLit(_) | BoolLit(_) -> 0 //~ ERROR E0215 take it apart in a `match` arm //~^ ERROR E0215 take it apart in a `match` arm , _ -> 1 } } pub fn not_showable :: -> Showable { Showable(fn x { x }) //~ ERROR E0401 does not implement `Show` } pub type Wrong { Bad(Int) -> Option //~ ERROR E0214 must be `Wrong` applied to 1 type } pub type Loose { Loose(Int) -> Loose where Show //~ ERROR E0211 `z` } @derive Show pub type Derived //~ ERROR E0408 refines or hides types { Derived(b) -> Derived where Show } pub type Rec { Rec(count: Int) -> Rec } pub fn read :: r: Rec -> Int { r.count //~ ERROR E0215 use `match` to read `count` } // Equalities are between types: no refined constructor for a type with a // higher-kinded parameter. pub type Hk { Wrap //~ ERROR E0214 must be types, or rows passed through unchanged (f) -> Hk } // A row parameter passed through unchanged is fine (ADR 0041, Revision 2): // the state `s` is existential, the row is carried. pub type FineRow { MkFine(s, step: fn(s) -> a / ) -> FineRow } // A match without arms on an index no constructor fits needs none, and // one some constructor fits is not exhaustive (issue #218). pub type OnlyInt { OnlyInt(Int) -> OnlyInt } pub fn no_constructor_fits :: o: OnlyInt -> Int { match o {} } pub fn ints_need_arms :: e: Expr -> Int { match e {} //~ ERROR E0301 is not covered } // One refinement, read by inference, trait resolution and exhaustiveness // alike (issue #224): in the `IntLit` and `Add` arms `a` is `Int`, so // `x + 1` needs `Num`, which holds there and only there; and the // arms must still cover every constructor that can build an `Expr`. pub fn plus_one :: e: Expr x: a -> a { match e { IntLit(_) -> x + 1 , BoolLit(_) -> x , Add(_, _) -> x + 1 , If(_, _, _) -> x , Pair(_, _) -> x } } pub fn plus_one_partial :: e: Expr x: a -> a { match e //~ ERROR E0301 is not covered { IntLit(_) -> x + 1, Add(_, _) -> x + 1, If(_, _, _) -> x, Pair(_, _) -> x } } // An index no finite type satisfies: `x` would be `List`. Every check // refuses it, and none loops (issue #224: exhaustiveness overflowed its // stack, even on a parameter of such a type). pub type Cyclic { Cycle(Int) -> Cyclic, a> } pub fn takes_a_cyclic :: t: Cyclic -> Int { 0 } pub fn matches_a_cyclic :: t: Cyclic -> Int { match t { Cycle //~ WARN W0301 //~^ ERROR E0202 `Cycle` cannot match a value of type `Cyclic` (n) -> n } }