3 # Some miscellaneous checks for the list assignment operator, OP_AASSIGN.
5 # This file was only added in 2015; before then, such tests were
6 # typically in various other random places like op/array.t. This test file
7 # doesn't therefore attempt to be comprehensive; it merely provides a
8 # central place to new put additional tests, especially those related to
9 # the trickiness of commonality, e.g. ($a,$b) = ($b,$a).
11 # In particular, it's testing the flags
12 # OPpASSIGN_COMMON_SCALAR
13 # OPpASSIGN_COMMON_RC1
14 # OPpASSIGN_COMMON_AGG
25 # general purpose package vars
31 sub f_ret_14 { return 1..4 }
33 # stringify a hash ref
37 join ',', map "$_:$rh->{$_}", sort keys %$rh;
41 # where the RHS has surplus elements
46 is("$a:$b", "1:2", "surplus");
54 is("$a[0]:$a[1]", "2:1", "lex array slice");
61 for $pkg_scalar ($a) {
62 ($pkg_scalar, $b) = (3, $a);
63 is($pkg_scalar, 3, "package alias pkg");
64 is("$a:$b", "3:1", "package alias a:b");
68 # my array/hash populated via closure
73 sub f1 { $x = 1; @a = 2..4; \@a }
74 is($x, 2, "my: array closure x");
75 is("@a", "3 4", "my: array closure a");
78 my ($k, $v, %h) = (d => 4, %$rh, e => 6);
79 sub f2 { $k = 'a'; $v = 1; %h = qw(b 2 c 3); \%h }
80 is("$k:$v", "d:4", "my: hash closure k:v");
81 is(sh(\%h), "b:2,c:3,e:6", "my: hash closure h");
85 # various shared element scenarios within a my (...)
88 my ($x,$y) = f3(); # $x and $y on both sides
89 sub f3 : lvalue { ($x,$y) = (1,2); $y, $x }
90 is ("$x:$y", "2:1", "my: scalar and lvalue sub");
95 my @a = @$ra; # elements of @a on both sides
96 sub f4 { @a = 1..4; \@a }
97 is("@a", "1 2 3 4", "my: array and elements");
102 my %h = %$rh; # elements of %h on both sides
103 sub f5 { %h = qw(a 1 b 2 c 3); \%h }
104 is(sh(\%h), "a:1,b:2,c:3", "my: hash and elements");
110 my ($x, $y) = (2, $xalias6);
111 sub f6 { $x = 1; *xalias6 = \$x; }
112 is ("$x:$y", "2:1", "my: pkg var aliased to lexical");
120 is ("$x:$y", "2:1", "my: lex array elements aliased");
124 use feature 'refaliasing';
125 no warnings 'experimental';
126 \($a[0], $a[1]) = \($y,$x);
133 my ($x,$y) = @pkg_array;
134 is ("$x:$y", "2:1", "my: pkg array elements aliased");
138 use feature 'refaliasing';
139 no warnings 'experimental';
140 \($pkg_array[0], $pkg_array[1]) = \($y,$x);
147 is ("$x:$y", "2:1", "my: pkg scalar alias");
158 use feature 'refaliasing';
159 no warnings 'experimental';
163 \(my $lex) = \$pkg10;
164 my @a = ($lex,3); # equivalent to ($a[0],3)
165 is("@a", "1 3", "my: lex alias of array alement");
175 use feature 'refaliasing';
176 no warnings 'experimental';
181 is("@a", "2 1", "my: lex alias of array alements");
195 for $pkg_scalar ($x) {
196 ($pkg_scalar, $y) = (3, $x);
197 is("$pkg_scalar,$y", "3,1", "package scalar aliased");
205 sub f12 : lvalue { @a }
207 is("@a", "1 2 3", "lvalue sub on RHS returns array");
212 sub f13 : lvalue { $x,$y }
214 is("$x:$y", "1:2", "lvalue sub on RHS returns scalars");
218 # package shared scalar vars
223 ($pkg14a,$pkg14b) = ($pkg14b,$pkg14a);
224 is("$pkg14a:$pkg14b", "2:1", "shared package scalars");
227 # lexical shared scalar vars
233 is("$a:$b", "2:1", "shared lexical scalars");
237 # lexical nested array elem swap
243 ($a[0][0],$a[0][1]) = ($a[0][1],$a[0][0]);
244 is("$a[0][0]:$a[0][1]", "2:1", "lexical nested array elem swap");
247 # package nested array elem swap
253 ($a15[0][0],$a15[0][1]) = ($a15[0][1],$a15[0][0]);
254 is("$a15[0][0]:$a15[0][1]", "2:1", "package nested array elem swap");
261 ($a16, undef, $b16) = 1..30;
262 is("$a16:$b16", "1:3", "surplus RHS junk");
265 # my ($scalar,....) = @_
267 # technically this is an unsafe usage commonality-wise, but
268 # a) you have to try really hard to break it, as this test shows;
269 # b) it's such an important usage that for performance reasons we
270 # mark it as safe even though it isn't really. Hence it's a TODO.
274 # debugging builds will detect this failure and panic
275 skip "DEBUGGING build" if $::Config{ccflags} =~ /(?<!\S)-DDEBUGGING(?!\S)/
276 or $^O eq 'VMS' && $::Config{usedebugging_perl} eq 'Y';
277 local $::TODO = 'cheat and optimise my (....) = @_';
281 is("($a)(@b)", "(3)(2 1)", 'my (....) = @_');
284 use feature 'refaliasing';
285 no warnings 'experimental';
287 \($_[2], $_[1], $_[0]) = \($a, $b[0], $b[1]);
291 # single scalar on RHS that's in an aggregate on LHS
297 is ("(@a)", "(1)", 'single scalar on RHS, agg');
301 # TEMP buffer stealing.
304 # the same TEMP RHS element may be used more than once, so when copying
305 # it, we mustn't steal its buffer.
306 # DAPM 10/2016 - but in that case the SvTEMP flag is sometimes getting
307 # cleared: using split() instead as a source of temps seems more reliable,
308 # so I've added splut variants too.
311 # a string long enough for COW and buffer stealing to be enabled
312 my $long = 'def' . ('x' x 2000);
314 # a sub that is intended to return a TEMP string that isn't COW
315 # the concat returns a non-COW PADTMP; pp_leavesub sees a long
316 # stealable string, so creates a TEMP with the stolen buffer from the
317 # PADTMP - hence it returns a non-COW string. It also returns a couple
318 # of key strings for the hash tests
321 ($x . $long, "key1", "key2");
326 # with @a initially empty,the code path creates a new copy of each
327 # RHS element to store in the array
330 is (substr($a[0], 0, 7), "abcdefx", 'NOSTEAL f18 empty $a[0]');
331 is (substr($a[1], 0, 7), "abcdefx", 'NOSTEAL f18 empty $a[1]');
332 @a = (split /-/, "abc-def")[0,0];
333 is ($a[0], "abc", 'NOSTEAL split empty $a[0]');
334 is ($a[1], "abc", 'NOSTEAL split empty $a[1]');
336 # with @a initially non-empty, it takes a different code path that
337 # makes a mortal copy of each RHS element
340 is (substr($a[0], 0, 7), "abcdefx", 'NOSTEAL f18 non-empty $a[0]');
341 is (substr($a[1], 0, 7), "abcdefx", 'NOSTEAL f18 non-empty $a[1]');
343 @a = (split /-/, "abc-def")[0,0];
344 is ($a[0], "abc", 'NOSTEAL split non-empty $a[0]');
345 is ($a[1], "abc", 'NOSTEAL split non-empty $a[1]');
347 # similarly with PADTMPs
350 @a = ($long . "x")[0,0];
351 is (substr($a[0], 0, 4), "defx", 'NOSTEAL PADTMP empty $a[0]');
352 is (substr($a[1], 0, 4), "defx", 'NOSTEAL PADTMP empty $a[1]');
355 @a = ($long . "x")[0,0];
356 is (substr($a[0], 0, 4), "defx", 'NOSTEAL PADTMP non-empty $a[0]');
357 is (substr($a[1], 0, 4), "defx", 'NOSTEAL PADTMP non-empty $a[1]');
359 # as above, but assigning to a hash
361 %h = (f18())[1,0,2,0];
362 is (substr($h{key1}, 0, 7), "abcdefx", 'NOSTEAL f18 empty $h{key1}');
363 is (substr($h{key2}, 0, 7), "abcdefx", 'NOSTEAL f18 empty $h{key2}');
364 %h = (split /-/, "key1-val-key2")[0,1,2,1];
365 is ($h{key1}, "val", 'NOSTEAL split empty $h{key1}');
366 is ($h{key2}, "val", 'NOSTEAL split empty $h{key2}');
368 %h = qw(key1 foo key2 bar key3 baz);
369 %h = (f18())[1,0,2,0];
370 is (substr($h{key1}, 0, 7), "abcdefx", 'NOSTEAL f18 non-empty $h{key1}');
371 is (substr($h{key2}, 0, 7), "abcdefx", 'NOSTEAL f18 non-empty $h{key2}');
372 %h = qw(key1 foo key2 bar key3 baz);
373 %h = (split /-/, "key1-val-key2")[0,1,2,1];
374 is ($h{key1}, "val", 'NOSTEAL split non-empty $h{key1}');
375 is ($h{key2}, "val", 'NOSTEAL split non-empty $h{key2}');
378 %h = ($long . "x", "key1", "key2")[1,0,2,0];
379 is (substr($h{key1}, 0, 4), "defx", 'NOSTEAL PADTMP empty $h{key1}');
380 is (substr($h{key2}, 0, 4), "defx", 'NOSTEAL PADTMP empty $h{key2}');
382 %h = qw(key1 foo key2 bar key3 baz);
383 %h = ($long . "x", "key1", "key2")[1,0,2,0];
384 is (substr($h{key1}, 0, 4), "defx", 'NOSTEAL PADTMP non-empty $h{key1}');
385 is (substr($h{key2}, 0, 4), "defx", 'NOSTEAL PADTMP non-empty $h{key2}');
387 # both keys and values stealable
388 @a = (%h = (split /-/, "abc-def")[0,1,0,1]);
389 is (join(':', keys %h), "abc", "NOSTEAL split G_ARRAY keys");
390 is (join(':', values %h), "def", "NOSTEAL split G_ARRAY values");
391 is (join(':', @a), "abc:def", "NOSTEAL split G_ARRAY result");
397 ($x,$y) = (undef, $x);
398 is($x, undef, 'single scalar on RHS, but two on LHS: x');
399 is($y, 1, 'single scalar on RHS, but two on LHS: y');
402 { # magic handling, see #126633
406 sub TIEARRAY { bless [ $_[1] ] }
407 sub STORE { $_[0][0]->[$_[1]] = $_[2]; $set = 1 }
408 sub FETCH { $_[0][0]->[$_[1]] }
409 sub CLEAR { @{$_[0][0]} = () }
412 my @base = ( "a", "b" );
416 tie @proxy, "ArrayProxy", \@real;
417 @proxy[0, 1] = @real[1, 0];
418 is($real[0], "b", "tied left first");
419 is($real[1], "a", "tied left second");
421 @real[0, 1] = @proxy[1, 0];
422 is($real[0], "b", "tied right first");
423 is($real[1], "a", "tied right second");
425 @proxy[0, 1] = @proxy[1, 0];
426 is($real[0], "b", "tied both first");
427 is($real[1], "a", "tied both second");
429 ($temp, @real) = @proxy[1, 0];
430 is($real[0], "a", "scalar/array tied right");
432 ($temp, @proxy) = @real[1, 0];
433 is($real[0], "a", "scalar/array tied left");
435 ($temp, @proxy) = @proxy[1, 0];
436 is($real[0], "a", "scalar/array tied both");
439 ($proxy[0], $orig) = (1, $set);
440 is($orig, 0, 'previous value of $set');
444 skip "no List::Util::min on miniperl", 2, if is_miniperl;
448 ( $x, $y ) = ( List::Util::min($y), List::Util::min($x) );
449 is($x, 2, "check swap for \$x");
450 is($y, 1, "check swap for \$y");
455 # check that a second aggregate is empted but doesn't suck up
458 my (@a, @b) = qw(x y);
459 is(+@a, 2, "double array A len");
460 is(+@b, 0, "double array B len");
461 is("@a", "x y", "double array A contents");
466 is(+@a, 2, "double array non-empty A len");
467 is(+@b, 0, "double array non-empty B len");
468 is("@a", "x y", "double array non-empty A contents");
470 my (%a, %b) = qw(k1 v1 k2 v2);
471 is(+(keys %a), 2, "double hash A len");
472 is(+(keys %b), 0, "double hash B len");
473 is(join(' ', sort keys %a), "k1 k2", "double hash A keys");
474 is(join(' ', sort values %a), "v1 v2", "double hash A values");
478 (%a, %b) = qw(k1 v1 k2 v2);
479 is(+(keys %a), 2, "double hash non-empty A len");
480 is(+(keys %b), 0, "double hash non-empty B len");
481 is(join(' ', sort keys %a), "k1 k2", "double hash non-empty A keys");
482 is(join(' ', sort values %a), "v1 v2", "double hash non-empty A values");
485 # list and lval context: filling of missing elements, returning correct
487 # ( Note that these partially duplicate some tests in hashassign.t which
488 # I didn't spot at first - DAPM)
497 is($x, $n >= 1 ? "assign1" : undef, "lval: X pre $n $desc");
498 is($y, $n >= 2 ? "assign2" : undef, "lval: Y pre $n $desc");
499 is($z, undef, "lval: Z pre $n $desc");
506 is($x, "lval0", "lval: a post $n $desc");
507 is($y, "lval1", "lval: b post $n $desc");
508 is($z, "lval2", "lval: c post $n $desc");
510 lval(0, "XYZ", (($x,$y,$z) = ()));
511 lval(1, "XYZ", (($x,$y,$z) = (qw(assign1))));
512 lval(2, "XYZ", (($x,$y,$z) = (qw(assign1 assign2))));
514 lval(0, "XYZA", (($x,$y,$z,@a) = ()));
515 lval(1, "XYZA", (($x,$y,$z,@a) = (qw(assign1))));
516 lval(2, "XYZA", (($x,$y,$z,@a) = (qw(assign1 assign2))));
518 lval(0, "XYAZ", (($x,$y,@a,$z) = ()));
519 lval(1, "XYAZ", (($x,$y,@a,$z) = (qw(assign1))));
520 lval(2, "XYAZ", (($x,$y,@a,$z) = (qw(assign1 assign2))));
522 lval(0, "XYZH", (($x,$y,$z,%h) = ()));
523 lval(1, "XYZH", (($x,$y,$z,%h) = (qw(assign1))));
524 lval(2, "XYZH", (($x,$y,$z,%h) = (qw(assign1 assign2))));
526 lval(0, "XYHZ", (($x,$y,%h,$z) = ()));
527 lval(1, "XYHZ", (($x,$y,%h,$z) = (qw(assign1))));
528 lval(2, "XYHZ", (($x,$y,%h,$z) = (qw(assign1 assign2))));
530 # odd number of hash elements
535 is (join(":", map $_ // "u", @a), "X:u", "lval odd singleton");
536 @a = (($x, $y, %h) = qw(X Y K));
537 is (join(":", map $_ // "u", @a), "X:Y:K:u", "lval odd");
538 @a = (($x, $y, %h, $z) = qw(X Y K));
539 is (join(":", map $_ // "u", @a), "X:Y:K:u:u", "lval odd with z");
542 # undef on LHS uses RHS as lvalue instead
543 # Note this this just codifies existing behaviour - it may not be
544 # correct. See http://nntp.perl.org/group/perl.perl5.porters/240358.
547 ($x, $y, $z) = (0, 10, 20);
548 $_++ for ((undef, $x) = ($y, $z));
549 is "$x:$y:$z", "21:11:20", "undef as lvalue";
555 # [perl #129991] assert failure in S_aassign_copy_common
556 # the LHS of a list assign can be aliased to an immortal SV;
557 # we used to assert that this couldn't happen
558 eval { ($_,$0)=(1,0) for 0 gt 0 };
559 like($@, qr//, "RT #129991");
564 # lexical refs on LHS, dereffed on the RHS
568 my $sref = do { my $tmp = 2; \$tmp };
569 ($sref, $fill) = (1, $$sref);
570 is ($sref, 1, "RT #130132 scalar 1");
571 is ($fill, 2, "RT #130132 scalar 2");
575 ($sref, $$sref) = (2, 3);
576 is ($sref, 2, "RT #130132 scalar derefffed 1");
577 is ($x, 3, "RT #130132 scalar derefffed 2");
581 ($sref, $$sref) = (2);
582 is ($sref, 2, "RT #130132 scalar undef 1");
583 is ($x, undef, "RT #130132 scalar undef 2");
586 $sref = do { my $tmp = 2; \$tmp };
587 @a = (($sref) = (1, $$sref));
588 is ($sref, 1, "RT #130132 scalar list cxt 1");
589 is ($a[0], 1, "RT #130132 scalar list cxt a[0]");
592 ($aref, $fill) = @$aref;
593 is ($aref, 1, "RT #130132 array 1");
594 is ($fill, 2, "RT #130132 array 2");