From bc68304497d89c410a56bfdef86fd14ebc09610e Mon Sep 17 00:00:00 2001 From: Fernando Nunes Date: Fri, 25 Sep 2026 07:08:01 -0300 Subject: [PATCH] fix #7596 and #7511: proc literal as union variant in compound literal crashes backend --- src/check_expr.cpp | 4 +++- tests/issues/run.bat | 1 + tests/issues/run.sh | 1 + tests/issues/test_issue_7596.odin | 24 ++++++++++++++++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/issues/test_issue_7596.odin diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 1a4229475..dad9810de 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -9527,7 +9527,9 @@ gb_internal bool check_is_operand_compound_lit_constant(CheckerContext *c, Opera return true; } if (expr->kind == Ast_ProcLit) { - add_type_and_value(c, expr, Addressing_Constant, type_of_expr(expr), exact_value_procedure(expr)); + ExactValue value = exact_value_procedure(expr); + value.variant_type = o->value.variant_type; + add_type_and_value(c, expr, Addressing_Constant, type_of_expr(expr), value); return true; } diff --git a/tests/issues/run.bat b/tests/issues/run.bat index 6f4c05c13..f34417797 100644 --- a/tests/issues/run.bat +++ b/tests/issues/run.bat @@ -60,6 +60,7 @@ clang -c ..\test_issue_sysv_abi.c -o test_issue_sysv_abi_c.o || exit /b ..\..\..\odin test ..\test_issue_swizzle_multi_assign.odin %COMMON% || exit /b ..\..\..\odin run ..\test_issue_7562.odin %COMMON% -no-crt -no-thread-local || exit /b ..\..\..\odin run ..\test_issue_7562.odin %COMMON% -no-crt -no-thread-local -o:speed || exit /b +..\..\..\odin run ..\test_issue_7596.odin %COMMON% || exit /b @echo off diff --git a/tests/issues/run.sh b/tests/issues/run.sh index 7cd74fcda..0393e17ee 100755 --- a/tests/issues/run.sh +++ b/tests/issues/run.sh @@ -91,6 +91,7 @@ $ODIN test ../test_issue_7008.odin $COMMON $ODIN check ../test_issue_7012.odin -no-entry-point $COMMON_CHECK $ODIN build ../test_issue_7037.odin $COMMON -o:none $ODIN run ../test_issue_7564.odin $COMMON +$ODIN run ../test_issue_7596.odin $COMMON $ODIN test ../test_issue_7421.odin $COMMON if [[ $($ODIN check ../test_issue_7421_tagged_duplicate.odin $COMMON_CHECK 2>&1 >/dev/null | grep -c "Error: Duplicate case") -eq 1 ]]; then echo "SUCCESSFUL 1/1" diff --git a/tests/issues/test_issue_7596.odin b/tests/issues/test_issue_7596.odin new file mode 100644 index 000000000..05a20b5b0 --- /dev/null +++ b/tests/issues/test_issue_7596.odin @@ -0,0 +1,24 @@ +// Tests issue #7596 and #7511: a procedure literal converted to a union variant inside a +// compound literal crashed the backend with `value_type != nullptr`. +// https://github.com/odin-lang/Odin/issues/7596 +package test_issues + +P :: proc() -> int +U :: union {P, int} +S :: struct { + u: U, + m: Maybe(P), +} + +global_slice := []U{proc() -> int { return 1 }, 2} +global_struct := S{u = proc() -> int { return 3 }, m = proc() -> int { return 4 }} + +main :: proc() { + local := [1]U{proc() -> int { return 5 }} + + assert(global_slice[0].(P)() == 1) + assert(global_slice[1].(int) == 2) + assert(global_struct.u.(P)() == 3) + assert(global_struct.m.?() == 4) + assert(local[0].(P)() == 5) +}