harec

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

commit c3480506fea714cc9dff999329e33535f575127d
parent 00ce5ce8ee0c9cd5bb792bff7d1edd458b688a4d
Author: Bor Grošelj Simić <bgs@turminal.net>
Date:   Tue, 30 Aug 2022 15:41:20 +0200

Detect and report tuple lenght mismatches in unpack

Signed-off-by: Bor Grošelj Simić <bgs@turminal.net>

Diffstat:
Msrc/check.c | 7++++++-
Mtests/21-tuples.ha | 3+++
2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/check.c b/src/check.c @@ -1040,7 +1040,7 @@ check_binding_unpack(struct context *ctx, } const struct type_tuple *type_tuple = &type->tuple; bool found_binding = false; - while (cur) { + while (cur && type_tuple) { if (type_tuple->type->storage == STORAGE_NULL) { error(ctx, aexpr->loc, expr, "Null is not a valid type for a binding"); @@ -1095,6 +1095,11 @@ check_binding_unpack(struct context *ctx, "Fewer bindings than tuple elements were provided when unpacking"); return; } + if (cur) { + error(ctx, aexpr->loc, expr, + "More bindings than tuple elements were provided when unpacking"); + return; + } } static void diff --git a/tests/21-tuples.ha b/tests/21-tuples.ha @@ -135,6 +135,9 @@ fn reject() void = { "fn t() void = { let a: (u8, u8) = (2, 3, 4); };", "fn t() void = { let a: (u8, u8, u8) = (2, 3, 4); let b: (u8, u8) = a; };", "fn t() void = { let (x, y) = (1, 2, 3); };", + "fn t() void = { let (x, y, z) = (1, 2); };", + "fn t() void = { let (x, y, _) = (1, 2); };", + "fn t() void = { let (x, _, _) = (1, 2); };", // empty tuple "fn t() void = { let a: (() | void) = void; };", "fn t() void = { let a = () };",