From 056c15c64818287d2d5399b35c762ab9286848ca Mon Sep 17 00:00:00 2001 From: Sergei Zharinov Date: Sat, 3 Jan 2026 22:46:26 -0300 Subject: [PATCH] test(engine): add regression test for enum tag with definition refs BUG #6: Tagged alternation used wrong $tag when branches reference named definitions. The fix is already in master - this adds the regression test. --- crates/plotnik-lib/src/engine/engine_tests.rs | 18 ++++++++++++++++ ...ression_enum_tag_with_definition_refs.snap | 21 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 crates/plotnik-lib/src/engine/snapshots/plotnik_lib__engine__engine_tests__regression_enum_tag_with_definition_refs.snap diff --git a/crates/plotnik-lib/src/engine/engine_tests.rs b/crates/plotnik-lib/src/engine/engine_tests.rs index 4a6c4529..d631f82c 100644 --- a/crates/plotnik-lib/src/engine/engine_tests.rs +++ b/crates/plotnik-lib/src/engine/engine_tests.rs @@ -398,3 +398,21 @@ fn regression_call_searches_among_siblings() { entry: "Q" ); } + +/// BUG #6: Tagged alternation used wrong $tag when branches reference named definitions. +/// The issue was that enum members are emitted in BTreeMap (Symbol interning) order, +/// but the compiler used AST branch order for member indices. When a definition name +/// (e.g., "Second") is interned before a branch label (e.g., "First"), the indices mismatch. +#[test] +fn regression_enum_tag_with_definition_refs() { + // "Second" is interned as a definition name before "First" is seen as a branch label. + // This caused the First branch to incorrectly get tagged as "Second". + snap!( + indoc! {r#" + Second = (binary_expression left: (number) @left right: (number) @right) + Q = (program (expression_statement [First: (number) @num Second: (Second) @bin])) + "#}, + "42", + entry: "Q" + ); +} diff --git a/crates/plotnik-lib/src/engine/snapshots/plotnik_lib__engine__engine_tests__regression_enum_tag_with_definition_refs.snap b/crates/plotnik-lib/src/engine/snapshots/plotnik_lib__engine__engine_tests__regression_enum_tag_with_definition_refs.snap new file mode 100644 index 00000000..789e214f --- /dev/null +++ b/crates/plotnik-lib/src/engine/snapshots/plotnik_lib__engine__engine_tests__regression_enum_tag_with_definition_refs.snap @@ -0,0 +1,21 @@ +--- +source: crates/plotnik-lib/src/engine/engine_tests.rs +--- +Second = (binary_expression left: (number) @left right: (number) @right) +Q = (program (expression_statement [First: (number) @num Second: (Second) @bin])) +--- +42 +--- +{ + "$tag": "First", + "$data": { + "num": { + "kind": "number", + "text": "42", + "span": [ + 0, + 2 + ] + } + } +}