// User-defined derives (ADR 0019). @derivable describe_constructor trait Describe { fn describe :: x: a -> String } fn describe_constructor :: c: Constructor -> String { c.name } impl Describe { fn describe :: n { show n } } @derive Describe pub type Point { Point(x: Int, y: Int) } @derive Describe pub type Labelled //~^ ERROR E0410 field of type `String`, which does not implement it { Labelled(String) } @derive Describe pub type Box { Box(a) } pub fn boxed :: -> String { describe Box(Point(1, 2)) } pub fn unboxable :: b: Box -> String { describe //~^ ERROR E0401 `Bool` does not implement `Describe` (needed for `Describe>`) b } trait Plain { fn plain :: x: a -> Int } @derive Plain pub type NotDerivable //~ ERROR E0408 it is not a trait marked `@derivable` { NotDerivable } // The derive function is checked against the method's result type. @derivable wrong_type trait Measure< //~^ ERROR E0412 derive function `wrong_type` must have type `fn(Constructor) -> Int`, but it has type `fn(Constructor) -> Int` a > { fn measure :: x: a -> Int } // A parameter no constructor stores is phantom (issue #236): the derived // impl needs nothing of it. @derive Eq pub type UserId { UserId(Int) } pub type User { User } // `User` has no `Eq`, and `UserId` needs none either. pub fn phantom_eq :: a: UserId b: UserId -> Bool { a == b } //~ TYPE phantom_eq: fn(UserId, UserId) -> Bool // But a stored parameter still constrains. @derive Eq pub type Marked { Marked(id: Int, marker: tag) } pub fn not_phantom :: a: Marked b: Marked -> Bool { a //~^ ERROR E0401 `User` does not implement `Eq` (needed for `Eq>`) == b } fn wrong_type :: c: Constructor -> Int { 0 } @derivable missing trait Missing //~ ERROR E0412 no function `missing` is in scope { fn missing_method :: x: a -> Int } @derivable trait NoFunction //~ ERROR E0412 needs a derive function { fn no_function :: x: a -> Int } @derivable two_constructor trait TwoMethods //~ ERROR E0412 exactly one method; `TwoMethods` has 2 { fn first :: x: a -> Int fn second :: x: a -> Int } fn two_constructor :: c: Constructor -> Int { 0 } @derivable two_constructor trait Binary //~ ERROR E0412 must have the form `fn same :: x: a -> R` { fn same :: x: a y: a -> Int } @derivable two_constructor trait Producer //~ ERROR E0412 must have the form { fn make :: -> a } @derivable effectful trait Logged //~ ERROR E0412 derive function `effectful` must have type { fn logged :: x: a -> Int } effect log { fn log :: message: String -> () } fn effectful :: c: Constructor -> Int / { log c.name 0 } // Polymorphic derive functions fit any result type. @derivable constructor_index trait Index { fn index :: x: a -> Int } fn constructor_index :: c: Constructor -> Int { c.index }