summary refs log tree commit diff
path: root/src/test/compile-fail/pure-higher-order.rs
blob: 13455b1d74f8d9a37605ee502f38a4df58523e80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Test rules governing higher-order pure fns.

pure fn range(from: uint, to: uint, f: fn(uint)) {
    let mut i = from;
    while i < to {
        f(i); // Note: legal to call argument, even if it is not pure.
        i += 1u;
    }
}

pure fn range2(from: uint, to: uint, f: fn(uint)) {
    do range(from, to) |i| {
        f(i*2u);
    }
}

pure fn range3(from: uint, to: uint, f: fn(uint)) {
    range(from, to, f)
}

pure fn range4(from: uint, to: uint) {
    range(from, to, print) //~ ERROR access to impure function prohibited in pure context
}

pure fn range5(from: uint, to: uint, x: {f: fn(uint)}) {
    range(from, to, x.f) //~ ERROR access to impure function prohibited in pure context
}

pure fn range6(from: uint, to: uint, x: @{f: fn(uint)}) {
    range(from, to, x.f) //~ ERROR access to impure function prohibited in pure context
}

pure fn range7(from: uint, to: uint) {
    do range(from, to) |i| {
        print(i); //~ ERROR access to impure function prohibited in pure context
    }
}

pure fn range8(from: uint, to: uint) {
    range(from, to, noop);
}

fn print(i: uint) { #error["i=%u", i]; }

pure fn noop(_i: uint) {}

fn main() {
}