Skip to content

Commit 4914731

Browse files
committed
py/parse: Remove unnecessary check in const folding for ** operator.
In this part of the code there is no way to get the ** operator, so no need to check for it. This commit also adds tests for this, and other related, invalid const operations.
1 parent a5f2ae1 commit 4914731

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

py/parse.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,8 +655,8 @@ STATIC bool fold_constants(parser_t *parser, uint8_t rule_id, size_t num_args) {
655655
return false;
656656
}
657657
mp_token_kind_t tok = MP_PARSE_NODE_LEAF_ARG(peek_result(parser, i));
658-
if (tok == MP_TOKEN_OP_AT || tok == MP_TOKEN_OP_SLASH || tok == MP_TOKEN_OP_DBL_STAR) {
659-
// Can't fold @ or / or **
658+
if (tok == MP_TOKEN_OP_AT || tok == MP_TOKEN_OP_SLASH) {
659+
// Can't fold @ or /
660660
return false;
661661
}
662662
mp_binary_op_t op = MP_BINARY_OP_LSHIFT + (tok - MP_TOKEN_OP_DBL_LESS);

tests/micropython/const_error.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,12 @@ def test_syntax(code):
1515

1616
# redefined constant
1717
test_syntax("A = const(1); A = const(2)")
18+
19+
# these operations are not supported within const
20+
test_syntax("A = const(1 @ 2)")
21+
test_syntax("A = const(1 / 2)")
22+
test_syntax("A = const(1 ** 2)")
23+
test_syntax("A = const(1 << -2)")
24+
test_syntax("A = const(1 >> -2)")
25+
test_syntax("A = const(1 % 0)")
26+
test_syntax("A = const(1 // 0)")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
SyntaxError
22
SyntaxError
3+
SyntaxError
4+
SyntaxError
5+
SyntaxError
6+
SyntaxError
7+
SyntaxError
8+
SyntaxError
9+
SyntaxError

0 commit comments

Comments
 (0)