Skip to content

Commit f55a059

Browse files
committed
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.
1 parent 5010d19 commit f55a059

1 file changed

Lines changed: 4 additions & 9 deletions

File tree

py/compile.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,7 @@ STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_
456456
break;
457457
}
458458
} else {
459-
compile_syntax_error(comp, pn, "can't assign to literal");
460-
return;
459+
goto cannot_assign;
461460
}
462461
} else {
463462
// 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_
472471
case PN_exprlist:
473472
// lhs is a tuple
474473
if (assign_kind != ASSIGN_STORE) {
475-
goto bad_aug;
474+
goto cannot_assign;
476475
}
477476
c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
478477
break;
@@ -485,7 +484,7 @@ STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_
485484
} else {
486485
assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp));
487486
if (assign_kind != ASSIGN_STORE) {
488-
goto bad_aug;
487+
goto cannot_assign;
489488
}
490489
pns = (mp_parse_node_struct_t*)pns->nodes[0];
491490
goto testlist_comp;
@@ -495,7 +494,7 @@ STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_
495494
case PN_atom_bracket:
496495
// lhs is something in brackets
497496
if (assign_kind != ASSIGN_STORE) {
498-
goto bad_aug;
497+
goto cannot_assign;
499498
}
500499
if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
501500
// empty list, assignment allowed
@@ -543,10 +542,6 @@ STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_
543542

544543
cannot_assign:
545544
compile_syntax_error(comp, pn, "can't assign to expression");
546-
return;
547-
548-
bad_aug:
549-
compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
550545
}
551546

552547
// stuff for lambda and comprehensions and generators:

0 commit comments

Comments
 (0)