This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Get t/uni/cache.t working under minitest
[perl5.git] / t / op / goto.t
old mode 100755 (executable)
new mode 100644 (file)
index 7f502bd..13e6b04
@@ -10,27 +10,37 @@ BEGIN {
 
 use warnings;
 use strict;
-plan tests => 56;
+plan tests => 94;
+our $TODO;
+
+my $deprecated = 0;
+local $SIG{__WARN__} = sub { if ($_[0] =~ m/jump into a construct/) { $deprecated++; } else { warn $_[0] } };
 
 our $foo;
 while ($?) {
     $foo = 1;
   label1:
+    is($deprecated, 1, "following label1");
+    $deprecated = 0;
     $foo = 2;
     goto label2;
 } continue {
     $foo = 0;
     goto label4;
   label3:
+    is($deprecated, 1, "following label3");
+    $deprecated = 0;
     $foo = 4;
     goto label4;
 }
+is($deprecated, 0, "after 'while' loop");
 goto label1;
 
 $foo = 3;
 
 label2:
 is($foo, 2, 'escape while loop');
+is($deprecated, 0, "following label2");
 goto label3;
 
 label4:
@@ -59,7 +69,7 @@ sub bar {
 exit;
 
 FINALE:
-is(curr_test(), 16, 'FINALE');
+is(curr_test(), 20, 'FINALE');
 
 # does goto LABEL handle block contexts correctly?
 # note that this scope-hopping differs from last & next,
@@ -173,13 +183,18 @@ ok($ok, 'works correctly in a nested eval string');
        A: { if ($false) { redo A; B: $ok = 1; redo A; } }
        goto B unless $count++;
     }
+    is($deprecated, 0, "before calling sub a()");
     a();
     ok($ok, '#19061 loop label wiped away by goto');
+    is($deprecated, 1, "after calling sub a()");
+    $deprecated = 0;
 
     $ok = 0;
     my $p;
     for ($p=1;$p && goto A;$p=0) { A: $ok = 1 }
     ok($ok, 'weird case of goto and for(;;) loop');
+    is($deprecated, 1, "following goto and for(;;) loop");
+    $deprecated = 0;
 }
 
 # bug #9990 - don't prematurely free the CV we're &going to.
@@ -190,6 +205,17 @@ sub f1 {
 }
 f1();
 
+# bug #99850, which is similar - freeing the subroutine we are about to
+# go(in)to during a FREETMPS call should not crash perl.
+
+package _99850 {
+    sub reftype{}
+    DESTROY { undef &reftype }
+    eval { sub { my $guard = bless []; goto &reftype }->() };
+}
+like $@, qr/^Goto undefined subroutine &_99850::reftype at /,
+   'goto &foo undefining &foo on sub cleanup';
+
 # bug #22181 - this used to coredump or make $x undefined, due to
 # erroneous popping of the inner BLOCK context
 
@@ -204,7 +230,7 @@ is($ok, 1, 'goto in for(;;) with continuation');
 
 # bug #22299 - goto in require doesn't find label
 
-open my $f, ">goto01.pm" or die;
+open my $f, ">Op_goto01.pm" or die;
 print $f <<'EOT';
 package goto01;
 goto YYY;
@@ -214,9 +240,9 @@ YYY: print "OK\n";
 EOT
 close $f;
 
-$r = runperl(prog => 'use goto01; print qq[DONE\n]');
+$r = runperl(prog => 'use Op_goto01; print qq[DONE\n]');
 is($r, "OK\nDONE\n", "goto within use-d file"); 
-unlink "goto01.pm";
+unlink_all "Op_goto01.pm";
 
 # test for [perl #24108]
 $ok = 1;
@@ -249,7 +275,7 @@ exit;
 
 bypass:
 
-is(curr_test(), 5, 'eval "goto $x"');
+is(curr_test(), 9, 'eval "goto $x"');
 
 # Test autoloading mechanism.
 
@@ -400,7 +426,11 @@ sub recurse2 {
     my $x = shift;
     $_[0] ? +1 + recurse1($_[0] - 1) : 0
 }
+my $w = 0;
+$SIG{__WARN__} = sub { ++$w };
 is(recurse1(500), 500, 'recursive goto &foo');
+is $w, 0, 'no recursion warnings for "no warnings; goto &sub"';
+delete $SIG{__WARN__};
 
 # [perl #32039] Chained goto &sub drops data too early. 
 
@@ -416,23 +446,259 @@ a32039();
     my $r = runperl(
                stderr => 1,
                prog =>
-'for ($_=0;$_<3;$_++){A: if($_==1){next} if($_==2){$_++;goto A}}print qq(ok)'
+'for ($_=0;$_<3;$_++){A: if($_==1){next} if($_==2){$_++;goto A}}print qq(ok\n)'
     );
-    is($r, "ok", 'next and goto');
+    is($r, "ok\n", 'next and goto');
 
     $r = runperl(
                stderr => 1,
                prog =>
-'for ($_=0;$_<3;$_++){A: if($_==1){$_++;redo} if($_==2){$_++;goto A}}print qq(ok)'
+'for ($_=0;$_<3;$_++){A: if($_==1){$_++;redo} if($_==2){$_++;goto A}}print qq(ok\n)'
     );
-    is($r, "ok", 'redo and goto');
+    is($r, "ok\n", 'redo and goto');
 }
 
 # goto &foo not allowed in evals
 
-
 sub null { 1 };
 eval 'goto &null';
 like($@, qr/Can't goto subroutine from an eval-string/, 'eval string');
 eval { goto &null };
 like($@, qr/Can't goto subroutine from an eval-block/, 'eval block');
+# goto &foo leaves @_ alone when called from a sub
+sub returnarg { $_[0] };
+is sub {
+    local *_ = ["ick and queasy"];
+    goto &returnarg;
+}->("quick and easy"), "ick and queasy",
+  'goto &foo with *_{ARRAY} replaced';
+my @__ = "\xc4\x80";
+sub { local *_ = \@__; goto &utf8::decode }->("no thinking aloud");
+is "@__", chr 256, 'goto &xsub with replaced *_{ARRAY}';
+
+# And goto &foo should leave reified @_ alone
+sub { *__ = \@_;  goto &null } -> ("rough and tubbery");
+is ${*__}[0], 'rough and tubbery', 'goto &foo leaves reified @_ alone';
+
+# goto &xsub when @_ has nonexistent elements
+{
+    no warnings "uninitialized";
+    local @_ = ();
+    $#_++;
+    & {sub { goto &utf8::encode }};
+    is @_, 1, 'num of elems in @_ after goto &xsub with nonexistent $_[0]';
+    is $_[0], "", 'content of nonexistent $_[0] is modified by goto &xsub';
+}
+
+# goto &xsub when @_ itself does not exist
+undef *_;
+eval { & { sub { goto &utf8::encode } } };
+# The main thing we are testing is that it did not crash.  But make sure 
+# *_{ARRAY} was untouched, too.
+is *_{ARRAY}, undef, 'goto &xsub when @_ does not exist';
+
+# goto &perlsub when @_ itself does not exist [perl #119949]
+# This was only crashing when the replaced sub call had an argument list.
+# (I.e., &{ sub { goto ... } } did not crash.)
+sub {
+    undef *_;
+    goto sub {
+       is *_{ARRAY}, undef, 'goto &perlsub when @_ does not exist';
+    }
+}->();
+sub {
+    local *_;
+    goto sub {
+       is *_{ARRAY}, undef, 'goto &sub when @_ does not exist (local *_)';
+    }
+}->();
+
+
+# [perl #36521] goto &foo in warn handler could defeat recursion avoider
+
+{
+    my $r = runperl(
+               stderr => 1,
+               prog => 'my $d; my $w = sub { return if $d++; warn q(bar)}; local $SIG{__WARN__} = sub { goto &$w; }; warn q(foo);'
+    );
+    like($r, qr/bar/, "goto &foo in warn");
+}
+
+TODO: {
+    local $TODO = "[perl #43403] goto() from an if to an else doesn't undo local () changes";
+    our $global = "unmodified";
+    if ($global) { # true but not constant-folded
+         local $global = "modified";
+         goto ELSE;
+    } else {
+         ELSE: is($global, "unmodified");
+    }
+}
+
+is($deprecated, 0, "following TODOed test for #43403");
+
+#74290
+{
+    my $x;
+    my $y;
+    F1:++$x and eval 'return if ++$y == 10; goto F1;';
+    is($x, 10,
+       'labels outside evals can be distinguished from the start of the eval');
+}
+
+goto wham_eth;
+die "You can't get here";
+
+wham_eth: 1 if 0;
+ouch_eth: pass('labels persist even if their statement is optimised away');
+
+$foo = "(0)";
+if($foo eq $foo) {
+    goto bungo;
+}
+$foo .= "(9)";
+bungo:
+format CHOLET =
+wellington
+.
+$foo .= "(1)";
+SKIP: {
+    skip_if_miniperl("no dynamic loading on miniperl, so can't load PerlIO::scalar", 1);
+    my $cholet;
+    open(CHOLET, ">", \$cholet);
+    write CHOLET;
+    close CHOLET;
+    $foo .= "(".$cholet.")";
+    is($foo, "(0)(1)(wellington\n)", "label before format decl");
+}
+
+$foo = "(A)";
+if($foo eq $foo) {
+    goto orinoco;
+}
+$foo .= "(X)";
+orinoco:
+sub alderney { return "tobermory"; }
+$foo .= "(B)";
+$foo .= "(".alderney().")";
+is($foo, "(A)(B)(tobermory)", "label before sub decl");
+
+$foo = "[0:".__PACKAGE__."]";
+if($foo eq $foo) {
+    goto bulgaria;
+}
+$foo .= "[9]";
+bulgaria:
+package Tomsk;
+$foo .= "[1:".__PACKAGE__."]";
+$foo .= "[2:".__PACKAGE__."]";
+package main;
+$foo .= "[3:".__PACKAGE__."]";
+is($foo, "[0:main][1:Tomsk][2:Tomsk][3:main]", "label before package decl");
+
+$foo = "[A:".__PACKAGE__."]";
+if($foo eq $foo) {
+    goto adelaide;
+}
+$foo .= "[Z]";
+adelaide:
+package Cairngorm {
+    $foo .= "[B:".__PACKAGE__."]";
+}
+$foo .= "[C:".__PACKAGE__."]";
+is($foo, "[A:main][B:Cairngorm][C:main]", "label before package block");
+
+our $obidos;
+$foo = "{0}";
+if($foo eq $foo) {
+    goto shansi;
+}
+$foo .= "{9}";
+shansi:
+BEGIN { $obidos = "x"; }
+$foo .= "{1$obidos}";
+is($foo, "{0}{1x}", "label before BEGIN block");
+
+$foo = "{A:".(1.5+1.5)."}";
+if($foo eq $foo) {
+    goto stepney;
+}
+$foo .= "{Z}";
+stepney:
+use integer;
+$foo .= "{B:".(1.5+1.5)."}";
+is($foo, "{A:3}{B:2}", "label before use decl");
+
+$foo = "<0>";
+if($foo eq $foo) {
+    goto tom;
+}
+$foo .= "<9>";
+tom: dick: harry:
+$foo .= "<1>";
+$foo .= "<2>";
+is($foo, "<0><1><2>", "first of three stacked labels");
+
+$foo = "<A>";
+if($foo eq $foo) {
+    goto beta;
+}
+$foo .= "<Z>";
+alpha: beta: gamma:
+$foo .= "<B>";
+$foo .= "<C>";
+is($foo, "<A><B><C>", "second of three stacked labels");
+
+$foo = ",0.";
+if($foo eq $foo) {
+    goto gimel;
+}
+$foo .= ",9.";
+alef: bet: gimel:
+$foo .= ",1.";
+$foo .= ",2.";
+is($foo, ",0.,1.,2.", "third of three stacked labels");
+
+# [perl #112316] Wrong behavior regarding labels with same prefix
+sub same_prefix_labels {
+    my $pass;
+    my $first_time = 1;
+    CATCH: {
+        if ( $first_time ) {
+            CATCHLOOP: {
+                if ( !$first_time ) {
+                  return 0;
+                }
+                $first_time--;
+                goto CATCH;
+            }
+        }
+        else {
+            return 1;
+        }
+    }
+}
+
+ok(
+   same_prefix_labels(),
+   "perl 112316: goto and labels with the same prefix doesn't get mixed up"
+);
+
+eval { my $x = ""; goto $x };
+like $@, qr/^goto must have label at /, 'goto $x where $x is empty string';
+eval { goto "" };
+like $@, qr/^goto must have label at /, 'goto ""';
+eval { goto };
+like $@, qr/^goto must have label at /, 'argless goto';
+
+eval { my $x = "\0"; goto $x };
+like $@, qr/^Can't find label \0 at /, 'goto $x where $x begins with \0';
+eval { goto "\0" };
+like $@, qr/^Can't find label \0 at /, 'goto "\0"';
+
+sub TIESCALAR { bless [pop] }
+sub FETCH     { $_[0][0] }
+tie my $t, "", sub { "cluck up porridge" };
+is eval { sub { goto $t }->() }//$@, 'cluck up porridge',
+  'tied arg returning sub ref';