Skip to content

Commit d6d0c19

Browse files
committed
---
yaml --- r: 7016 b: refs/heads/master c: 43a9d50 h: refs/heads/master v: v3
1 parent 9c35b63 commit d6d0c19

8 files changed

+127
-1
lines changed

[refs]

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: de383bcfedc5ac67c6a27c51f1a8226f5a9bdcd0
2+
refs/heads/master: 43a9d50a74096c73f268f0600f6b1287a1cee880
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
fn compute1() -> float {
3+
let v = [0f, 1f, 2f, 3f];
4+
5+
// Here the "-10f" parses as a second
6+
// statement in tail position:
7+
vec::foldl(0f, v) { |x, y| x + y } - 10f
8+
}
9+
10+
fn compute2() -> float {
11+
let v = [0f, 1f, 2f, 3f];
12+
13+
// Here the ret makes this explicit:
14+
ret vec::foldl(0f, v) { |x, y| x + y } - 10f;
15+
}
16+
17+
fn main() {
18+
let x = compute1();
19+
log(debug, x);
20+
assert(x == -10f);
21+
22+
let y = compute2();
23+
log(debug, y);
24+
assert(y == -4f);
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
let v = [-1f, 0f, 1f, 2f, 3f];
3+
4+
// Trailing expressions require parentheses:
5+
let y = vec::foldl(0f, v) { |x, y| x + y } + 10f;
6+
7+
assert y == 15f;
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
fn f(i: block() -> uint) -> uint { i() }
3+
let v = [-1f, 0f, 1f, 2f, 3f];
4+
let z = vec::foldl(f, v) { |x, _y| x } { || 22u };
5+
assert z == 22u;
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
fn f(i: uint) -> uint { i }
3+
let v = [-1f, 0f, 1f, 2f, 3f];
4+
let z = vec::foldl(f, v) { |x, _y| x } (22u);
5+
assert z == 22u;
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// xfail-test
2+
3+
// FIXME: Parser doesn't distinguish expression in parentheses (as in
4+
// this example) from one that is not! It is somewhat of a pain to
5+
// fix this though there are no theoretical difficulties. We could
6+
// either add paren to the AST (better for pretty-print, I suppose) or
7+
// modify the parser to track whether the expression in question is
8+
// parenthesized. I did the latter once and it was a bit of pain but
9+
// not terribly difficult. We could also the decision as to whether
10+
// something is an "expression with a value" down into the
11+
// parse_expr() codepath, where we *know* if there are parentheses or
12+
// not, but we'd probably have to be a bit more careful then with
13+
// clearing the top-level restrction flag (which we ought to do
14+
// anyhow!)
15+
16+
fn main() {
17+
let v = [1f, 2f, 3f];
18+
let w =
19+
if true { (vec::any(v) { |e| float::nonnegative(e) }) }
20+
else { false };
21+
assert w;
22+
}
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Allow block arguments with ternary... why not, no chance of ambig.
2+
fn main() {
3+
let v = [-1f, 1f];
4+
let foo = vec::any(v) { |e| float::negative(e) } ? true : false;
5+
assert foo;
6+
}

trunk/src/test/run-pass/block-arg.rs

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Check usage and precedence of block arguments in expressions:
2+
fn main() {
3+
let v = [-1f, 0f, 1f, 2f, 3f];
4+
5+
// Statement form does not require parentheses:
6+
vec::iter(v) { |i|
7+
log(info, i);
8+
}
9+
10+
// Usable at all:
11+
let any_negative = vec::any(v) { |e| float::negative(e) };
12+
assert any_negative;
13+
14+
// Higher precedence than assignments:
15+
any_negative = vec::any(v) { |e| float::negative(e) };
16+
assert any_negative;
17+
18+
// Higher precedence than unary operations:
19+
let abs_v = vec::map(v) { |e| float::abs(e) };
20+
assert vec::all(abs_v) { |e| float::nonnegative(e) };
21+
assert !vec::any(abs_v) { |e| float::negative(e) };
22+
23+
// Usable in funny statement-like forms:
24+
if !vec::any(v) { |e| float::positive(e) } {
25+
assert false;
26+
}
27+
alt vec::all(v) { |e| float::negative(e) } {
28+
true { fail "incorrect answer."; }
29+
false { }
30+
}
31+
alt 3 {
32+
_ when vec::any(v) { |e| float::negative(e) } {
33+
}
34+
_ {
35+
fail "wrong answer.";
36+
}
37+
}
38+
39+
40+
// Lower precedence than binary operations:
41+
let w = vec::foldl(0f, v, { |x, y| x + y }) + 10f;
42+
let y = vec::foldl(0f, v) { |x, y| x + y } + 10f;
43+
let z = 10f + vec::foldl(0f, v) { |x, y| x + y };
44+
assert w == y;
45+
assert y == z;
46+
47+
// They are not allowed as the tail of a block without parentheses:
48+
let w =
49+
if true { vec::any(abs_v, { |e| float::nonnegative(e) }) }
50+
else { false };
51+
assert w;
52+
}

0 commit comments

Comments
 (0)