commit 371d85f2a8fc8085aeb54403b8d39305ee60aa11
parent 0397b5f11741d0be92e5ba04c155c416b914ffd0
Author: Eyal Sawady <ecs@d2evs.net>
Date: Fri, 13 Aug 2021 10:40:06 +0000
gen: fix type assertions to tagged unions
Minimal reproduction:
type a = (int | void);
type b = (a | void);
fn test() void = {
let x: b = void;
x as a;
};
Signed-off-by: Eyal Sawady <ecs@d2evs.net>
Diffstat:
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/src/gen.c b/src/gen.c
@@ -1094,6 +1094,11 @@ cast_prefers_at(const struct expression *expr)
if (expr->cast.kind == C_TEST) {
return false;
}
+ // tagged => *; subtype compatible
+ if (type_dealias(from)->storage == STORAGE_TAGGED
+ && tagged_select_subtype(from, to)) {
+ return false;
+ }
// * => tagged
if (type_dealias(to)->storage == STORAGE_TAGGED) {
return true;
@@ -1139,7 +1144,7 @@ gen_expr_cast(struct gen_context *ctx, const struct expression *expr)
return gen_expr_type_test(ctx, expr);
case C_ASSERTION:
assert(type_dealias(from)->storage == STORAGE_TAGGED);
- assert(type_dealias(to)->storage != STORAGE_TAGGED);
+ assert(tagged_select_subtype(from, to));
// Fallthrough
case C_CAST:
break;