xref: /openbsd/gnu/usr.bin/perl/lib/B/Deparse.t (revision cecf84d4)
1#!./perl
2
3BEGIN {
4    unshift @INC, 't';
5    require Config;
6    if (($Config::Config{'extensions'} !~ /\bB\b/) ){
7        print "1..0 # Skip -- Perl configured without B module\n";
8        exit 0;
9    }
10}
11
12use warnings;
13use strict;
14use Test::More;
15
16my $tests = 20; # not counting those in the __DATA__ section
17
18use B::Deparse;
19my $deparse = B::Deparse->new();
20isa_ok($deparse, 'B::Deparse', 'instantiate a B::Deparse object');
21my %deparse;
22
23$/ = "\n####\n";
24while (<DATA>) {
25    chomp;
26    $tests ++;
27    # This code is pinched from the t/lib/common.pl for TODO.
28    # It's not clear how to avoid duplication
29    my %meta = (context => '');
30    foreach my $what (qw(skip todo context options)) {
31	s/^#\s*\U$what\E\s*(.*)\n//m and $meta{$what} = $1;
32	# If the SKIP reason starts ? then it's taken as a code snippet to
33	# evaluate. This provides the flexibility to have conditional SKIPs
34	if ($meta{$what} && $meta{$what} =~ s/^\?//) {
35	    my $temp = eval $meta{$what};
36	    if ($@) {
37		die "# In \U$what\E code reason:\n# $meta{$what}\n$@";
38	    }
39	    $meta{$what} = $temp;
40	}
41    }
42
43    s/^\s*#\s*(.*)$//mg;
44    my $desc = $1;
45    die "Missing name in test $_" unless defined $desc;
46
47    if ($meta{skip}) {
48	# Like this to avoid needing a label SKIP:
49	Test::More->builder->skip($meta{skip});
50	next;
51    }
52
53    my ($input, $expected);
54    if (/(.*)\n>>>>\n(.*)/s) {
55	($input, $expected) = ($1, $2);
56    }
57    else {
58	($input, $expected) = ($_, $_);
59    }
60
61    # parse options if necessary
62    my $deparse = $meta{options}
63	? $deparse{$meta{options}} ||=
64	    new B::Deparse split /,/, $meta{options}
65	: $deparse;
66
67    my $coderef = eval "$meta{context};\n" . <<'EOC' . "sub {$input}";
68# Tell B::Deparse about our ambient pragmas
69my ($hint_bits, $warning_bits, $hinthash);
70BEGIN {
71    ($hint_bits, $warning_bits, $hinthash) = ($^H, ${^WARNING_BITS}, \%^H);
72}
73$deparse->ambient_pragmas (
74    hint_bits    => $hint_bits,
75    warning_bits => $warning_bits,
76    '%^H'        => $hinthash,
77);
78EOC
79
80    if ($@) {
81	is($@, "", "compilation of $desc");
82    }
83    else {
84	my $deparsed = $deparse->coderef2text( $coderef );
85	my $regex = $expected;
86	$regex =~ s/(\S+)/\Q$1/g;
87	$regex =~ s/\s+/\\s+/g;
88	$regex = '^\{\s*' . $regex . '\s*\}$';
89
90	local $::TODO = $meta{todo};
91        like($deparsed, qr/$regex/, $desc);
92    }
93}
94
95use constant 'c', 'stuff';
96is((eval "sub ".$deparse->coderef2text(\&c))->(), 'stuff',
97   'the subroutine generated by use constant deparses');
98
99my $a = 0;
100is($deparse->coderef2text(sub{(-1) ** $a }), "{\n    (-1) ** \$a;\n}",
101   'anon sub capturing an external lexical');
102
103use constant cr => ['hello'];
104my $string = "sub " . $deparse->coderef2text(\&cr);
105my $val = (eval $string)->() or diag $string;
106is(ref($val), 'ARRAY', 'constant array references deparse');
107is($val->[0], 'hello', 'and return the correct value');
108
109my $path = join " ", map { qq["-I$_"] } @INC;
110
111$a = `$^X $path "-MO=Deparse" -anlwi.bak -e 1 2>&1`;
112$a =~ s/-e syntax OK\n//g;
113$a =~ s/.*possible typo.*\n//;	   # Remove warning line
114$a =~ s/.*-i used with no filenames.*\n//;	# Remove warning line
115$a =~ s{\\340\\242}{\\s} if (ord("\\") == 224); # EBCDIC, cp 1047 or 037
116$a =~ s{\\274\\242}{\\s} if (ord("\\") == 188); # $^O eq 'posix-bc'
117$b = <<'EOF';
118BEGIN { $^I = ".bak"; }
119BEGIN { $^W = 1; }
120BEGIN { $/ = "\n"; $\ = "\n"; }
121LINE: while (defined($_ = <ARGV>)) {
122    chomp $_;
123    our(@F) = split(' ', $_, 0);
124    '???';
125}
126EOF
127is($a, $b,
128   'command line flags deparse as BEGIN blocks setting control variables');
129
130$a = `$^X $path "-MO=Deparse" -e "use constant PI => 4" 2>&1`;
131$a =~ s/-e syntax OK\n//g;
132is($a, "use constant ('PI', 4);\n",
133   "Proxy Constant Subroutines must not show up as (incorrect) prototypes");
134
135#Re: perlbug #35857, patch #24505
136#handle warnings::register-ed packages properly.
137package B::Deparse::Wrapper;
138use strict;
139use warnings;
140use warnings::register;
141sub getcode {
142   my $deparser = B::Deparse->new();
143   return $deparser->coderef2text(shift);
144}
145
146package Moo;
147use overload '0+' => sub { 42 };
148
149package main;
150use strict;
151use warnings;
152use constant GLIPP => 'glipp';
153use constant PI => 4;
154use constant OVERLOADED_NUMIFICATION => bless({}, 'Moo');
155use Fcntl qw/O_TRUNC O_APPEND O_EXCL/;
156BEGIN { delete $::Fcntl::{O_APPEND}; }
157use POSIX qw/O_CREAT/;
158sub test {
159   my $val = shift;
160   my $res = B::Deparse::Wrapper::getcode($val);
161   like($res, qr/use warnings/,
162	'[perl #35857] [PATCH] B::Deparse doesnt handle warnings register properly');
163}
164my ($q,$p);
165my $x=sub { ++$q,++$p };
166test($x);
167eval <<EOFCODE and test($x);
168   package bar;
169   use strict;
170   use warnings;
171   use warnings::register;
172   package main;
173   1
174EOFCODE
175
176# Exotic sub declarations
177$a = `$^X $path "-MO=Deparse" -e "sub ::::{}sub ::::::{}" 2>&1`;
178$a =~ s/-e syntax OK\n//g;
179is($a, <<'EOCODG', "sub :::: and sub ::::::");
180sub :::: {
181
182}
183sub :::::: {
184
185}
186EOCODG
187
188# [perl #117311]
189$a = `$^X $path "-MO=Deparse,-l" -e "map{ eval(0) }()" 2>&1`;
190$a =~ s/-e syntax OK\n//g;
191is($a, <<'EOCODH', "[perl #117311] [PATCH] -l option ('#line ...') does not emit ^Ls in the output");
192#line 1 "-e"
193map {
194#line 1 "-e"
195eval 0;} ();
196EOCODH
197
198# [perl #33752]
199{
200  my $code = <<"EOCODE";
201{
202    our \$\x{1e1f}\x{14d}\x{14d};
203}
204EOCODE
205  my $deparsed
206   = $deparse->coderef2text(eval "sub { our \$\x{1e1f}\x{14d}\x{14d} }" );
207  s/$ \n//x for $deparsed, $code;
208  is $deparsed, $code, 'our $funny_Unicode_chars';
209}
210
211# [perl #62500]
212$a =
213  `$^X $path "-MO=Deparse" -e "BEGIN{*CORE::GLOBAL::require=sub{1}}" 2>&1`;
214$a =~ s/-e syntax OK\n//g;
215is($a, <<'EOCODF', "CORE::GLOBAL::require override causing panick");
216sub BEGIN {
217    *CORE::GLOBAL::require = sub {
218        1;
219    }
220    ;
221}
222EOCODF
223
224# [perl #91384]
225$a =
226  `$^X $path "-MO=Deparse" -e "BEGIN{*Acme::Acme:: = *Acme::}" 2>&1`;
227like($a, qr/-e syntax OK/,
228    "Deparse does not hang when traversing stash circularities");
229
230# [perl #93990]
231@] = ();
232is($deparse->coderef2text(sub{ print "@{]}" }),
233q<{
234    print "@{]}";
235}>, 'curly around to interpolate "@{]}"');
236is($deparse->coderef2text(sub{ print "@{-}" }),
237q<{
238    print "@-";
239}>, 'no need to curly around to interpolate "@-"');
240
241# Strict hints in %^H are mercilessly suppressed
242$a =
243  `$^X $path "-MO=Deparse" -e "use strict; print;" 2>&1`;
244unlike($a, qr/BEGIN/,
245    "Deparse does not emit strict hh hints");
246
247# ambient_pragmas should not mess with strict settings.
248SKIP: {
249    skip "requires 5.11", 1 unless $] >= 5.011;
250    eval q`
251	BEGIN {
252	    # Clear out all hints
253	    %^H = ();
254	    $^H = 0;
255	    new B::Deparse -> ambient_pragmas(strict => 'all');
256	}
257	use 5.011;  # should enable strict
258	ok !eval '$do_noT_create_a_variable_with_this_name = 1',
259	  'ambient_pragmas do not mess with compiling scope';
260   `;
261}
262
263# multiple statements on format lines
264$a = `$^X $path "-MO=Deparse" -e "format =" -e "\@" -e "x();z()" -e. 2>&1`;
265$a =~ s/-e syntax OK\n//g;
266is($a, <<'EOCODH', 'multiple statements on format lines');
267format STDOUT =
268@
269x(); z()
270.
271EOCODH
272
273# literal big chars under 'use utf8'
274is($deparse->coderef2text(sub{ use utf8; /€/; }),
275'{
276    /\x{20ac}/;
277}',
278"qr/euro/");
279
280
281done_testing($tests);
282
283__DATA__
284# TODO [perl #120950] This succeeds when run a 2nd time
285# y/uni/code/
286tr/\x{345}/\x{370}/;
287####
288# y/uni/code/  [perl #120950] This 2nd instance succeeds
289tr/\x{345}/\x{370}/;
290####
291# A constant
2921;
293####
294# Constants in a block
295{
296    no warnings;
297    '???';
298    2;
299}
300####
301# Lexical and simple arithmetic
302my $test;
303++$test and $test /= 2;
304>>>>
305my $test;
306$test /= 2 if ++$test;
307####
308# list x
309-((1, 2) x 2);
310####
311# lvalue sub
312{
313    my $test = sub : lvalue {
314	my $x;
315    }
316    ;
317}
318####
319# method
320{
321    my $test = sub : method {
322	my $x;
323    }
324    ;
325}
326####
327# block with continue
328{
329    234;
330}
331continue {
332    123;
333}
334####
335# lexical and package scalars
336my $x;
337print $main::x;
338####
339# lexical and package arrays
340my @x;
341print $main::x[1];
342####
343# lexical and package hashes
344my %x;
345$x{warn()};
346####
347# <>
348my $foo;
349$_ .= <ARGV> . <$foo>;
350####
351# \x{}
352my $foo = "Ab\x{100}\200\x{200}\237Cd\000Ef\x{1000}\cA\x{2000}\cZ";
353####
354# s///e
355s/x/'y';/e;
356s/x/$a;/e;
357s/x/complex_expression();/e;
358####
359# block
360{ my $x; }
361####
362# while 1
363while (1) { my $k; }
364####
365# trailing for
366my ($x,@a);
367$x=1 for @a;
368>>>>
369my($x, @a);
370$x = 1 foreach (@a);
371####
372# 2 arguments in a 3 argument for
373for (my $i = 0; $i < 2;) {
374    my $z = 1;
375}
376####
377# 3 argument for
378for (my $i = 0; $i < 2; ++$i) {
379    my $z = 1;
380}
381####
382# 3 argument for again
383for (my $i = 0; $i < 2; ++$i) {
384    my $z = 1;
385}
386####
387# while/continue
388my $i;
389while ($i) { my $z = 1; } continue { $i = 99; }
390####
391# foreach with my
392foreach my $i (1, 2) {
393    my $z = 1;
394}
395####
396# OPTIONS -p
397# foreach with my under -p
398foreach my $i (1) {
399    die;
400}
401####
402# foreach
403my $i;
404foreach $i (1, 2) {
405    my $z = 1;
406}
407####
408# foreach, 2 mys
409my $i;
410foreach my $i (1, 2) {
411    my $z = 1;
412}
413####
414# foreach
415foreach my $i (1, 2) {
416    my $z = 1;
417}
418####
419# foreach with our
420foreach our $i (1, 2) {
421    my $z = 1;
422}
423####
424# foreach with my and our
425my $i;
426foreach our $i (1, 2) {
427    my $z = 1;
428}
429####
430# reverse sort
431my @x;
432print reverse sort(@x);
433####
434# sort with cmp
435my @x;
436print((sort {$b cmp $a} @x));
437####
438# reverse sort with block
439my @x;
440print((reverse sort {$b <=> $a} @x));
441####
442# foreach reverse
443our @a;
444print $_ foreach (reverse @a);
445####
446# foreach reverse (not inplace)
447our @a;
448print $_ foreach (reverse 1, 2..5);
449####
450# bug #38684
451our @ary;
452@ary = split(' ', 'foo', 0);
453####
454# bug #40055
455do { () };
456####
457# bug #40055
458do { my $x = 1; $x };
459####
460# <20061012113037.GJ25805@c4.convolution.nl>
461my $f = sub {
462    +{[]};
463} ;
464####
465# bug #43010
466'!@$%'->();
467####
468# bug #43010
469::();
470####
471# bug #43010
472'::::'->();
473####
474# bug #43010
475&::::;
476####
477# [perl #77172]
478package rt77172;
479sub foo {} foo & & & foo;
480>>>>
481package rt77172;
482foo(&{&} & foo());
483####
484# variables as method names
485my $bar;
486'Foo'->$bar('orz');
487'Foo'->$bar('orz') = 'a stranger stranger than before';
488####
489# constants as method names
490'Foo'->bar('orz');
491####
492# constants as method names without ()
493'Foo'->bar;
494####
495# [perl #47359] "indirect" method call notation
496our @bar;
497foo{@bar}+1,->foo;
498(foo{@bar}+1),foo();
499foo{@bar}1 xor foo();
500>>>>
501our @bar;
502(foo { @bar } 1)->foo;
503(foo { @bar } 1), foo();
504foo { @bar } 1 xor foo();
505####
506# SKIP ?$] < 5.010 && "say not implemented on this Perl version"
507# CONTEXT use feature ':5.10';
508# say
509say 'foo';
510####
511# SKIP ?$] < 5.010 && "say not implemented on this Perl version"
512# CONTEXT use 5.10.0;
513# say in the context of use 5.10.0
514say 'foo';
515####
516# SKIP ?$] < 5.010 && "say not implemented on this Perl version"
517# say with use 5.10.0
518use 5.10.0;
519say 'foo';
520>>>>
521no feature;
522use feature ':5.10';
523say 'foo';
524####
525# SKIP ?$] < 5.010 && "say not implemented on this Perl version"
526# say with use feature ':5.10';
527use feature ':5.10';
528say 'foo';
529>>>>
530use feature 'say', 'state', 'switch';
531say 'foo';
532####
533# SKIP ?$] < 5.010 && "say not implemented on this Perl version"
534# CONTEXT use feature ':5.10';
535# say with use 5.10.0 in the context of use feature
536use 5.10.0;
537say 'foo';
538>>>>
539no feature;
540use feature ':5.10';
541say 'foo';
542####
543# SKIP ?$] < 5.010 && "say not implemented on this Perl version"
544# CONTEXT use 5.10.0;
545# say with use feature ':5.10' in the context of use 5.10.0
546use feature ':5.10';
547say 'foo';
548>>>>
549say 'foo';
550####
551# SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
552# CONTEXT use feature ':5.15';
553# __SUB__
554__SUB__;
555####
556# SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
557# CONTEXT use 5.15.0;
558# __SUB__ in the context of use 5.15.0
559__SUB__;
560####
561# SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
562# __SUB__ with use 5.15.0
563use 5.15.0;
564__SUB__;
565>>>>
566no feature;
567use feature ':5.16';
568__SUB__;
569####
570# SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
571# __SUB__ with use feature ':5.15';
572use feature ':5.15';
573__SUB__;
574>>>>
575use feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval';
576__SUB__;
577####
578# SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
579# CONTEXT use feature ':5.15';
580# __SUB__ with use 5.15.0 in the context of use feature
581use 5.15.0;
582__SUB__;
583>>>>
584no feature;
585use feature ':5.16';
586__SUB__;
587####
588# SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
589# CONTEXT use 5.15.0;
590# __SUB__ with use feature ':5.15' in the context of use 5.15.0
591use feature ':5.15';
592__SUB__;
593>>>>
594__SUB__;
595####
596# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
597# CONTEXT use feature ':5.10';
598# state vars
599state $x = 42;
600####
601# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
602# CONTEXT use feature ':5.10';
603# state var assignment
604{
605    my $y = (state $x = 42);
606}
607####
608# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
609# CONTEXT use feature ':5.10';
610# state vars in anonymous subroutines
611$a = sub {
612    state $x;
613    return $x++;
614}
615;
616####
617# SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
618# each @array;
619each @ARGV;
620each @$a;
621####
622# SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
623# keys @array; values @array
624keys @$a if keys @ARGV;
625values @ARGV if values @$a;
626####
627# Anonymous arrays and hashes, and references to them
628my $a = {};
629my $b = \{};
630my $c = [];
631my $d = \[];
632####
633# SKIP ?$] < 5.010 && "smartmatch and given/when not implemented on this Perl version"
634# CONTEXT use feature ':5.10'; no warnings 'experimental::smartmatch';
635# implicit smartmatch in given/when
636given ('foo') {
637    when ('bar') { continue; }
638    when ($_ ~~ 'quux') { continue; }
639    default { 0; }
640}
641####
642# conditions in elsifs (regression in change #33710 which fixed bug #37302)
643if ($a) { x(); }
644elsif ($b) { x(); }
645elsif ($a and $b) { x(); }
646elsif ($a or $b) { x(); }
647else { x(); }
648####
649# interpolation in regexps
650my($y, $t);
651/x${y}z$t/;
652####
653# TODO new undocumented cpan-bug #33708
654# cpan-bug #33708
655%{$_ || {}}
656####
657# TODO hash constants not yet fixed
658# cpan-bug #33708
659use constant H => { "#" => 1 }; H->{"#"}
660####
661# TODO optimized away 0 not yet fixed
662# cpan-bug #33708
663foreach my $i (@_) { 0 }
664####
665# tests with not, not optimized
666my $c;
667x() unless $a;
668x() if not $a and $b;
669x() if $a and not $b;
670x() unless not $a and $b;
671x() unless $a and not $b;
672x() if not $a or $b;
673x() if $a or not $b;
674x() unless not $a or $b;
675x() unless $a or not $b;
676x() if $a and not $b and $c;
677x() if not $a and $b and not $c;
678x() unless $a and not $b and $c;
679x() unless not $a and $b and not $c;
680x() if $a or not $b or $c;
681x() if not $a or $b or not $c;
682x() unless $a or not $b or $c;
683x() unless not $a or $b or not $c;
684####
685# tests with not, optimized
686my $c;
687x() if not $a;
688x() unless not $a;
689x() if not $a and not $b;
690x() unless not $a and not $b;
691x() if not $a or not $b;
692x() unless not $a or not $b;
693x() if not $a and not $b and $c;
694x() unless not $a and not $b and $c;
695x() if not $a or not $b or $c;
696x() unless not $a or not $b or $c;
697x() if not $a and not $b and not $c;
698x() unless not $a and not $b and not $c;
699x() if not $a or not $b or not $c;
700x() unless not $a or not $b or not $c;
701x() unless not $a or not $b or not $c;
702>>>>
703my $c;
704x() unless $a;
705x() if $a;
706x() unless $a or $b;
707x() if $a or $b;
708x() unless $a and $b;
709x() if $a and $b;
710x() if not $a || $b and $c;
711x() unless not $a || $b and $c;
712x() if not $a && $b or $c;
713x() unless not $a && $b or $c;
714x() unless $a or $b or $c;
715x() if $a or $b or $c;
716x() unless $a and $b and $c;
717x() if $a and $b and $c;
718x() unless not $a && $b && $c;
719####
720# tests that should be constant folded
721x() if 1;
722x() if GLIPP;
723x() if !GLIPP;
724x() if GLIPP && GLIPP;
725x() if !GLIPP || GLIPP;
726x() if do { GLIPP };
727x() if do { no warnings 'void'; 5; GLIPP };
728x() if do { !GLIPP };
729if (GLIPP) { x() } else { z() }
730if (!GLIPP) { x() } else { z() }
731if (GLIPP) { x() } elsif (GLIPP) { z() }
732if (!GLIPP) { x() } elsif (GLIPP) { z() }
733if (GLIPP) { x() } elsif (!GLIPP) { z() }
734if (!GLIPP) { x() } elsif (!GLIPP) { z() }
735if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (GLIPP) { t() }
736if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
737if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
738>>>>
739x();
740x();
741'???';
742x();
743x();
744x();
745x();
746do {
747    '???'
748};
749do {
750    x()
751};
752do {
753    z()
754};
755do {
756    x()
757};
758do {
759    z()
760};
761do {
762    x()
763};
764'???';
765do {
766    t()
767};
768'???';
769!1;
770####
771# TODO constant deparsing has been backed out for 5.12
772# XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
773# tests that shouldn't be constant folded
774# It might be fundamentally impossible to make this work on ithreads, in which
775# case the TODO should become a SKIP
776x() if $a;
777if ($a == 1) { x() } elsif ($b == 2) { z() }
778if (do { foo(); GLIPP }) { x() }
779if (do { $a++; GLIPP }) { x() }
780>>>>
781x() if $a;
782if ($a == 1) { x(); } elsif ($b == 2) { z(); }
783if (do { foo(); GLIPP }) { x(); }
784if (do { ++$a; GLIPP }) { x(); }
785####
786# TODO constant deparsing has been backed out for 5.12
787# tests for deparsing constants
788warn PI;
789####
790# TODO constant deparsing has been backed out for 5.12
791# tests for deparsing imported constants
792warn O_TRUNC;
793####
794# TODO constant deparsing has been backed out for 5.12
795# tests for deparsing re-exported constants
796warn O_CREAT;
797####
798# TODO constant deparsing has been backed out for 5.12
799# tests for deparsing imported constants that got deleted from the original namespace
800warn O_APPEND;
801####
802# TODO constant deparsing has been backed out for 5.12
803# XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
804# tests for deparsing constants which got turned into full typeglobs
805# It might be fundamentally impossible to make this work on ithreads, in which
806# case the TODO should become a SKIP
807warn O_EXCL;
808eval '@Fcntl::O_EXCL = qw/affe tiger/;';
809warn O_EXCL;
810####
811# TODO constant deparsing has been backed out for 5.12
812# tests for deparsing of blessed constant with overloaded numification
813warn OVERLOADED_NUMIFICATION;
814####
815# strict
816no strict;
817print $x;
818use strict 'vars';
819print $main::x;
820use strict 'subs';
821print $main::x;
822use strict 'refs';
823print $main::x;
824no strict 'vars';
825$x;
826####
827# TODO Subsets of warnings could be encoded textually, rather than as bitflips.
828# subsets of warnings
829no warnings 'deprecated';
830my $x;
831####
832# TODO Better test for CPAN #33708 - the deparsed code has different behaviour
833# CPAN #33708
834use strict;
835no warnings;
836
837foreach (0..3) {
838    my $x = 2;
839    {
840	my $x if 0;
841	print ++$x, "\n";
842    }
843}
844####
845# no attribute list
846my $pi = 4;
847####
848# SKIP ?$] > 5.013006 && ":= is now a syntax error"
849# := treated as an empty attribute list
850no warnings;
851my $pi := 4;
852>>>>
853no warnings;
854my $pi = 4;
855####
856# : = empty attribute list
857my $pi : = 4;
858>>>>
859my $pi = 4;
860####
861# in place sort
862our @a;
863my @b;
864@a = sort @a;
865@b = sort @b;
866();
867####
868# in place reverse
869our @a;
870my @b;
871@a = reverse @a;
872@b = reverse @b;
873();
874####
875# #71870 Use of uninitialized value in bitwise and B::Deparse
876my($r, $s, @a);
877@a = split(/foo/, $s, 0);
878$r = qr/foo/;
879@a = split(/$r/, $s, 0);
880();
881####
882# package declaration before label
883{
884    package Foo;
885    label: print 123;
886}
887####
888# shift optimisation
889shift;
890>>>>
891shift();
892####
893# shift optimisation
894shift @_;
895####
896# shift optimisation
897pop;
898>>>>
899pop();
900####
901# shift optimisation
902pop @_;
903####
904#[perl #20444]
905"foo" =~ (1 ? /foo/ : /bar/);
906"foo" =~ (1 ? y/foo// : /bar/);
907"foo" =~ (1 ? y/foo//r : /bar/);
908"foo" =~ (1 ? s/foo// : /bar/);
909>>>>
910'foo' =~ ($_ =~ /foo/);
911'foo' =~ ($_ =~ tr/fo//);
912'foo' =~ ($_ =~ tr/fo//r);
913'foo' =~ ($_ =~ s/foo//);
914####
915# The fix for [perl #20444] broke this.
916'foo' =~ do { () };
917####
918# [perl #81424] match against aelemfast_lex
919my @s;
920print /$s[1]/;
921####
922# /$#a/
923print /$#main::a/;
924####
925# [perl #91318] /regexp/applaud
926print /a/a, s/b/c/a;
927print /a/aa, s/b/c/aa;
928print /a/p, s/b/c/p;
929print /a/l, s/b/c/l;
930print /a/u, s/b/c/u;
931{
932    use feature "unicode_strings";
933    print /a/d, s/b/c/d;
934}
935{
936    use re "/u";
937    print /a/d, s/b/c/d;
938}
939{
940    use 5.012;
941    print /a/d, s/b/c/d;
942}
943>>>>
944print /a/a, s/b/c/a;
945print /a/aa, s/b/c/aa;
946print /a/p, s/b/c/p;
947print /a/l, s/b/c/l;
948print /a/u, s/b/c/u;
949{
950    use feature 'unicode_strings';
951    print /a/d, s/b/c/d;
952}
953{
954    BEGIN { $^H{'reflags'}         = '0';
955	    $^H{'reflags_charset'} = '2'; }
956    print /a/d, s/b/c/d;
957}
958{
959    no feature;
960    use feature ':5.12';
961    print /a/d, s/b/c/d;
962}
963####
964# [perl #119807] s//\(3)/ge should not warn when deparsed (\3 warns)
965s/foo/\(3);/eg;
966####
967# Test @threadsv_names under 5005threads
968foreach $' (1, 2) {
969    sleep $';
970}
971####
972# y///r
973tr/a/b/r;
974####
975# [perl #90898]
976<a,>;
977####
978# [perl #91008]
979# CONTEXT no warnings 'experimental::autoderef';
980each $@;
981keys $~;
982values $!;
983####
984# readpipe with complex expression
985readpipe $a + $b;
986####
987# aelemfast
988$b::a[0] = 1;
989####
990# aelemfast for a lexical
991my @a;
992$a[0] = 1;
993####
994# feature features without feature
995# CONTEXT no warnings 'experimental::smartmatch';
996CORE::state $x;
997CORE::say $x;
998CORE::given ($x) {
999    CORE::when (3) {
1000        continue;
1001    }
1002    CORE::default {
1003        CORE::break;
1004    }
1005}
1006CORE::evalbytes '';
1007() = CORE::__SUB__;
1008() = CORE::fc $x;
1009####
1010# feature features when feature has been disabled by use VERSION
1011# CONTEXT no warnings 'experimental::smartmatch';
1012use feature (sprintf(":%vd", $^V));
1013use 1;
1014CORE::state $x;
1015CORE::say $x;
1016CORE::given ($x) {
1017    CORE::when (3) {
1018        continue;
1019    }
1020    CORE::default {
1021        CORE::break;
1022    }
1023}
1024CORE::evalbytes '';
1025() = CORE::__SUB__;
1026>>>>
1027CORE::state $x;
1028CORE::say $x;
1029CORE::given ($x) {
1030    CORE::when (3) {
1031        continue;
1032    }
1033    CORE::default {
1034        CORE::break;
1035    }
1036}
1037CORE::evalbytes '';
1038() = CORE::__SUB__;
1039####
1040# (the above test with CONTEXT, and the output is equivalent but different)
1041# CONTEXT use feature ':5.10'; no warnings 'experimental::smartmatch';
1042# feature features when feature has been disabled by use VERSION
1043use feature (sprintf(":%vd", $^V));
1044use 1;
1045CORE::state $x;
1046CORE::say $x;
1047CORE::given ($x) {
1048    CORE::when (3) {
1049        continue;
1050    }
1051    CORE::default {
1052        CORE::break;
1053    }
1054}
1055CORE::evalbytes '';
1056() = CORE::__SUB__;
1057>>>>
1058no feature;
1059use feature ':default';
1060CORE::state $x;
1061CORE::say $x;
1062CORE::given ($x) {
1063    CORE::when (3) {
1064        continue;
1065    }
1066    CORE::default {
1067        CORE::break;
1068    }
1069}
1070CORE::evalbytes '';
1071() = CORE::__SUB__;
1072####
1073# Feature hints
1074use feature 'current_sub', 'evalbytes';
1075print;
1076use 1;
1077print;
1078use 5.014;
1079print;
1080no feature 'unicode_strings';
1081print;
1082>>>>
1083use feature 'current_sub', 'evalbytes';
1084print $_;
1085no feature;
1086use feature ':default';
1087print $_;
1088no feature;
1089use feature ':5.12';
1090print $_;
1091no feature 'unicode_strings';
1092print $_;
1093####
1094# $#- $#+ $#{%} etc.
1095my @x;
1096@x = ($#{`}, $#{~}, $#{!}, $#{@}, $#{$}, $#{%}, $#{^}, $#{&}, $#{*});
1097@x = ($#{(}, $#{)}, $#{[}, $#{{}, $#{]}, $#{}}, $#{'}, $#{"}, $#{,});
1098@x = ($#{<}, $#{.}, $#{>}, $#{/}, $#{?}, $#{=}, $#+, $#{\}, $#{|}, $#-);
1099@x = ($#{;}, $#{:});
1100####
1101# ${#} interpolated
1102# It's a known TODO that warnings are deparsed as bits, not textually.
1103no warnings;
1104() = "${#}a";
1105####
1106# [perl #86060] $( $| $) in regexps need braces
1107/${(}/;
1108/${|}/;
1109/${)}/;
1110/${(}${|}${)}/;
1111####
1112# ()[...]
1113my(@a) = ()[()];
1114####
1115# sort(foo(bar))
1116# sort(foo(bar)) is interpreted as sort &foo(bar)
1117# sort foo(bar) is interpreted as sort foo bar
1118# parentheses are not optional in this case
1119print sort(foo('bar'));
1120>>>>
1121print sort(foo('bar'));
1122####
1123# substr assignment
1124substr(my $a, 0, 0) = (foo(), bar());
1125$a++;
1126####
1127# This following line works around an unfixed bug that we are not trying to
1128# test for here:
1129# CONTEXT BEGIN { $^H{a} = "b"; delete $^H{a} } # make %^H localised
1130# hint hash
1131BEGIN { $^H{'foo'} = undef; }
1132{
1133 BEGIN { $^H{'bar'} = undef; }
1134 {
1135  BEGIN { $^H{'baz'} = undef; }
1136  {
1137   print $_;
1138  }
1139  print $_;
1140 }
1141 print $_;
1142}
1143BEGIN { $^H{q[']} = '('; }
1144print $_;
1145####
1146# This following line works around an unfixed bug that we are not trying to
1147# test for here:
1148# CONTEXT BEGIN { $^H{a} = "b"; delete $^H{a} } # make %^H localised
1149# hint hash changes that serialise the same way with sort %hh
1150BEGIN { $^H{'a'} = 'b'; }
1151{
1152 BEGIN { $^H{'b'} = 'a'; delete $^H{'a'}; }
1153 print $_;
1154}
1155print $_;
1156####
1157# [perl #47361] do({}) and do +{} (variants of do-file)
1158do({});
1159do +{};
1160sub foo::do {}
1161package foo;
1162CORE::do({});
1163CORE::do +{};
1164>>>>
1165do({});
1166do({});
1167package foo;
1168CORE::do({});
1169CORE::do({});
1170####
1171# [perl #77096] functions that do not follow the llafr
1172() = (return 1) + time;
1173() = (return ($1 + $2) * $3) + time;
1174() = (return ($a xor $b)) + time;
1175() = (do 'file') + time;
1176() = (do ($1 + $2) * $3) + time;
1177() = (do ($1 xor $2)) + time;
1178() = (goto 1) + 3;
1179() = (require 'foo') + 3;
1180() = (require foo) + 3;
1181() = (CORE::dump 1) + 3;
1182() = (last 1) + 3;
1183() = (next 1) + 3;
1184() = (redo 1) + 3;
1185() = (-R $_) + 3;
1186() = (-W $_) + 3;
1187() = (-X $_) + 3;
1188() = (-r $_) + 3;
1189() = (-w $_) + 3;
1190() = (-x $_) + 3;
1191####
1192# [perl #97476] not() *does* follow the llafr
1193$_ = ($a xor not +($1 || 2) ** 2);
1194####
1195# Precedence conundrums with argument-less function calls
1196() = (eof) + 1;
1197() = (return) + 1;
1198() = (return, 1);
1199() = warn;
1200() = warn() + 1;
1201() = setpgrp() + 1;
1202####
1203# loopexes have assignment prec
1204() = (CORE::dump a) | 'b';
1205() = (goto a) | 'b';
1206() = (last a) | 'b';
1207() = (next a) | 'b';
1208() = (redo a) | 'b';
1209####
1210# [perl #63558] open local(*FH)
1211open local *FH;
1212pipe local *FH, local *FH;
1213####
1214# [perl #91416] open "string"
1215open 'open';
1216open '####';
1217open '^A';
1218open "\ca";
1219>>>>
1220open *open;
1221open '####';
1222open '^A';
1223open *^A;
1224####
1225# "string"->[] ->{}
1226no strict 'vars';
1227() = 'open'->[0]; #aelemfast
1228() = '####'->[0];
1229() = '^A'->[0];
1230() = "\ca"->[0];
1231() = 'a::]b'->[0];
1232() = 'open'->[$_]; #aelem
1233() = '####'->[$_];
1234() = '^A'->[$_];
1235() = "\ca"->[$_];
1236() = 'a::]b'->[$_];
1237() = 'open'->{0}; #helem
1238() = '####'->{0};
1239() = '^A'->{0};
1240() = "\ca"->{0};
1241() = 'a::]b'->{0};
1242>>>>
1243no strict 'vars';
1244() = $open[0];
1245() = '####'->[0];
1246() = '^A'->[0];
1247() = $^A[0];
1248() = 'a::]b'->[0];
1249() = $open[$_];
1250() = '####'->[$_];
1251() = '^A'->[$_];
1252() = $^A[$_];
1253() = 'a::]b'->[$_];
1254() = $open{'0'};
1255() = '####'->{'0'};
1256() = '^A'->{'0'};
1257() = $^A{'0'};
1258() = 'a::]b'->{'0'};
1259####
1260# [perl #74740] -(f()) vs -f()
1261$_ = -(f());
1262####
1263# require <binop>
1264require 'a' . $1;
1265####
1266#[perl #30504] foreach-my postfix/prefix difference
1267$_ = 'foo' foreach my ($foo1, $bar1, $baz1);
1268foreach (my ($foo2, $bar2, $baz2)) { $_ = 'foo' }
1269foreach my $i (my ($foo3, $bar3, $baz3)) { $i = 'foo' }
1270>>>>
1271$_ = 'foo' foreach (my($foo1, $bar1, $baz1));
1272foreach $_ (my($foo2, $bar2, $baz2)) {
1273    $_ = 'foo';
1274}
1275foreach my $i (my($foo3, $bar3, $baz3)) {
1276    $i = 'foo';
1277}
1278####
1279#[perl #108224] foreach with continue block
1280foreach (1 .. 3) { print } continue { print "\n" }
1281foreach (1 .. 3) { } continue { }
1282foreach my $i (1 .. 3) { print $i } continue { print "\n" }
1283foreach my $i (1 .. 3) { } continue { }
1284>>>>
1285foreach $_ (1 .. 3) {
1286    print $_;
1287}
1288continue {
1289    print "\n";
1290}
1291foreach $_ (1 .. 3) {
1292    ();
1293}
1294continue {
1295    ();
1296}
1297foreach my $i (1 .. 3) {
1298    print $i;
1299}
1300continue {
1301    print "\n";
1302}
1303foreach my $i (1 .. 3) {
1304    ();
1305}
1306continue {
1307    ();
1308}
1309####
1310# file handles
1311no strict;
1312my $mfh;
1313open F;
1314open *F;
1315open $fh;
1316open $mfh;
1317open 'a+b';
1318select *F;
1319select F;
1320select $f;
1321select $mfh;
1322select 'a+b';
1323####
1324# 'my' works with padrange op
1325my($z, @z);
1326my $m1;
1327$m1 = 1;
1328$z = $m1;
1329my $m2 = 2;
1330my($m3, $m4);
1331($m3, $m4) = (1, 2);
1332@z = ($m3, $m4);
1333my($m5, $m6) = (1, 2);
1334my($m7, undef, $m8) = (1, 2, 3);
1335@z = ($m7, undef, $m8);
1336($m7, undef, $m8) = (1, 2, 3);
1337####
1338# 'our/local' works with padrange op
1339no strict;
1340our($z, @z);
1341our $o1;
1342local $o11;
1343$o1 = 1;
1344local $o1 = 1;
1345$z = $o1;
1346$z = local $o1;
1347our $o2 = 2;
1348our($o3, $o4);
1349($o3, $o4) = (1, 2);
1350local($o3, $o4) = (1, 2);
1351@z = ($o3, $o4);
1352@z = local($o3, $o4);
1353our($o5, $o6) = (1, 2);
1354our($o7, undef, $o8) = (1, 2, 3);
1355@z = ($o7, undef, $o8);
1356@z = local($o7, undef, $o8);
1357($o7, undef, $o8) = (1, 2, 3);
1358local($o7, undef, $o8) = (1, 2, 3);
1359####
1360# 'state' works with padrange op
1361no strict;
1362use feature 'state';
1363state($z, @z);
1364state $s1;
1365$s1 = 1;
1366$z = $s1;
1367state $s2 = 2;
1368state($s3, $s4);
1369($s3, $s4) = (1, 2);
1370@z = ($s3, $s4);
1371# assignment of state lists isn't implemented yet
1372#state($s5, $s6) = (1, 2);
1373#state($s7, undef, $s8) = (1, 2, 3);
1374#@z = ($s7, undef, $s8);
1375($s7, undef, $s8) = (1, 2, 3);
1376####
1377# anon lists with padrange
1378my($a, $b);
1379my $c = [$a, $b];
1380my $d = {$a, $b};
1381####
1382# slices with padrange
1383my($a, $b);
1384my(@x, %y);
1385@x = @x[$a, $b];
1386@x = @y{$a, $b};
1387####
1388# binops with padrange
1389my($a, $b, $c);
1390$c = $a cmp $b;
1391$c = $a + $b;
1392$a += $b;
1393$c = $a - $b;
1394$a -= $b;
1395$c = my $a1 cmp $b;
1396$c = my $a2 + $b;
1397$a += my $b1;
1398$c = my $a3 - $b;
1399$a -= my $b2;
1400####
1401# 'x' with padrange
1402my($a, $b, $c, $d, @e);
1403$c = $a x $b;
1404$a x= $b;
1405@e = ($a) x $d;
1406@e = ($a, $b) x $d;
1407@e = ($a, $b, $c) x $d;
1408@e = ($a, 1) x $d;
1409####
1410# @_ with padrange
1411my($a, $b, $c) = @_;
1412####
1413# SKIP ?$] < 5.017004 && "lexical subs not implemented on this Perl version"
1414# TODO unimplemented in B::Deparse; RT #116553
1415# lexical subroutine
1416use feature 'lexical_subs';
1417no warnings "experimental::lexical_subs";
1418my sub f {}
1419print f();
1420####
1421# SKIP ?$] < 5.017004 && "lexical subs not implemented on this Perl version"
1422# TODO unimplemented in B::Deparse; RT #116553
1423# lexical "state" subroutine
1424use feature 'state', 'lexical_subs';
1425no warnings 'experimental::lexical_subs';
1426state sub f {}
1427print f();
1428####
1429# Elements of %# should not be confused with $#{ array }
1430() = ${#}{'foo'};
1431####
1432# [perl #121050] Prototypes with whitespace
1433sub _121050(\$ \$) { }
1434_121050($a,$b);
1435sub _121050empty( ) {}
1436() = _121050empty() + 1;
1437>>>>
1438_121050 $a, $b;
1439() = _121050empty + 1;
1440####
1441# ensure aelemfast works in the range -128..127 and that there's no
1442# funky edge cases
1443my $x;
1444no strict 'vars';
1445$x = $a[-256] + $a[-255] + $a[-129] + $a[-128] + $a[-127] + $a[-1] + $a[0];
1446$x = $a[1] + $a[126] + $a[127] + $a[128] + $a[255] + $a[256];
1447my @b;
1448$x = $b[-256] + $b[-255] + $b[-129] + $b[-128] + $b[-127] + $b[-1] + $b[0];
1449$x = $b[1] + $b[126] + $b[127] + $b[128] + $b[255] + $b[256];
1450