13 is(empty_sub,undef,"Is empty");
14 is(empty_sub(1,2,3),undef,"Is still empty");
16 is(scalar(@test), 0, 'Didnt return anything');
17 @test = empty_sub(1,2,3);
18 is(scalar(@test), 0, 'Didnt return anything');
20 # [perl #91844] return should always copy
23 for my $x ($foo{bar}) {
24 # Pity test.pl doesnt have isn't.
25 isnt \sub { delete $foo{bar} }->(), \$x,
26 'result of delete(helem) is copied when returned';
29 for my $x ($foo{bar}) {
30 isnt \sub { return delete $foo{bar} }->(), \$x,
31 'result of delete(helem) is copied when explicitly returned';
34 isnt \sub { delete $_[0] }->($x), \$x,
35 'result of delete(aelem) is copied when returned';
36 isnt \sub { return delete $_[0] }->($x), \$x,
37 'result of delete(aelem) is copied when explicitly returned';
38 isnt \sub { ()=\@_; shift }->($x), \$x,
39 'result of shift is copied when returned';
40 isnt \sub { ()=\@_; return shift }->($x), \$x,
41 'result of shift is copied when explicitly returned';
47 isnt($_[0], $$r, "result of delete(helem) is copied: practical test");
48 }->(sub { delete $foo{bar} }->());
52 <<'end', "main::foo\n", {}, 'sub redefinition sets CvGV';
55 eval 'sub bar { print +(caller 0)[3], "\n" }';
60 <<'end', "main::foo\nok\n", {}, 'no double free redefining anon stub';
65 eval 'sub bar { print +(caller 0)[3], "\n" }';
72 # The outer call sets the scalar returned by ${\""}.${\""} to the current
74 # The inner call sets it to "road".
75 # Each call records the value twice, the outer call surrounding the inner
76 # call. In 5.10-5.18 under ithreads, what gets pushed is
77 # qw(main road road road) because the inner call is clobbering the same
78 # scalar. If __PACKAGE__ is changed to "main", it works, the last element
85 a("road",1) unless $_[1];
91 is "@scratch", "main road road main",
92 'recursive calls do not share shared-hash-key TARGs';
94 # Another test for the same bug, that does not rely on foreach. It depends
95 # on ref returning a shared hash key TARG.
98 my ($pack, $depth) = @_;
99 my $o = bless[], $pack;
101 push @scratch, (ref $o, $depth||b($pack,$depth+1))[0];
104 is "@scratch", "o n",
105 'recursive calls do not share shared-hash-key TARGs (2)';
107 # [perl #78194] @_ aliasing op return values
108 sub { is \$_[0], \$_[0],
109 '[perl #78194] \$_[0] == \$_[0] when @_ aliases "$x"' }
112 # The return statement should make no difference in this case:
113 sub not_constant () { 42 }
114 sub not_constantr() { return 42 }
115 use feature 'lexical_subs'; no warnings 'experimental::lexical_subs';
116 my sub not_constantm () { 42 }
117 my sub not_constantmr() { return 42 }
118 eval { ${\not_constant}++ };
119 is $@, "", 'sub (){42} returns a mutable value';
120 eval { ${\not_constantr}++ };
121 is $@, "", 'sub (){ return 42 } returns a mutable value';
122 eval { ${\not_constantm}++ };
123 is $@, "", 'my sub (){42} returns a mutable value';
124 eval { ${\not_constantmr}++ };
125 is $@, "", 'my sub (){ return 42 } returns a mutable value';
128 sub Munchy { $_[0] = 2 }
129 eval "Crunchy"; # test that freeing this op does not turn off PADTMP
131 } || $@, 2, 'freeing ops does not make sub(){42} immutable';
133 # &xsub when @_ has nonexistent elements
135 no warnings "uninitialized";
139 is @_, 1, 'num of elems in @_ after &xsub with nonexistent $_[0]';
140 is $_[0], "", 'content of nonexistent $_[0] is modified by &xsub';
143 # &xsub when @_ itself does not exist
145 eval { &utf8::encode };
146 # The main thing we are testing is that it did not crash. But make sure
147 # *_{ARRAY} was untouched, too.
148 is *_{ARRAY}, undef, 'goto &xsub when @_ does not exist';
150 # We do not want re.pm loaded at this point. Move this test up or find
151 # another XSUB if this fails.
152 ok !exists $INC{"re.pm"}, 're.pm not loaded yet';
157 no warnings 'redefine', 'prototype';
159 *re::regmust = sub{}; # GvSV had no refcount, so this freed it
160 my $str2 = "$_[0]"; # used to be UNKNOWN(0x7fdda29310e0)
161 @str = ($str1, $str2);
163 local $^W; # Suppress redef warnings in XSLoader
166 'XSUB clobbering sub whose DESTROY assigns to the glob';
169 no warnings 'redefine';
171 bless \&foo, 'newATTRSUBbug';
172 sub newATTRSUBbug::DESTROY {
174 *foo = sub{}; # GvSV had no refcount, so this freed it
175 my $str2 = "$_[0]"; # used to be UNKNOWN(0x7fdda29310e0)
176 @str = ($str1, $str2);
181 'Pure-Perl sub clobbering sub whose DESTROY assigns to the glob';
184 # [perl #122107] previously this would return
185 # Subroutine BEGIN redefined at (eval 2) line 2.
186 fresh_perl_is(<<'EOS', "", { stderr => 1 },
187 use strict; use warnings; eval q/use File::{Spec}/; eval q/use File::Spec/;
189 "check special blocks are cleared on error");
191 use constant { constant1 => 1, constant2 => 2 };
194 local $SIG{__WARN__} = sub { $w++ };
195 eval 'sub constant1; sub constant2($)';
196 is eval '&constant1', '1',
197 'stub re-declaration of constant with no prototype';
198 is eval '&constant2', '2',
199 'stub re-declaration of constant with wrong prototype';
200 is $w, 2, 'two warnings from the above';
205 my $parent; # just to make the sub a closure
208 local $depth = $depth + 1;
209 our $ok++, return if $depth == 2;
211 ()= $parent; # just to make the sub a closure
212 our $whatever; # this causes the crash
218 '[perl #122845] no crash in closure recursion with our-vars';
220 () = *predeclared; # vivify the glob at compile time
221 sub predeclared; # now we have a CV stub with no body (incorporeal? :-)
224 sub inside_predeclared {
225 is eval '$x', 42, 'eval q/$var/ in named sub in predeclared sub';
228 predeclared(); # set $x to 42
229 $main::x = $main::x = "You should not see this.";
230 inside_predeclared(); # run test
232 # RT #126845: this used to fail an assertion in Perl_newATTRSUB_x()
233 eval 'sub rt126845_1 (); sub rt126845_1 () :lvalue';
234 pass("RT #126845: stub with prototype, then with attribute");
236 eval 'sub rt126845_2 (); sub rt126845_2 () :lvalue {}';
237 pass("RT #126845: stub with prototype, then definition with attribute");
239 # RT #124156 death during unwinding causes crash
240 # the tie allows us to trigger another die while cleaning up the stack
241 # from an earlier die.
246 sub TIEHASH { bless({}, $_[0]) }
250 sub DELETE { die "outer\n" }
258 local $a{foo} = "bar";
265 ::is($@, "outer\n", "RT124156 plain");
268 sub DESTROY { $destroyed = 1 }
273 $x = 1; # force closure
276 local $a{foo} = "bar";
279 bless $f, 'RT124156';
284 # as opposed to $@ eq "Can't undef active subroutine"
285 ::is($@, "outer\n", "RT124156 depth");
286 ::is($destroyed, 1, "RT124156 freed cv");
289 # trapping dying while popping a scope needs to have the right pad at all
290 # times. Localising a tied array then dying in STORE raises an exception
291 # while leaving g(). Note that using an object and destructor wouldn't be
292 # sufficient since DESTROY is called with call_sv(...,G_EVAL).
293 # We make sure that the first item in every sub's pad is a lexical with
294 # different values per sub.
297 package tie_exception;
298 sub TIEARRAY { my $x = 4; bless [0] }
299 sub FETCH { my $x = 5; 1 }
300 sub STORE { my $x = 6; die if $_[0][0]; $_[0][0] = 1 }
303 sub f { my $x = 7; eval { g() }; $y = $x }
307 tie @a, "tie_exception";
312 ::is($y, 7, "tie_exception");
316 # check that return pops extraneous stuff from the stack
319 # the extra scopes push contexts and extra SVs on the stack
321 my @a = map $_ + 20, @_;
323 return if defined $_[0] && $_[0] < 0;
326 check_ret(1, do { (2,3,4, return @a ? @a[0..$#a] : ()) }, 4.5);
331 is(scalar check_ret(), undef, "check_ret() scalar");
332 is(scalar check_ret(5), 25, "check_ret(5) scalar");
333 is(scalar check_ret(5,6), 26, "check_ret(5,6) scalar");
334 is(scalar check_ret(5,6,7), 27, "check_ret(5,6,7) scalar");
335 is(scalar check_ret(5,6,7,8), 28, "check_ret(5,6,7,8) scalar");
336 is(scalar check_ret(5,6,7,8,9), 29, "check_ret(5,6,7,8,9) scalar");
338 is(scalar check_ret(-1), undef, "check_ret(-1) scalar");
339 is(scalar check_ret(-1,5), undef, "check_ret(-1,5) scalar");
341 is(join('-', 10, check_ret()), "10", "check_ret() list");
342 is(join('-', 10, check_ret(5)), "10-25", "check_ret(5) list");
343 is(join('-', 10, check_ret(5,6)), "10-25-26", "check_ret(5,6) list");
344 is(join('-', 10, check_ret(5,6,7)), "10-25-26-27", "check_ret(5,6,7) list");
345 is(join('-', 10, check_ret(5,6,7,8)), "10-25-26-27-28", "check_ret(5,6,7,8) list");
346 is(join('-', 10, check_ret(5,6,7,8,9)), "10-25-26-27-28-29", "check_ret(5,6,7,8,9) list");
348 is(join('-', 10, check_ret(-1)), "10", "check_ret(-1) list");
349 is(join('-', 10, check_ret(-1,5)), "10", "check_ret(-1,5) list");
351 # a sub without nested scopes that still leaves rubbish on the stack
352 # which needs popping
356 # conditional leaves rubbish on stack
357 return @_ unless $false and $false;
360 is(join('-', @res), "a-b", "unnested rubbish");
363 # a sub should copy returned PADTMPs
366 sub f99 { $_[0] . "x" };
367 my $a = [ f99(1), f99(2) ];
368 is("@$a", "1x 2x", "PADTMPs copied on return");
371 # A sub should FREETMPS on exit
378 sub f { ::is($d, 1, "RT 124248"); }
379 sub g { !!(my $x = bless []); }
383 # return should have the right PL_curpm while copying its return args
393 is(curpm(), 'c', 'return and PL_curpm');
396 is ref($main::{rt_129916}), 'CODE', 'simple sub stored as CV in stash (main::)';
402 local $::TODO = "disabled for now";
403 is ref($RT129916::{foo}), 'CODE', 'simple sub stored as CV in stash (non-main::)';
406 # Calling xsub via ampersand syntax when @_ has holes
408 skip "no XS::APItest on miniperl" if is_miniperl;
409 skip "XS::APItest not available", 1 if ! eval { require XS::APItest };
412 &XS::APItest::unshift_and_set_defav;
416 # [perl #129090] Crashes and hangs
419 eval '$a=qq|a$a|;my sub b;%c;sub c{sub b;sub c}';
423 {my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$t,$u);}
424 {my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$t,$u);}
425 {my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$t,$u);}
426 CORE::state sub b; sub d { sub b {} sub d }
428 eval '()=%e; sub e { sub e; eval q|$x| } e;';
431 q#<s,,$0[sub{m]]]],}>0,shift#,
434 "GH Issue #16944 - Syntax error with sub and shift causes segfault"