| author | |
| committer | |
| log | 1bae04f02026427d1754f704bd24fce3317f3e89 |
| tree | 67ecdb6e49e44b791efc7adf7f313e27a544c7c8 |
| parent | 2d3eece3f4c6ab67165aab2c6c0bc890ad2338ea |
4 files changed, 12 insertions(+), 18 deletions(-)
doc/langref/ambiguous_precedence_mul_bitand.zig+2-4| ... | @@ -3,10 +3,8 @@ test "ambiguous precedence" { | ... | @@ -3,10 +3,8 @@ test "ambiguous precedence" { |
| 3 | const y: u8 = 2; | 3 | const y: u8 = 2; |
| 4 | const z: u8 = 3; | 4 | const z: u8 = 3; |
| 5 | 5 | ||
| 6 | // Its not obvious below if this is multiplication by a masked value | 6 | // 1. Could be multiplication by a masked value: (x&y) *% z |
| 7 | // `(x&y) *% z` | 7 | // 2. Or masking the result of multiplication: x & (y*%z) |
| 8 | // or a masking of wrapped multiplication | ||
| 9 | // `x & (y*%z)` | ||
| 10 | _ = x & y *% z; | 8 | _ = x & y *% z; |
| 11 | } | 9 | } |
| 12 | 10 |
doc/langref/ambiguous_precedence_orelse_ne.zig+3-6| ... | @@ -9,12 +9,9 @@ test "ambiguous precedence" { | ... | @@ -9,12 +9,9 @@ test "ambiguous precedence" { |
| 9 | const y: u8 = 0; | 9 | const y: u8 = 0; |
| 10 | const z: u8 = 0; | 10 | const z: u8 = 0; |
| 11 | 11 | ||
| 12 | // We might expect the line below to parse something like: | 12 | // Without parentheses the compiler cannot distinguish between two conflicting interpretations: |
| 13 | // `makeBool(x) orelse (y != z)` | 13 | // 1. Compare on the value which may be recovered: (makeBool(x) orelse y) != z |
| 14 | // but we might elsewhere want something like | 14 | // 2. Recover to the result of the comparison: makeBool(x) orelse (y != z) |
| 15 | // `(nullable(x) orelse y) != w` | ||
| 16 | // to perform a fallback and then a check, either way its | ||
| 17 | // not obvious the behavior so we get an error | ||
| 18 | _ = makeBool(x) orelse y != z; | 15 | _ = makeBool(x) orelse y != z; |
| 19 | } | 16 | } |
| 20 | 17 |
doc/langref/ambiguous_precedence_shift_sub.zig+2-2| ... | @@ -2,8 +2,8 @@ test "ambiguous precedence" { | ... | @@ -2,8 +2,8 @@ test "ambiguous precedence" { |
| 2 | const x: u64 = 1; | 2 | const x: u64 = 1; |
| 3 | const y: u6 = 63; | 3 | const y: u6 = 63; |
| 4 | 4 | ||
| 5 | // We probably meant `(x << y) - 1` | 5 | // 1. Could mean shift then subtract: (x<<y) - 1 such as to generate a mask |
| 6 | // But thats not how it would have compiled | 6 | // 2. Or to shift after the subtract: x << (y-1) such as selecting bits |
| 7 | _ = x << y - 1; | 7 | _ = x << y - 1; |
| 8 | } | 8 | } |
| 9 | 9 |
doc/langref/operator_precedence.zig+5-6| ... | @@ -5,20 +5,19 @@ test "operator precedence" { | ... | @@ -5,20 +5,19 @@ test "operator precedence" { |
| 5 | const y = 2; | 5 | const y = 2; |
| 6 | const z = 3; | 6 | const z = 3; |
| 7 | 7 | ||
| 8 | // Because comparisons have a lower precedence than any arithmetic | ||
| 9 | // We can safely avoid parenthesizing the expressions | ||
| 10 | assert(x + y * z == x + (y*z)); | 8 | assert(x + y * z == x + (y*z)); |
| 11 | assert(x * y + z == (x*y) + z); | 9 | assert(x * y + z == (x*y) + z); |
| 12 | 10 | ||
| 13 | // We can also use ambiguous operators as long as they are seperated | 11 | // Ambiguously related operators may exist in the same expression if they are not adjacent |
| 14 | // by a well ordered one such as below | ||
| 15 | assert(x*y + x%y == (x*y) + (x%y)); | 12 | assert(x*y + x%y == (x*y) + (x%y)); |
| 16 | //That includes comparisons | 13 | // Thus includes comparisons |
| 17 | assert(x & y != x + y); | 14 | assert(x & y != x + y); |
| 18 | 15 | ||
| 19 | // We can freely chain or's and and's, but not with each other | 16 | // Or's and and's may be freely chained, but not with each other |
| 20 | assert(false or x == y or x + y == z); | 17 | assert(false or x == y or x + y == z); |
| 21 | assert(true and x != y and y != z); | 18 | assert(true and x != y and y != z); |
| 19 | // The following line would be a compile error | ||
| 20 | // assert(false or x != y and y != z); | ||
| 22 | } | 21 | } |
| 23 | 22 | ||
| 24 | // test | 23 | // test |
| \ No newline at end of file | |||