From 4914731e5831b289f7933e53cf34bdc79c7b7403 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 6 Apr 2020 12:19:03 +1000 Subject: [PATCH] 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. --- py/parse.c | 4 ++-- tests/micropython/const_error.py | 9 +++++++++ tests/micropython/const_error.py.exp | 7 +++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/py/parse.c b/py/parse.c index 6ac5447330..bb18904d78 100644 --- a/py/parse.c +++ b/py/parse.c @@ -655,8 +655,8 @@ STATIC bool fold_constants(parser_t *parser, uint8_t rule_id, size_t num_args) { return false; } mp_token_kind_t tok = MP_PARSE_NODE_LEAF_ARG(peek_result(parser, i)); - if (tok == MP_TOKEN_OP_AT || tok == MP_TOKEN_OP_SLASH || tok == MP_TOKEN_OP_DBL_STAR) { - // Can't fold @ or / or ** + if (tok == MP_TOKEN_OP_AT || tok == MP_TOKEN_OP_SLASH) { + // Can't fold @ or / return false; } mp_binary_op_t op = MP_BINARY_OP_LSHIFT + (tok - MP_TOKEN_OP_DBL_LESS); diff --git a/tests/micropython/const_error.py b/tests/micropython/const_error.py index 311cfb4d5e..395fe0f776 100644 --- a/tests/micropython/const_error.py +++ b/tests/micropython/const_error.py @@ -15,3 +15,12 @@ test_syntax("a = const(x)") # redefined constant test_syntax("A = const(1); A = const(2)") + +# these operations are not supported within const +test_syntax("A = const(1 @ 2)") +test_syntax("A = const(1 / 2)") +test_syntax("A = const(1 ** 2)") +test_syntax("A = const(1 << -2)") +test_syntax("A = const(1 >> -2)") +test_syntax("A = const(1 % 0)") +test_syntax("A = const(1 // 0)") diff --git a/tests/micropython/const_error.py.exp b/tests/micropython/const_error.py.exp index 5275689b41..3edc3efe9c 100644 --- a/tests/micropython/const_error.py.exp +++ b/tests/micropython/const_error.py.exp @@ -1,2 +1,9 @@ SyntaxError SyntaxError +SyntaxError +SyntaxError +SyntaxError +SyntaxError +SyntaxError +SyntaxError +SyntaxError