]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/issue-2185.rs
more commits on issue 2185
[rust.git] / src / test / run-pass / issue-2185.rs
1 // Copyright 2012 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 // does the second one subsume the first?
12 // xfail-test
13 // xfail-fast
14
15 // notes on this test case:
16 // On Thu, Apr 18, 2013 at 6:30 PM, John Clements <clements@brinckerhoff.org> wrote:
17 // the "issue-2185.rs" test was xfailed with a ref to #2263. Issue #2263 is now fixed, so I tried it again, and after adding some &self parameters, I got this error:
18 // 
19 // Running /usr/local/bin/rustc:
20 // issue-2185.rs:24:0: 26:1 error: conflicting implementations for a trait
21 // issue-2185.rs:24 impl iterable<uint> for @fn(&fn(uint)) {
22 // issue-2185.rs:25     fn iter(&self, blk: &fn(v: uint)) { self( |i| blk(i) ) }
23 // issue-2185.rs:26 }
24 // issue-2185.rs:20:0: 22:1 note: note conflicting implementation here
25 // issue-2185.rs:20 impl<A> iterable<A> for @fn(&fn(A)) {
26 // issue-2185.rs:21     fn iter(&self, blk: &fn(A)) { self(blk); }
27 // issue-2185.rs:22 }
28 // 
29 // … so it looks like it's just not possible to implement both the generic iterable<uint> and iterable<A> for the type iterable<uint>. Is it okay if I just remove this test?
30 //
31 // but Niko responded:
32 // think it's fine to remove this test, just because it's old and cruft and not hard to reproduce. *However* it should eventually be possible to implement the same interface for the same type multiple times with different type parameters, it's just that our current trait implementation has accidental limitations.
33
34 // so I'm leaving it in.
35 // actually, it looks like this is related to bug #3429. I'll rename this bug.
36
37 // This test had to do with an outdated version of the iterable trait.
38 // However, the condition it was testing seemed complex enough to
39 // warrant still having a test, so I inlined the old definitions.
40
41 trait iterable<A> {
42     fn iter(&self, blk: &fn(A));
43 }
44
45 impl<A> iterable<A> for @fn(&fn(A)) {
46     fn iter(&self, blk: &fn(A)) { self(blk); }
47 }
48
49 impl iterable<uint> for @fn(&fn(uint)) {
50     fn iter(&self, blk: &fn(v: uint)) { self( |i| blk(i) ) }
51 }
52
53 fn filter<A,IA:iterable<A>>(self: IA, prd: @fn(A) -> bool, blk: &fn(A)) {
54     do self.iter |a| {
55         if prd(a) { blk(a) }
56     }
57 }
58
59 fn foldl<A,B,IA:iterable<A>>(self: IA, b0: B, blk: &fn(B, A) -> B) -> B {
60     let mut b = b0;
61     do self.iter |a| {
62         b = blk(b, a);
63     }
64     b
65 }
66
67 fn range(lo: uint, hi: uint, it: &fn(uint)) {
68     let mut i = lo;
69     while i < hi {
70         it(i);
71         i += 1u;
72     }
73 }
74
75 pub fn main() {
76     let range: @fn(&fn(uint)) = |a| range(0u, 1000u, a);
77     let filt: @fn(&fn(v: uint)) = |a| filter(
78         range,
79         |&&n: uint| n % 3u != 0u && n % 5u != 0u,
80         a);
81     let sum = foldl(filt, 0u, |accum, &&n: uint| accum + n );
82
83     io::println(fmt!("%u", sum));
84 }