Skip to content

Implement foreach on lvref lexical without magic#24202

Open
leonerd wants to merge 2 commits intoPerl:bleadfrom
leonerd:foreach-lvref-lexical-without-magic
Open

Implement foreach on lvref lexical without magic#24202
leonerd wants to merge 2 commits intoPerl:bleadfrom
leonerd:foreach-lvref-lexical-without-magic

Conversation

@leonerd
Copy link
Contributor

@leonerd leonerd commented Feb 16, 2026

Previously, commit 091a08d made a more efficient implemention of foreach loops where the iteration variable was declared using \my $VAR. A cornercase of that logic was missed, wherein the iteration variable was previously declared as a lexical before the foreach loop itself. That resulted in an optree using OP_SREFGEN on OP_LVREF directly, rather than on a OP_PADxV, so a case was missed.

By covering this case here as well, we now manage to remove any uses of PERL_MAGIC_lvref to implement foreach loops on refaliased lexicals. The code to implement those now-dead cases has been removed and replaced with an assert(mg->mg_obj), to cover the still-remaining case of iteration over a refalias to a package variable. Those may be covered in a later commit.

  • This set of changes does not require a perldelta entry.

The previous giant list was too big to debug if a single test case
happened to fail
Previously, commit 091a08d made a more efficient implemention of
`foreach` loops where the iteration variable was declared using
`\my $VAR`. A cornercase of that logic was missed, wherein the iteration
variable was *previously* declared as a lexical before the foreach loop
itself. That resulted in an optree using `OP_SREFGEN` on `OP_LVREF`
directly, rather than on a `OP_PADxV`, so a case was missed.

By covering this case here as well, we now manage to remove any uses of
`PERL_MAGIC_lvref` to implement `foreach` loops on refaliased lexicals.
The code to implement those now-dead cases has been removed and replaced
with an `assert(mg->mg_obj)`, to cover the still-remaining case of
iteration over a refalias to a package variable. Those may be covered in
a later commit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments