]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/block-expr-precedence.rs
librustc: Remove the fallback to `int` from typechecking.
[rust.git] / src / test / run-pass / block-expr-precedence.rs
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
16 /*
17  *
18  *  When you write a block-expression thing followed by
19  *  a lone unary operator, you can get a surprising parse:
20  *
21  *  if (...) { ... }
22  *  -num;
23  *
24  * for example, or:
25  *
26  *  if (...) { ... }
27  *  *box;
28  *
29  * These will parse as subtraction and multiplication binops.
30  * To get them to parse "the way you want" you need to brace
31  * the leading unops:
32
33  *  if (...) { ... }
34  *  {-num};
35  *
36  * or alternatively, semi-separate them:
37  *
38  *  if (...) { ... };
39  *  -num;
40  *
41  * This seems a little wonky, but the alternative is to lower
42  * precedence of such block-like exprs to the point where
43  * you have to parenthesize them to get them to occur in the
44  * RHS of a binop. For example, you'd have to write:
45  *
46  *   12 + (if (foo) { 13 } else { 14 });
47  *
48  * rather than:
49  *
50  *   12 + if (foo) { 13 } else { 14 };
51  *
52  * Since we want to maintain the ability to write the latter,
53  * we leave the parens-burden on the trailing unop case.
54  *
55  */
56
57 pub fn main() {
58
59   let num = 12;
60
61   assert_eq!(if (true) { 12i } else { 12 } - num, 0);
62   assert_eq!(12i - if (true) { 12i } else { 12 }, 0);
63   if (true) { 12i; } {-num};
64   if (true) { 12i; }; {-num};
65   if (true) { 12i; };;; -num;
66 }