Change 31833 by [EMAIL PROTECTED] on 2007/09/09 19:51:50 state variables shouldn't be shared between anon subs
Affected files ... ... //depot/perl/op.c#954 edit ... //depot/perl/t/op/state.t#17 edit Differences ... ==== //depot/perl/op.c#954 (text) ==== Index: perl/op.c --- perl/op.c#953~31824~ 2007-09-08 15:34:29.000000000 -0700 +++ perl/op.c 2007-09-09 12:51:50.000000000 -0700 @@ -395,6 +395,12 @@ 0, /* not fake */ PL_parser->in_my == KEY_state ); + /* anon sub prototypes contains state vars should always be cloned, + * otherwise the state var would be shared between anon subs */ + + if (PL_parser->in_my == KEY_state && CvANON(PL_compcv)) + CvCLONE_on(PL_compcv); + return off; } ==== //depot/perl/t/op/state.t#17 (text) ==== Index: perl/t/op/state.t --- perl/t/op/state.t#16~31824~ 2007-09-08 15:34:29.000000000 -0700 +++ perl/t/op/state.t 2007-09-09 12:51:50.000000000 -0700 @@ -10,7 +10,7 @@ use strict; use feature ":5.10"; -plan tests => 117; +plan tests => 119; ok( ! defined state $uninit, q(state vars are undef by default) ); @@ -321,6 +321,18 @@ is $x, "two", "masked" } +# normally closureless anon subs share a CV and pad. If the anon sub has a +# state var, this would mean that it is shared. Check that this doesn't +# happen + +{ + my @f; + push @f, sub { state $x; ++$x } for 1..2; + $f[0]->() for 1..10; + is $f[0]->(), 11; + is $f[1]->(), 1; +} + foreach my $forbidden (<DATA>) { chomp $forbidden; no strict 'vars'; End of Patch.