// Traits with several parameters (ADR 0018). trait Convert { fn convert :: x: a -> b } impl Convert { fn convert :: n { show n } } impl Convert, List> where Convert { fn convert :: xs { map xs convert } } fn map :: xs f { match xs { [] -> [], [x, ..rest] -> [f x, ..map rest f] } } // Constraints on generalized variables become `where` clauses. fn convert_each :: xs { map xs convert } //~ TYPE convert_each: fn(List) -> List where Convert fn to_strings :: xs: List -> List { convert xs } // Every argument must be known: there are no functional dependencies. fn unknown_result :: -> String { show //~ ERROR E0403 cannot tell which type should implement `Show` (convert 1) //~^ ERROR E0403 cannot tell which impl of `Convert` to use here: `Convert` has unknown types } fn known_result :: -> String { let s: String = convert 1 s } pub fn no_impl :: -> String { convert True //~ ERROR E0401 no impl of `Convert` exists } pub fn nested_no_impl :: -> List { convert //~ ERROR E0401 no impl of `Convert` exists [True] } pub fn missing_given :: x: a -> String where Show { convert x //~ ERROR E0402 `Convert` is not known to hold } pub fn given :: x: a -> b where Convert { convert x } // A variable only in the `where` clause can never be determined. pub fn through :: x: a -> c where Convert, Convert //~^ ERROR E0403 type variable `b` appears only in the `where` clause of `through` { panic "unused" } trait Empty { fn empty :: -> c //~^ ERROR E0403 method `empty` does not mention the trait's type parameter `e` } trait Twice //~ ERROR E0411 must be distinct { fn twice :: x: a -> a } // Two impls overlap exactly when their head types are equal. impl Convert, Bool> { fn convert :: _ { True } } impl Convert< //~^ ERROR E0404 `Convert` is implemented more than once for `Option, Bool` Option , Bool > { fn convert :: _ { False } } // Every argument is a named type applied to distinct variables. impl Convert //~ ERROR E0409 distinct type variables { fn convert :: _ { 0 } } impl Convert, List> //~ ERROR E0409 distinct type variables { fn convert :: _ { [] } } impl Convert< //~ ERROR E0409 takes 2 type arguments, but the impl gives 1 Bool > { fn convert :: _ { 0 } } pub fn wrong_arity :: x: a -> a where Convert //~^ ERROR E0212 takes 2 type arguments, but 1 was given { x } // Orphan rule: the impl must be in the module of the trait or of one of // its head types (here, neither). impl Show< //~ ERROR E0405 must be in the module of the trait or of the type Int > { fn show :: _ { "" } } // A head may also be a tuple of distinct variables (ADR 0018 Rev. 1). // A tuple has no module, so such an impl lives with its trait. impl Convert<(a, b), Int> { fn convert :: _ { 2 } } impl Convert< //~^ ERROR E0404 `Convert` is implemented more than once for `(_, _), Int` (c, d) , Int > { fn convert :: _ { 0 } } impl Convert<(a, a), Bool> //~ ERROR E0409 distinct type variables { fn convert :: _ { True } } impl Convert<(a, Int), Bool> //~ ERROR E0409 distinct type variables { fn convert :: _ { True } } impl Show< //~^ ERROR E0405 impl of `Show` for `(_, _, _, _, _, _, _, _, _, _, _, _, _)` must be in the module of the trait (a, b, c, d, e, f, g, h, i, j, k, l, m) > { fn show :: _ { "" } } pub fn pairs_convert :: p: (Bool, String) -> Int { convert p }