harec

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

commit bd6ec6bb88e4805f32fa344b51ad65b944fc8dfb
parent 62b883ac62dcdfc20afecacd0e4f7f680f569daf
Author: Bor Grošelj Simić <bor.groseljsimic@telemach.net>
Date:   Mon, 24 Jan 2022 05:28:42 +0100

append/insert: pass correct type hints

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

Diffstat:
Msrc/check.c | 6+-----
Mtests/19-append.ha | 13+++++++++++++
Mtests/28-insert.ha | 11+++++++++++
3 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/src/check.c b/src/check.c @@ -453,12 +453,7 @@ check_expr_append_insert(struct context *ctx, return; } - const struct type *valuehint = sltype; - if (!expr->append.is_multi) { - valuehint = sltype->array.members; - } expr->append.value = xcalloc(sizeof(struct expression), 1); - check_expression(ctx, aexpr->append.value, expr->append.value, valuehint); if (!expr->append.is_multi && !aexpr->append.length) { check_expression(ctx, aexpr->append.value, expr->append.value, @@ -474,6 +469,7 @@ check_expr_append_insert(struct context *ctx, return; } + check_expression(ctx, aexpr->append.value, expr->append.value, sltype); const struct type *valtype = type_dereference(expr->append.value->result); valtype = type_dealias(valtype); if (aexpr->append.length) { diff --git a/tests/19-append.ha b/tests/19-append.ha @@ -55,6 +55,18 @@ fn withlength() void = { free(x); }; +fn typehints() void = { + let x: []u8 = []; + append(x, 42); + append(x, [42]...); + append(x, [42...], 3); + assert(len(x) == 5); + for (let i = 0z; i < 5; i += 1) { + assert(x[i] == 42u8); + }; + free(x); +}; + fn reject() void = { assert(rt::compile(" let x: []u8 = [0u8]; @@ -90,5 +102,6 @@ export fn main() void = { multi(); _static(); withlength(); + typehints(); reject(); }; diff --git a/tests/28-insert.ha b/tests/28-insert.ha @@ -50,6 +50,16 @@ fn _static() void = { }; }; +fn typehints() void = { + let x: []u8 = []; + insert(x[0], 42); + insert(x[1], [42]...); + assert(len(x) == 2); + assert(x[0] == 42u8); + assert(x[1] == 42u8); + free(x); +}; + fn reject() void = { assert(rt::compile(" let x: []u8 = [0u8]; @@ -77,5 +87,6 @@ export fn main() void = { basics(); multi(); _static(); + typehints(); reject(); };