harec

[hare] Hare compiler, written in C11 for POSIX OSs
Log | Files | Refs | README | LICENSE

commit 7bfb0a5e3bdb49aa44fd3a6a5309be9157d04b3d
parent 2b6425aeac1d91fd4c34062b762042e8f5fd1b7e
Author: Ember Sawady <ecs@d2evs.net>
Date:   Mon,  6 Feb 2023 16:34:24 +0000

Don't crash on assigning to constants

Signed-off-by: Ember Sawady <ecs@d2evs.net>

Diffstat:
Msrc/check.c | 6++++++
Mtests/26-regression.ha | 4++++
2 files changed, 10 insertions(+), 0 deletions(-)

diff --git a/src/check.c b/src/check.c @@ -718,6 +718,12 @@ check_expr_assign(struct context *ctx, expr->assign.op = aexpr->assign.op; check_expression(ctx, aexpr->assign.value, value, object->result); + if (object->type == EXPR_CONSTANT + && object->result != &builtin_type_error) { + error(ctx, aexpr->assign.object->loc, expr, + "Cannot assign to constant"); + return; + } if (object->type == EXPR_SLICE) { if (expr->assign.op != BIN_LEQUAL) { diff --git a/tests/26-regression.ha b/tests/26-regression.ha @@ -104,4 +104,8 @@ export fn main() void = { let x: (void | int) = abort(); };" ) as rt::exited == rt::EXIT_SUCCESS); + assert(rt::compile(" + def A = 0; + fn a() void = A = 0;" + ) as rt::exited != rt::EXIT_SUCCESS); };