commit b2341bcd39cf7e04969a6bb1ee1c2887ce28a8e6
parent 9635244cf232e1919f2695b159b2834e7f29d133
Author: Drew DeVault <sir@cmpwn.com>
Date: Wed, 19 Jan 2022 13:29:40 +0100
check: fix allocation of array pointers
Fixes: https://todo.sr.ht/~sircmpwn/hare/552
Signed-off-by: Drew DeVault <sir@cmpwn.com>
Diffstat:
1 file changed, 17 insertions(+), 1 deletion(-)
diff --git a/src/check.c b/src/check.c
@@ -291,7 +291,6 @@ check_expr_alloc_init(struct context *ctx,
ptrflags = htype->pointer.flags;
break;
case STORAGE_SLICE:
- case STORAGE_ARRAY:
inithint = hint;
break;
default:
@@ -304,6 +303,23 @@ check_expr_alloc_init(struct context *ctx,
check_expression(ctx, aexpr->alloc.init, expr->alloc.init, inithint);
const struct type *objtype = expr->alloc.init->result;
+ if (type_dealias(objtype)->storage == STORAGE_ARRAY
+ && type_dealias(objtype)->array.expandable) {
+ const struct type *atype = type_dealias(objtype);
+ if (!hint) {
+ error(ctx, aexpr->loc, expr,
+ "Cannot infer expandable array length without type hint");
+ return;
+ }
+ const struct type *htype = type_dereference(hint);
+ if (htype->storage != STORAGE_ARRAY) {
+ error(ctx, aexpr->loc, expr,
+ "Cannot assign expandable array from non-array type");
+ return;
+ }
+ assert(htype->array.members == atype->array.members);
+ objtype = hint;
+ }
expr->result = type_store_lookup_pointer(ctx->store, objtype, ptrflags);
}