This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
eliminate HINT_HH_FOR_EVAL
[perl5.git] / t / comp / parser.t
1 #!./perl
2
3 # Checks if the parser behaves correctly in edge cases
4 # (including weird syntax errors)
5
6 BEGIN {
7     chdir 't' if -d 't';
8     @INC = '../lib';
9 }
10
11 require "./test.pl";
12 plan( tests => 56 );
13
14 eval '%@x=0;';
15 like( $@, qr/^Can't modify hash dereference in repeat \(x\)/, '%@x=0' );
16
17 # Bug 20010422.005
18 eval q{{s//${}/; //}};
19 like( $@, qr/syntax error/, 'syntax error, used to dump core' );
20
21 # Bug 20010528.007
22 eval q/"\x{"/;
23 like( $@, qr/^Missing right brace on \\x/,
24     'syntax error in string, used to dump core' );
25
26 eval q/"\N{"/;
27 like( $@, qr/^Missing right brace on \\N/,
28     'syntax error in string with incomplete \N' );
29 eval q/"\Nfoo"/;
30 like( $@, qr/^Missing braces on \\N/,
31     'syntax error in string with incomplete \N' );
32
33 eval "a.b.c.d.e.f;sub";
34 like( $@, qr/^Illegal declaration of anonymous subroutine/,
35     'found by Markov chain stress testing' );
36
37 # Bug 20010831.001
38 eval '($a, b) = (1, 2);';
39 like( $@, qr/^Can't modify constant item in list assignment/,
40     'bareword in list assignment' );
41
42 eval 'tie FOO, "Foo";';
43 like( $@, qr/^Can't modify constant item in tie /,
44     'tying a bareword causes a segfault in 5.6.1' );
45
46 eval 'undef foo';
47 like( $@, qr/^Can't modify constant item in undef operator /,
48     'undefing constant causes a segfault in 5.6.1 [ID 20010906.019]' );
49
50 eval 'read($bla, FILE, 1);';
51 like( $@, qr/^Can't modify constant item in read /,
52     'read($var, FILE, 1) segfaults on 5.6.1 [ID 20011025.054]' );
53
54 # This used to dump core (bug #17920)
55 eval q{ sub { sub { f1(f2();); my($a,$b,$c) } } };
56 like( $@, qr/error/, 'lexical block discarded by yacc' );
57
58 # bug #18573, used to corrupt memory
59 eval q{ "\c" };
60 like( $@, qr/^Missing control char name in \\c/, q("\c" string) );
61
62 eval q{ qq(foo$) };
63 like( $@, qr/Final \$ should be \\\$ or \$name/, q($ at end of "" string) );
64
65 # two tests for memory corruption problems in the said variables
66 # (used to dump core or produce strange results)
67
68 is( "\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Qa", "a", "PL_lex_casestack" );
69
70 eval {
71 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
72 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
73 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
74 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
75 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
76 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
77 };
78 is( $@, '', 'PL_lex_brackstack' );
79
80 {
81     # tests for bug #20716
82     undef $a;
83     undef @b;
84     my $a="A";
85     is("${a}{", "A{", "interpolation, qq//");
86     is("${a}[", "A[", "interpolation, qq//");
87     my @b=("B");
88     is("@{b}{", "B{", "interpolation, qq//");
89     is(qr/${a}{/, '(?-xism:A{)', "interpolation, qr//");
90     my $c = "A{";
91     $c =~ /${a}{/;
92     is($&, 'A{', "interpolation, m//");
93     $c =~ s/${a}{/foo/;
94     is($c, 'foo', "interpolation, s/...//");
95     $c =~ s/foo/${a}{/;
96     is($c, 'A{', "interpolation, s//.../");
97     is(<<"${a}{", "A{ A[ B{\n", "interpolation, here doc");
98 ${a}{ ${a}[ @{b}{
99 ${a}{
100 }
101
102 eval q{ sub a(;; &) { } a { } };
103 is($@, '', "';&' sub prototype confuses the lexer");
104
105 # Bug #21575
106 # ensure that the second print statement works, by playing a bit
107 # with the test output.
108 my %data = ( foo => "\n" );
109 print "#";
110 print(
111 $data{foo});
112 pass();
113
114 # Bug #21875
115 # { q.* => ... } should be interpreted as hash, not block
116
117 foreach my $line (split /\n/, <<'EOF')
118 1 { foo => 'bar' }
119 1 { qoo => 'bar' }
120 1 { q   => 'bar' }
121 1 { qq  => 'bar' }
122 0 { q,'bar', }
123 0 { q=bar= }
124 0 { qq=bar= }
125 1 { q=bar= => 'bar' }
126 EOF
127 {
128     my ($expect, $eval) = split / /, $line, 2;
129     my $result = eval $eval;
130     ok($@ eq  '', "eval $eval");
131     is(ref $result, $expect ? 'HASH' : '', $eval);
132 }
133
134 # Bug #24212
135 {
136     local $SIG{__WARN__} = sub { }; # silence mandatory warning
137     eval q{ my $x = -F 1; };
138     like( $@, qr/(?:syntax|parse) error .* near "F 1"/, "unknown filetest operators" );
139     is(
140         eval q{ sub F { 42 } -F 1 },
141         '-42',
142         '-F calls the F function'
143     );
144 }
145
146 # Bug #24762
147 {
148     eval q{ *foo{CODE} ? 1 : 0 };
149     is( $@, '', "glob subscript in conditional" );
150 }
151
152 # Bug #25824
153 {
154     eval q{ sub f { @a=@b=@c;  {use} } };
155     like( $@, qr/syntax error/, "use without body" );
156 }
157
158 # Bug #27024
159 {
160     # this used to segfault (because $[=1 is optimized away to a null block)
161     my $x;
162     $[ = 1 while $x;
163     pass();
164     $[ = 0; # restore the original value for less side-effects
165 }
166
167 # [perl #2738] perl segfautls on input
168 {
169     eval q{ sub _ <> {} };
170     like($@, qr/Illegal declaration of subroutine main::_/, "readline operator as prototype");
171
172     eval q{ $s = sub <> {} };
173     like($@, qr/Illegal declaration of anonymous subroutine/, "readline operator as prototype");
174
175     eval q{ sub _ __FILE__ {} };
176     like($@, qr/Illegal declaration of subroutine main::_/, "__FILE__ as prototype");
177 }
178
179 # [perl #36313] perl -e "1for$[=0" crash
180 {
181     my $x;
182     $x = 1 for ($[) = 0;
183     pass('optimized assignment to $[ used to segfault in list context');
184     if ($[ = 0) { $x = 1 }
185     pass('optimized assignment to $[ used to segfault in scalar context');
186     $x = ($[=2.4);
187     is($x, 2, 'scalar assignment to $[ behaves like other variables');
188     $x = (($[) = 0);
189     is($x, 1, 'list assignment to $[ behaves like other variables');
190     $x = eval q{ ($[, $x) = (0) };
191     like($@, qr/That use of \$\[ is unsupported/,
192              'cannot assign to $[ in a list');
193     eval q{ ($[) = (0, 1) };
194     like($@, qr/That use of \$\[ is unsupported/,
195              'cannot assign list of >1 elements to $[');
196     eval q{ ($[) = () };
197     like($@, qr/That use of \$\[ is unsupported/,
198              'cannot assign list of <1 elements to $[');
199 }