From f55a059e7a31ceb301196ecea80c1f4a043f4150 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 29 Mar 2017 12:28:33 +1100 Subject: [PATCH] py/compile: Simplify syntax-error messages for illegal assignments. With this patch all illegal assignments are reported as "can't assign to expression". Before the patch there were special cases for a literal on the LHS, and for augmented assignments (eg +=), but it seems a waste of bytes (and there are lots of bytes used in error messages) to spend on distinguishing such errors which a user will rarely encounter. --- py/compile.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/py/compile.c b/py/compile.c index 1e49a350f..1f9eb84fe 100644 --- a/py/compile.c +++ b/py/compile.c @@ -456,8 +456,7 @@ STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_ break; } } else { - compile_syntax_error(comp, pn, "can't assign to literal"); - return; + goto cannot_assign; } } else { // pn must be a struct @@ -472,7 +471,7 @@ STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_ case PN_exprlist: // lhs is a tuple if (assign_kind != ASSIGN_STORE) { - goto bad_aug; + goto cannot_assign; } c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes); break; @@ -485,7 +484,7 @@ STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_ } else { assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)); if (assign_kind != ASSIGN_STORE) { - goto bad_aug; + goto cannot_assign; } pns = (mp_parse_node_struct_t*)pns->nodes[0]; goto testlist_comp; @@ -495,7 +494,7 @@ STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_ case PN_atom_bracket: // lhs is something in brackets if (assign_kind != ASSIGN_STORE) { - goto bad_aug; + goto cannot_assign; } if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { // empty list, assignment allowed @@ -543,10 +542,6 @@ STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_ cannot_assign: compile_syntax_error(comp, pn, "can't assign to expression"); - return; - - bad_aug: - compile_syntax_error(comp, pn, "illegal expression for augmented assignment"); } // stuff for lambda and comprehensions and generators: