harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit cd83be45847f4a17c15ae88a9edc068e2d7a63bc
parent cc69317b8e60c86eff3d3833b19c1a033f56caf5
Author: Sebastian <sebastian@sebsite.pw>
Date:   Sun, 24 Apr 2022 23:40:24 -0400

check: implicit dereferencing for insert() and delete()

Prior to this commit, attempting to index a slice pointer within an
insert or delete expression would result in a compile error.

Signed-off-by: Sebastian <sebastian@sebsite.pw>

Diffstat:
Msrc/check.c | 7++-----
Mtests/22-delete.ha | 4+++-
Mtests/28-insert.ha | 3++-
3 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/src/check.c b/src/check.c @@ -444,7 +444,7 @@ check_expr_append_insert(struct context *ctx, case EXPR_INSERT: assert(expr->append.object->access.type == ACCESS_INDEX); sltype = expr->append.object->access.array->result; - sltype = type_dealias(sltype); + sltype = type_dealias(type_dereference(sltype)); exprtype_name = "insert"; break; default: @@ -1532,10 +1532,7 @@ check_expr_delete(struct context *ctx, "Deleted expression must be slicing or indexing expression"); return; } - otype = type_dealias(otype); - while (otype->storage == STORAGE_POINTER) { - otype = type_dealias(otype->pointer.referent); - } + otype = type_dealias(type_dereference(otype)); if (otype->storage != STORAGE_SLICE) { error(ctx, aexpr->delete.expr->loc, expr, "delete must operate on a slice"); diff --git a/tests/22-delete.ha b/tests/22-delete.ha @@ -12,7 +12,9 @@ fn index() void = { static delete(y[3]); assert(len(x) == 3); - assert(x[0] == 1 && x[1] == 3 && x[2] == 4); + delete(y[0]); + assert(len(x) == 2); + assert(x[0] == 3 && x[1] == 4); free(x); }; diff --git a/tests/28-insert.ha b/tests/28-insert.ha @@ -2,13 +2,14 @@ use rt; fn basics() void = { let x: []int = alloc([1, 2, 5]); + let y = &x; insert(x[2], 4); insert(x[2], 3); assert(len(x) == 5); for (let i = 0z; i < len(x); i += 1) { assert(x[i] == i: int + 1); }; - insert(x[0], 42); + insert(y[0], 42); assert(len(x) == 6 && x[0] == 42); free(x); };