This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Don't use require in comp/retainedlines.t, as require isn't tested yet.
[perl5.git] / t / comp / parser.t
CommitLineData
923be969
RGS
1#!./perl
2
3# Checks if the parser behaves correctly in edge cases
4# (including weird syntax errors)
5
6BEGIN {
7 chdir 't' if -d 't';
8 @INC = '../lib';
9}
10
768fd157 11BEGIN { require "./test.pl"; }
26b6dc3f 12plan( tests => 112 );
f15b33d3
RGS
13
14eval '%@x=0;';
15like( $@, qr/^Can't modify hash dereference in repeat \(x\)/, '%@x=0' );
16
17# Bug 20010422.005
18eval q{{s//${}/; //}};
19like( $@, qr/syntax error/, 'syntax error, used to dump core' );
20
21# Bug 20010528.007
22eval q/"\x{"/;
23like( $@, qr/^Missing right brace on \\x/,
24 'syntax error in string, used to dump core' );
25
fd7808cb
RGS
26eval q/"\N{"/;
27like( $@, qr/^Missing right brace on \\N/,
28 'syntax error in string with incomplete \N' );
29eval q/"\Nfoo"/;
30like( $@, qr/^Missing braces on \\N/,
31 'syntax error in string with incomplete \N' );
32
f15b33d3
RGS
33eval "a.b.c.d.e.f;sub";
34like( $@, qr/^Illegal declaration of anonymous subroutine/,
35 'found by Markov chain stress testing' );
36
37# Bug 20010831.001
38eval '($a, b) = (1, 2);';
39like( $@, qr/^Can't modify constant item in list assignment/,
40 'bareword in list assignment' );
41
42eval 'tie FOO, "Foo";';
43like( $@, qr/^Can't modify constant item in tie /,
44 'tying a bareword causes a segfault in 5.6.1' );
45
46eval 'undef foo';
47like( $@, qr/^Can't modify constant item in undef operator /,
48 'undefing constant causes a segfault in 5.6.1 [ID 20010906.019]' );
49
50eval 'read($bla, FILE, 1);';
51like( $@, qr/^Can't modify constant item in read /,
52 'read($var, FILE, 1) segfaults on 5.6.1 [ID 20011025.054]' );
923be969
RGS
53
54# This used to dump core (bug #17920)
55eval q{ sub { sub { f1(f2();); my($a,$b,$c) } } };
f15b33d3 56like( $@, qr/error/, 'lexical block discarded by yacc' );
961ce445
RGS
57
58# bug #18573, used to corrupt memory
59eval q{ "\c" };
60like( $@, qr/^Missing control char name in \\c/, q("\c" string) );
8edd5f42 61
a40a317b
RGS
62eval q{ qq(foo$) };
63like( $@, qr/Final \$ should be \\\$ or \$name/, q($ at end of "" string) );
64
8edd5f42
RGS
65# two tests for memory corruption problems in the said variables
66# (used to dump core or produce strange results)
67
68is( "\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Q\Qa", "a", "PL_lex_casestack" );
69
70eval {
71{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
72{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
73{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
74}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
75}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
76}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
77};
78is( $@, '', 'PL_lex_brackstack' );
437fd210
AE
79
80{
7df0d042
AE
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}{
437fd210 100}
4a202259 101
0f5d0394
AE
102eval q{ sub a(;; &) { } a { } };
103is($@, '', "';&' sub prototype confuses the lexer");
104
4a202259
AE
105# Bug #21575
106# ensure that the second print statement works, by playing a bit
107# with the test output.
108my %data = ( foo => "\n" );
109print "#";
110print(
111$data{foo});
112pass();
abc667d1
DM
113
114# Bug #21875
115# { q.* => ... } should be interpreted as hash, not block
116
117foreach my $line (split /\n/, <<'EOF')
1181 { foo => 'bar' }
1191 { qoo => 'bar' }
1201 { q => 'bar' }
1211 { qq => 'bar' }
1220 { q,'bar', }
1230 { q=bar= }
1240 { qq=bar= }
1251 { q=bar= => 'bar' }
126EOF
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}
3cf7b4c4
RGS
133
134# Bug #24212
135{
136 local $SIG{__WARN__} = sub { }; # silence mandatory warning
137 eval q{ my $x = -F 1; };
250d67eb 138 like( $@, qr/(?i:syntax|parse) error .* near "F 1"/, "unknown filetest operators" );
3cf7b4c4
RGS
139 is(
140 eval q{ sub F { 42 } -F 1 },
141 '-42',
142 '-F calls the F function'
143 );
144}
1c03aa9b
RGS
145
146# Bug #24762
147{
148 eval q{ *foo{CODE} ? 1 : 0 };
149 is( $@, '', "glob subscript in conditional" );
150}
500bedb6
DM
151
152# Bug #25824
153{
154 eval q{ sub f { @a=@b=@c; {use} } };
155 like( $@, qr/syntax error/, "use without body" );
156}
e1548254
RGS
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();
1bca678f 164 $[ = 0; # restore the original value for less side-effects
e1548254 165}
8e742a20
MHM
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}
dbfe47cf
RD
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}
917949e3
RGS
200
201# tests for "Bad name"
202eval q{ foo::$bar };
203like( $@, qr/Bad name after foo::/, 'Bad name after foo::' );
204eval q{ foo''bar };
205like( $@, qr/Bad name after foo'/, 'Bad name after foo\'' );
a6a8bb49
RGS
206
207# test for ?: context error
208eval q{($a ? $x : ($y)) = 5};
209like( $@, qr/Assignment to both a list and a scalar/, 'Assignment to both a list and a scalar' );
2d997502
BC
210
211eval q{ s/x/#/e };
212is( $@, '', 'comments in s///e' );
2af555bf 213
718a7425 214# these five used to coredump because the op cleanup on parse error could
2af555bf
DM
215# be to the wrong pad
216
217eval q[
218 sub { our $a= 1;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;
219 sub { my $z
220];
221
222like($@, qr/Missing right curly/, 'nested sub syntax error' );
223
224eval q[
225 sub { my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$r);
226 sub { my $z
227];
228like($@, qr/Missing right curly/, 'nested sub syntax error 2' );
229
718a7425
DM
230eval q[
231 sub { our $a= 1;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;$a;
232 use DieDieDie;
233];
234
235like($@, qr/Can't locate DieDieDie.pm/, 'croak cleanup' );
236
237eval q[
238 sub { my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$r);
239 use DieDieDie;
240];
241
242like($@, qr/Can't locate DieDieDie.pm/, 'croak cleanup 2' );
243
244
245eval q[
246 my @a;
247 my ($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$r);
248 @a =~ s/a/b/; # compile-time error
249 use DieDieDie;
250];
251
252like($@, qr/Can't modify/, 'croak cleanup 3' );
253
7e5d8ed2
DM
254# these might leak, or have duplicate frees, depending on the bugginess of
255# the parser stack 'fail in reduce' cleanup code. They're here mainly as
256# something to be run under valgrind, with PERL_DESTRUCT_LEVEL=1.
257
258eval q[ BEGIN { } ] for 1..10;
259is($@, "", 'BEGIN 1' );
260
261eval q[ BEGIN { my $x; $x = 1 } ] for 1..10;
262is($@, "", 'BEGIN 2' );
263
264eval q[ BEGIN { \&foo1 } ] for 1..10;
265is($@, "", 'BEGIN 3' );
266
267eval q[ sub foo2 { } ] for 1..10;
268is($@, "", 'BEGIN 4' );
269
270eval q[ sub foo3 { my $x; $x=1 } ] for 1..10;
271is($@, "", 'BEGIN 5' );
272
273eval q[ BEGIN { die } ] for 1..10;
274like($@, qr/BEGIN failed--compilation aborted/, 'BEGIN 6' );
275
276eval q[ BEGIN {\&foo4; die } ] for 1..10;
277like($@, qr/BEGIN failed--compilation aborted/, 'BEGIN 7' );
44867030
NC
278
279# Add new tests HERE:
280
281# More awkward tests for #line. Keep these at the end, as they will screw
282# with sane line reporting for any other test failures
283
284sub check ($$$) {
285 my ($file, $line, $name) = @_;
286 my (undef, $got_file, $got_line) = caller;
287 like ($got_file, $file, "file of $name");
44867030
NC
288 is ($got_line, $line, "line of $name");
289}
290
291#line 3
292check(qr/parser\.t$/, 3, "bare line");
293
294# line 5
295check(qr/parser\.t$/, 5, "bare line with leading space");
296
297#line 7
298check(qr/parser\.t$/, 7, "trailing space still valid");
299
300# line 11
301check(qr/parser\.t$/, 11, "leading and trailing");
302
303# line 13
304check(qr/parser\.t$/, 13, "leading tab");
305
306#line 17
307check(qr/parser\.t$/, 17, "middle tab");
308
309#line 19
310check(qr/parser\.t$/, 19, "loadsaspaces");
311
312#line 23 KASHPRITZA
313check(qr/^KASHPRITZA$/, 23, "bare filename");
314
315#line 29 "KAHEEEE"
316check(qr/^KAHEEEE$/, 29, "filename in quotes");
317
318#line 31 "CLINK CLOINK BZZT"
319check(qr/^CLINK CLOINK BZZT$/, 31, "filename with spaces in quotes");
320
321#line 37 "THOOM THOOM"
322check(qr/^THOOM THOOM$/, 37, "filename with tabs in quotes");
323
324#line 41 "GLINK PLINK GLUNK DINK"
325check(qr/^GLINK PLINK GLUNK DINK$/, 41, "a space after the quotes");
326
327#line 43 "BBFRPRAFPGHPP
328check(qr/^"BBFRPRAFPGHPP$/, 43, "actually missing a quote is still valid");
329
330#line 47 bang eth
331check(qr/^"BBFRPRAFPGHPP$/, 46, "but spaces aren't allowed without quotes");
332
26b6dc3f
RGS
333#line 77sevenseven
334check(qr/^"BBFRPRAFPGHPP$/, 49, "need a space after the line number");
335
44867030
NC
336eval <<'EOSTANZA'; die $@ if $@;
337#line 51 "With wonderful deathless ditties|We build up the world's great cities,|And out of a fabulous story|We fashion an empire's glory:|One man with a dream, at pleasure,|Shall go forth and conquer a crown;|And three with a new song's measure|Can trample a kingdom down."
338check(qr/^With.*down\.$/, 51, "Overflow the second small buffer check");
339EOSTANZA
340
341# And now, turn on the debugger flag for long names
342$^P = 0x100;
343
344#line 53 "For we are afar with the dawning|And the suns that are not yet high,|And out of the infinite morning|Intrepid you hear us cry-|How, spite of your human scorning,|Once more God's future draws nigh,|And already goes forth the warning|That ye of the past must die."
345check(qr/^For we.*must die\.$/, 53, "Our long line is set up");
346
347eval <<'EOT'; die $@ if $@;
348#line 59 " "
349check(qr/^ $/, 59, "Overflow the first small buffer check only");
350EOT
351
352eval <<'EOSTANZA'; die $@ if $@;
353#line 61 "Great hail! we cry to the comers|From the dazzling unknown shore;|Bring us hither your sun and your summers;|And renew our world as of yore;|You shall teach us your song's new numbers,|And things that we dreamed not before:|Yea, in spite of a dreamer who slumbers,|And a singer who sings no more."
354check(qr/^Great hail!.*no more\.$/, 61, "Overflow both small buffer checks");
355EOSTANZA
356
02255c60
FC
357{
358 my @x = 'string';
359 is(eval q{ "$x[0]->strung" }, 'string->strung',
360 'literal -> after an array subscript within ""');
361 @x = ['string'];
362 # this used to give "string"
363 like("$x[0]-> [0]", qr/^ARRAY\([^)]*\)-> \[0]\z/,
364 'literal -> [0] after an array subscript within ""');
365}
366
44867030
NC
367__END__
368# Don't add new tests HERE. See note above