commit 9e5f1f7497940ecb22780c77e52bb4b508ab1a4f
parent 72fea91ecc5b4735548f3e3965a8f734c841839b
Author: Alexey Yerin <yyp@disroot.org>
Date: Fri, 10 Jun 2022 16:52:33 +0300
check: dealias initializer in tuple unpacking case
This makes 'const (opt, arg) = cmd.opts[i]: getopt::option' and such
work.
Signed-off-by: Alexey Yerin <yyp@disroot.org>
Diffstat:
2 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/src/check.c b/src/check.c
@@ -1007,7 +1007,7 @@ check_binding_unpack(struct context *ctx,
struct expression *initializer = xcalloc(1, sizeof(struct expression));
check_expression(ctx, abinding->initializer, initializer, type);
- if (initializer->result->storage != STORAGE_TUPLE) {
+ if (type_dealias(initializer->result)->storage != STORAGE_TUPLE) {
error(ctx, aexpr->loc, expr, "Could not unpack non-tuple type");
return;
}
@@ -1016,6 +1016,7 @@ check_binding_unpack(struct context *ctx,
type = type_store_lookup_with_flags(
ctx->store, initializer->result, abinding->flags);
}
+ type = type_dealias(type);
binding->initializer = lower_implicit_cast(type, initializer);
diff --git a/tests/21-tuples.ha b/tests/21-tuples.ha
@@ -56,6 +56,7 @@ fn unpacking_addone() int = {
return unpacking_global;
};
+type tuple_alias = (int, int);
fn unpacking() void = {
const (a, b, c) = (42, 8, 12);
assert(a == 42);
@@ -89,6 +90,11 @@ fn unpacking() void = {
assert(a == 1);
assert(b == 2);
+ const t: tuple_alias = (1, 2);
+ const (a, b) = t;
+ assert(a == 1);
+ assert(b == 2);
+
unpacking_static();
unpacking_static();
const a = unpacking_static();