This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #71870] Use of uninitialized value in bitwise and B::Deparse
[perl5.git] / dist / B-Deparse / t / deparse.t
CommitLineData
87a42246
MS
1#!./perl
2
3BEGIN {
62a6bb71 4 unshift @INC, 't';
9cd8f857
NC
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 }
87a42246
MS
10}
11
87a42246
MS
12use warnings;
13use strict;
e9c69003
NC
14BEGIN {
15 # BEGIN block is acutally a subroutine :-)
16 return unless $] > 5.009;
17 require feature;
18 feature->import(':5.10');
19}
06fc6867 20use Test::More tests => 84;
1bb3cfc5 21use Config ();
87a42246
MS
22
23use B::Deparse;
09d856fb
CK
24my $deparse = B::Deparse->new();
25ok($deparse);
87a42246
MS
26
27# Tell B::Deparse about our ambient pragmas
0ced6c29
RGS
28{ my ($hint_bits, $warning_bits, $hinthash);
29 BEGIN { ($hint_bits, $warning_bits, $hinthash) = ($^H, ${^WARNING_BITS}, \%^H); }
87a42246
MS
30 $deparse->ambient_pragmas (
31 hint_bits => $hint_bits,
32 warning_bits => $warning_bits,
0ced6c29
RGS
33 '$[' => 0 + $[,
34 '%^H' => $hinthash,
87a42246
MS
35 );
36}
37
ad46c0be
RH
38$/ = "\n####\n";
39while (<DATA>) {
40 chomp;
e9c69003
NC
41 # This code is pinched from the t/lib/common.pl for TODO.
42 # It's not clear how to avoid duplication
b871937f
NC
43 # Now tweaked a bit to do skip or todo
44 my %reason;
45 foreach my $what (qw(skip todo)) {
46 s/^#\s*\U$what\E\s*(.*)\n//m and $reason{$what} = $1;
47 # If the SKIP reason starts ? then it's taken as a code snippet to
48 # evaluate. This provides the flexibility to have conditional SKIPs
49 if ($reason{$what} && $reason{$what} =~ s/^\?//) {
50 my $temp = eval $reason{$what};
51 if ($@) {
52 die "# In \U$what\E code reason:\n# $reason{$what}\n$@";
53 }
54 $reason{$what} = $temp;
e9c69003 55 }
e9c69003
NC
56 }
57
4a4b8592 58 s/^\s*#\s*(.*)$//mg;
ec59cdf2 59 my ($num, $testname) = $1 =~ m/(\d+)\s*(.*)/;
e9c69003 60
b871937f 61 if ($reason{skip}) {
e9c69003 62 # Like this to avoid needing a label SKIP:
b871937f 63 Test::More->builder->skip($reason{skip});
e9c69003
NC
64 next;
65 }
66
ad46c0be
RH
67 my ($input, $expected);
68 if (/(.*)\n>>>>\n(.*)/s) {
69 ($input, $expected) = ($1, $2);
70 }
71 else {
72 ($input, $expected) = ($_, $_);
73 }
87a42246 74
ad46c0be 75 my $coderef = eval "sub {$input}";
87a42246 76
ad46c0be 77 if ($@) {
ec59cdf2
RGS
78 diag("$num deparsed: $@");
79 ok(0, $testname);
ad46c0be
RH
80 }
81 else {
82 my $deparsed = $deparse->coderef2text( $coderef );
31c6271a
RD
83 my $regex = $expected;
84 $regex =~ s/(\S+)/\Q$1/g;
85 $regex =~ s/\s+/\\s+/g;
86 $regex = '^\{\s*' . $regex . '\s*\}$';
b871937f 87
4a4b8592 88 local $::TODO = $reason{todo};
ec59cdf2 89 like($deparsed, qr/$regex/, $testname);
87a42246 90 }
87a42246
MS
91}
92
87a42246 93use constant 'c', 'stuff';
09d856fb 94is((eval "sub ".$deparse->coderef2text(\&c))->(), 'stuff');
87a42246 95
09d856fb
CK
96my $a = 0;
97is("{\n (-1) ** \$a;\n}", $deparse->coderef2text(sub{(-1) ** $a }));
87a42246 98
d989cdac
SM
99use constant cr => ['hello'];
100my $string = "sub " . $deparse->coderef2text(\&cr);
0707d6cc
NC
101my $val = (eval $string)->() or diag $string;
102is(ref($val), 'ARRAY');
103is($val->[0], 'hello');
87a42246 104
87a42246 105my $path = join " ", map { qq["-I$_"] } @INC;
87a42246 106
7cde0a5f 107$a = `$^X $path "-MO=Deparse" -anlwi.bak -e 1 2>&1`;
e69a2255 108$a =~ s/-e syntax OK\n//g;
d2bc402e 109$a =~ s/.*possible typo.*\n//; # Remove warning line
87a42246
MS
110$a =~ s{\\340\\242}{\\s} if (ord("\\") == 224); # EBCDIC, cp 1047 or 037
111$a =~ s{\\274\\242}{\\s} if (ord("\\") == 188); # $^O eq 'posix-bc'
112$b = <<'EOF';
d2bc402e
RGS
113BEGIN { $^I = ".bak"; }
114BEGIN { $^W = 1; }
115BEGIN { $/ = "\n"; $\ = "\n"; }
87a42246
MS
116LINE: while (defined($_ = <ARGV>)) {
117 chomp $_;
f86ea535 118 our(@F) = split(' ', $_, 0);
87a42246
MS
119 '???';
120}
87a42246 121EOF
09d856fb 122is($a, $b);
87a42246 123
5b4ee549
NC
124$a = `$^X $path "-MO=Deparse" -e "use constant PI => 4" 2>&1`;
125$a =~ s/-e syntax OK\n//g;
126is($a, "use constant ('PI', 4);\n",
127 "Proxy Constant Subroutines must not show up as (incorrect) prototypes");
128
579a54dc 129#Re: perlbug #35857, patch #24505
b3980c39
YO
130#handle warnings::register-ed packages properly.
131package B::Deparse::Wrapper;
132use strict;
133use warnings;
134use warnings::register;
135sub getcode {
579a54dc 136 my $deparser = B::Deparse->new();
b3980c39
YO
137 return $deparser->coderef2text(shift);
138}
139
2990415a
FR
140package Moo;
141use overload '0+' => sub { 42 };
142
b3980c39
YO
143package main;
144use strict;
145use warnings;
71c4dbc3 146use constant GLIPP => 'glipp';
2990415a
FR
147use constant PI => 4;
148use constant OVERLOADED_NUMIFICATION => bless({}, 'Moo');
3779476a 149use Fcntl qw/O_TRUNC O_APPEND O_EXCL/;
aaf9c2b2 150BEGIN { delete $::Fcntl::{O_APPEND}; }
2990415a 151use POSIX qw/O_CREAT/;
b3980c39 152sub test {
579a54dc
RGS
153 my $val = shift;
154 my $res = B::Deparse::Wrapper::getcode($val);
09d856fb 155 like( $res, qr/use warnings/);
b3980c39
YO
156}
157my ($q,$p);
158my $x=sub { ++$q,++$p };
159test($x);
160eval <<EOFCODE and test($x);
161 package bar;
162 use strict;
163 use warnings;
164 use warnings::register;
165 package main;
166 1
167EOFCODE
168
ad46c0be 169__DATA__
14a55f98 170# 2
ad46c0be
RH
1711;
172####
14a55f98 173# 3
ad46c0be
RH
174{
175 no warnings;
176 '???';
177 2;
178}
179####
14a55f98 180# 4
ad46c0be
RH
181my $test;
182++$test and $test /= 2;
183>>>>
184my $test;
185$test /= 2 if ++$test;
186####
14a55f98 187# 5
ad46c0be
RH
188-((1, 2) x 2);
189####
14a55f98 190# 6
ad46c0be
RH
191{
192 my $test = sub : lvalue {
193 my $x;
194 }
195 ;
196}
197####
14a55f98 198# 7
ad46c0be
RH
199{
200 my $test = sub : method {
201 my $x;
202 }
203 ;
204}
205####
14a55f98 206# 8
8e5dadda
NC
207# Was sub : locked method { ... }
208# This number could be re-used.
ad46c0be 209####
14a55f98 210# 9
87a42246 211{
ad46c0be 212 234;
f99a63a2 213}
ad46c0be
RH
214continue {
215 123;
87a42246 216}
ce4e655d 217####
14a55f98 218# 10
ce4e655d
RH
219my $x;
220print $main::x;
221####
14a55f98 222# 11
ce4e655d
RH
223my @x;
224print $main::x[1];
14a55f98
RH
225####
226# 12
227my %x;
228$x{warn()};
ad8caead
RGS
229####
230# 13
231my $foo;
232$_ .= <ARGV> . <$foo>;
cef22867
JH
233####
234# 14
235my $foo = "Ab\x{100}\200\x{200}\377Cd\000Ef\x{1000}\cA\x{2000}\cZ";
4ae52e81
RGS
236####
237# 15
238s/x/'y';/e;
241416b8
DM
239####
240# 16 - various lypes of loop
241{ my $x; }
242####
243# 17
244while (1) { my $k; }
245####
246# 18
247my ($x,@a);
248$x=1 for @a;
249>>>>
250my($x, @a);
0bb5f065 251$x = 1 foreach (@a);
241416b8
DM
252####
253# 19
254for (my $i = 0; $i < 2;) {
255 my $z = 1;
256}
257####
258# 20
259for (my $i = 0; $i < 2; ++$i) {
260 my $z = 1;
261}
262####
263# 21
264for (my $i = 0; $i < 2; ++$i) {
265 my $z = 1;
266}
267####
268# 22
269my $i;
270while ($i) { my $z = 1; } continue { $i = 99; }
271####
272# 23
09d856fb 273foreach my $i (1, 2) {
241416b8
DM
274 my $z = 1;
275}
276####
277# 24
278my $i;
279foreach $i (1, 2) {
280 my $z = 1;
281}
282####
283# 25
284my $i;
285foreach my $i (1, 2) {
286 my $z = 1;
287}
288####
289# 26
290foreach my $i (1, 2) {
291 my $z = 1;
292}
293####
294# 27
295foreach our $i (1, 2) {
296 my $z = 1;
297}
298####
299# 28
300my $i;
301foreach our $i (1, 2) {
302 my $z = 1;
303}
3ac6e0f9
RGS
304####
305# 29
306my @x;
307print reverse sort(@x);
308####
309# 30
310my @x;
311print((sort {$b cmp $a} @x));
312####
313# 31
314my @x;
315print((reverse sort {$b <=> $a} @x));
36d57d93
RGS
316####
317# 32
318our @a;
319print $_ foreach (reverse @a);
aae53c41 320####
579a54dc 321# 33
aae53c41
RGS
322our @a;
323print $_ foreach (reverse 1, 2..5);
f86ea535
SM
324####
325# 34 (bug #38684)
326our @ary;
327@ary = split(' ', 'foo', 0);
31c6271a
RD
328####
329# 35 (bug #40055)
330do { () };
331####
332# 36 (ibid.)
333do { my $x = 1; $x };
d9002312
SM
334####
335# 37 <20061012113037.GJ25805@c4.convolution.nl>
336my $f = sub {
337 +{[]};
338} ;
8b2d6640
FC
339####
340# 38 (bug #43010)
341'!@$%'->();
342####
343# 39 (ibid.)
344::();
345####
346# 40 (ibid.)
347'::::'->();
348####
349# 41 (ibid.)
350&::::;
09d856fb
CK
351####
352# 42
353my $bar;
354'Foo'->$bar('orz');
355####
356# 43
357'Foo'->bar('orz');
358####
359# 44
360'Foo'->bar;
0ced6c29 361####
e9c69003 362# SKIP ?$] < 5.010 && "say not implemented on this Perl version"
7ddd1a01
NC
363# 45 say
364say 'foo';
365####
e9c69003 366# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
7ddd1a01 367# 46 state vars
0ced6c29
RGS
368state $x = 42;
369####
e9c69003 370# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
7ddd1a01
NC
371# 47 state var assignment
372{
373 my $y = (state $x = 42);
374}
375####
e9c69003 376# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
7ddd1a01
NC
377# 48 state vars in anoymous subroutines
378$a = sub {
379 state $x;
380 return $x++;
381}
382;
644741fd
NC
383####
384# SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
385# 49 each @array;
386each @ARGV;
387each @$a;
388####
389# SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
390# 50 keys @array; values @array
391keys @$a if keys @ARGV;
392values @ARGV if values @$a;
35925e80 393####
43b09ad7 394# 51 Anonymous arrays and hashes, and references to them
35925e80
RGS
395my $a = {};
396my $b = \{};
397my $c = [];
398my $d = \[];
9210de83
FR
399####
400# SKIP ?$] < 5.010 && "smartmatch and given/when not implemented on this Perl version"
43b09ad7 401# 52 implicit smartmatch in given/when
9210de83
FR
402given ('foo') {
403 when ('bar') { continue; }
404 when ($_ ~~ 'quux') { continue; }
405 default { 0; }
406}
7ecdd211
PJ
407####
408# 53 conditions in elsifs (regression in change #33710 which fixed bug #37302)
409if ($a) { x(); }
410elsif ($b) { x(); }
411elsif ($a and $b) { x(); }
412elsif ($a or $b) { x(); }
413else { x(); }
03b22f1b
RGS
414####
415# 54 interpolation in regexps
416my($y, $t);
417/x${y}z$t/;
227375e1 418####
4a4b8592 419# TODO new undocumented cpan-bug #33708
227375e1
RU
420# 55 (cpan-bug #33708)
421%{$_ || {}}
422####
4a4b8592 423# TODO hash constants not yet fixed
227375e1
RU
424# 56 (cpan-bug #33708)
425use constant H => { "#" => 1 }; H->{"#"}
426####
4a4b8592 427# TODO optimized away 0 not yet fixed
227375e1
RU
428# 57 (cpan-bug #33708)
429foreach my $i (@_) { 0 }
edbe35ea
VP
430####
431# 58 tests with not, not optimized
07f3cdf5 432my $c;
edbe35ea
VP
433x() unless $a;
434x() if not $a and $b;
435x() if $a and not $b;
436x() unless not $a and $b;
437x() unless $a and not $b;
438x() if not $a or $b;
439x() if $a or not $b;
440x() unless not $a or $b;
441x() unless $a or not $b;
07f3cdf5
VP
442x() if $a and not $b and $c;
443x() if not $a and $b and not $c;
444x() unless $a and not $b and $c;
445x() unless not $a and $b and not $c;
446x() if $a or not $b or $c;
447x() if not $a or $b or not $c;
448x() unless $a or not $b or $c;
449x() unless not $a or $b or not $c;
edbe35ea
VP
450####
451# 59 tests with not, optimized
07f3cdf5 452my $c;
edbe35ea
VP
453x() if not $a;
454x() unless not $a;
455x() if not $a and not $b;
456x() unless not $a and not $b;
457x() if not $a or not $b;
458x() unless not $a or not $b;
07f3cdf5
VP
459x() if not $a and not $b and $c;
460x() unless not $a and not $b and $c;
461x() if not $a or not $b or $c;
462x() unless not $a or not $b or $c;
463x() if not $a and not $b and not $c;
464x() unless not $a and not $b and not $c;
465x() if not $a or not $b or not $c;
466x() unless not $a or not $b or not $c;
467x() unless not $a or not $b or not $c;
edbe35ea 468>>>>
07f3cdf5 469my $c;
edbe35ea
VP
470x() unless $a;
471x() if $a;
472x() unless $a or $b;
473x() if $a or $b;
474x() unless $a and $b;
07f3cdf5
VP
475x() if $a and $b;
476x() if not $a || $b and $c;
477x() unless not $a || $b and $c;
478x() if not $a && $b or $c;
479x() unless not $a && $b or $c;
480x() unless $a or $b or $c;
481x() if $a or $b or $c;
482x() unless $a and $b and $c;
483x() if $a and $b and $c;
484x() unless not $a && $b && $c;
71c4dbc3
VP
485####
486# 60 tests that should be constant folded
487x() if 1;
488x() if GLIPP;
489x() if !GLIPP;
490x() if GLIPP && GLIPP;
491x() if !GLIPP || GLIPP;
492x() if do { GLIPP };
493x() if do { no warnings 'void'; 5; GLIPP };
494x() if do { !GLIPP };
495if (GLIPP) { x() } else { z() }
496if (!GLIPP) { x() } else { z() }
497if (GLIPP) { x() } elsif (GLIPP) { z() }
498if (!GLIPP) { x() } elsif (GLIPP) { z() }
499if (GLIPP) { x() } elsif (!GLIPP) { z() }
500if (!GLIPP) { x() } elsif (!GLIPP) { z() }
501if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (GLIPP) { t() }
502if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
503if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
504>>>>
505x();
506x();
507'???';
508x();
509x();
510x();
511x();
512do {
513 '???'
514};
515do {
516 x()
517};
518do {
519 z()
520};
521do {
522 x()
523};
524do {
525 z()
526};
527do {
528 x()
529};
530'???';
531do {
532 t()
533};
534'???';
535!1;
536####
ac0f1413 537# TODO ? $Config::Config{useithreads} && "doesn't work with threads"
71c4dbc3 538# 61 tests that shouldn't be constant folded
ac0f1413
NC
539# It might be fundamentally impossible to make this work on ithreads, in which
540# case the TODO should become a SKIP
71c4dbc3
VP
541x() if $a;
542if ($a == 1) { x() } elsif ($b == 2) { z() }
543if (do { foo(); GLIPP }) { x() }
544if (do { $a++; GLIPP }) { x() }
545>>>>
546x() if $a;
547if ($a == 1) { x(); } elsif ($b == 2) { z(); }
2990415a
FR
548if (do { foo(); GLIPP }) { x(); }
549if (do { ++$a; GLIPP }) { x(); }
550####
551# 62 tests for deparsing constants
552warn PI;
553####
554# 63 tests for deparsing imported constants
3779476a 555warn O_TRUNC;
2990415a
FR
556####
557# 64 tests for deparsing re-exported constants
558warn O_CREAT;
559####
560# 65 tests for deparsing imported constants that got deleted from the original namespace
aaf9c2b2 561warn O_APPEND;
2990415a 562####
ac0f1413 563# TODO ? $Config::Config{useithreads} && "doesn't work with threads"
2990415a 564# 66 tests for deparsing constants which got turned into full typeglobs
ac0f1413
NC
565# It might be fundamentally impossible to make this work on ithreads, in which
566# case the TODO should become a SKIP
2990415a
FR
567warn O_EXCL;
568eval '@Fcntl::O_EXCL = qw/affe tiger/;';
569warn O_EXCL;
570####
571# 67 tests for deparsing of blessed constant with overloaded numification
572warn OVERLOADED_NUMIFICATION;
79289e05
NC
573####
574# TODO Only strict 'refs' currently supported
575# 68 strict
576no strict;
577$x;
578####
579# TODO Subsets of warnings could be encoded textually, rather than as bitflips.
580no warnings 'deprecated';
581my $x;
582####
583# TODO Better test for CPAN #33708 - the deparsed code has different behaviour
584use strict;
585no warnings;
586
587foreach (0..3) {
588 my $x = 2;
589 {
590 my $x if 0;
591 print ++$x, "\n";
592 }
593}
d83f38d8
NC
594####
595my $pi = 4;
596####
597no warnings;
598my $pi := 4;
599>>>>
600no warnings;
601my $pi = 4;
602####
603my $pi : = 4;
604>>>>
605my $pi = 4;
689e417f
VP
606####
607our @a;
608my @b;
609@a = sort @a;
610@b = sort @b;
611();
612####
613our @a;
614my @b;
615@a = reverse @a;
616@b = reverse @b;
617();
06fc6867
VP
618####
619my($r, $s, @a);
620@a = split(/foo/, $s, 0);
621$r = qr/foo/;
622@a = split(/$r/, $s, 0);
623();