Skip to content

Commit 7114702

Browse files
committed
Change the way block calls are parsed, mark them as block-calls.
This makes it possible to omit the semicolon after the block, and will cause the pretty-printer to properly print such calls (if pretty-printing of blocks wasn't so broken). Block calls (with the block outside of the parentheses) can now only occur at statement level, and their value can not be used. When calling a block-style function that returns a useful value, the block must be put insde the parentheses. Issue #1054
1 parent 0ce40f6 commit 7114702

18 files changed

+56
-46
lines changed

src/comp/front/test.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ fn mk_test_wrapper(cx: test_ctxt,
302302
span: span) -> @ast::expr {
303303
let call_expr: ast::expr = {
304304
id: cx.next_node_id(),
305-
node: ast::expr_call(@fn_path_expr, []),
305+
node: ast::expr_call(@fn_path_expr, [], false),
306306
span: span
307307
};
308308

@@ -401,7 +401,7 @@ fn mk_test_main_call(cx: test_ctxt) -> @ast::expr {
401401
let test_path_expr: ast::expr =
402402
{id: cx.next_node_id(), node: test_path_expr_, span: dummy_sp()};
403403

404-
let test_call_expr_: ast::expr_ = ast::expr_call(@test_path_expr, []);
404+
let test_call_expr_ = ast::expr_call(@test_path_expr, [], false);
405405

406406
let test_call_expr: ast::expr =
407407
{id: cx.next_node_id(), node: test_call_expr_, span: dummy_sp()};
@@ -419,7 +419,7 @@ fn mk_test_main_call(cx: test_ctxt) -> @ast::expr {
419419

420420
let test_main_call_expr_: ast::expr_ =
421421
ast::expr_call(@test_main_path_expr,
422-
[@args_path_expr, @test_call_expr]);
422+
[@args_path_expr, @test_call_expr], false);
423423

424424
let test_main_call_expr: ast::expr =
425425
{id: cx.next_node_id(), node: test_main_call_expr_, span: dummy_sp()};

src/comp/middle/alias.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ fn visit_fn(cx: @ctx, f: ast::_fn, _tp: [ast::ty_param], sp: span,
9393
fn visit_expr(cx: @ctx, ex: @ast::expr, sc: scope, v: vt<scope>) {
9494
let handled = true;
9595
alt ex.node {
96-
ast::expr_call(f, args) {
96+
ast::expr_call(f, args, _) {
9797
check_call(*cx, f, args);
9898
handled = false;
9999
}
@@ -667,7 +667,7 @@ fn expr_root(cx: ctx, ex: @ast::expr, autoderef: bool)
667667
}
668668
if is_none(path_def_id(cx, base_root.ex)) {
669669
alt base_root.ex.node {
670-
ast::expr_call(f, args) {
670+
ast::expr_call(f, args, _) {
671671
let fty = ty::expr_ty(cx.tcx, f);
672672
alt ty::ty_fn_ret_style(cx.tcx, fty) {
673673
ast::return_ref(mut, arg_n) {

src/comp/middle/fn_usage.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn fn_usage_expr(expr: @ast::expr,
4242
}
4343
}
4444

45-
ast::expr_call(f, args) {
45+
ast::expr_call(f, args, _) {
4646
let f_ctx = {unsafe_fn_legal: true,
4747
generic_bare_fn_legal: true with ctx};
4848
v.visit_expr(f, f_ctx, v);

src/comp/middle/kind.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ fn need_shared_or_pinned_ctor(tcx: ty::ctxt, a: @ast::expr, descr: str) {
174174
fn pinned_ctor(a: @ast::expr) -> bool {
175175
// FIXME: Technically a lambda block is also a pinned ctor
176176
alt a.node {
177-
ast::expr_call(cexpr, _) {
177+
ast::expr_call(cexpr, _, _) {
178178
// Assuming that if it's a call that it's safe to move in, mostly
179179
// because I don't know offhand how to ensure that it's a call
180180
// specifically to a resource constructor
@@ -224,7 +224,7 @@ fn check_expr(tcx: ty::ctxt, e: @ast::expr) {
224224
ast::expr_fail(option::some(a)) {
225225
need_expr_kind(tcx, a, ast::kind_shared, "'fail' operand");
226226
}
227-
ast::expr_call(callee, _) {
227+
ast::expr_call(callee, _, _) {
228228
let tpt = ty::expr_ty_params_and_ty(tcx, callee);
229229

230230
// If we have typarams, we're calling an item; we need to check

src/comp/middle/mut.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ fn visit_decl(cx: @ctx, d: @decl, &&e: (), v: visit::vt<()>) {
150150

151151
fn visit_expr(cx: @ctx, ex: @expr, &&e: (), v: visit::vt<()>) {
152152
alt ex.node {
153-
expr_call(f, args) { check_call(cx, f, args); }
153+
expr_call(f, args, _) { check_call(cx, f, args); }
154154
expr_swap(lhs, rhs) {
155155
check_lval(cx, lhs, msg_assign);
156156
check_lval(cx, rhs, msg_assign);

src/comp/middle/trans.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3147,7 +3147,7 @@ fn expr_is_lval(tcx: ty::ctxt, e: @ast::expr) -> bool {
31473147
ty::ty_rec(_) { true }
31483148
}
31493149
}
3150-
ast::expr_call(f, _) {
3150+
ast::expr_call(f, _, _) {
31513151
let fty = ty::expr_ty(tcx, f);
31523152
ast_util::ret_by_ref(ty::ty_fn_ret_style(tcx, fty))
31533153
}
@@ -3198,7 +3198,7 @@ fn trans_lval(cx: @block_ctxt, e: @ast::expr) -> lval_result {
31983198
ret lval_owned(sub.bcx, val);
31993199
}
32003200
// This is a by-ref returning call. Regular calls are not lval
3201-
ast::expr_call(f, args) {
3201+
ast::expr_call(f, args, _) {
32023202
let cell = empty_dest_cell();
32033203
let bcx = trans_call(cx, f, args, e.id, by_val(cell));
32043204
ret lval_owned(bcx, *cell);
@@ -4175,7 +4175,7 @@ fn trans_expr(bcx: @block_ctxt, e: @ast::expr, dest: dest) -> @block_ctxt {
41754175
ast::expr_anon_obj(anon_obj) {
41764176
ret trans_anon_obj(bcx, e.span, anon_obj, e.id, dest);
41774177
}
4178-
ast::expr_call(f, args) {
4178+
ast::expr_call(f, args, _) {
41794179
ret trans_call(bcx, f, args, e.id, dest);
41804180
}
41814181
ast::expr_field(_, _) {

src/comp/middle/tstate/auxiliary.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ fn exprs_to_constr_args(tcx: ty::ctxt, args: [@expr]) -> [@constr_arg_use] {
607607

608608
fn expr_to_constr(tcx: ty::ctxt, e: @expr) -> sp_constr {
609609
alt e.node {
610-
expr_call(operator, args) {
610+
expr_call(operator, args, _) {
611611
alt operator.node {
612612
expr_path(p) {
613613
ret respan(e.span,

src/comp/middle/tstate/collect_locals.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn collect_pred(e: @expr, cx: ctxt, v: visit::vt<ctxt>) {
3232

3333
// If it's a call, generate appropriate instances of the
3434
// call's constraints.
35-
expr_call(operator, operands) {
35+
expr_call(operator, operands, _) {
3636
for c: @ty::constr in constraints_expr(cx.tcx, operator) {
3737
let ct: sp_constr =
3838
respan(c.span,

src/comp/middle/tstate/pre_post_conditions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ fn find_pre_post_expr(fcx: fn_ctxt, e: @expr) {
309309

310310

311311
alt e.node {
312-
expr_call(operator, operands) {
312+
expr_call(operator, operands, _) {
313313
/* copy */
314314

315315
let args = operands;

src/comp/middle/tstate/states.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ fn find_pre_post_state_expr(fcx: fn_ctxt, pres: prestate, e: @expr) -> bool {
335335
vec::len(elts)), elts,
336336
return_val);
337337
}
338-
expr_call(operator, operands) {
338+
expr_call(operator, operands, _) {
339339
ret find_pre_post_state_call(fcx, pres, operator, e.id,
340340
callee_arg_init_ops(fcx, operator.id),
341341
operands,

src/comp/middle/typeck.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2072,7 +2072,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
20722072
out_args, rt, cf, constrs);
20732073
write::ty_only_fixup(fcx, id, ft);
20742074
}
2075-
ast::expr_call(f, args) {
2075+
ast::expr_call(f, args, _) {
20762076
bot = check_call_full(fcx, expr.span, f, args, expr.id);
20772077
}
20782078
ast::expr_self_method(ident) {
@@ -2535,7 +2535,7 @@ fn check_pred_expr(fcx: @fn_ctxt, e: @ast::expr) -> bool {
25352535
/* e must be a call expr where all arguments are either
25362536
literals or slots */
25372537
alt e.node {
2538-
ast::expr_call(operator, operands) {
2538+
ast::expr_call(operator, operands, _) {
25392539
if !ty::is_pred_ty(fcx.ccx.tcx, expr_ty(fcx.ccx.tcx, operator)) {
25402540
fcx.ccx.tcx.sess.span_err
25412541
(operator.span,
@@ -2632,7 +2632,7 @@ fn check_constraints(fcx: @fn_ctxt, cs: [@ast::constr], args: [ast::arg]) {
26322632
let call_expr_id = fcx.ccx.tcx.sess.next_node_id();
26332633
let call_expr =
26342634
@{id: call_expr_id,
2635-
node: ast::expr_call(oper, c_args),
2635+
node: ast::expr_call(oper, c_args, false),
26362636
span: c.span};
26372637
check_pred_expr(fcx, call_expr);
26382638
}

src/comp/syntax/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ type expr = {id: node_id, node: expr_, span: span};
184184
tag expr_ {
185185
expr_vec([@expr], mutability);
186186
expr_rec([field], option::t<@expr>);
187-
expr_call(@expr, [@expr]);
187+
expr_call(@expr, [@expr], bool);
188188
expr_tup([@expr]);
189189
expr_self_method(ident);
190190
expr_bind(@expr, [option::t<@expr>]);

src/comp/syntax/ast_util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ fn is_exported(i: ident, m: _mod) -> bool {
163163
}
164164

165165
pure fn is_call_expr(e: @expr) -> bool {
166-
alt e.node { expr_call(_, _) { true } _ { false } }
166+
alt e.node { expr_call(_, _, _) { true } _ { false } }
167167
}
168168

169169
fn is_constraint_arg(e: @expr) -> bool {

src/comp/syntax/ext/fmt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn pieces_to_expr(cx: ext_ctxt, sp: span, pieces: [piece], args: [@ast::expr])
7979
fn make_call(cx: ext_ctxt, sp: span, fn_path: [ast::ident],
8080
args: [@ast::expr]) -> @ast::expr {
8181
let pathexpr = make_path_expr(cx, sp, fn_path);
82-
let callexpr = ast::expr_call(pathexpr, args);
82+
let callexpr = ast::expr_call(pathexpr, args, false);
8383
ret @{id: cx.next_id(), node: callexpr, span: sp};
8484
}
8585
fn make_rec_expr(cx: ext_ctxt, sp: span,

src/comp/syntax/fold.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,9 @@ fn noop_fold_expr(e: expr_, fld: ast_fold) -> expr_ {
348348
option::map(fld.fold_expr, maybe_expr))
349349
}
350350
expr_tup(elts) { expr_tup(vec::map(fld.fold_expr, elts)) }
351-
expr_call(f, args) {
352-
expr_call(fld.fold_expr(f), fld.map_exprs(fld.fold_expr, args))
351+
expr_call(f, args, blk) {
352+
expr_call(fld.fold_expr(f), fld.map_exprs(fld.fold_expr, args),
353+
blk)
353354
}
354355
expr_self_method(id) { expr_self_method(fld.fold_ident(id)) }
355356
expr_bind(f, args) {

src/comp/syntax/parse/parser.rs

+18-17
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ fn parse_bottom_expr(p: parser) -> @ast::expr {
977977
parse_seq(token::LPAREN, token::RPAREN, some(token::COMMA),
978978
parse_expr, p);
979979
hi = es.span.hi;
980-
ex = ast::expr_call(f, es.node);
980+
ex = ast::expr_call(f, es.node, false);
981981
} else if p.peek() == token::MOD_SEP ||
982982
is_ident(p.peek()) && !is_word(p, "true") &&
983983
!is_word(p, "false") {
@@ -1051,7 +1051,7 @@ fn parse_dot_or_call_expr_with(p: parser, e: @ast::expr) -> @ast::expr {
10511051
let es = parse_seq(token::LPAREN, token::RPAREN,
10521052
some(token::COMMA), parse_expr, p);
10531053
hi = es.span.hi;
1054-
let nd = ast::expr_call(e, es.node);
1054+
let nd = ast::expr_call(e, es.node, false);
10551055
e = mk_expr(p, lo, hi, nd);
10561056
}
10571057
}
@@ -1073,19 +1073,6 @@ fn parse_dot_or_call_expr_with(p: parser, e: @ast::expr) -> @ast::expr {
10731073
t { unexpected(p, t); }
10741074
}
10751075
}
1076-
token::LBRACE. when is_bar(p.look_ahead(1u)) {
1077-
p.bump();
1078-
let blk = parse_fn_block_expr(p);
1079-
alt e.node {
1080-
ast::expr_call(f, args) {
1081-
e = @{node: ast::expr_call(f, args + [blk]) with *e};
1082-
}
1083-
_ {
1084-
e = mk_expr(p, lo, p.get_last_hi_pos(),
1085-
ast::expr_call(e, [blk]));
1086-
}
1087-
}
1088-
}
10891076
_ { ret e; }
10901077
}
10911078
}
@@ -1569,7 +1556,6 @@ fn parse_source_stmt(p: parser) -> @ast::stmt {
15691556
let decl = parse_let(p);
15701557
ret @spanned(lo, decl.span.hi, ast::stmt_decl(decl, p.get_id()));
15711558
} else {
1572-
15731559
let item_attrs;
15741560
alt parse_outer_attrs_or_ext(p) {
15751561
none. { item_attrs = []; }
@@ -1589,7 +1575,6 @@ fn parse_source_stmt(p: parser) -> @ast::stmt {
15891575
}
15901576
}
15911577

1592-
15931578
alt maybe_item {
15941579
some(i) {
15951580
let hi = i.span.hi;
@@ -1599,6 +1584,21 @@ fn parse_source_stmt(p: parser) -> @ast::stmt {
15991584
none. {
16001585
// Remainder are line-expr stmts.
16011586
let e = parse_expr(p);
1587+
// See if it is a block call
1588+
if p.peek() == token::LBRACE && is_bar(p.look_ahead(1u)) {
1589+
p.bump();
1590+
let blk = parse_fn_block_expr(p);
1591+
alt e.node {
1592+
ast::expr_call(f, args, false) {
1593+
e = @{node: ast::expr_call(f, args + [blk], true)
1594+
with *e};
1595+
}
1596+
_ {
1597+
e = mk_expr(p, lo, p.get_last_hi_pos(),
1598+
ast::expr_call(e, [blk], true));
1599+
}
1600+
}
1601+
}
16021602
ret @spanned(lo, e.span.hi, ast::stmt_expr(e, p.get_id()));
16031603
}
16041604
_ { p.fatal("expected statement"); }
@@ -1624,6 +1624,7 @@ fn expr_has_value(e: @ast::expr) -> bool {
16241624
ast::expr_for(_, _, blk) | ast::expr_do_while(blk, _) {
16251625
!option::is_none(blk.node.expr)
16261626
}
1627+
ast::expr_call(_, _, true) { false }
16271628
_ { true }
16281629
}
16291630
}

src/comp/syntax/print/pprust.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -727,11 +727,19 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
727727
commasep_exprs(s, inconsistent, exprs);
728728
pclose(s);
729729
}
730-
ast::expr_call(func, args) {
730+
ast::expr_call(func, args, has_block) {
731731
print_expr_parens_if_not_bot(s, func);
732-
popen(s);
733-
commasep_exprs(s, inconsistent, args);
734-
pclose(s);
732+
let base_args = args, blk = none;
733+
if has_block { blk = some(vec::pop(base_args)); }
734+
if !has_block || vec::len(base_args) > 0u {
735+
popen(s);
736+
commasep_exprs(s, inconsistent, base_args);
737+
pclose(s);
738+
}
739+
if has_block {
740+
nbsp(s);
741+
print_expr(s, option::get(blk));
742+
}
735743
}
736744
ast::expr_self_method(ident) {
737745
word(s.s, "self.");

src/comp/syntax/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ fn visit_expr<E>(ex: @expr, e: E, v: vt<E>) {
245245
visit_expr_opt(base, e, v);
246246
}
247247
expr_tup(elts) { for el in elts { v.visit_expr(el, e, v); } }
248-
expr_call(callee, args) {
248+
expr_call(callee, args, _) {
249249
visit_exprs(args, e, v);
250250
v.visit_expr(callee, e, v);
251251
}

0 commit comments

Comments
 (0)