]> git.lizzy.rs Git - rust.git/blob - tests/ui/closures/old-closure-expr-precedence.rs
Rollup merge of #107248 - erikdesjardins:addrspace, r=oli-obk
[rust.git] / tests / ui / closures / old-closure-expr-precedence.rs
1 // run-pass
2
3 #![allow(unused_must_use)]
4 #![allow(unused_parens)]
5 // This test has some extra semis in it that the pretty-printer won't
6 // reproduce so we don't want to automatically reformat it
7
8 // no-reformat
9
10
11 /*
12  *
13  *  When you write a block-expression thing followed by
14  *  a lone unary operator, you can get a surprising parse:
15  *
16  *  if (...) { ... }
17  *  -num;
18  *
19  * for example, or:
20  *
21  *  if (...) { ... }
22  *  *box;
23  *
24  * These will parse as subtraction and multiplication binops.
25  * To get them to parse "the way you want" you need to brace
26  * the leading unops:
27
28  *  if (...) { ... }
29  *  {-num};
30  *
31  * or alternatively, semi-separate them:
32  *
33  *  if (...) { ... };
34  *  -num;
35  *
36  * This seems a little wonky, but the alternative is to lower
37  * precedence of such block-like exprs to the point where
38  * you have to parenthesize them to get them to occur in the
39  * RHS of a binop. For example, you'd have to write:
40  *
41  *   12 + (if (foo) { 13 } else { 14 });
42  *
43  * rather than:
44  *
45  *   12 + if (foo) { 13 } else { 14 };
46  *
47  * Since we want to maintain the ability to write the latter,
48  * we leave the parens-burden on the trailing unop case.
49  *
50  */
51
52 pub fn main() {
53
54   let num = 12;
55
56   assert_eq!(if (true) { 12 } else { 12 } - num, 0);
57   assert_eq!(12 - if (true) { 12 } else { 12 }, 0);
58   if (true) { 12; } {-num};
59   if (true) { 12; }; {-num};
60   if (true) { 12; };;; -num;
61   //~^ WARNING unnecessary trailing semicolons
62 }