harec

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

commit f6369572ec56f72d8449306face08cbb216e8740
parent c9d3c524809560d697095515b2622206269bdec1
Author: Pierre Curto <pierre.curto@gmail.com>
Date:   Wed, 23 Nov 2022 13:55:31 +0100

fix allocations of pointer type arrays

Fixes: https://todo.sr.ht/~sircmpwn/hare/476

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>

Diffstat:
Msrc/check.c | 4++--
Mtests/17-alloc.ha | 12++++++++++++
2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/src/check.c b/src/check.c @@ -333,12 +333,12 @@ check_expr_alloc_init(struct context *ctx, if (type_dealias(objtype)->storage == STORAGE_ARRAY && type_dealias(objtype)->array.expandable) { const struct type *atype = type_dealias(objtype); - if (!hint) { + if (!inithint) { error(ctx, aexpr->loc, expr, "Cannot infer expandable array length without type hint"); return; } - const struct type *htype = type_dereference(hint); + const struct type *htype = type_dealias(type_dereference(inithint)); if (htype->storage != STORAGE_ARRAY) { error(ctx, aexpr->loc, expr, "Cannot assign expandable array from non-array type"); diff --git a/tests/17-alloc.ha b/tests/17-alloc.ha @@ -53,6 +53,17 @@ fn double_alloc() void = { type aslice = []int; type aarray = [3]int; +fn array() void = { + let aa: *aarray = alloc([0...]); + free(aa); + let aa: nullable *aarray = alloc([0...]); + free(aa); + let aa: *aarray = alloc([0...]: aarray); + free(aa); + let aa: nullable *aarray = alloc([0...]: aarray); + free(aa); +}; + fn slice() void = { let x: aslice = alloc([1, 2, 3]: aarray, 10); assert(len(x) == 3); @@ -131,5 +142,6 @@ export fn main() void = { allocation(); double_pointer(); double_alloc(); + array(); slice(); };