2012-12-10 19:32:48 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-09-18 17:52:21 -05:00
|
|
|
#[legacy_modes];
|
|
|
|
|
2012-05-25 01:44:58 -05:00
|
|
|
// 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)) {
|
2012-06-30 18:19:07 -05:00
|
|
|
do range(from, to) |i| {
|
2012-05-25 01:44:58 -05:00
|
|
|
f(i*2u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pure fn range3(from: uint, to: uint, f: fn(uint)) {
|
|
|
|
range(from, to, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
pure fn range4(from: uint, to: uint) {
|
2012-06-30 06:23:59 -05:00
|
|
|
range(from, to, print) //~ ERROR access to impure function prohibited in pure context
|
2012-05-25 01:44:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pure fn range5(from: uint, to: uint, x: {f: fn(uint)}) {
|
2012-06-30 06:23:59 -05:00
|
|
|
range(from, to, x.f) //~ ERROR access to impure function prohibited in pure context
|
2012-05-25 01:44:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pure fn range6(from: uint, to: uint, x: @{f: fn(uint)}) {
|
2012-06-30 06:23:59 -05:00
|
|
|
range(from, to, x.f) //~ ERROR access to impure function prohibited in pure context
|
2012-05-25 01:44:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pure fn range7(from: uint, to: uint) {
|
2012-06-30 18:19:07 -05:00
|
|
|
do range(from, to) |i| {
|
2012-06-30 06:23:59 -05:00
|
|
|
print(i); //~ ERROR access to impure function prohibited in pure context
|
2012-05-25 01:44:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pure fn range8(from: uint, to: uint) {
|
|
|
|
range(from, to, noop);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:24:52 -05:00
|
|
|
fn print(i: uint) { error!("i=%u", i); }
|
2012-05-25 01:44:58 -05:00
|
|
|
|
|
|
|
pure fn noop(_i: uint) {}
|
|
|
|
|
|
|
|
fn main() {
|
2012-09-18 17:52:21 -05:00
|
|
|
}
|