Commit a5b1e2bc6e for perl

commit a5b1e2bc6e9d0a031f25967173b859629c4b449a
Author: Richard Leach <rich+perl@hyphen-dash-hyphen.info>
Date:   Thu Nov 12 12:13:12 2026 +0000

    S_flatten_inner_branches: don't leave an early NOTHING regnode

    The function originally just converted the outer BRANCH to a NOTHING.
    However this (or converting the first inner BRANCH to a NOTHING) would
    cause regnode walks to stop at the start of the branch chain, preventing
    identification of a valid stclass.

    This commit repurposes the outer BRANCH regnode instead and shifts the
    inner BRANCH chain up to overwrite the first inner BRANCH regnode.
    Trailing gaps are then converted to OPTIMIZED regnodes.

diff --git a/regcomp.c b/regcomp.c
index 7b371f3b08..0868381a7e 100644
--- a/regcomp.c
+++ b/regcomp.c
@@ -3227,14 +3227,63 @@ S_flatten_inner_branches(pTHX_ RExC_state_t *pRExC_state, regnode *first_outer_b
             NEXT_OFF_set(prev_outer_br, first_inner_br - prev_outer_br);
             OP(outer_br) = OPTIMIZED;
             NEXT_OFF_set(outer_br, 0);
+
+            regnode *detritus = outer_br + NODE_STEP_REGNODE;
+            OP(detritus) = OPTIMIZED;
+            NEXT_OFF_set(detritus, 0);
         } else {
-            OP(outer_br) = NOTHING;
-            NEXT_OFF_set(outer_br, first_inner_br - outer_br);
-        }
+             /* Note: the following code depends upon there being no
+              * capture groups in the outer branch. If this function is
+              * revised to enable flattening when they are present, some
+              * revision here may be necessary. */
+            assert(BRANCH_CAPTURE_FLOOR(outer_br)
+                == BRANCH_CAPTURE_FLOOR(first_inner_br));
+            assert(BRANCH_LAST_CAPTURE(outer_br)
+                == BRANCH_LAST_CAPTURE(first_inner_br));
+
+            /* If the size of a BRANCH regnode ever changes, the 1s and 2s
+               below will need updating. */
+            assert(sizeof(tregnode_BRANCH) == 2 * sizeof(regnode));
+
+            /* If we flatten by doing:
+             *     OP(outer_br) = NOTHING;
+             * or
+             *     OP(first_inner_branch) = NOTHING;
+             * that would cause code elsewhere to stop at that regnode
+             * rather than walking the whole flattened branch chain.
+             * That could prevent a valid stclass being identified.
+             *
+             * Instead, the outer_br is repurposed and the inner
+             * branches are moved to overwrite the first_inner_branch. */
+            regnode * const src = REGNODE_AFTER(first_inner_br);
+            regnode * const src_end = regnext(first_inner_br);
+            regnode * const unused = src_end - 2;
+
+            /* Update inner branch regnode offsets so they can be moved */
+            assert(src == first_inner_br + 2);
+            for (regnode *n = src; n < src_end; n = REGNODE_AFTER(n)) {
+                if (NEXT_OFF(n) && n + NEXT_OFF(n) >= src_end) {
+                    NEXT_OFF_set(n, NEXT_OFF(n) + 2);
+                }
+            }

-        regnode *detritus = outer_br + NODE_STEP_REGNODE;
-        OP(detritus) = OPTIMIZED;
-        NEXT_OFF_set(detritus, 0);
+            /* Now move them to overwrite the first inner branch regnore */
+            Move(src, first_inner_br, src_end - src, regnode);
+
+            /* Deal with the now-trailing slack */
+            OP(unused) = OPTIMIZED;
+            NEXT_OFF_set(unused, 0);
+            OP(unused + 1) = OPTIMIZED;
+            NEXT_OFF_set(unused + 1, 0);
+
+            /* Fixup the remaining offsets */
+            if (first_inner_br == last_inner_br) {
+                last_inner_br = outer_br;
+            } else {
+                NEXT_OFF_set(outer_br, src_end - outer_br);
+            }
+            first_inner_br = outer_br;
+        }

         /* Fix up the inner branch. */
         NEXT_OFF_set(last_inner_br, outer_successor - last_inner_br);
diff --git a/t/re/opt.t b/t/re/opt.t
index f184e15b91..0b035c33ab 100644
--- a/t/re/opt.t
+++ b/t/re/opt.t
@@ -281,9 +281,9 @@ abc(*ACCEPT)xyz	3	0+abc	-	-
 (?:foo|bar|(?:baz|bop|bing)|zoop)	3	-	-	stclass=AHOCORASICK-EXACT	flattened multiple branches
 (?:(?:frog|fan)|fog)|(?:farce|(?:forge|flambe))	3	0+f	-	-	flattened multiple, deeper branches

-(?:(?:cat|dog|fish)|bird)x	4	-	3:4+x	-	flattened trie, trailing literal floats
-(?:(?:(?:a|b)|(?:c|d))|(?:(?:e|f)|(?:g|h)))z	2	1+z	-	-	single-char trie, trailing literal anchored
-(?:(?:(?:aa|ab|ac)|(?:ba|bb|bc))|(?:(?:ca|cb|cc)|(?:da|db|dc)))e	3	2+e	-	-	two-char trie, trailing literal anchored
+(?:(?:cat|dog|fish)|bird)x	4	-	3:4+x	stclass=AHOCORASICK-EXACT	flattened trie, trailing literal floats
+(?:(?:(?:a|b)|(?:c|d))|(?:(?:e|f)|(?:g|h)))z	2	1+z	-	stclass=AHOCORASICK-EXACT	single-char trie, trailing literal anchored
+(?:(?:(?:aa|ab|ac)|(?:ba|bb|bc))|(?:(?:ca|cb|cc)|(?:da|db|dc)))e	3	2+e	-	stclass=AHOCORASICK-EXACT	two-char trie, trailing literal anchored

 # Nested branches that should not be flattened
 (?:(?:(?:|x)|(?:|y))|(?:(?:|z)|w))q	1	-	0:1+q	-	empty alternatives; prefix is zero-width, literal floats