Commit b5708db896 for perl
commit b5708db89666450b6278b00ce78480c77367468f
Author: Paul Evans <leonerd@leonerd.org.uk>
Date: Mon Nov 16 23:49:55 2026 +0000
Allow `foreach` + multivar refalias to operate more than once
The previous implementation of this code relied on looking up the
SvTYPE() of the actual pad variables, to know whether to expect ARRAY,
HASH, CODE or other scalar references. This worked the first time the
loop was entered after being compiled, but would not work on subsequent
entries after the pad had been cleared. This was because the pad gets
cleared by totally wiping those SVs, thus they would not have an SvTYPE
that is meaningful next time round.
A less fragile way to detect this is to inspect the first character of
the padname associated with each variable. This new behaviour avoids
making any changes to the underlying optree or scope stack structures,
so it's a valid backporting candidate for later releases of the 5.44.x
series.
diff --git a/pp_hot.c b/pp_hot.c
index 6cd7cde166..b52fd2f179 100644
--- a/pp_hot.c
+++ b/pp_hot.c
@@ -4983,6 +4983,7 @@ PP(pp_iter)
behaviour */
PADOFFSET how_many = PL_op->op_targ;
U32 refalias_mask = 0;
+ PADNAME **itervar_pnp = NULL;
if (pflag_refalias) {
refalias_mask = how_many >> 8;
how_many &= 0xFF;
@@ -4992,6 +4993,18 @@ PP(pp_iter)
* refaliased
*/
refalias_mask = 1;
+
+ /* TODO(leonerd): This code structure isn't very fast or efficient
+ * at runtime, but it's the best we can do without altering the
+ * data structure of any of the ops involved.
+ * For perl 5.46 we can try to do better and store the refalias
+ * expected types directly in this op so we don't have to look up
+ * sigils out of the pad.
+ */
+ PADOFFSET itervar_idx = itersvp - PL_curpad;
+ PADNAMELIST *pnl = PadlistNAMES(CvPADLIST(find_runcv(0)));
+ assert(itervar_idx > 0 && itervar_idx <= PadnamelistMAX(pnl));
+ itervar_pnp = PadnamelistARRAY(pnl) + itervar_idx;
}
PADOFFSET i = 0;
@@ -5160,6 +5173,8 @@ PP(pp_iter)
++i;
++itersvp;
+ if(itervar_pnp)
+ ++itervar_pnp;
refalias_this = refalias_mask & (1<<i);
}
@@ -5195,6 +5210,8 @@ PP(pp_iter)
++i;
++itersvp;
+ if(itervar_pnp)
+ ++itervar_pnp;
refalias_this = refalias_mask & (1<<i);
}
@@ -5210,15 +5227,14 @@ PP(pp_iter)
SV *rv = SvRV(sv);
const char *bad = NULL;
- /* TODO(leonerd): is it sufficient to use the existing var type? */
- switch (SvTYPE(*itersvp)) {
- case SVt_PVAV:
+ switch (PadnamePV(*itervar_pnp)[0]) {
+ case '@':
if(SvTYPE(rv) != SVt_PVAV) bad = "an ARRAY";
break;
- case SVt_PVHV:
+ case '%':
if(SvTYPE(rv) != SVt_PVHV) bad = "a HASH";
break;
- case SVt_PVCV:
+ case '&':
if(SvTYPE(rv) != SVt_PVCV) bad = "a CODE";
break;
default:
@@ -5270,6 +5286,8 @@ PP(pp_iter)
/* Only relevant for a many-at-a-time loop: */
++itersvp;
+ if(UNLIKELY(itervar_pnp))
+ ++itervar_pnp;
}
/* Try to bypass pushing &PL_sv_yes and calling pp_and(); instead
diff --git a/t/op/for-many.t b/t/op/for-many.t
index e390809a6a..8af2c7c80a 100644
--- a/t/op/for-many.t
+++ b/t/op/for-many.t
@@ -493,6 +493,14 @@ is($continue, 'xx', 'continue reached twice');
push @ret, "$k=" . join("|", %hash );
}
is(join(",", @ret), "one=1|1,two=2|2,three=3|3", 'iterator vars can refalias arrays');
+
+ # GH25051 - refalias iterator variables can be reüsed
+ foreach my $pass ( 1, 2 ) {
+ foreach my ( $idx, \@array ) ( 0 => [], 1 => [], 2 => [] ) {
+ # empty
+ }
+ pass("Reused refalias iterator variable pass $pass");
+ }
}
# Spaces shouldn't trigger parsing errors: