]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/block-expr-precedence.rs
Imported Upstream version 1.0.0~beta
[rustc.git] / src / test / run-pass / block-expr-precedence.rs
CommitLineData
223e47cc
LB
1// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11// This test has some extra semis in it that the pretty-printer won't
12// reproduce so we don't want to automatically reformat it
13
14// no-reformat
15
c34b1796
AL
16// pretty-expanded FIXME #23616
17
223e47cc
LB
18/*
19 *
20 * When you write a block-expression thing followed by
21 * a lone unary operator, you can get a surprising parse:
22 *
23 * if (...) { ... }
24 * -num;
25 *
26 * for example, or:
27 *
28 * if (...) { ... }
29 * *box;
30 *
31 * These will parse as subtraction and multiplication binops.
32 * To get them to parse "the way you want" you need to brace
33 * the leading unops:
34
35 * if (...) { ... }
36 * {-num};
37 *
38 * or alternatively, semi-separate them:
39 *
40 * if (...) { ... };
41 * -num;
42 *
43 * This seems a little wonky, but the alternative is to lower
44 * precedence of such block-like exprs to the point where
45 * you have to parenthesize them to get them to occur in the
46 * RHS of a binop. For example, you'd have to write:
47 *
48 * 12 + (if (foo) { 13 } else { 14 });
49 *
50 * rather than:
51 *
52 * 12 + if (foo) { 13 } else { 14 };
53 *
54 * Since we want to maintain the ability to write the latter,
55 * we leave the parens-burden on the trailing unop case.
56 *
57 */
58
59pub fn main() {
60
61 let num = 12;
62
85aaf69f
SL
63 assert_eq!(if (true) { 12 } else { 12 } - num, 0);
64 assert_eq!(12 - if (true) { 12 } else { 12 }, 0);
65 if (true) { 12; } {-num};
66 if (true) { 12; }; {-num};
67 if (true) { 12; };;; -num;
223e47cc 68}