File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ *
3
+ * When you write a block-expression thing followed by
4
+ * a lone unary operator, you can get a surprising parse:
5
+ *
6
+ * if (...) { ... }
7
+ * -num;
8
+ *
9
+ * for example, or:
10
+ *
11
+ * if (...) { ... }
12
+ * *box;
13
+ *
14
+ * These will parse as subtraction and multiplication binops.
15
+ * To get them to parse "the way you want" you need to brace
16
+ * the leading unops:
17
+
18
+ * if (...) { ... }
19
+ * {-num};
20
+ *
21
+ * or alternatively, semi-separate them:
22
+ *
23
+ * if (...) { ... };
24
+ * -num;
25
+ *
26
+ * This seems a little wonky, but the alternative is to lower
27
+ * precedence of such block-like exprs to the point where
28
+ * you have to parenthesize them to get them to occur in the
29
+ * RHS of a binop. For example, you'd have to write:
30
+ *
31
+ * 12 + (if (foo) { 13 } else { 14 });
32
+ *
33
+ * rather than:
34
+ *
35
+ * 12 + if (foo) { 13 } else { 14 };
36
+ *
37
+ * Since we want to maintain the ability to write the latter,
38
+ * we leave the parens-burden on the trailing unop case.
39
+ *
40
+ */
41
+
42
+ fn main ( ) {
43
+
44
+ auto num = 12 ;
45
+
46
+ assert if ( true ) { 12 } else { 12 } - num == 0 ;
47
+ assert 12 - if ( true ) { 12 } else { 12 } == 0 ;
48
+ if ( true ) { 12 } { -num} ;
49
+ if ( true ) { 12 } ; { -num} ;
50
+ if ( true ) { 12 } ; ; ; -num;
51
+ }
You can’t perform that action at this time.
0 commit comments