Add pass-by-ref annotation to the tests to make them typecheck

Issue #1008
This commit is contained in:
Marijn Haverbeke 2011-10-06 12:50:24 +02:00
parent fe916fb9f0
commit 41528dc543
24 changed files with 49 additions and 47 deletions

View File

@ -1,4 +1,4 @@
// error-pattern:expected fn() but found fn(int)
// error-pattern:expected fn() but found fn(+int)
fn main() {
fn f() { }

View File

@ -1,9 +1,11 @@
// error-pattern:assigning to upvar
// Make sure that nesting a block within a lambda doesn't let us
// mutate upvars from a lambda.
fn f2(x: block()) { x(); }
fn main() {
let i = 0;
let ctr = lambda () -> int { block () { i = i + 1; }(); ret i; };
let ctr = lambda () -> int { f2({|| i = i + 1; }); ret i; };
log_err ctr();
log_err ctr();
log_err ctr();

View File

@ -10,7 +10,7 @@ fn test_generic<@T>(expected: T, eq: compare<T>) {
}
fn test_vec() {
fn compare_box(v1: @int, v2: @int) -> bool { ret v1 == v2; }
fn compare_box(&&v1: @int, &&v2: @int) -> bool { ret v1 == v2; }
let eq = bind compare_box(_, _);
test_generic::<@int>(@1, eq);
}

View File

@ -10,7 +10,7 @@ fn test_generic<@T>(expected: T, eq: compare<T>) {
}
fn test_vec() {
fn compare_box(v1: ~int, v2: ~int) -> bool { ret v1 == v2; }
fn compare_box(&&v1: ~int, &&v2: ~int) -> bool { ret v1 == v2; }
let eq = bind compare_box(_, _);
test_generic::<~int>(~1, eq);
}

View File

@ -10,7 +10,7 @@ fn test_generic<@T>(expected: T, eq: compare<T>) {
}
fn test_bool() {
fn compare_bool(b1: bool, b2: bool) -> bool { ret b1 == b2; }
fn compare_bool(&&b1: bool, &&b2: bool) -> bool { ret b1 == b2; }
let eq = bind compare_bool(_, _);
test_generic::<bool>(true, eq);
}

View File

@ -10,7 +10,7 @@ fn test_generic<@T>(expected: T, eq: compare<T>) {
}
fn test_vec() {
fn compare_vec(v1: @int, v2: @int) -> bool { ret v1 == v2; }
fn compare_vec(&&v1: @int, &&v2: @int) -> bool { ret v1 == v2; }
let eq = bind compare_vec(_, _);
test_generic::<@int>(@1, eq);
}

View File

@ -10,7 +10,7 @@ fn test_generic<@T>(expected: T, eq: compare<T>) {
}
fn test_vec() {
fn compare_vec(v1: ~int, v2: ~int) -> bool { ret v1 == v2; }
fn compare_vec(&&v1: ~int, &&v2: ~int) -> bool { ret v1 == v2; }
let eq = bind compare_vec(_, _);
test_generic::<~int>(~1, eq);
}

View File

@ -12,7 +12,7 @@ fn test_generic<@T>(expected: T, eq: compare<T>) {
}
fn test_bool() {
fn compare_bool(b1: bool, b2: bool) -> bool { ret b1 == b2; }
fn compare_bool(&&b1: bool, &&b2: bool) -> bool { ret b1 == b2; }
let eq = bind compare_bool(_, _);
test_generic::<bool>(true, eq);
}

View File

@ -10,7 +10,7 @@ fn test_generic<@T>(expected: T, not_expected: T, eq: compare<T>) {
}
fn test_vec() {
fn compare_box(v1: @int, v2: @int) -> bool { ret v1 == v2; }
fn compare_box(&&v1: @int, &&v2: @int) -> bool { ret v1 == v2; }
let eq = bind compare_box(_, _);
test_generic::<@int>(@1, @2, eq);
}

View File

@ -12,7 +12,7 @@ fn test_generic<@T>(expected: T, not_expected: T, eq: compare<T>) {
}
fn test_bool() {
fn compare_bool(b1: bool, b2: bool) -> bool { ret b1 == b2; }
fn compare_bool(&&b1: bool, &&b2: bool) -> bool { ret b1 == b2; }
let eq = bind compare_bool(_, _);
test_generic::<bool>(true, false, eq);
}

View File

@ -6,7 +6,7 @@ fn fix<A, @B>(f: fn(fn(A) -> B, A) -> B) -> fn(A) -> B {
ret bind fix_help(f, _);
}
fn fact_(f: fn(int) -> int, n: int) -> int {
fn fact_(f: fn(&&int) -> int, &&n: int) -> int {
// fun fact 0 = 1
ret if n == 0 { 1 } else { n * f(n - 1) };
}

View File

@ -6,7 +6,7 @@ fn fix<A, ~B>(f: fn(fn(A) -> B, A) -> B) -> fn(A) -> B {
ret bind fix_help(f, _);
}
fn fact_(f: fn(int) -> int, n: int) -> int {
fn fact_(f: fn(&&int) -> int, &&n: int) -> int {
// fun fact 0 = 1
ret if n == 0 { 1 } else { n * f(n - 1) };
}

View File

@ -2,12 +2,12 @@
fn mk() -> int { ret 1; }
fn chk(a: int) { log a; assert (a == 1); }
fn chk(&&a: int) { log a; assert (a == 1); }
fn apply<T>(produce: fn() -> T, consume: fn(T)) { consume(produce()); }
fn main() {
let produce: fn() -> int = mk;
let consume: fn(int) = chk;
let consume: fn(&&int) = chk;
apply::<int>(produce, consume);
}

View File

@ -2,7 +2,7 @@
fn main() {
let box = @mutable 10;
fn dec_box(i: @mutable int) { *i -= 1; }
fn dec_box(&&i: @mutable int) { *i -= 1; }
{ let i <- finish({val: box, fin: dec_box}); }
assert (*box == 9);

View File

@ -12,7 +12,7 @@ fn pure_foldl<@T, @U>(ls: list<T>, u: U, f: block(T, U) -> U) -> U {
// Shows how to use an "unchecked" block to call a general
// fn from a pure fn
pure fn pure_length<@T>(ls: list<T>) -> uint {
fn count<T>(_t: T, u: uint) -> uint { u + 1u }
fn count<T>(_t: T, &&u: uint) -> uint { u + 1u }
unchecked{ pure_foldl(ls, 0u, count) }
}

View File

@ -119,8 +119,8 @@ fn test_parameterized<@T>(e: eqfn<T>, a: T, b: T, c: T, d: T) {
#[test]
fn test() {
fn inteq(a: int, b: int) -> bool { ret a == b; }
fn intboxeq(a: @int, b: @int) -> bool { ret a == b; }
fn inteq(&&a: int, &&b: int) -> bool { ret a == b; }
fn intboxeq(&&a: @int, &&b: @int) -> bool { ret a == b; }
fn taggyeq(a: taggy, b: taggy) -> bool {
alt a {
one(a1) { alt b { one(b1) { ret a1 == b1; } _ { ret false; } } }

View File

@ -5,16 +5,16 @@
#[test]
fn test_either_left() {
let val = left(10);
fn f_left(x: int) -> bool { x == 10 }
fn f_right(_x: uint) -> bool { false }
fn f_left(&&x: int) -> bool { x == 10 }
fn f_right(&&_x: uint) -> bool { false }
assert (either(f_left, f_right, val));
}
#[test]
fn test_either_right() {
let val = right(10u);
fn f_left(_x: int) -> bool { false }
fn f_right(x: uint) -> bool { x == 10u }
fn f_left(&&_x: int) -> bool { false }
fn f_right(&&x: uint) -> bool { x == 10u }
assert (either(f_left, f_right, val));
}

View File

@ -17,7 +17,7 @@ fn test_from_vec() {
#[test]
fn test_foldl() {
let l = from_vec([0, 1, 2, 3, 4]);
fn add(a: int, b: uint) -> uint { ret (a as uint) + b; }
fn add(&&a: int, &&b: uint) -> uint { ret (a as uint) + b; }
let rs = list::foldl(l, 0u, add);
assert (rs == 10u);
}
@ -25,7 +25,7 @@ fn test_foldl() {
#[test]
fn test_find_success() {
let l = from_vec([0, 1, 2]);
fn match(i: int) -> option::t<int> {
fn match(&&i: int) -> option::t<int> {
ret if i == 2 { option::some(i) } else { option::none::<int> };
}
let rs = list::find(l, match);
@ -35,7 +35,7 @@ fn match(i: int) -> option::t<int> {
#[test]
fn test_find_fail() {
let l = from_vec([0, 1, 2]);
fn match(_i: int) -> option::t<int> { ret option::none::<int>; }
fn match(&&_i: int) -> option::t<int> { ret option::none::<int>; }
let rs = list::find(l, match);
assert (rs == option::none::<int>);
}

View File

@ -11,7 +11,7 @@
#[test]
fn test_simple() {
log "*** starting test_simple";
fn eq_uint(x: uint, y: uint) -> bool { ret x == y; }
fn eq_uint(&&x: uint, &&y: uint) -> bool { ret x == y; }
let hasher_uint: map::hashfn<uint> = util::id;
let eqer_uint: map::eqfn<uint> = eq_uint;
let hasher_str: map::hashfn<str> = str::hash;
@ -83,7 +83,7 @@ fn test_simple() {
fn test_growth() {
log "*** starting test_growth";
let num_to_insert: uint = 64u;
fn eq_uint(x: uint, y: uint) -> bool { ret x == y; }
fn eq_uint(&&x: uint, &&y: uint) -> bool { ret x == y; }
log "uint -> uint";
let hasher_uint: map::hashfn<uint> = util::id;
let eqer_uint: map::eqfn<uint> = eq_uint;
@ -157,8 +157,8 @@ fn test_growth() {
fn test_removal() {
log "*** starting test_removal";
let num_to_insert: uint = 64u;
fn eq(x: uint, y: uint) -> bool { ret x == y; }
fn hash(u: uint) -> uint {
fn eq(&&x: uint, &&y: uint) -> bool { ret x == y; }
fn hash(&&u: uint) -> uint {
// This hash function intentionally causes collisions between
// consecutive integer pairs.

View File

@ -7,7 +7,7 @@
fn check_sort(v1: [mutable int], v2: [mutable int]) {
let len = std::vec::len::<int>(v1);
fn ltequal(a: int, b: int) -> bool { ret a <= b; }
fn ltequal(&&a: int, &&b: int) -> bool { ret a <= b; }
let f = ltequal;
std::sort::quick_sort::<int>(f, v1);
let i = 0u;
@ -46,7 +46,7 @@ fn test_simple() {
let expected = [1, 2, 3];
fn lteq(a: int, b: int) -> bool { int::le(a, b) }
fn lteq(&&a: int, &&b: int) -> bool { int::le(a, b) }
sort::quick_sort(lteq, names);
let immut_names = vec::from_mut(names);

View File

@ -3,8 +3,8 @@
fn check_sort(v1: [mutable int], v2: [mutable int]) {
let len = std::vec::len::<int>(v1);
fn lt(a: int, b: int) -> bool { ret a < b; }
fn equal(a: int, b: int) -> bool { ret a == b; }
fn lt(&&a: int, &&b: int) -> bool { ret a < b; }
fn equal(&&a: int, &&b: int) -> bool { ret a == b; }
let f1 = lt;
let f2 = equal;
std::sort::quick_sort3::<int>(f1, f2, v1);

View File

@ -3,7 +3,7 @@
fn check_sort(v1: [int], v2: [int]) {
let len = std::vec::len::<int>(v1);
fn lteq(a: int, b: int) -> bool { ret a <= b; }
fn lteq(&&a: int, &&b: int) -> bool { ret a <= b; }
let f = lteq;
let v3 = std::sort::merge_sort::<int>(f, v1);
let i = 0u;

View File

@ -40,7 +40,7 @@ fn traverse_in_order() {
insert(m, 1, ());
let n = 0;
fn t(&n: int, k: int, _v: ()) { assert (n == k); n += 1; }
fn t(&n: int, &&k: int, &&_v: ()) { assert (n == k); n += 1; }
traverse(m, bind t(n, _, _));
}

View File

@ -8,17 +8,17 @@
fn square(n: uint) -> uint { ret n * n; }
fn square_alias(n: uint) -> uint { ret n * n; }
fn square_ref(&&n: uint) -> uint { ret n * n; }
pure fn is_three(n: uint) -> bool { ret n == 3u; }
pure fn is_three(&&n: uint) -> bool { ret n == 3u; }
pure fn is_odd(n: uint) -> bool { ret n % 2u == 1u; }
pure fn is_odd(&&n: uint) -> bool { ret n % 2u == 1u; }
fn square_if_odd(n: uint) -> option::t<uint> {
fn square_if_odd(&&n: uint) -> option::t<uint> {
ret if n % 2u == 1u { some(n * n) } else { none };
}
fn add(x: uint, y: uint) -> uint { ret x + y; }
fn add(&&x: uint, &&y: uint) -> uint { ret x + y; }
#[test]
fn test_unsafe_ptrs() {
@ -211,7 +211,7 @@ fn test_grow_set() {
fn test_map() {
// Test on-stack map.
let v = [1u, 2u, 3u];
let w = vec::map(square_alias, v);
let w = vec::map(square_ref, v);
assert (vec::len(w) == 3u);
assert (w[0] == 1u);
assert (w[1] == 4u);
@ -219,7 +219,7 @@ fn test_map() {
// Test on-heap map.
v = [1u, 2u, 3u, 4u, 5u];
w = vec::map(square_alias, v);
w = vec::map(square_ref, v);
assert (vec::len(w) == 5u);
assert (w[0] == 1u);
assert (w[1] == 4u);
@ -230,7 +230,7 @@ fn test_map() {
#[test]
fn test_map2() {
fn times(x: int, y: int) -> int { ret x * y; }
fn times(&&x: int, &&y: int) -> int { ret x * y; }
let f = times;
let v0 = [1, 2, 3, 4, 5];
let v1 = [5, 4, 3, 2, 1];
@ -256,12 +256,12 @@ fn test_filter_map() {
assert (w[1] == 9u);
assert (w[2] == 25u);
fn halve(i: int) -> option::t<int> {
fn halve(&&i: int) -> option::t<int> {
if i % 2 == 0 {
ret option::some::<int>(i / 2);
} else { ret option::none::<int>; }
}
fn halve_for_sure(i: int) -> int { ret i / 2; }
fn halve_for_sure(&&i: int) -> int { ret i / 2; }
let all_even: [int] = [0, 2, 8, 6];
let all_odd1: [int] = [1, 7, 3];
let all_odd2: [int] = [];
@ -335,8 +335,8 @@ fn test_position() {
#[test]
fn test_position_pred() {
fn less_than_three(i: int) -> bool { ret i < 3; }
fn is_eighteen(i: int) -> bool { ret i == 18; }
fn less_than_three(&&i: int) -> bool { ret i < 3; }
fn is_eighteen(&&i: int) -> bool { ret i == 18; }
let v1: [int] = [5, 4, 3, 2, 1];
assert (position_pred(less_than_three, v1) == option::some::<uint>(3u));
assert (position_pred(is_eighteen, v1) == option::none::<uint>);