harec

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

commit e2343e7755256f7fa08020b4d674575ab65de424
parent 53d135c28b364c527a570d0c88608182a209f9dc
Author: Bor Grošelj Simić <bor.groseljsimic@telemach.net>
Date:   Wed,  2 Feb 2022 15:15:09 +0100

check: implicitly cast slice allocation capacity to size

Signed-off-by: Bor Grošelj Simić <bor.groseljsimic@telemach.net>

Diffstat:
Msrc/check.c | 1+
Mtests/17-alloc.ha | 6++++++
2 files changed, 7 insertions(+), 0 deletions(-)

diff --git a/src/check.c b/src/check.c @@ -351,6 +351,7 @@ check_expr_alloc_slice(struct context *ctx, "Slice capacity must be assignable to size"); return; } + expr->alloc.cap = lower_implicit_cast(&builtin_type_size, expr->alloc.cap); const struct type *membtype = type_dealias(objtype)->array.members; expr->result = type_store_lookup_slice(ctx->store, membtype); diff --git a/tests/17-alloc.ha b/tests/17-alloc.ha @@ -84,6 +84,12 @@ fn slice() void = { for (let i = 2z; i < len(x); i += 1) { assert(x[i] == 3); }; + + // ensure capacity is cast to size correctly + let a: u32 = 4; + let y: []u64 = alloc([1...], a); + defer free(y); + assert(len(y) == 4 && y[0] == 1 && y[3] == 1); }; fn slice_copy() void = {