PageRenderTime 13ms CodeModel.GetById 2ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

/src/test/run-pass/block-expr-precedence.rs

http://github.com/jruderman/rust
Rust | 56 lines | 8 code | 5 blank | 43 comment | 9 complexity | f2d0c6bd3b5cb58d69c6e50bb71ed7a3 MD5 | raw file
 1// This test has some extra semis in it that the pretty-printer won't
 2// reproduce so we don't want to automatically reformat it
 3
 4// no-reformat
 5
 6/*
 7 *
 8 *  When you write a block-expression thing followed by
 9 *  a lone unary operator, you can get a surprising parse:
10 *
11 *  if (...) { ... }
12 *  -num;
13 *
14 * for example, or:
15 *
16 *  if (...) { ... }
17 *  *box;
18 *
19 * These will parse as subtraction and multiplication binops.
20 * To get them to parse "the way you want" you need to brace
21 * the leading unops:
22
23 *  if (...) { ... }
24 *  {-num};
25 *
26 * or alternatively, semi-separate them:
27 *
28 *  if (...) { ... };
29 *  -num;
30 *
31 * This seems a little wonky, but the alternative is to lower
32 * precedence of such block-like exprs to the point where
33 * you have to parenthesize them to get them to occur in the
34 * RHS of a binop. For example, you'd have to write:
35 *
36 *   12 + (if (foo) { 13 } else { 14 });
37 *
38 * rather than:
39 *
40 *   12 + if (foo) { 13 } else { 14 };
41 *
42 * Since we want to maintain the ability to write the latter,
43 * we leave the parens-burden on the trailing unop case.
44 *
45 */
46
47fn main() {
48
49  let num = 12;
50
51  assert if (true) { 12 } else { 12 } - num == 0;
52  assert 12 - if (true) { 12 } else { 12 } == 0;
53  if (true) { 12; } {-num};
54  if (true) { 12; }; {-num};
55  if (true) { 12; };;; -num;
56}