commit 39a715cc39c7afb8828be728709fea328a5a4096
parent 4a535032d787fd4f7aeab7327e578621ebcee630
Author: Ember Sawady <ecs@d2evs.net>
Date: Mon, 6 Feb 2023 16:34:28 +0000
Disallow casting slices to arrays
Rather than crashing in gen
Signed-off-by: Ember Sawady <ecs@d2evs.net>
Diffstat:
2 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/src/types.c b/src/types.c
@@ -997,12 +997,13 @@ type_is_castable(const struct type *to, const struct type *from)
|| to->storage == STORAGE_UINTPTR
? to_orig : NULL;
case STORAGE_SLICE:
- case STORAGE_ARRAY:
return to->storage == STORAGE_SLICE
- || to->storage == STORAGE_ARRAY
|| (to->storage == STORAGE_POINTER
- && to->pointer.referent->storage == STORAGE_ARRAY
- && from->storage == STORAGE_SLICE)
+ && to->pointer.referent->storage == STORAGE_ARRAY)
+ ? to_orig : NULL;
+ case STORAGE_ARRAY:
+ return to->storage == STORAGE_ARRAY
+ || to->storage == STORAGE_SLICE
? to_orig : NULL;
// Cannot be cast:
case STORAGE_STRING:
diff --git a/tests/26-regression.ha b/tests/26-regression.ha
@@ -111,4 +111,5 @@ export fn main() void = {
assert(rt::compile("def A = x && true;") as rt::exited != rt::EXIT_SUCCESS);
assert(rt::compile("type a = struct { b: fn() void };") as rt::exited != rt::EXIT_SUCCESS);
assert(rt::compile("fn a() []int = alloc([]: [*]int, 0);") as rt::exited != rt::EXIT_SUCCESS);
+ assert(rt::compile("fn a() [1]int = [1]: []int: [1]int;") as rt::exited != rt::EXIT_SUCCESS);
};