Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/eval/interpreter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7147,9 +7147,12 @@ pub const Interpreter = struct {
}
} else if (arg_vars.len == 1) {
const arg_var = arg_vars[0];
const arg_layout = try self.getRuntimeLayout(arg_var);
// Use the variant layout from the actual tag union data, not computed from type.
// This is critical for recursive types where the payload is boxed in memory
// even though the type says it's the recursive type directly.
const variant_layout = acc.getVariantLayout(tag_index);
payload_value = StackValue{
.layout = arg_layout,
.layout = variant_layout,
.ptr = value.ptr,
.is_initialized = true,
.rt_var = arg_var,
Expand Down
56 changes: 56 additions & 0 deletions src/eval/test/comptime_eval_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2701,6 +2701,62 @@ test "encode - custom format type with infallible encoding (empty error type)" {
try testing.expectEqual(@as(u32, 0), summary.crashed);
}

test "issue 8754: pattern matching on recursive tag union variant payload" {
// Regression test for issue #8754: pattern matching on direct recursive tag union
// variant payload was returning the wrong discriminant.
//
// When Wrapper(Tree) is created where Tree := [..., Wrapper(Tree)], the payload is
// stored as a Box. The bug was extractTagValue using getRuntimeLayout(arg_var)
// which returns the non-boxed layout, causing pattern matching on the extracted
// payload to fail.
const src =
\\Tree := [Node(Str, List(Tree)), Text(Str), Wrapper(Tree)]
\\
\\inner : Tree
\\inner = Text("hello")
\\
\\wrapped : Tree
\\wrapped = Wrapper(inner)
\\
\\result = match wrapped {
\\ Wrapper(inner_tree) =>
\\ match inner_tree {
\\ Text(_) => 1
\\ Node(_, _) => 2
\\ Wrapper(_) => 3
\\ }
\\ _ => 0
\\}
;

var result = try parseCheckAndEvalModule(src);
defer cleanupEvalModule(&result);

const summary = try result.evaluator.evalAll();
try testing.expectEqual(@as(u32, 0), summary.crashed);

// Verify 'result' was folded to 1 (matched Text, not Wrapper)
const defs = result.module_env.store.sliceDefs(result.module_env.all_defs);

for (defs) |def_idx| {
const def = result.module_env.store.getDef(def_idx);
const pattern = result.module_env.store.getPattern(def.pattern);

if (pattern == .assign) {
const ident_text = result.module_env.getIdent(pattern.assign.ident);
if (std.mem.eql(u8, ident_text, "result")) {
const expr = result.module_env.store.getExpr(def.expr);
try testing.expect(expr == .e_num);
const value = expr.e_num.value.toI128();
try testing.expectEqual(@as(i128, 1), value);
return; // Test passed
}
}
}

return error.TestExpectedDefNotFound;
}

test "comptime eval - attached methods on tag union type aliases (issue #8637)" {
// Regression test for GitHub issue #8637
// Methods attached to transparent tag union type aliases with type parameters
Expand Down
Loading