This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regcomp.c: Rename a variable
[perl5.git] / t / op / state.t
index 7be1666..7aef435 100644 (file)
@@ -3,14 +3,20 @@
 
 BEGIN {
     chdir 't' if -d 't';
-    @INC = '../lib';
     require './test.pl';
+    set_up_inc('../lib');
 }
 
 use strict;
+
+plan tests => 164;
+
+# Before loading feature.pm, test it with CORE::
+ok eval 'CORE::state $x = 1;', 'CORE::state outside of feature.pm scope';
+
+
 use feature ":5.10";
 
-plan tests => 108;
 
 ok( ! defined state $uninit, q(state vars are undef by default) );
 
@@ -129,6 +135,16 @@ is( $xsize, 0, 'uninitialized state array' );
 $xsize = stateful_array();
 is( $xsize, 1, 'uninitialized state array after one iteration' );
 
+sub stateful_init_array {
+    state @x = qw(a b c);
+    push @x, "x";
+    return join(",", @x);
+}
+
+is stateful_init_array(), "a,b,c,x";
+is stateful_init_array(), "a,b,c,x,x";
+is stateful_init_array(), "a,b,c,x,x,x";
+
 # hash state vars
 
 sub stateful_hash {
@@ -142,6 +158,46 @@ is( $xhval, 0, 'uninitialized state hash' );
 $xhval = stateful_hash();
 is( $xhval, 1, 'uninitialized state hash after one iteration' );
 
+sub stateful_init_hash {
+    state %x = qw(a b c d);
+    $x{foo}++;
+    return join(",", map { ($_, $x{$_}) } sort keys %x);
+}
+
+is stateful_init_hash(), "a,b,c,d,foo,1";
+is stateful_init_hash(), "a,b,c,d,foo,2";
+is stateful_init_hash(), "a,b,c,d,foo,3";
+
+# declarations with attributes
+
+SKIP: {
+skip "no attributes in miniperl", 3, if is_miniperl;
+
+eval q{
+sub stateful_attr {
+    state $a :shared;
+    state $b :shared = 3;
+    state @c :shared;
+    state @d :shared = qw(a b c);
+    state %e :shared;
+    state %f :shared = qw(a b c d);
+    $a++;
+    $b++;
+    push @c, "x";
+    push @d, "x";
+    $e{e}++;
+    $f{e}++;
+    return join(",", $a, $b, join(":", @c), join(":", @d), join(":", %e),
+           join(":", map { ($_, $f{$_}) } sort keys %f));
+}
+};
+
+is stateful_attr(), "1,4,x,a:b:c:x,e:1,a:b:c:d:e:1";
+is stateful_attr(), "2,5,x:x,a:b:c:x:x,e:2,a:b:c:d:e:2";
+is stateful_attr(), "3,6,x:x:x,a:b:c:x:x:x,e:3,a:b:c:d:e:3";
+}
+
+
 # Recursion
 
 sub noseworth {
@@ -198,21 +254,6 @@ $y = 0;
 
 
 #
-# Check state $_
-#
-my @stones = qw [fred wilma barny betty];
-my $first  = $stones [0];
-my $First  = ucfirst $first;
-$_ = "bambam";
-foreach my $flint (@stones) {
-    state $_ = $flint;
-    is $_, $first, 'state $_';
-    ok /$first/, '/.../ binds to $_';
-    is ucfirst, $First, '$_ default argument';
-}
-is $_, "bambam", '$_ is still there';
-
-#
 # Goto.
 #
 my @simpsons = qw [Homer Marge Bart Lisa Maggie];
@@ -222,9 +263,9 @@ again:
     is $simpson, 'Homer', 'goto 1';
     goto again if @simpsons;
 
-goto Elvis;
 my $vi;
 {
+    goto Elvis unless $vi;
            state $calvin = ++ $vi;
     Elvis: state $vile   = ++ $vi;
     redo unless defined $calvin;
@@ -301,21 +342,11 @@ foreach my $x (0 .. 4) {
 
 
 #
-# List context reassigns, but scalar doesn't.
-#
-my @swords = qw [Stormbringer Szczerbiec Grimtooth Corrougue];
-foreach my $sword (@swords) {
-    state ($s1) = state $s2 = $sword;
-    is $s1, $swords [0], 'mixed context';
-    is $s2, $swords [0], 'mixed context';
-}
-
-
-#
 # Use with given.
 #
 my @spam = qw [spam ham bacon beans];
 foreach my $spam (@spam) {
+    no warnings 'experimental::smartmatch';
     given (state $spam = $spam) {
         when ($spam [0]) {ok 1, "given"}
         default          {ok 0, "given"}
@@ -331,3 +362,191 @@ foreach my $spam (@spam) {
     state $x = "two";
     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;
+}
+
+# each copy of an anon sub should get its own 'once block'
+
+{
+    my $x; # used to force a closure
+    my @f;
+    push @f, sub { $x=0; state $s = $_[0]; $s } for 1..2;
+    is $f[0]->(1), 1;
+    is $f[0]->(2), 1;
+    is $f[1]->(3), 3;
+    is $f[1]->(4), 3;
+}
+
+
+
+
+foreach my $forbidden (<DATA>) {
+    SKIP: {
+        skip_if_miniperl("miniperl can't load attributes.pm", 1)
+                if $forbidden =~ /:shared/;
+
+        chomp $forbidden;
+        no strict 'vars';
+        eval $forbidden;
+        like $@,
+            qr/Initialization of state variables in list currently forbidden/,
+            "Currently forbidden: $forbidden";
+    }
+}
+
+# [perl #49522] state variable not available
+
+{
+    my @warnings;
+    local $SIG{__WARN__} = sub { push @warnings, $_[0] };
+
+    eval q{
+       use warnings;
+
+       sub f_49522 {
+           state $s = 88;
+           sub g_49522 { $s }
+           sub { $s };
+       }
+
+       sub h_49522 {
+           state $t = 99;
+           sub i_49522 {
+               sub { $t };
+           }
+       }
+    };
+    is $@, '', "eval f_49522";
+    # shouldn't be any 'not available' or 'not stay shared' warnings
+    ok !@warnings, "suppress warnings part 1 [@warnings]";
+
+    @warnings = ();
+    my $f = f_49522();
+    is $f->(), 88, "state var closure 1";
+    is g_49522(), 88, "state var closure 2";
+    ok !@warnings, "suppress warnings part 2 [@warnings]";
+
+
+    @warnings = ();
+    $f = i_49522();
+    h_49522(); # initialise $t
+    is $f->(), 99, "state var closure 3";
+    ok !@warnings, "suppress warnings part 3 [@warnings]";
+
+
+}
+
+
+# [perl #117095] state var initialisation getting skipped
+# the 'if 0' code below causes a call to op_free at compile-time,
+# which used to inadvertently mark the state var as initialised.
+
+{
+    state $f = 1;
+    foo($f) if 0; # this calls op_free on padmy($f)
+    ok(defined $f, 'state init not skipped');
+}
+
+# [perl #121134] Make sure padrange doesn't mess with these
+{
+    sub thing {
+       my $expect = shift;
+        my ($x, $y);
+        state $z;
+
+        is($z, $expect, "State variable is correct");
+
+        $z = 5;
+    }
+
+    thing(undef);
+    thing(5);
+
+    sub thing2 {
+        my $expect = shift;
+        my $x;
+        my $y;
+        state $z;
+
+        is($z, $expect, "State variable is correct");
+
+        $z = 6;
+    }
+
+    thing2(undef);
+    thing2(6);
+}
+
+# [perl #123029] regression in "state" under PERL_NO_COW
+sub rt_123029 {
+    state $s;
+    $s = 'foo'x500;
+    my $c = $s;
+    return defined $s;
+}
+ok(rt_123029(), "state variables don't surprisingly disappear when accessed");
+
+# make sure multiconcat doesn't break state
+
+for (1,2) {
+    state $s = "-$_-";
+    is($s, "-1-", "state with multiconcat pass $_");
+}
+
+__DATA__
+(state $a) = 1;
+(state @a) = 1;
+(state @a :shared) = 1;
+(state %a) = ();
+(state %a :shared) = ();
+state ($a) = 1;
+(state ($a)) = 1;
+state (@a) = 1;
+(state (@a)) = 1;
+state (@a) :shared = 1;
+(state (@a) :shared) = 1;
+state (%a) = ();
+(state (%a)) = ();
+state (%a) :shared = ();
+(state (%a) :shared) = ();
+state (undef, $a) = ();
+(state (undef, $a)) = ();
+state (undef, @a) = ();
+(state (undef, @a)) = ();
+state ($a, undef) = ();
+(state ($a, undef)) = ();
+state ($a, $b) = ();
+(state ($a, $b)) = ();
+state ($a, $b) :shared = ();
+(state ($a, $b) :shared) = ();
+state ($a, @b) = ();
+(state ($a, @b)) = ();
+state ($a, @b) :shared = ();
+(state ($a, @b) :shared) = ();
+state (@a, undef) = ();
+(state (@a, undef)) = ();
+state (@a, $b) = ();
+(state (@a, $b)) = ();
+state (@a, $b) :shared = ();
+(state (@a, $b) :shared) = ();
+state (@a, @b) = ();
+(state (@a, @b)) = ();
+state (@a, @b) :shared = ();
+(state (@a, @b) :shared) = ();
+(state $a, state $b) = ();
+(state $a, $b) = ();
+(state $a, my $b) = ();
+(state $a, state @b) = ();
+(state $a, local @b) = ();
+(state $a, undef, state $b) = ();
+state ($a, undef, $b) = ();