From 99aa0d3a35a86d00c488554ff712c922f7133005 Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Mon, 12 Aug 2024 00:02:05 +0200 Subject: [PATCH] fix type switching over internal pointer union Fixes #3947 --- src/llvm_backend_stmt.cpp | 2 +- tests/internal/test_union_switch.odin | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 tests/internal/test_union_switch.odin diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index e70cc503e..8f1c3a123 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -1646,7 +1646,7 @@ gb_internal void lb_build_type_switch_stmt(lbProcedure *p, AstTypeSwitchStmt *ss union_data = lb_emit_conv(p, parent_ptr, t_rawptr); Type *union_type = type_deref(parent_ptr.type); if (is_type_union_maybe_pointer(union_type)) { - tag = lb_emit_conv(p, lb_emit_comp_against_nil(p, Token_NotEq, union_data), t_int); + tag = lb_emit_conv(p, lb_emit_comp_against_nil(p, Token_NotEq, parent_value), t_int); } else if (union_tag_size(union_type) == 0) { tag = {}; // there is no tag for a zero sized union } else { diff --git a/tests/internal/test_union_switch.odin b/tests/internal/test_union_switch.odin new file mode 100644 index 000000000..f96c0e55e --- /dev/null +++ b/tests/internal/test_union_switch.odin @@ -0,0 +1,24 @@ +package test_internal + +import "core:log" +import "core:testing" + +@(test) +test_internal_pointer_union_switch :: proc(t: ^testing.T) { + foo: Maybe(^int) + + switch _ in foo { + case ^int: + log.error("incorrect case") + case nil: + } + + v := 1 + foo = &v + + switch _ in foo { + case ^int: + case nil: + log.error("incorrect case") + } +}