From b106ef81164a33e2f18820274b48cb9673c423af Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Tue, 7 Feb 2012 06:37:08 -0800 Subject: [PATCH] make bind syntax unnecessary: just use _ for one of the arguments --- src/comp/syntax/parse/parser.rs | 31 ++++++++++++------- src/comp/syntax/print/pprust.rs | 7 ++++- .../run-pass/bind-parameterized-args-2.rs | 2 +- src/test/run-pass/bind-parameterized-args.rs | 2 +- src/test/run-pass/bind-trivial.rs | 2 +- src/test/run-pass/drop-bind-thunk-args.rs | 2 +- .../drop-parametric-closure-with-bound-box.rs | 2 +- src/test/run-pass/expr-alt-generic-box1.rs | 2 +- src/test/run-pass/expr-alt-generic-box2.rs | 2 +- src/test/run-pass/expr-alt-generic-unique1.rs | 2 +- src/test/run-pass/expr-alt-generic-unique2.rs | 2 +- src/test/run-pass/expr-alt-generic.rs | 4 +-- src/test/run-pass/expr-block-generic-box1.rs | 2 +- src/test/run-pass/expr-block-generic-box2.rs | 2 +- .../run-pass/expr-block-generic-unique1.rs | 2 +- .../run-pass/expr-block-generic-unique2.rs | 2 +- src/test/run-pass/expr-block-generic.rs | 4 +-- src/test/run-pass/expr-if-generic-box1.rs | 2 +- src/test/run-pass/expr-if-generic-box2.rs | 2 +- src/test/run-pass/expr-if-generic.rs | 4 +-- src/test/run-pass/fixed-point-bind-box.rs | 4 +-- src/test/run-pass/fixed-point-bind-unique.rs | 4 +-- src/test/run-pass/fun-call-variants.rs | 2 +- src/test/run-pass/generic-bind.rs | 14 ++++----- src/test/run-pass/hashmap-memory.rs | 2 +- src/test/run-pass/issue-333.rs | 2 +- src/test/run-pass/issue-898.rs | 2 +- src/test/run-pass/rebind-fn.rs | 2 +- src/test/run-pass/task-comm-17.rs | 7 ++--- src/test/run-pass/unchecked-predicates.rs | 2 +- 30 files changed, 66 insertions(+), 55 deletions(-) diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs index 8e2897407a38..3ce4d38013e0 100644 --- a/src/comp/syntax/parse/parser.rs +++ b/src/comp/syntax/parse/parser.rs @@ -854,15 +854,9 @@ fn parse_bottom_expr(p: parser) -> pexpr { ret pexpr(mk_mac_expr(p, lo, p.span.hi, ast::mac_ellipsis)); } else if eat_word(p, "bind") { let e = parse_expr_res(p, RESTRICT_NO_CALL_EXPRS); - fn parse_expr_opt(p: parser) -> option<@ast::expr> { - alt p.token { - token::UNDERSCORE { p.bump(); ret none; } - _ { ret some(parse_expr(p)); } - } - } let es = parse_seq(token::LPAREN, token::RPAREN, seq_sep(token::COMMA), - parse_expr_opt, p); + parse_expr_or_hole, p); hi = es.span.hi; ex = ast::expr_bind(e, es.node); } else if p.token == token::POUND { @@ -1036,10 +1030,18 @@ fn parse_dot_or_call_expr_with(p: parser, e0: pexpr) -> pexpr { alt p.token { // expr(...) token::LPAREN if permits_call(p) { - let es = parse_seq(token::LPAREN, token::RPAREN, - seq_sep(token::COMMA), parse_expr, p); - hi = es.span.hi; - let nd = ast::expr_call(to_expr(e), es.node, false); + let es_opt = + parse_seq(token::LPAREN, token::RPAREN, + seq_sep(token::COMMA), parse_expr_or_hole, p); + hi = es_opt.span.hi; + + let nd = + if vec::any(es_opt.node, {|e| option::is_none(e) }) { + ast::expr_bind(to_expr(e), es_opt.node) + } else { + let es = vec::map(es_opt.node) {|e| option::get(e) }; + ast::expr_call(to_expr(e), es, false) + }; e = mk_pexpr(p, lo, hi, nd); } @@ -1388,6 +1390,13 @@ fn parse_expr(p: parser) -> @ast::expr { ret parse_expr_res(p, UNRESTRICTED); } +fn parse_expr_or_hole(p: parser) -> option<@ast::expr> { + alt p.token { + token::UNDERSCORE { p.bump(); ret none; } + _ { ret some(parse_expr(p)); } + } +} + fn parse_expr_res(p: parser, r: restriction) -> @ast::expr { let old = p.restriction; p.restriction = r; diff --git a/src/comp/syntax/print/pprust.rs b/src/comp/syntax/print/pprust.rs index 31360c5bc57e..9ed73b17d9da 100644 --- a/src/comp/syntax/print/pprust.rs +++ b/src/comp/syntax/print/pprust.rs @@ -846,7 +846,12 @@ fn print_expr(s: ps, &&expr: @ast::expr) { _ { word(s.s, "_"); } } } - word_nbsp(s, "bind"); + + // "bind" keyword is only needed if there are no "_" arguments. + if !vec::any(args) {|arg| option::is_none(arg) } { + word_nbsp(s, "bind"); + } + print_expr(s, func); popen(s); commasep(s, inconsistent, args, print_opt); diff --git a/src/test/run-pass/bind-parameterized-args-2.rs b/src/test/run-pass/bind-parameterized-args-2.rs index af308c71064a..997bc2b17990 100644 --- a/src/test/run-pass/bind-parameterized-args-2.rs +++ b/src/test/run-pass/bind-parameterized-args-2.rs @@ -1,7 +1,7 @@ fn main() { fn echo(c: int, x: fn@(T)) { #error("wee"); } - let y = bind echo(42, _); + let y = echo(42, _); y(fn@(&&i: str) { }); } diff --git a/src/test/run-pass/bind-parameterized-args.rs b/src/test/run-pass/bind-parameterized-args.rs index e0a21fba2a54..a990ecc4da17 100644 --- a/src/test/run-pass/bind-parameterized-args.rs +++ b/src/test/run-pass/bind-parameterized-args.rs @@ -1,7 +1,7 @@ fn main() { fn echo(c: int, x: [T]) { } - let y: fn@([int]) = bind echo(42, _); + let y: fn@([int]) = echo(42, _); y([1]); } diff --git a/src/test/run-pass/bind-trivial.rs b/src/test/run-pass/bind-trivial.rs index 8ae7e4cf2001..205809a2b694 100644 --- a/src/test/run-pass/bind-trivial.rs +++ b/src/test/run-pass/bind-trivial.rs @@ -5,7 +5,7 @@ fn f(n: int) -> int { ret n; } fn main() { - let g: fn@(int) -> int = bind f(_); + let g: fn@(int) -> int = f(_); let i: int = g(42); assert (i == 42); } diff --git a/src/test/run-pass/drop-bind-thunk-args.rs b/src/test/run-pass/drop-bind-thunk-args.rs index 2a2d2c5e630d..e75320c1e754 100644 --- a/src/test/run-pass/drop-bind-thunk-args.rs +++ b/src/test/run-pass/drop-bind-thunk-args.rs @@ -2,4 +2,4 @@ fn f(x: @int) { } -fn main() { let x = @10; let ff = bind f(_); ff(x); ff(x); } +fn main() { let x = @10; let ff = f(_); ff(x); ff(x); } diff --git a/src/test/run-pass/drop-parametric-closure-with-bound-box.rs b/src/test/run-pass/drop-parametric-closure-with-bound-box.rs index 0671f6178502..39785d10bbf2 100644 --- a/src/test/run-pass/drop-parametric-closure-with-bound-box.rs +++ b/src/test/run-pass/drop-parametric-closure-with-bound-box.rs @@ -2,4 +2,4 @@ fn f(i: @uint, t: T) { } -fn main() { let x = bind f::(@0xdeafbeefu, _); } +fn main() { let x = f::(@0xdeafbeefu, _); } diff --git a/src/test/run-pass/expr-alt-generic-box1.rs b/src/test/run-pass/expr-alt-generic-box1.rs index 0c58d9d6727b..98f3b86aec98 100644 --- a/src/test/run-pass/expr-alt-generic-box1.rs +++ b/src/test/run-pass/expr-alt-generic-box1.rs @@ -11,7 +11,7 @@ fn test_generic(expected: @T, eq: compare) { fn test_box() { fn compare_box(b1: @bool, b2: @bool) -> bool { ret *b1 == *b2; } - let eq = bind compare_box(_, _); + let eq = compare_box(_, _); test_generic::(@true, eq); } diff --git a/src/test/run-pass/expr-alt-generic-box2.rs b/src/test/run-pass/expr-alt-generic-box2.rs index c2f933a3480a..b4a40c51361a 100644 --- a/src/test/run-pass/expr-alt-generic-box2.rs +++ b/src/test/run-pass/expr-alt-generic-box2.rs @@ -11,7 +11,7 @@ fn test_generic(expected: T, eq: compare) { fn test_vec() { fn compare_box(&&v1: @int, &&v2: @int) -> bool { ret v1 == v2; } - let eq = bind compare_box(_, _); + let eq = compare_box(_, _); test_generic::<@int>(@1, eq); } diff --git a/src/test/run-pass/expr-alt-generic-unique1.rs b/src/test/run-pass/expr-alt-generic-unique1.rs index 2bbb0fea5f79..2b8b9f53946b 100644 --- a/src/test/run-pass/expr-alt-generic-unique1.rs +++ b/src/test/run-pass/expr-alt-generic-unique1.rs @@ -10,7 +10,7 @@ fn test_generic(expected: ~T, eq: compare) { fn test_box() { fn compare_box(b1: ~bool, b2: ~bool) -> bool { ret *b1 == *b2; } - let eq = bind compare_box(_, _); + let eq = compare_box(_, _); test_generic::(~true, eq); } diff --git a/src/test/run-pass/expr-alt-generic-unique2.rs b/src/test/run-pass/expr-alt-generic-unique2.rs index 30581222bfa3..0a0dd1dd8d5b 100644 --- a/src/test/run-pass/expr-alt-generic-unique2.rs +++ b/src/test/run-pass/expr-alt-generic-unique2.rs @@ -11,7 +11,7 @@ fn test_generic(expected: T, eq: compare) { fn test_vec() { fn compare_box(&&v1: ~int, &&v2: ~int) -> bool { ret v1 == v2; } - let eq = bind compare_box(_, _); + let eq = compare_box(_, _); test_generic::<~int>(~1, eq); } diff --git a/src/test/run-pass/expr-alt-generic.rs b/src/test/run-pass/expr-alt-generic.rs index 393cb1b7a4ec..13da160c73c4 100644 --- a/src/test/run-pass/expr-alt-generic.rs +++ b/src/test/run-pass/expr-alt-generic.rs @@ -11,7 +11,7 @@ fn test_generic(expected: T, eq: compare) { fn test_bool() { fn compare_bool(&&b1: bool, &&b2: bool) -> bool { ret b1 == b2; } - let eq = bind compare_bool(_, _); + let eq = compare_bool(_, _); test_generic::(true, eq); } @@ -19,7 +19,7 @@ fn test_rec() { type t = {a: int, b: int}; fn compare_rec(t1: t, t2: t) -> bool { ret t1 == t2; } - let eq = bind compare_rec(_, _); + let eq = compare_rec(_, _); test_generic::({a: 1, b: 2}, eq); } diff --git a/src/test/run-pass/expr-block-generic-box1.rs b/src/test/run-pass/expr-block-generic-box1.rs index 768f2aba16d6..432c1438be6d 100644 --- a/src/test/run-pass/expr-block-generic-box1.rs +++ b/src/test/run-pass/expr-block-generic-box1.rs @@ -15,7 +15,7 @@ fn test_box() { log(debug, *b2); ret *b1 == *b2; } - let eq = bind compare_box(_, _); + let eq = compare_box(_, _); test_generic::(@true, eq); } diff --git a/src/test/run-pass/expr-block-generic-box2.rs b/src/test/run-pass/expr-block-generic-box2.rs index 4ffc190716c4..d099875a4623 100644 --- a/src/test/run-pass/expr-block-generic-box2.rs +++ b/src/test/run-pass/expr-block-generic-box2.rs @@ -11,7 +11,7 @@ fn test_generic(expected: T, eq: compare) { fn test_vec() { fn compare_vec(&&v1: @int, &&v2: @int) -> bool { ret v1 == v2; } - let eq = bind compare_vec(_, _); + let eq = compare_vec(_, _); test_generic::<@int>(@1, eq); } diff --git a/src/test/run-pass/expr-block-generic-unique1.rs b/src/test/run-pass/expr-block-generic-unique1.rs index 060fba26051c..5647eab3dfe7 100644 --- a/src/test/run-pass/expr-block-generic-unique1.rs +++ b/src/test/run-pass/expr-block-generic-unique1.rs @@ -14,7 +14,7 @@ fn test_box() { log(debug, *b2); ret *b1 == *b2; } - let eq = bind compare_box(_, _); + let eq = compare_box(_, _); test_generic::(~true, eq); } diff --git a/src/test/run-pass/expr-block-generic-unique2.rs b/src/test/run-pass/expr-block-generic-unique2.rs index ce6489cdd12d..903d93eb3c0c 100644 --- a/src/test/run-pass/expr-block-generic-unique2.rs +++ b/src/test/run-pass/expr-block-generic-unique2.rs @@ -11,7 +11,7 @@ fn test_generic(expected: T, eq: compare) { fn test_vec() { fn compare_vec(&&v1: ~int, &&v2: ~int) -> bool { ret v1 == v2; } - let eq = bind compare_vec(_, _); + let eq = compare_vec(_, _); test_generic::<~int>(~1, eq); } diff --git a/src/test/run-pass/expr-block-generic.rs b/src/test/run-pass/expr-block-generic.rs index bf31ffd58088..f9b8a43d3440 100644 --- a/src/test/run-pass/expr-block-generic.rs +++ b/src/test/run-pass/expr-block-generic.rs @@ -13,7 +13,7 @@ fn test_generic(expected: T, eq: compare) { fn test_bool() { fn compare_bool(&&b1: bool, &&b2: bool) -> bool { ret b1 == b2; } - let eq = bind compare_bool(_, _); + let eq = compare_bool(_, _); test_generic::(true, eq); } @@ -21,7 +21,7 @@ fn test_rec() { type t = {a: int, b: int}; fn compare_rec(t1: t, t2: t) -> bool { ret t1 == t2; } - let eq = bind compare_rec(_, _); + let eq = compare_rec(_, _); test_generic::({a: 1, b: 2}, eq); } diff --git a/src/test/run-pass/expr-if-generic-box1.rs b/src/test/run-pass/expr-if-generic-box1.rs index f7869b093b90..e69f022c2918 100644 --- a/src/test/run-pass/expr-if-generic-box1.rs +++ b/src/test/run-pass/expr-if-generic-box1.rs @@ -11,7 +11,7 @@ fn test_generic(expected: @T, not_expected: @T, eq: compare) { fn test_box() { fn compare_box(b1: @bool, b2: @bool) -> bool { ret *b1 == *b2; } - let eq = bind compare_box(_, _); + let eq = compare_box(_, _); test_generic::(@true, @false, eq); } diff --git a/src/test/run-pass/expr-if-generic-box2.rs b/src/test/run-pass/expr-if-generic-box2.rs index 01daf198dfe2..68b8e0173b12 100644 --- a/src/test/run-pass/expr-if-generic-box2.rs +++ b/src/test/run-pass/expr-if-generic-box2.rs @@ -11,7 +11,7 @@ fn test_generic(expected: T, not_expected: T, eq: compare) { fn test_vec() { fn compare_box(&&v1: @int, &&v2: @int) -> bool { ret v1 == v2; } - let eq = bind compare_box(_, _); + let eq = compare_box(_, _); test_generic::<@int>(@1, @2, eq); } diff --git a/src/test/run-pass/expr-if-generic.rs b/src/test/run-pass/expr-if-generic.rs index 9c565f35f676..3152f9eb8014 100644 --- a/src/test/run-pass/expr-if-generic.rs +++ b/src/test/run-pass/expr-if-generic.rs @@ -13,7 +13,7 @@ fn test_generic(expected: T, not_expected: T, eq: compare) { fn test_bool() { fn compare_bool(&&b1: bool, &&b2: bool) -> bool { ret b1 == b2; } - let eq = bind compare_bool(_, _); + let eq = compare_bool(_, _); test_generic::(true, false, eq); } @@ -21,7 +21,7 @@ fn test_rec() { type t = {a: int, b: int}; fn compare_rec(t1: t, t2: t) -> bool { ret t1 == t2; } - let eq = bind compare_rec(_, _); + let eq = compare_rec(_, _); test_generic::({a: 1, b: 2}, {a: 2, b: 3}, eq); } diff --git a/src/test/run-pass/fixed-point-bind-box.rs b/src/test/run-pass/fixed-point-bind-box.rs index 98a9018b02a4..319f24a7b02b 100644 --- a/src/test/run-pass/fixed-point-bind-box.rs +++ b/src/test/run-pass/fixed-point-bind-box.rs @@ -1,9 +1,9 @@ fn fix_help(f: native fn(fn@(A) -> B, A) -> B, x: A) -> B { - ret f(bind fix_help(f, _), x); + ret f(fix_help(f, _), x); } fn fix(f: native fn(fn@(A) -> B, A) -> B) -> fn@(A) -> B { - ret bind fix_help(f, _); + ret fix_help(f, _); } fn fact_(f: fn@(&&int) -> int, &&n: int) -> int { diff --git a/src/test/run-pass/fixed-point-bind-unique.rs b/src/test/run-pass/fixed-point-bind-unique.rs index bee5ad13ec76..a15b2fbbd53e 100644 --- a/src/test/run-pass/fixed-point-bind-unique.rs +++ b/src/test/run-pass/fixed-point-bind-unique.rs @@ -1,9 +1,9 @@ fn fix_help(f: native fn(fn@(A) -> B, A) -> B, x: A) -> B { - ret f(bind fix_help(f, _), x); + ret f(fix_help(f, _), x); } fn fix(f: native fn(fn@(A) -> B, A) -> B) -> fn@(A) -> B { - ret bind fix_help(f, _); + ret fix_help(f, _); } fn fact_(f: fn@(&&int) -> int, &&n: int) -> int { diff --git a/src/test/run-pass/fun-call-variants.rs b/src/test/run-pass/fun-call-variants.rs index c11e64c234fc..2f28b7c41921 100644 --- a/src/test/run-pass/fun-call-variants.rs +++ b/src/test/run-pass/fun-call-variants.rs @@ -7,7 +7,7 @@ fn main() { let a: int = direct(3); // direct let b: int = ho(direct); // indirect unbound - let c: int = ho(bind direct(_)); // indirect bound + let c: int = ho(direct(_)); // indirect bound assert (a == b); assert (b == c); } diff --git a/src/test/run-pass/generic-bind.rs b/src/test/run-pass/generic-bind.rs index d0d3ea98a476..5a9795d7ec60 100644 --- a/src/test/run-pass/generic-bind.rs +++ b/src/test/run-pass/generic-bind.rs @@ -6,12 +6,12 @@ fn main() { let t = {_0: 1, _1: 2, _2: 3, _3: 4, _4: 5, _5: 6, _6: 7}; assert (t._5 == 6); let f1 = - bind id::<{_0: int, - _1: int, - _2: int, - _3: int, - _4: int, - _5: int, - _6: int}>(_); + id::<{_0: int, + _1: int, + _2: int, + _3: int, + _4: int, + _5: int, + _6: int}>(_); assert (f1(t)._5 == 6); } diff --git a/src/test/run-pass/hashmap-memory.rs b/src/test/run-pass/hashmap-memory.rs index eeed3d32800a..bc16c319374f 100644 --- a/src/test/run-pass/hashmap-memory.rs +++ b/src/test/run-pass/hashmap-memory.rs @@ -58,7 +58,7 @@ mod map_reduce { } } - map(input, bind emit(intermediates, ctrl, _, _)); + map(input, emit(intermediates, ctrl, _, _)); send(ctrl, mapper_done); } diff --git a/src/test/run-pass/issue-333.rs b/src/test/run-pass/issue-333.rs index be253b9afdd5..1f0e2d830e5f 100644 --- a/src/test/run-pass/issue-333.rs +++ b/src/test/run-pass/issue-333.rs @@ -1,4 +1,4 @@ -fn quux(x: T) -> T { let f = bind id::(_); ret f(x); } +fn quux(x: T) -> T { let f = id::(_); ret f(x); } fn id(x: T) -> T { ret x; } diff --git a/src/test/run-pass/issue-898.rs b/src/test/run-pass/issue-898.rs index 180fe1d0041a..7b6f3033ab41 100644 --- a/src/test/run-pass/issue-898.rs +++ b/src/test/run-pass/issue-898.rs @@ -7,5 +7,5 @@ fn log_if(c: native fn(T)->bool, e: T) { } fn main() { - (bind log_if(even, _))(2); + (log_if(even, _))(2); } diff --git a/src/test/run-pass/rebind-fn.rs b/src/test/run-pass/rebind-fn.rs index c651bb50af86..49cc7ef6d5a9 100644 --- a/src/test/run-pass/rebind-fn.rs +++ b/src/test/run-pass/rebind-fn.rs @@ -1,5 +1,5 @@ fn add(i: int, j: int) -> int { ret i + j; } -fn binder(n: int) -> fn@() -> int { let f = bind add(n, _); ret bind f(2); } +fn binder(n: int) -> fn@() -> int { let f = add(n, _); ret bind f(2); } fn main() { binder(5); let f = binder(1); diff --git a/src/test/run-pass/task-comm-17.rs b/src/test/run-pass/task-comm-17.rs index eb6ab0034176..0db319c4c358 100644 --- a/src/test/run-pass/task-comm-17.rs +++ b/src/test/run-pass/task-comm-17.rs @@ -1,9 +1,6 @@ -// xfail-test // Issue #922 -// This test is specifically about spawning temporary closures, which -// isn't possible under the bare-fn regime. I'm keeping it around -// until such time as we have unique closures. +// This test is specifically about spawning temporary closures. use std; import task; @@ -12,5 +9,5 @@ fn f() { } fn main() { - task::spawn(bind f()); + task::spawn {|| f() }; } \ No newline at end of file diff --git a/src/test/run-pass/unchecked-predicates.rs b/src/test/run-pass/unchecked-predicates.rs index d54a7f5a5e01..166243cad119 100644 --- a/src/test/run-pass/unchecked-predicates.rs +++ b/src/test/run-pass/unchecked-predicates.rs @@ -18,7 +18,7 @@ fn pure_foldl(ls: list, u: U, f: fn(T, U) -> U) -> U { // fn from a pure fn pure fn pure_length(ls: list) -> uint { fn count(_t: T, &&u: uint) -> uint { u + 1u } - unchecked{ pure_foldl(ls, 0u, bind count(_, _)) } + unchecked{ pure_foldl(ls, 0u, count(_, _)) } } pure fn nonempty_list(ls: list) -> bool { pure_length(ls) > 0u }