Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perl_ck_refassign: support refassigning into a state variable #21351

Merged
merged 1 commit into from
Aug 25, 2023
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
6 changes: 5 additions & 1 deletion op.c
Original file line number Diff line number Diff line change
Expand Up @@ -13251,7 +13251,8 @@ Perl_ck_refassign(pTHX_ OP *o)
settarg:
o->op_private |= (varop->op_private & (OPpLVAL_INTRO|OPpPAD_STATE));
o->op_targ = varop->op_targ;
varop->op_targ = 0;
if (!(o->op_private & (OPpPAD_STATE|OPpLVAL_INTRO)))
varop->op_targ = 0;
PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
break;

Expand Down Expand Up @@ -13325,6 +13326,9 @@ Perl_ck_refassign(pTHX_ OP *o)
o->op_flags &=~ OPf_STACKED;
op_sibling_splice(o, right, 1, NULL);
}
if (o->op_private & OPpPAD_STATE && o->op_private & OPpLVAL_INTRO) {
o = S_newONCEOP(aTHX_ o, varop);
}
op_free(left);
return o;
}
Expand Down
8 changes: 7 additions & 1 deletion t/op/lvref.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ BEGIN {
set_up_inc("../lib");
}

plan 167;
plan 170;

eval '\$x = \$y';
like $@, qr/^Experimental aliasing via reference not enabled/,
Expand Down Expand Up @@ -76,11 +76,13 @@ for (1,2) {
\my($y) = \3,
\state $a = \3,
\state($b) = \3 if $_ == 1;
\state $c = \$_;
if ($_ == 2) {
is $x, undef, '\my $x = ... clears $x on scope exit';
is $y, undef, '\my($x) = ... clears $x on scope exit';
is $a, 3, '\state $x = ... does not clear $x on scope exit';
is $b, 3, '\state($x) = ... does not clear $x on scope exit';
is $c, 1, '\state $x = ... can be used with refaliasing';
}
}

Expand Down Expand Up @@ -210,11 +212,13 @@ for (1,2) {
\my(@y) = \3,
\state @a = [1..3],
\state(@b) = \3 if $_ == 1;
\state @c = [$_];
if ($_ == 2) {
is @x, 0, '\my @x = ... clears @x on scope exit';
is @y, 0, '\my(@x) = ... clears @x on scope exit';
is "@a", "1 2 3", '\state @x = ... does not clear @x on scope exit';
is "@b", 3, '\state(@x) = ... does not clear @x on scope exit';
is $c[0], 1, '\state @x = ... can be used with refaliasing';
}
}

Expand Down Expand Up @@ -254,9 +258,11 @@ package HashTest {
for (1,2) {
\state %y = {1,2},
\my %x = {1,2} if $_ == 1;
\state %c = {X => $_};
if ($_ == 2) {
is %x, 0, '\my %x = ... clears %x on scope exit';
is "@{[%y]}", "1 2", '\state %x = ... does not clear %x on scope exit';
is $c{X}, 1, '\state %x = ... can be used with refaliasing';
}
}

Expand Down