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
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;
14BEGIN {
15 # BEGIN block is acutally a subroutine :-)
16 return unless $] > 5.009;
17 require feature;
18 feature->import(':5.10');
19}
20use Test::More tests => 84;
21use Config ();
22
23use B::Deparse;
24my $deparse = B::Deparse->new();
25ok($deparse);
26
27# Tell B::Deparse about our ambient pragmas
28{ my ($hint_bits, $warning_bits, $hinthash);
29 BEGIN { ($hint_bits, $warning_bits, $hinthash) = ($^H, ${^WARNING_BITS}, \%^H); }
30 $deparse->ambient_pragmas (
31 hint_bits => $hint_bits,
32 warning_bits => $warning_bits,
33 '$[' => 0 + $[,
34 '%^H' => $hinthash,
35 );
36}
37
38$/ = "\n####\n";
39while (<DATA>) {
40 chomp;
41 # This code is pinched from the t/lib/common.pl for TODO.
42 # It's not clear how to avoid duplication
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;
55 }
56 }
57
58 s/^\s*#\s*(.*)$//mg;
59 my ($num, $testname) = $1 =~ m/(\d+)\s*(.*)/;
60
61 if ($reason{skip}) {
62 # Like this to avoid needing a label SKIP:
63 Test::More->builder->skip($reason{skip});
64 next;
65 }
66
67 my ($input, $expected);
68 if (/(.*)\n>>>>\n(.*)/s) {
69 ($input, $expected) = ($1, $2);
70 }
71 else {
72 ($input, $expected) = ($_, $_);
73 }
74
75 my $coderef = eval "sub {$input}";
76
77 if ($@) {
78 diag("$num deparsed: $@");
79 ok(0, $testname);
80 }
81 else {
82 my $deparsed = $deparse->coderef2text( $coderef );
83 my $regex = $expected;
84 $regex =~ s/(\S+)/\Q$1/g;
85 $regex =~ s/\s+/\\s+/g;
86 $regex = '^\{\s*' . $regex . '\s*\}$';
87
88 local $::TODO = $reason{todo};
89 like($deparsed, qr/$regex/, $testname);
90 }
91}
92
93use constant 'c', 'stuff';
94is((eval "sub ".$deparse->coderef2text(\&c))->(), 'stuff');
95
96my $a = 0;
97is("{\n (-1) ** \$a;\n}", $deparse->coderef2text(sub{(-1) ** $a }));
98
99use constant cr => ['hello'];
100my $string = "sub " . $deparse->coderef2text(\&cr);
101my $val = (eval $string)->() or diag $string;
102is(ref($val), 'ARRAY');
103is($val->[0], 'hello');
104
105my $path = join " ", map { qq["-I$_"] } @INC;
106
107$a = `$^X $path "-MO=Deparse" -anlwi.bak -e 1 2>&1`;
108$a =~ s/-e syntax OK\n//g;
109$a =~ s/.*possible typo.*\n//; # Remove warning line
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';
113BEGIN { $^I = ".bak"; }
114BEGIN { $^W = 1; }
115BEGIN { $/ = "\n"; $\ = "\n"; }
116LINE: while (defined($_ = <ARGV>)) {
117 chomp $_;
118 our(@F) = split(' ', $_, 0);
119 '???';
120}
121EOF
122is($a, $b);
123
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
129#Re: perlbug #35857, patch #24505
130#handle warnings::register-ed packages properly.
131package B::Deparse::Wrapper;
132use strict;
133use warnings;
134use warnings::register;
135sub getcode {
136 my $deparser = B::Deparse->new();
137 return $deparser->coderef2text(shift);
138}
139
140package Moo;
141use overload '0+' => sub { 42 };
142
143package main;
144use strict;
145use warnings;
146use constant GLIPP => 'glipp';
147use constant PI => 4;
148use constant OVERLOADED_NUMIFICATION => bless({}, 'Moo');
149use Fcntl qw/O_TRUNC O_APPEND O_EXCL/;
150BEGIN { delete $::Fcntl::{O_APPEND}; }
151use POSIX qw/O_CREAT/;
152sub test {
153 my $val = shift;
154 my $res = B::Deparse::Wrapper::getcode($val);
155 like( $res, qr/use warnings/);
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
169__DATA__
170# 2
1711;
172####
173# 3
174{
175 no warnings;
176 '???';
177 2;
178}
179####
180# 4
181my $test;
182++$test and $test /= 2;
183>>>>
184my $test;
185$test /= 2 if ++$test;
186####
187# 5
188-((1, 2) x 2);
189####
190# 6
191{
192 my $test = sub : lvalue {
193 my $x;
194 }
195 ;
196}
197####
198# 7
199{
200 my $test = sub : method {
201 my $x;
202 }
203 ;
204}
205####
206# 8
207# Was sub : locked method { ... }
208# This number could be re-used.
209####
210# 9
211{
212 234;
213}
214continue {
215 123;
216}
217####
218# 10
219my $x;
220print $main::x;
221####
222# 11
223my @x;
224print $main::x[1];
225####
226# 12
227my %x;
228$x{warn()};
229####
230# 13
231my $foo;
232$_ .= <ARGV> . <$foo>;
233####
234# 14
235my $foo = "Ab\x{100}\200\x{200}\377Cd\000Ef\x{1000}\cA\x{2000}\cZ";
236####
237# 15
238s/x/'y';/e;
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);
251$x = 1 foreach (@a);
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
273foreach my $i (1, 2) {
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}
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));
316####
317# 32
318our @a;
319print $_ foreach (reverse @a);
320####
321# 33
322our @a;
323print $_ foreach (reverse 1, 2..5);
324####
325# 34 (bug #38684)
326our @ary;
327@ary = split(' ', 'foo', 0);
328####
329# 35 (bug #40055)
330do { () };
331####
332# 36 (ibid.)
333do { my $x = 1; $x };
334####
335# 37 <20061012113037.GJ25805@c4.convolution.nl>
336my $f = sub {
337 +{[]};
338} ;
339####
340# 38 (bug #43010)
341'!@$%'->();
342####
343# 39 (ibid.)
344::();
345####
346# 40 (ibid.)
347'::::'->();
348####
349# 41 (ibid.)
350&::::;
351####
352# 42
353my $bar;
354'Foo'->$bar('orz');
355####
356# 43
357'Foo'->bar('orz');
358####
359# 44
360'Foo'->bar;
361####
362# SKIP ?$] < 5.010 && "say not implemented on this Perl version"
363# 45 say
364say 'foo';
365####
366# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
367# 46 state vars
368state $x = 42;
369####
370# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
371# 47 state var assignment
372{
373 my $y = (state $x = 42);
374}
375####
376# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
377# 48 state vars in anoymous subroutines
378$a = sub {
379 state $x;
380 return $x++;
381}
382;
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;
393####
394# 51 Anonymous arrays and hashes, and references to them
395my $a = {};
396my $b = \{};
397my $c = [];
398my $d = \[];
399####
400# SKIP ?$] < 5.010 && "smartmatch and given/when not implemented on this Perl version"
401# 52 implicit smartmatch in given/when
402given ('foo') {
403 when ('bar') { continue; }
404 when ($_ ~~ 'quux') { continue; }
405 default { 0; }
406}
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(); }
414####
415# 54 interpolation in regexps
416my($y, $t);
417/x${y}z$t/;
418####
419# TODO new undocumented cpan-bug #33708
420# 55 (cpan-bug #33708)
421%{$_ || {}}
422####
423# TODO hash constants not yet fixed
424# 56 (cpan-bug #33708)
425use constant H => { "#" => 1 }; H->{"#"}
426####
427# TODO optimized away 0 not yet fixed
428# 57 (cpan-bug #33708)
429foreach my $i (@_) { 0 }
430####
431# 58 tests with not, not optimized
432my $c;
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;
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;
450####
451# 59 tests with not, optimized
452my $c;
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;
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;
468>>>>
469my $c;
470x() unless $a;
471x() if $a;
472x() unless $a or $b;
473x() if $a or $b;
474x() unless $a and $b;
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;
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####
537# TODO ? $Config::Config{useithreads} && "doesn't work with threads"
538# 61 tests that shouldn't be constant folded
539# It might be fundamentally impossible to make this work on ithreads, in which
540# case the TODO should become a SKIP
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(); }
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
555warn O_TRUNC;
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
561warn O_APPEND;
562####
563# TODO ? $Config::Config{useithreads} && "doesn't work with threads"
564# 66 tests for deparsing constants which got turned into full typeglobs
565# It might be fundamentally impossible to make this work on ithreads, in which
566# case the TODO should become a SKIP
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;
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}
594####
595my $pi = 4;
596####
597no warnings;
598my $pi := 4;
599>>>>
600no warnings;
601my $pi = 4;
602####
603my $pi : = 4;
604>>>>
605my $pi = 4;
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();
618####
619my($r, $s, @a);
620@a = split(/foo/, $s, 0);
621$r = qr/foo/;
622@a = split(/$r/, $s, 0);
623();