This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix bug with (??{$overload}) regexp caching
[perl5.git] / t / re / reg_mesg.t
1 #!./perl -w
2
3 BEGIN {
4         chdir 't' if -d 't';
5         @INC = '../lib';
6         require './test.pl';
7         eval 'require Config'; # assume defaults if this fails
8 }
9
10 use strict;
11 use open qw(:utf8 :std);
12
13 ##
14 ## If the markers used are changed (search for "MARKER1" in regcomp.c),
15 ## update only these two regexs, and leave the {#} in the @death/@warning
16 ## arrays below. The {#} is a meta-marker -- it marks where the marker should
17 ## go.
18 ##
19 ## Returns empty string if that is what is expected.  Otherwise, handles
20 ## either a scalar, turning it into a single element array; or a ref to an
21 ## array, adjusting each element.  If called in array context, returns an
22 ## array, otherwise the join of all elements
23
24 sub fixup_expect {
25     my $expect_ref = shift;
26     return if $expect_ref eq "";
27
28     my @expect;
29     if (ref $expect_ref) {
30         @expect = @$expect_ref;
31     }
32     else {
33         @expect = $expect_ref;
34     }
35
36     foreach my $element (@expect) {
37         $element =~ s/{\#}/in regex; marked by <-- HERE in/;
38         $element =~ s/{\#}/ <-- HERE /;
39         $element .= " at ";
40     }
41     return wantarray ? @expect : join "", @expect;
42 }
43
44 ## Because we don't "use utf8" in this file, we need to do some extra legwork
45 ## for the utf8 tests: Append 'use utf8' to the pattern, and mark the strings
46 ## to check against as UTF-8
47 ##
48 ## This also creates a second variant of the tests to check if the
49 ## latin1 error messages are working correctly.
50 my $l1   = "\x{ef}";
51 my $utf8 = "\x{30cd}";
52 utf8::encode($utf8);
53
54 sub mark_as_utf8 {
55     my @ret;
56     while ( my ($pat, $msg) = splice(@_, 0, 2) ) {
57         my $l1_pat = $pat =~ s/$utf8/$l1/gr;
58         my $l1_msg;
59         $pat = "use utf8; $pat";
60         
61         if (ref $msg) {
62             $l1_msg = [ map { s/$utf8/$l1/gr } @$msg ];
63             @$msg   = map { my $c = $_; utf8::decode($c); $c } @$msg;
64         }
65         else {
66             $l1_msg = $msg =~ s/$utf8/$l1/gr;
67             utf8::decode($msg);
68         }
69         push @ret, $pat => $msg;
70         push @ret, $l1_pat => $l1_msg unless $l1_pat =~ /#no latin1/;
71     }
72     return @ret;
73 }
74
75 my $inf_m1 = ($Config::Config{reg_infty} || 32767) - 1;
76 my $inf_p1 = $inf_m1 + 2;
77
78 ##
79 ## Key-value pairs of code/error of code that should have fatal errors.
80 ##
81 my @death =
82 (
83  '/[[=foo=]]/' => 'POSIX syntax [= =] is reserved for future extensions {#} m/[[=foo=]{#}]/',
84
85  '/(?<= .*)/' =>  'Variable length lookbehind not implemented in regex m/(?<= .*)/',
86
87  '/(?<= x{1000})/' => 'Lookbehind longer than 255 not implemented in regex m/(?<= x{1000})/',
88
89  '/(?@)/' => 'Sequence (?@...) not implemented {#} m/(?@{#})/',
90
91  '/(?{ 1/' => 'Missing right curly or square bracket',
92
93  '/(?(1x))/' => 'Switch condition not recognized {#} m/(?(1x{#}))/',
94  '/(?(1x(?#)))/'=> 'Switch condition not recognized {#} m/(?(1x{#}(?#)))/',
95
96  '/(?(1)x|y|z)/' => 'Switch (?(condition)... contains too many branches {#} m/(?(1)x|y|{#}z)/',
97
98  '/(?(x)y|x)/' => 'Unknown switch condition (?(...)) {#} m/(?(x{#})y|x)/',
99
100  '/(?/' => 'Sequence (? incomplete {#} m/(?{#}/',
101
102  '/(?;x/' => 'Sequence (?;...) not recognized {#} m/(?;{#}x/',
103  '/(?<;x/' => 'Group name must start with a non-digit word character {#} m/(?<;{#}x/',
104  '/(?\ix/' => 'Sequence (?\...) not recognized {#} m/(?\{#}ix/',
105  '/(?\mx/' => 'Sequence (?\...) not recognized {#} m/(?\{#}mx/',
106  '/(?\:x/' => 'Sequence (?\...) not recognized {#} m/(?\{#}:x/',
107  '/(?\=x/' => 'Sequence (?\...) not recognized {#} m/(?\{#}=x/',
108  '/(?\!x/' => 'Sequence (?\...) not recognized {#} m/(?\{#}!x/',
109  '/(?\<=x/' => 'Sequence (?\...) not recognized {#} m/(?\{#}<=x/',
110  '/(?\<!x/' => 'Sequence (?\...) not recognized {#} m/(?\{#}<!x/',
111  '/(?\>x/' => 'Sequence (?\...) not recognized {#} m/(?\{#}>x/',
112  '/(?^-i:foo)/' => 'Sequence (?^-...) not recognized {#} m/(?^-{#}i:foo)/',
113  '/(?^-i)foo/' => 'Sequence (?^-...) not recognized {#} m/(?^-{#}i)foo/',
114  '/(?^d:foo)/' => 'Sequence (?^d...) not recognized {#} m/(?^d{#}:foo)/',
115  '/(?^d)foo/' => 'Sequence (?^d...) not recognized {#} m/(?^d{#})foo/',
116  '/(?^lu:foo)/' => 'Regexp modifiers "l" and "u" are mutually exclusive {#} m/(?^lu{#}:foo)/',
117  '/(?^lu)foo/' => 'Regexp modifiers "l" and "u" are mutually exclusive {#} m/(?^lu{#})foo/',
118 '/(?da:foo)/' => 'Regexp modifiers "d" and "a" are mutually exclusive {#} m/(?da{#}:foo)/',
119 '/(?lil:foo)/' => 'Regexp modifier "l" may not appear twice {#} m/(?lil{#}:foo)/',
120 '/(?aaia:foo)/' => 'Regexp modifier "a" may appear a maximum of twice {#} m/(?aaia{#}:foo)/',
121 '/(?i-l:foo)/' => 'Regexp modifier "l" may not appear after the "-" {#} m/(?i-l{#}:foo)/',
122
123  '/((x)/' => 'Unmatched ( {#} m/({#}(x)/',
124
125  "/x{$inf_p1}/" => "Quantifier in {,} bigger than $inf_m1 {#} m/x{{#}$inf_p1}/",
126
127
128  '/x**/' => 'Nested quantifiers {#} m/x**{#}/',
129
130  '/x[/' => 'Unmatched [ {#} m/x[{#}/',
131
132  '/*/', => 'Quantifier follows nothing {#} m/*{#}/',
133
134  '/\p{x/' => 'Missing right brace on \p{} {#} m/\p{{#}x/',
135
136  '/[\p{x]/' => 'Missing right brace on \p{} {#} m/[\p{{#}x]/',
137
138  '/(x)\2/' => 'Reference to nonexistent group {#} m/(x)\2{#}/',
139
140  '/\g/' => 'Unterminated \g... pattern {#} m/\g{#}/',
141  '/\g{1/' => 'Unterminated \g{...} pattern {#} m/\g{1{#}/',
142
143  'my $m = "\\\"; $m =~ $m', => 'Trailing \ in regex m/\/',
144
145  '/\x{1/' => 'Missing right brace on \x{} {#} m/\x{1{#}/',
146  '/\x{X/' => 'Missing right brace on \x{} {#} m/\x{{#}X/',
147
148  '/[\x{X]/' => 'Missing right brace on \x{} {#} m/[\x{{#}X]/',
149  '/[\x{A]/' => 'Missing right brace on \x{} {#} m/[\x{A{#}]/',
150
151  '/\o{1/' => 'Missing right brace on \o{ {#} m/\o{1{#}/',
152  '/\o{X/' => 'Missing right brace on \o{ {#} m/\o{{#}X/',
153
154  '/[\o{X]/' => 'Missing right brace on \o{ {#} m/[\o{{#}X]/',
155  '/[\o{7]/' => 'Missing right brace on \o{ {#} m/[\o{7{#}]/',
156
157  '/[[:barf:]]/' => 'POSIX class [:barf:] unknown {#} m/[[:barf:]{#}]/',
158
159  '/[[=barf=]]/' => 'POSIX syntax [= =] is reserved for future extensions {#} m/[[=barf=]{#}]/',
160
161  '/[[.barf.]]/' => 'POSIX syntax [. .] is reserved for future extensions {#} m/[[.barf.]{#}]/',
162
163  '/[z-a]/' => 'Invalid [] range "z-a" {#} m/[z-a{#}]/',
164
165  '/\p/' => 'Empty \p{} {#} m/\p{#}/',
166
167  '/\P{}/' => 'Empty \P{} {#} m/\P{{#}}/',
168  '/(?[[[:word]]])/' => "Unmatched ':' in POSIX class {#} m/(?[[[:word{#}]]])/",
169  '/(?[[:word]])/' => "Unmatched ':' in POSIX class {#} m/(?[[:word{#}]])/",
170  '/(?[[[:digit: ])/' => "Unmatched '[' in POSIX class {#} m/(?[[[:digit:{#} ])/",
171  '/(?[[:digit: ])/' => "Unmatched '[' in POSIX class {#} m/(?[[:digit:{#} ])/",
172  '/(?[[[::]]])/' => "POSIX class [::] unknown {#} m/(?[[[::]{#}]])/",
173  '/(?[[[:w:]]])/' => "POSIX class [:w:] unknown {#} m/(?[[[:w:]{#}]])/",
174  '/(?[[:w:]])/' => "POSIX class [:w:] unknown {#} m/(?[[:w:]{#}])/",
175  '/(?[a])/' =>  'Unexpected character {#} m/(?[a{#}])/',
176  '/(?[\t])/l' => '(?[...]) not valid in locale {#} m/(?[{#}\t])/',
177  '/(?[ + \t ])/' => 'Unexpected binary operator \'+\' with no preceding operand {#} m/(?[ +{#} \t ])/',
178  '/(?[ \cK - ( + \t ) ])/' => 'Unexpected binary operator \'+\' with no preceding operand {#} m/(?[ \cK - ( +{#} \t ) ])/',
179  '/(?[ \cK ( \t ) ])/' => 'Unexpected \'(\' with no preceding operator {#} m/(?[ \cK ({#} \t ) ])/',
180  '/(?[ \cK \t ])/' => 'Operand with no preceding operator {#} m/(?[ \cK \t{#} ])/',
181  '/(?[ \0004 ])/' => 'Need exactly 3 octal digits {#} m/(?[ \0004 {#}])/',
182  '/(?[ \05 ])/' => 'Need exactly 3 octal digits {#} m/(?[ \05 {#}])/',
183  '/(?[ \o{1038} ])/' => 'Non-octal character {#} m/(?[ \o{1038{#}} ])/',
184  '/(?[ \o{} ])/' => 'Number with no digits {#} m/(?[ \o{}{#} ])/',
185  '/(?[ \x{defg} ])/' => 'Non-hex character {#} m/(?[ \x{defg{#}} ])/',
186  '/(?[ \xabcdef ])/' => 'Use \\x{...} for more than two hex characters {#} m/(?[ \xabc{#}def ])/',
187  '/(?[ \x{} ])/' => 'Number with no digits {#} m/(?[ \x{}{#} ])/',
188  '/(?[ \cK + ) ])/' => 'Unexpected \')\' {#} m/(?[ \cK + ){#} ])/',
189  '/(?[ \cK + ])/' => 'Incomplete expression within \'(?[ ])\' {#} m/(?[ \cK + {#}])/',
190  '/(?[ \p{foo} ])/' => 'Property \'foo\' is unknown {#} m/(?[ \p{foo}{#} ])/',
191  '/(?[ \p{ foo = bar } ])/' => 'Property \'foo = bar\' is unknown {#} m/(?[ \p{ foo = bar }{#} ])/',
192  '/(?[ \8 ])/' => 'Unrecognized escape \8 in character class {#} m/(?[ \8{#} ])/',
193  '/(?[ \t ]/' => 'Syntax error in (?[...]) in regex m/(?[ \t ]/',
194  '/(?[ [ \t ]/' => 'Syntax error in (?[...]) in regex m/(?[ [ \t ]/',
195  '/(?[ \t ] ]/' => 'Syntax error in (?[...]) in regex m/(?[ \t ] ]/',
196  '/(?[ [ ] ]/' => 'Syntax error in (?[...]) in regex m/(?[ [ ] ]/',
197  '/(?[ \t + \e # This was supposed to be a comment ])/' => 'Syntax error in (?[...]) in regex m/(?[ \t + \e # This was supposed to be a comment ])/',
198  '/(?[ ])/' => 'Incomplete expression within \'(?[ ])\' {#} m/(?[ {#}])/',
199  'm/(?[[a-\d]])/' => 'False [] range "a-\d" {#} m/(?[[a-\d{#}]])/',
200  'm/(?[[\w-x]])/' => 'False [] range "\w-" {#} m/(?[[\w-{#}x]])/',
201  'm/(?[[a-\pM]])/' => 'False [] range "a-\pM" {#} m/(?[[a-\pM{#}]])/',
202  'm/(?[[\pM-x]])/' => 'False [] range "\pM-" {#} m/(?[[\pM-{#}x]])/',
203  'm/(?[[\N{LATIN CAPITAL LETTER A WITH MACRON AND GRAVE}]])/' => '\N{} in character class restricted to one character {#} m/(?[[\N{U+100.300{#}}]])/',
204  'm/(?[ \p{Digit} & (?(?[ \p{Thai} | \p{Lao} ]))])/' => 'Sequence (?(...) not recognized {#} m/(?[ \p{Digit} & (?({#}?[ \p{Thai} | \p{Lao} ]))])/',
205  'm/(?[ \p{Digit} & (?:(?[ \p{Thai} | \p{Lao} ]))])/' => 'Expecting \'(?flags:(?[...\' {#} m/(?[ \p{Digit} & (?{#}:(?[ \p{Thai} | \p{Lao} ]))])/',
206  'm/\o{/' => 'Missing right brace on \o{ {#} m/\o{{#}/',
207  'm/\o/' => 'Missing braces on \o{} {#} m/\o{#}/',
208  'm/\o{}/' => 'Number with no digits {#} m/\o{}{#}/',
209  'm/[\o{]/' => 'Missing right brace on \o{ {#} m/[\o{{#}]/',
210  'm/[\o]/' => 'Missing braces on \o{} {#} m/[\o{#}]/',
211  'm/[\o{}]/' => 'Number with no digits {#} m/[\o{}{#}]/',
212  'm/(?^-i:foo)/' => 'Sequence (?^-...) not recognized {#} m/(?^-{#}i:foo)/',
213  'm/\87/' => 'Reference to nonexistent group {#} m/\87{#}/',
214  'm/a\87/' => 'Reference to nonexistent group {#} m/a\87{#}/',
215  'm/a\97/' => 'Reference to nonexistent group {#} m/a\97{#}/',
216  'm/(*DOOF)/' => 'Unknown verb pattern \'DOOF\' {#} m/(*DOOF){#}/',
217  'm/(?&a/'  => 'Sequence (?&... not terminated {#} m/(?&a{#}/',
218  'm/(?P=/' => 'Sequence ?P=... not terminated {#} m/(?P={#}/',
219  "m/(?'/"  => "Sequence (?'... not terminated {#} m/(?'{#}/",
220  "m/(?</"  => "Sequence (?<... not terminated {#} m/(?<{#}/",
221  'm/(?&/'  => 'Sequence (?&... not terminated {#} m/(?&{#}/',
222  'm/(?(</' => 'Sequence (?(<... not terminated {#} m/(?(<{#}/',
223  "m/(?('/" => "Sequence (?('... not terminated {#} m/(?('{#}/",
224  'm/\g{/'  => 'Sequence \g{... not terminated {#} m/\g{{#}/',
225  'm/\k</'  => 'Sequence \k<... not terminated {#} m/\k<{#}/',
226 );
227
228 my @death_utf8 = mark_as_utf8(
229  '/ネ[[=ネ=]]ネ/' => 'POSIX syntax [= =] is reserved for future extensions {#} m/ネ[[=ネ=]{#}]ネ/',
230  '/ネ(?<= .*)/' =>  'Variable length lookbehind not implemented in regex m/ネ(?<= .*)/',
231
232  '/(?<= ネ{1000})/' => 'Lookbehind longer than 255 not implemented in regex m/(?<= ネ{1000})/',
233
234  '/ネ(?ネ)ネ/' => 'Sequence (?ネ...) not recognized {#} m/ネ(?ネ{#})ネ/',
235
236  '/ネ(?(1ネ))ネ/' => 'Switch condition not recognized {#} m/ネ(?(1ネ{#}))ネ/',
237
238  '/(?(1)ネ|y|ヌ)/' => 'Switch (?(condition)... contains too many branches {#} m/(?(1)ネ|y|{#}ヌ)/',
239
240  '/(?(ネ)y|ネ)/' => 'Unknown switch condition (?(...)) {#} m/(?(ネ{#})y|ネ)/',
241
242  '/ネ(?/' => 'Sequence (? incomplete {#} m/ネ(?{#}/',
243
244  '/ネ(?;ネ/' => 'Sequence (?;...) not recognized {#} m/ネ(?;{#}ネ/',
245  '/ネ(?<;ネ/' => 'Group name must start with a non-digit word character {#} m/ネ(?<;{#}ネ/',
246  '/ネ(?\ixネ/' => 'Sequence (?\...) not recognized {#} m/ネ(?\{#}ixネ/',
247  '/ネ(?^lu:ネ)/' => 'Regexp modifiers "l" and "u" are mutually exclusive {#} m/ネ(?^lu{#}:ネ)/',
248 '/ネ(?lil:ネ)/' => 'Regexp modifier "l" may not appear twice {#} m/ネ(?lil{#}:ネ)/',
249 '/ネ(?aaia:ネ)/' => 'Regexp modifier "a" may appear a maximum of twice {#} m/ネ(?aaia{#}:ネ)/',
250 '/ネ(?i-l:ネ)/' => 'Regexp modifier "l" may not appear after the "-" {#} m/ネ(?i-l{#}:ネ)/',
251
252  '/ネ((ネ)/' => 'Unmatched ( {#} m/ネ({#}(ネ)/',
253
254  "/ネ{$inf_p1}ネ/" => "Quantifier in {,} bigger than $inf_m1 {#} m/ネ{{#}$inf_p1}ネ/",
255
256
257  '/ネ**ネ/' => 'Nested quantifiers {#} m/ネ**{#}ネ/',
258
259  '/ネ[ネ/' => 'Unmatched [ {#} m/ネ[{#}ネ/',
260
261  '/*ネ/', => 'Quantifier follows nothing {#} m/*{#}ネ/',
262
263  '/ネ\p{ネ/' => 'Missing right brace on \p{} {#} m/ネ\p{{#}ネ/',
264
265  '/(ネ)\2ネ/' => 'Reference to nonexistent group {#} m/(ネ)\2{#}ネ/',
266
267  '/\g{ネ/; #no latin1' => 'Sequence \g{... not terminated {#} m/\g{ネ{#}/',
268
269  'my $m = "ネ\\\"; $m =~ $m', => 'Trailing \ in regex m/ネ\/',
270
271  '/\x{ネ/' => 'Missing right brace on \x{} {#} m/\x{{#}ネ/',
272  '/ネ[\x{ネ]ネ/' => 'Missing right brace on \x{} {#} m/ネ[\x{{#}ネ]ネ/',
273  '/ネ[\x{ネ]/' => 'Missing right brace on \x{} {#} m/ネ[\x{{#}ネ]/',
274
275  '/ネ\o{ネ/' => 'Missing right brace on \o{ {#} m/ネ\o{{#}ネ/',
276  '/ネ[[:ネ:]]ネ/' => 'POSIX class [:ネ:] unknown {#} m/ネ[[:ネ:]{#}]ネ/',
277
278  '/ネ[[=ネ=]]ネ/' => 'POSIX syntax [= =] is reserved for future extensions {#} m/ネ[[=ネ=]{#}]ネ/',
279
280  '/ネ[[.ネ.]]ネ/' => 'POSIX syntax [. .] is reserved for future extensions {#} m/ネ[[.ネ.]{#}]ネ/',
281
282  '/[ネ-a]ネ/' => 'Invalid [] range "ネ-a" {#} m/[ネ-a{#}]ネ/',
283
284  '/ネ\p{}ネ/' => 'Empty \p{} {#} m/ネ\p{{#}}ネ/',
285
286  '/ネ(?[[[:ネ]]])ネ/' => "Unmatched ':' in POSIX class {#} m/ネ(?[[[:ネ{#}]]])ネ/",
287  '/ネ(?[[[:ネ: ])ネ/' => "Unmatched '[' in POSIX class {#} m/ネ(?[[[:ネ:{#} ])ネ/",
288  '/ネ(?[[[::]]])ネ/' => "POSIX class [::] unknown {#} m/ネ(?[[[::]{#}]])ネ/",
289  '/ネ(?[[[:ネ:]]])ネ/' => "POSIX class [:ネ:] unknown {#} m/ネ(?[[[:ネ:]{#}]])ネ/",
290  '/ネ(?[[:ネ:]])ネ/' => "POSIX class [:ネ:] unknown {#} m/ネ(?[[:ネ:]{#}])ネ/",
291  '/ネ(?[ネ])ネ/' =>  'Unexpected character {#} m/ネ(?[ネ{#}])ネ/',
292  '/ネ(?[ネ])/l' => '(?[...]) not valid in locale {#} m/ネ(?[{#}ネ])/',
293  '/ネ(?[ + [ネ] ])/' => 'Unexpected binary operator \'+\' with no preceding operand {#} m/ネ(?[ +{#} [ネ] ])/',
294  '/ネ(?[ \cK - ( + [ネ] ) ])/' => 'Unexpected binary operator \'+\' with no preceding operand {#} m/ネ(?[ \cK - ( +{#} [ネ] ) ])/',
295  '/ネ(?[ \cK ( [ネ] ) ])/' => 'Unexpected \'(\' with no preceding operator {#} m/ネ(?[ \cK ({#} [ネ] ) ])/',
296  '/ネ(?[ \cK [ネ] ])ネ/' => 'Operand with no preceding operator {#} m/ネ(?[ \cK [ネ{#}] ])ネ/',
297  '/ネ(?[ \0004 ])ネ/' => 'Need exactly 3 octal digits {#} m/ネ(?[ \0004 {#}])ネ/',
298  '/(?[ \o{ネ} ])ネ/' => 'Non-octal character {#} m/(?[ \o{ネ{#}} ])ネ/',
299  '/ネ(?[ \o{} ])ネ/' => 'Number with no digits {#} m/ネ(?[ \o{}{#} ])ネ/',
300  '/(?[ \x{ネ} ])ネ/' => 'Non-hex character {#} m/(?[ \x{ネ{#}} ])ネ/',
301  '/(?[ \p{ネ} ])/' => 'Property \'ネ\' is unknown {#} m/(?[ \p{ネ}{#} ])/',
302  '/(?[ \p{ ネ = bar } ])/' => 'Property \'ネ = bar\' is unknown {#} m/(?[ \p{ ネ = bar }{#} ])/',
303  '/ネ(?[ \t ]/' => 'Syntax error in (?[...]) in regex m/ネ(?[ \t ]/',
304  '/(?[ \t + \e # ネ This was supposed to be a comment ])/' => 'Syntax error in (?[...]) in regex m/(?[ \t + \e # ネ This was supposed to be a comment ])/',
305  'm/(*ネ)ネ/' => q<Unknown verb pattern 'ネ' {#} m/(*ネ){#}ネ/>
306 );
307 push @death, @death_utf8;
308
309 # Tests involving a user-defined charnames translator are in pat_advanced.t
310
311 # In the following arrays of warnings, the value can be an array of things to
312 # expect.  If the empty string, it means no warning should be raised.
313
314 ##
315 ## Key-value pairs of code/error of code that should have non-fatal regexp warnings.
316 ##
317 my @warning = (
318     'm/\b*/' => '\b* matches null string many times {#} m/\b*{#}/',
319     'm/[:blank:]/' => 'POSIX syntax [: :] belongs inside character classes {#} m/[:blank:]{#}/',
320
321     "m'[\\y]'"     => 'Unrecognized escape \y in character class passed through {#} m/[\y{#}]/',
322
323     'm/[a-\d]/' => 'False [] range "a-\d" {#} m/[a-\d{#}]/',
324     'm/[\w-x]/' => 'False [] range "\w-" {#} m/[\w-{#}x]/',
325     'm/[a-\pM]/' => 'False [] range "a-\pM" {#} m/[a-\pM{#}]/',
326     'm/[\pM-x]/' => 'False [] range "\pM-" {#} m/[\pM-{#}x]/',
327     "m'\\y'"     => 'Unrecognized escape \y passed through {#} m/\y{#}/',
328     '/x{3,1}/'   => 'Quantifier {n,m} with n > m can\'t match {#} m/x{3,1}{#}/',
329     '/\08/' => '\'\08\' resolved to \'\o{0}8\' {#} m/\08{#}/',
330     '/\018/' => '\'\018\' resolved to \'\o{1}8\' {#} m/\018{#}/',
331     '/[\08]/' => '\'\08\' resolved to \'\o{0}8\' {#} m/[\08{#}]/',
332     '/[\018]/' => '\'\018\' resolved to \'\o{1}8\' {#} m/[\018{#}]/',
333     '/(?=a)*/' => '(?=a)* matches null string many times {#} m/(?=a)*{#}/',
334     'my $x = \'\m\'; qr/a$x/' => 'Unrecognized escape \m passed through {#} m/a\m{#}/',
335     '/\q/' => 'Unrecognized escape \q passed through {#} m/\q{#}/',
336     '/\q{/' => 'Unrecognized escape \q{ passed through {#} m/\q{{#}/',
337     '/(?=a){1,3}/' => 'Quantifier unexpected on zero-length expression {#} m/(?=a){1,3}{#}/',
338     '/(a|b)(?=a){3}/' => 'Quantifier unexpected on zero-length expression {#} m/(a|b)(?=a){3}{#}/',
339     '/\_/' => "",
340     '/[\_\0]/' => "",
341     '/[\07]/' => "",
342     '/[\006]/' => "",
343     '/[\0005]/' => "",
344     '/[\8\9]/' => ['Unrecognized escape \8 in character class passed through {#} m/[\8{#}\9]/',
345                    'Unrecognized escape \9 in character class passed through {#} m/[\8\9{#}]/',
346                   ],
347     '/[:alpha:]/' => 'POSIX syntax [: :] belongs inside character classes {#} m/[:alpha:]{#}/',
348     '/[:zog:]/' => 'POSIX syntax [: :] belongs inside character classes {#} m/[:zog:]{#}/',
349     '/[.zog.]/' => 'POSIX syntax [. .] belongs inside character classes {#} m/[.zog.]{#}/',
350     '/[a-b]/' => "",
351     '/[a-\d]/' => 'False [] range "a-\d" {#} m/[a-\d{#}]/',
352     '/[\d-b]/' => 'False [] range "\d-" {#} m/[\d-{#}b]/',
353     '/[\s-\d]/' => 'False [] range "\s-" {#} m/[\s-{#}\d]/',
354     '/[\d-\s]/' => 'False [] range "\d-" {#} m/[\d-{#}\s]/',
355     '/[a-[:digit:]]/' => 'False [] range "a-[:digit:]" {#} m/[a-[:digit:]{#}]/',
356     '/[[:digit:]-b]/' => 'False [] range "[:digit:]-" {#} m/[[:digit:]-{#}b]/',
357     '/[[:alpha:]-[:digit:]]/' => 'False [] range "[:alpha:]-" {#} m/[[:alpha:]-{#}[:digit:]]/',
358     '/[[:digit:]-[:alpha:]]/' => 'False [] range "[:digit:]-" {#} m/[[:digit:]-{#}[:alpha:]]/',
359     '/[a\zb]/' => 'Unrecognized escape \z in character class passed through {#} m/[a\z{#}b]/',
360     '/(?c)/' => 'Useless (?c) - use /gc modifier {#} m/(?c{#})/',
361     '/(?-c)/' => 'Useless (?-c) - don\'t use /gc modifier {#} m/(?-c{#})/',
362     '/(?g)/' => 'Useless (?g) - use /g modifier {#} m/(?g{#})/',
363     '/(?-g)/' => 'Useless (?-g) - don\'t use /g modifier {#} m/(?-g{#})/',
364     '/(?o)/' => 'Useless (?o) - use /o modifier {#} m/(?o{#})/',
365     '/(?-o)/' => 'Useless (?-o) - don\'t use /o modifier {#} m/(?-o{#})/',
366     '/(?g-o)/' => [ 'Useless (?g) - use /g modifier {#} m/(?g{#}-o)/',
367                     'Useless (?-o) - don\'t use /o modifier {#} m/(?g-o{#})/',
368                   ],
369     '/(?g-c)/' => [ 'Useless (?g) - use /g modifier {#} m/(?g{#}-c)/',
370                     'Useless (?-c) - don\'t use /gc modifier {#} m/(?g-c{#})/',
371                   ],
372       # (?c) means (?g) error won't be thrown
373      '/(?o-cg)/' => [ 'Useless (?o) - use /o modifier {#} m/(?o{#}-cg)/',
374                       'Useless (?-c) - don\'t use /gc modifier {#} m/(?o-c{#}g)/',
375                     ],
376     '/(?ogc)/' => [ 'Useless (?o) - use /o modifier {#} m/(?o{#}gc)/',
377                     'Useless (?g) - use /g modifier {#} m/(?og{#}c)/',
378                     'Useless (?c) - use /gc modifier {#} m/(?ogc{#})/',
379                   ],
380 );
381
382 my @warnings_utf8 = mark_as_utf8(
383     'm/ネ\b*ネ/' => '\b* matches null string many times {#} m/ネ\b*{#}ネ/',
384     '/(?=ネ)*/' => '(?=ネ)* matches null string many times {#} m/(?=ネ)*{#}/',
385     'm/ネ[:foo:]ネ/' => 'POSIX syntax [: :] belongs inside character classes {#} m/ネ[:foo:]{#}ネ/',
386     "m'ネ[\\y]ネ'" => 'Unrecognized escape \y in character class passed through {#} m/ネ[\y{#}]ネ/',
387     'm/ネ[ネ-\d]ネ/' => 'False [] range "ネ-\d" {#} m/ネ[ネ-\d{#}]ネ/',
388     'm/ネ[\w-ネ]ネ/' => 'False [] range "\w-" {#} m/ネ[\w-{#}ネ]ネ/',
389     'm/ネ[ネ-\pM]ネ/' => 'False [] range "ネ-\pM" {#} m/ネ[ネ-\pM{#}]ネ/',
390     '/ネ[ネ-[:digit:]]ネ/' => 'False [] range "ネ-[:digit:]" {#} m/ネ[ネ-[:digit:]{#}]ネ/',
391     '/ネ[\d-\s]ネ/' => 'False [] range "\d-" {#} m/ネ[\d-{#}\s]ネ/',
392     '/ネ[a\zb]ネ/' => 'Unrecognized escape \z in character class passed through {#} m/ネ[a\z{#}b]ネ/',
393     '/ネ(?c)ネ/' => 'Useless (?c) - use /gc modifier {#} m/ネ(?c{#})ネ/',    
394     '/utf8 ネ (?ogc) ネ/' => [
395         'Useless (?o) - use /o modifier {#} m/utf8 ネ (?o{#}gc) ネ/',
396         'Useless (?g) - use /g modifier {#} m/utf8 ネ (?og{#}c) ネ/',
397         'Useless (?c) - use /gc modifier {#} m/utf8 ネ (?ogc{#}) ネ/',
398     ],
399
400 );
401
402 push @warning, @warnings_utf8;
403
404 my @experimental_regex_sets = (
405     '/(?[ \t ])/' => 'The regex_sets feature is experimental {#} m/(?[{#} \t ])/',
406     'use utf8; /utf8 ネ (?[ [\tネ] ])/' => do { use utf8; 'The regex_sets feature is experimental {#} m/utf8 ネ (?[{#} [\tネ] ])/' },
407     '/noutf8 ネ (?[ [\tネ] ])/' => 'The regex_sets feature is experimental {#} m/noutf8 ネ (?[{#} [\tネ] ])/',
408 );
409
410 my @deprecated = (
411     '/a\b{cde/' => '"\b{" is deprecated; use "\b\{" or "\b[{]" instead {#} m/a\{#}b{cde/',
412     '/a\B{cde/' => '"\B{" is deprecated; use "\B\{" or "\B[{]" instead {#} m/a\{#}B{cde/',
413     "/(?x)latin1\\\x{85}\x{85}\\\x{85}/" => 'Escape literal pattern white space under /x {#} ' . "m/(?x)latin1\\\x{85}\x{85}{#}\\\x{85}/",
414     'use utf8; /(?x)utf8\\85\85\\85/' => 'Escape literal pattern white space under /x {#} ' . "m/(?x)utf8\\\N{NEXT LINE}\N{NEXT LINE}{#}\\\N{NEXT LINE}/",
415     '/((?# This is a comment in the middle of a token)?:foo)/' => 'In \'(?...)\', splitting the initial \'(?\' is deprecated {#} m/((?# This is a comment in the middle of a token)?{#}:foo)/',
416     '/((?# This is a comment in the middle of a token)*FAIL)/' => 'In \'(*VERB...)\', splitting the initial \'(*\' is deprecated {#} m/((?# This is a comment in the middle of a token)*{#}FAIL)/',
417 );
418
419 while (my ($regex, $expect) = splice @death, 0, 2) {
420     my $expect = fixup_expect($expect);
421     no warnings 'experimental::regex_sets';
422     # skip the utf8 test on EBCDIC since they do not die
423     next if $::IS_EBCDIC && $regex =~ /utf8/;
424
425     warning_is(sub {
426                    $_ = "x";
427                    eval $regex;
428                    like($@, qr/\Q$expect/, $regex);
429                }, undef, "... and died without any other warnings");
430 }
431
432 foreach my $ref (\@warning, \@experimental_regex_sets, \@deprecated) {
433     my $warning_type = ($ref == \@warning)
434                        ? 'regexp'
435                        : ($ref == \@deprecated)
436                          ? 'regexp, deprecated'
437                          : 'experimental::regex_sets';
438     while (my ($regex, $expect) = splice @$ref, 0, 2) {
439         my @expect = fixup_expect($expect);
440         {
441             $_ = "x";
442             no warnings;
443             eval $regex;
444         }
445         if (is($@, "", "$regex did not die")) {
446             my @got = capture_warnings(sub {
447                                     $_ = "x";
448                                     eval $regex });
449             my $count = @expect;
450             if (! is(scalar @got, scalar @expect, "... and gave expected number ($count) of warnings")) {
451                 if (@got < @expect) {
452                     $count = @got;
453                     note "Expected warnings not gotten:\n\t" . join "\n\t", @expect[$count .. $#expect];
454                 }
455                 else {
456                     note "Unexpected warnings gotten:\n\t" . join("\n\t", @got[$count .. $#got]);
457                 }
458             }
459             foreach my $i (0 .. $count - 1) {
460                 if (! like($got[$i], qr/\Q$expect[$i]/, "... and gave expected warning")) {
461                     chomp($got[$i]);
462                     chomp($expect[$i]);
463                     diag("GOT\n'$got[$i]'\nEXPECT\n'$expect[$i]'");
464                 }
465                 else {
466                     ok (0 == capture_warnings(sub {
467                                     $_ = "x";
468                                     eval "no warnings '$warning_type'; $regex;" }
469                                 ),
470                     "... and turning off '$warning_type' warnings suppressed it");
471                     # Test that whether the warning is on by default is
472                     # correct.  Experimental and deprecated warnings are;
473                     # others are not.  This test relies on the fact that we
474                     # are outside the scope of any ‘use warnings’.
475                     local $^W;
476                     my $on = 'on' x ($warning_type ne 'regexp');
477                     ok !!$on ==
478                         capture_warnings(sub { $_ = "x"; eval $regex }),
479                       "... and the warning is " . ($on||'off')
480                        . " by default";
481                 }
482             }
483         }
484     }
485 }
486
487 done_testing();