]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/recursion.rs
Doc says to avoid mixing allocator instead of forbiding it
[rust.git] / src / test / compile-fail / recursion.rs
1 // Copyright 2012-2014 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 enum Nil {Nil}
12 struct Cons<T> {head:int, tail:T}
13 trait Dot {fn dot(&self, other:Self) -> int;}
14 impl Dot for Nil {
15   fn dot(&self, _:Nil) -> int {0}
16 }
17 impl<T:Dot> Dot for Cons<T> {
18   fn dot(&self, other:Cons<T>) -> int {
19     self.head * other.head + self.tail.dot(other.tail)
20   }
21 }
22 fn test<T:Dot> (n:int, i:int, first:T, second:T) ->int {
23     //~^ ERROR: reached the recursion limit during monomorphization
24   match n {
25     0 => {first.dot(second)}
26       // Error message should be here. It should be a type error
27       // to instantiate `test` at a type other than T. (See #4287)
28     _ => {test (n-1, i+1, Cons {head:2*i+1, tail:first}, Cons{head:i*i, tail:second})}
29   }
30 }
31 pub fn main() {
32   let n = test(1, 0, Nil, Nil);
33   println!("{}", n);
34 }