mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
[FunC] Require parenthesis in tricky bitwise precedence cases
Example: "flags & 0xFF != 0" is equivalent to "flags & 1", most likely it's unexpected. Example: "a << 2 + 1" (equal to "a << 3", probably unexpected). The only way to suppress this error for the programmer is to use parenthesis.
This commit is contained in:
parent
5867d52926
commit
79721d230e
10 changed files with 189 additions and 10 deletions
9
crypto/func/auto-tests/tests/invalid-bitwise-1.fc
Normal file
9
crypto/func/auto-tests/tests/invalid-bitwise-1.fc
Normal file
|
@ -0,0 +1,9 @@
|
|||
int main(int flags) {
|
||||
return flags & 0xFF != 0;
|
||||
}
|
||||
|
||||
{-
|
||||
@compilation_should_fail
|
||||
@stderr & has lower precedence than !=
|
||||
@stderr Use parenthesis
|
||||
-}
|
8
crypto/func/auto-tests/tests/invalid-bitwise-2.fc
Normal file
8
crypto/func/auto-tests/tests/invalid-bitwise-2.fc
Normal file
|
@ -0,0 +1,8 @@
|
|||
int justTrue() { return true; }
|
||||
|
||||
const a = justTrue() | 1 < 9;
|
||||
|
||||
{-
|
||||
@compilation_should_fail
|
||||
@stderr | has lower precedence than <
|
||||
-}
|
8
crypto/func/auto-tests/tests/invalid-bitwise-3.fc
Normal file
8
crypto/func/auto-tests/tests/invalid-bitwise-3.fc
Normal file
|
@ -0,0 +1,8 @@
|
|||
int justTrue() { return true; }
|
||||
|
||||
const a = justTrue() | (1 < 9) | justTrue() != true;
|
||||
|
||||
{-
|
||||
@compilation_should_fail
|
||||
@stderr | has lower precedence than !=
|
||||
-}
|
6
crypto/func/auto-tests/tests/invalid-bitwise-4.fc
Normal file
6
crypto/func/auto-tests/tests/invalid-bitwise-4.fc
Normal file
|
@ -0,0 +1,6 @@
|
|||
const a = (1) <=> (0) ^ 8;
|
||||
|
||||
{-
|
||||
@compilation_should_fail
|
||||
@stderr ^ has lower precedence than <=>
|
||||
-}
|
11
crypto/func/auto-tests/tests/invalid-bitwise-5.fc
Normal file
11
crypto/func/auto-tests/tests/invalid-bitwise-5.fc
Normal file
|
@ -0,0 +1,11 @@
|
|||
const MAX_SLIPAGE = 100;
|
||||
|
||||
_ main(int jetton_amount, int msg_value, int slippage) {
|
||||
if (0 == jetton_amount) | (msg_value == 0) | true | false | slippage > MAX_SLIPAGE {
|
||||
}
|
||||
}
|
||||
|
||||
{-
|
||||
@compilation_should_fail
|
||||
@stderr | has lower precedence than >
|
||||
-}
|
9
crypto/func/auto-tests/tests/invalid-bitwise-6.fc
Normal file
9
crypto/func/auto-tests/tests/invalid-bitwise-6.fc
Normal file
|
@ -0,0 +1,9 @@
|
|||
_ main() {
|
||||
if ((1 == 1) | (2 == 2) & (3 == 3)) {
|
||||
}
|
||||
}
|
||||
|
||||
{-
|
||||
@compilation_should_fail
|
||||
@stderr mixing | with & without parenthesis
|
||||
-}
|
8
crypto/func/auto-tests/tests/invalid-shift-1.fc
Normal file
8
crypto/func/auto-tests/tests/invalid-shift-1.fc
Normal file
|
@ -0,0 +1,8 @@
|
|||
_ main(int flags) {
|
||||
return flags << 1 + 32;
|
||||
}
|
||||
|
||||
{-
|
||||
@compilation_should_fail
|
||||
@stderr << has lower precedence than +
|
||||
-}
|
|
@ -1,35 +1,44 @@
|
|||
int justTrue() { return true; }
|
||||
|
||||
int test1(int x, int y, int z) method_id(101) {
|
||||
return x > 0 & y > 0 & z > 0;
|
||||
return (x > 0) & (y > 0) & (z > 0);
|
||||
}
|
||||
|
||||
int test2(int x, int y, int z) method_id(102) {
|
||||
return x > (0 & y > 0 & z > 0);
|
||||
return x > (0 & (y > 0) & (z > 0));
|
||||
}
|
||||
|
||||
int test3(int x, int y, int z) method_id(103) {
|
||||
if (x < 0 | y < 0) {
|
||||
if ((x < 0) | (y < 0)) {
|
||||
return z < 0;
|
||||
}
|
||||
return x > 0 & y > 0;
|
||||
return (x > 0) & (y > 0);
|
||||
}
|
||||
|
||||
int test4(int x, int y, int mode) method_id(104) {
|
||||
if (mode == 1) {
|
||||
return x == 10 | (y == 20);
|
||||
return (x == 10) | (y == 20);
|
||||
} if (mode == 2) {
|
||||
return x == 10 | y == 20;
|
||||
return (x == 10) | (y == 20);
|
||||
} else {
|
||||
return x == (10 | (y == 20));
|
||||
}
|
||||
}
|
||||
|
||||
int test5(int status) method_id(105) {
|
||||
return justTrue() & status == 1 & (justTrue() & status) == 1;
|
||||
return justTrue() & (status == 1) & ((justTrue() & status) == 1);
|
||||
}
|
||||
|
||||
() main() { }
|
||||
int _<p(_ a, _ b) { return true; }
|
||||
|
||||
() main() {
|
||||
;; ok to parse
|
||||
var c = [
|
||||
(3 & 3) > 0, 3 & (3 > 0), 3 & (_<_(3, 0)),
|
||||
3 & _<p(3, 0), (1 & 2) ^ (3 | 4),
|
||||
1 & ((1) == 1)
|
||||
];
|
||||
}
|
||||
|
||||
{-
|
||||
TESTCASE | 101 | 1 2 3 | -1
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue