commit d4847c04bca741681432426d53a9f2e01f63c7dd
parent 555483740347dd7aacf361cea148151545255732
Author: Armin Weigl <tb46305@gmail.com>
Date: Mon, 24 May 2021 13:32:19 +0200
gen_slice_alloc: dealias initializer type
Signed-off-by: Armin Weigl <tb46305@gmail.com>
Diffstat:
2 files changed, 9 insertions(+), 5 deletions(-)
diff --git a/src/gen.c b/src/gen.c
@@ -539,9 +539,10 @@ gen_slice_alloc(struct gen_context *ctx,
"slice.size.%d");
const struct expression *initializer = expr->alloc.expr;
- if (initializer->result->storage == STORAGE_ARRAY) {
- assert(initializer->result->array.length != SIZE_UNDEFINED);
- constl(&len, initializer->result->array.length);
+ const struct type *itype = type_dealias(initializer->result);
+ if (itype->storage == STORAGE_ARRAY) {
+ assert(itype->array.length != SIZE_UNDEFINED);
+ constl(&len, itype->array.length);
} else {
assert(0); // TODO: Initialize one slice from another
}
@@ -575,7 +576,7 @@ gen_slice_alloc(struct gen_context *ctx,
pushi(ctx->current, &ptr, Q_ADD, &ptr, &temp, NULL);
pushi(ctx->current, NULL, Q_STOREL, &cap, &ptr, NULL);
- if (initializer->result->storage == STORAGE_ARRAY) {
+ if (itype->storage == STORAGE_ARRAY) {
ret.type = qtype_for_type(ctx, initializer->result, true);
ret.indirect = false;
gen_expression(ctx, initializer, &ret);
diff --git a/tests/17-alloc.ha b/tests/17-alloc.ha
@@ -50,8 +50,11 @@ fn double_alloc() void = {
free(y);
};
+type aslice = []int;
+type aarray = [3]int;
+
fn slice() void = {
- let x: []int = alloc([1, 2, 3], 10);
+ let x: aslice = alloc([1, 2, 3]: aarray, 10);
assert(len(x) == 3);
for (let i = 0z; i < len(x); i += 1) {
assert(x[i] == (i + 1): int);