This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
6136f6a927c4bc9b477f6bc0b915789291268997
[perl5.git] / t / re / reg_mesg.t
1 #!./perl -w
2
3 $|=1;   # outherwise things get mixed up in output
4
5 BEGIN {
6         chdir 't' if -d 't';
7         require './test.pl';
8     set_up_inc( qw '../lib ../ext/re' );
9         eval 'require Config'; # assume defaults if this fails
10 }
11
12 skip_all_without_unicode_tables();
13
14 use strict;
15 use open qw(:utf8 :std);
16
17 # Kind of a kludge to mark warnings to be expected only if we are testing
18 # under "use re 'strict'"
19 my $only_strict_marker = ':expected_only_under_strict';
20
21 ## If the markers used are changed (search for "MARKER1" in regcomp.c),
22 ## update only these two regexs, and leave the {#} in the @death/@warning
23 ## arrays below. The {#} is a meta-marker -- it marks where the marker should
24 ## go.
25
26 sub fixup_expect ($$) {
27
28     # Fixes up the expected results by inserting the boiler plate text.
29     # Returns empty string if that is what is expected.  Otherwise, handles
30     # either a scalar, turning it into a single element array; or a ref to an
31     # array, adjusting each element.  If called in array context, returns an
32     # array, otherwise the join of all elements.
33
34     # The string $only_strict_marker will be removed from any expect line it
35     # begins, and if $strict is not true, that expect line will be removed
36     # from the output (hence won't be expected)
37
38     my ($expect_ref, $strict) = @_;
39     return "" if $expect_ref eq "";
40
41     my @expect;
42     if (ref $expect_ref) {
43         @expect = @$expect_ref;
44     }
45     else {
46         @expect = $expect_ref;
47     }
48
49     my @new_expect;
50     foreach my $element (@expect) {
51         $element =~ s/\{\#\}/in regex; marked by <-- HERE in/;
52         $element =~ s/\{\#\}/ <-- HERE /;
53         $element .= " at ";
54         next if $element =~ s/ ^ $only_strict_marker \s* //x && ! $strict;
55         push @new_expect, $element;
56     }
57     return wantarray ? @new_expect : join "", @new_expect;
58 }
59
60 sub add_markers {
61     my ($element)= @_;
62     $element =~ s/ at .* line \d+\.?\n$//;
63     $element =~ s/in regex; marked by <-- HERE in/{#}/;
64     $element =~ s/ <-- HERE /{#}/;
65     return $element;
66 }
67
68 ## Because we don't "use utf8" in this file, we need to do some extra legwork
69 ## for the utf8 tests: Prepend 'use utf8' to the pattern, and mark the strings
70 ## to check against as UTF-8, but for this all to work properly, the character
71 ## 'ネ' (U+30CD) is required in each pattern somewhere as a marker.
72 ##
73 ## This also creates a second variant of the tests to check if the
74 ## latin1 error messages are working correctly.  Because we don't 'use utf8',
75 ## we can't tell if something is UTF-8 or Latin1, so you need the suffix
76 ## '; no latin1' to not have the second variant.
77 my $l1   = "\x{ef}";
78 my $utf8 = "\x{30cd}";
79 utf8::encode($utf8);
80
81 sub mark_as_utf8 {
82     my @ret;
83     for (my $i = 0; $i < @_; $i += 2) {
84         my $pat = $_[$i];
85         my $msg = $_[$i+1];
86         my $l1_pat = $pat =~ s/$utf8/$l1/gr;
87         my $l1_msg;
88         $pat = "use utf8; $pat";
89
90         if (ref $msg) {
91             $l1_msg = [ map { s/$utf8/$l1/gr } @$msg ];
92             @$msg   = map { my $c = $_; utf8::decode($c); $c } @$msg;
93         }
94         else {
95             $l1_msg = $msg =~ s/$utf8/$l1/gr;
96             utf8::decode($msg);
97         }
98         push @ret, $pat => $msg;
99
100         push @ret, $l1_pat => $l1_msg unless $l1_pat =~ /#no latin1/;
101     }
102     return @ret;
103 }
104
105 my $inf_m1 = ($Config::Config{reg_infty} || 65535) - 1;
106 my $inf_p1 = $inf_m1 + 2;
107
108 my $B_hex = sprintf("\\x%02X", ord "B");
109 my $low_mixed_alpha = ('A' lt 'a') ? 'A' : 'a';
110 my $high_mixed_alpha = ('A' lt 'a') ? 'a' : 'A';
111 my $low_mixed_digit = ('A' lt '0') ? 'A' : '0';
112 my $high_mixed_digit = ('A' lt '0') ? '0' : 'A';
113
114 my $colon_hex = sprintf "%02X", ord(":");
115 my $tab_hex = sprintf "%02X", ord("\t");
116
117 # Key-value pairs of strings eval'd as patterns => warn/error messages that
118 # they should generate.  In some cases, the value is an array of multiple
119 # messages.  Some groups have the message(s) be default on; others, default
120 # off.  This can be overridden on an individual key basis by preceding the
121 # pattern string with either 'default_on' or 'default_off'
122 #
123 # The first set are those that should be fatal errors.
124
125 my $bug133423 = "(?[(?^:(?[\\\x00]))\\]\x00|2[^^]\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80])R.\\670";
126
127 my @death =
128 (
129  '/[[=foo=]]/' => 'POSIX syntax [= =] is reserved for future extensions {#} m/[[=foo=]{#}]/',
130
131  '/(?<= .*)/' =>  'Lookbehind longer than 255 not implemented in regex m/(?<= .*)/',
132
133  '/(?<= x{1000})/' => 'Lookbehind longer than 255 not implemented in regex m/(?<= x{1000})/',
134
135  '/(?@)/' => 'Sequence (?@...) not implemented {#} m/(?@{#})/',
136
137  '/(?{ 1/' => 'Missing right curly or square bracket',
138
139  '/(?(1x))/' => 'Switch condition not recognized {#} m/(?(1x{#}))/',
140  '/(?(1x(?#)))/'=> 'Switch condition not recognized {#} m/(?(1x{#}(?#)))/',
141
142  '/(?(1)/'    => 'Switch (?(condition)... not terminated {#} m/(?(1){#}/',
143  '/(?(1)x/'    => 'Switch (?(condition)... not terminated {#} m/(?(1)x{#}/',
144  '/(?(1)x|y/'    => 'Switch (?(condition)... not terminated {#} m/(?(1)x|y{#}/',
145  '/(?(1)x|y|z)/' => 'Switch (?(condition)... contains too many branches {#} m/(?(1)x|y|{#}z)/',
146
147  '/(?(x)y|x)/' => 'Unknown switch condition (?(...)) {#} m/(?(x{#})y|x)/',
148  '/(?(??{}))/' => 'Unknown switch condition (?(...)) {#} m/(?(?{#}?{}))/',
149  '/(?(?[]))/' => 'Unknown switch condition (?(...)) {#} m/(?(?{#}[]))/',
150
151  '/(?/' => 'Sequence (? incomplete {#} m/(?{#}/',
152
153  '/(?;x/' => 'Sequence (?;...) not recognized {#} m/(?;{#}x/',
154  '/(?<;x/' => 'Group name must start with a non-digit word character {#} m/(?<;{#}x/',
155  '/(?\ix/' => 'Sequence (?\...) not recognized {#} m/(?\{#}ix/',
156  '/(?\mx/' => 'Sequence (?\...) not recognized {#} m/(?\{#}mx/',
157  '/(?\:x/' => 'Sequence (?\...) not recognized {#} m/(?\{#}:x/',
158  '/(?\=x/' => 'Sequence (?\...) not recognized {#} m/(?\{#}=x/',
159  '/(?\!x/' => 'Sequence (?\...) not recognized {#} m/(?\{#}!x/',
160  '/(?\<=x/' => 'Sequence (?\...) not recognized {#} m/(?\{#}<=x/',
161  '/(?\<!x/' => 'Sequence (?\...) not recognized {#} m/(?\{#}<!x/',
162  '/(?\>x/' => 'Sequence (?\...) not recognized {#} m/(?\{#}>x/',
163  '/(?^-i:foo)/' => 'Sequence (?^-...) not recognized {#} m/(?^-{#}i:foo)/',
164  '/(?^-i)foo/' => 'Sequence (?^-...) not recognized {#} m/(?^-{#}i)foo/',
165  '/(?^d:foo)/' => 'Sequence (?^d...) not recognized {#} m/(?^d{#}:foo)/',
166  '/(?^d)foo/' => 'Sequence (?^d...) not recognized {#} m/(?^d{#})foo/',
167  '/(?^lu:foo)/' => 'Regexp modifiers "l" and "u" are mutually exclusive {#} m/(?^lu{#}:foo)/',
168  '/(?^lu)foo/' => 'Regexp modifiers "l" and "u" are mutually exclusive {#} m/(?^lu{#})foo/',
169 '/(?da:foo)/' => 'Regexp modifiers "d" and "a" are mutually exclusive {#} m/(?da{#}:foo)/',
170 '/(?lil:foo)/' => 'Regexp modifier "l" may not appear twice {#} m/(?lil{#}:foo)/',
171 '/(?aaia:foo)/' => 'Regexp modifier "a" may appear a maximum of twice {#} m/(?aaia{#}:foo)/',
172 '/(?i-l:foo)/' => 'Regexp modifier "l" may not appear after the "-" {#} m/(?i-l{#}:foo)/',
173
174  '/((x)/' => 'Unmatched ( {#} m/({#}(x)/',
175  '/{(}/' => 'Unmatched ( {#} m/{({#}}/',    # [perl #127599]
176
177  "/x{$inf_p1}/" => "Quantifier in {,} bigger than $inf_m1 {#} m/x{{#}$inf_p1}/",
178
179
180  '/x**/' => 'Nested quantifiers {#} m/x**{#}/',
181
182  '/x[/' => 'Unmatched [ {#} m/x[{#}/',
183
184  '/*/', => 'Quantifier follows nothing {#} m/*{#}/',
185
186  '/\p{x/' => 'Missing right brace on \p{} {#} m/\p{{#}x/',
187
188  '/[\p{x]/' => 'Missing right brace on \p{} {#} m/[\p{{#}x]/',
189
190  '/(x)\2/' => 'Reference to nonexistent group {#} m/(x)\2{#}/',
191
192  '/\g/' => 'Unterminated \g... pattern {#} m/\g{#}/',
193  '/\g{1/' => 'Unterminated \g{...} pattern {#} m/\g{1{#}/',
194
195  'my $m = "\\\"; $m =~ $m', => 'Trailing \ in regex m/\/',
196
197  '/\x{1/' => 'Missing right brace on \x{} {#} m/\x{1{#}/',
198  '/\x{X/' => 'Missing right brace on \x{} {#} m/\x{{#}X/',
199
200  '/[\x{X]/' => 'Missing right brace on \x{} {#} m/[\x{{#}X]/',
201  '/[\x{A]/' => 'Missing right brace on \x{} {#} m/[\x{A{#}]/',
202
203  '/\o{1/' => 'Missing right brace on \o{ {#} m/\o{1{#}/',
204  '/\o{X/' => 'Missing right brace on \o{ {#} m/\o{{#}X/',
205
206  '/[\o{X]/' => 'Missing right brace on \o{ {#} m/[\o{{#}X]/',
207  '/[\o{7]/' => 'Missing right brace on \o{ {#} m/[\o{7{#}]/',
208
209  '/[[:barf:]]/' => 'POSIX class [:barf:] unknown {#} m/[[:barf:]{#}]/',
210
211  '/[[=barf=]]/' => 'POSIX syntax [= =] is reserved for future extensions {#} m/[[=barf=]{#}]/',
212
213  '/[[.barf.]]/' => 'POSIX syntax [. .] is reserved for future extensions {#} m/[[.barf.]{#}]/',
214
215  '/[z-a]/' => 'Invalid [] range "z-a" {#} m/[z-a{#}]/',
216
217  '/\p/' => 'Empty \p {#} m/\p{#}/',
218  '/\P/' => 'Empty \P {#} m/\P{#}/',
219  '/\p{}/' => 'Empty \p{} {#} m/\p{{#}}/',
220  '/\P{}/' => 'Empty \P{} {#} m/\P{{#}}/',
221
222 '/a\b{cde/' => 'Missing right brace on \b{} {#} m/a\b{{#}cde/',
223 '/a\B{cde/' => 'Missing right brace on \B{} {#} m/a\B{{#}cde/',
224
225  '/\b{}/' => 'Empty \b{} {#} m/\b{}{#}/',
226  '/\B{}/' => 'Empty \B{} {#} m/\B{}{#}/',
227
228  '/\b{gc}/' => "'gc' is an unknown bound type {#} m/\\b{gc{#}}/",
229  '/\B{gc}/' => "'gc' is an unknown bound type {#} m/\\B{gc{#}}/",
230
231  '/(?[[[::]]])/' => "Unexpected ']' with no following ')' in (?[... {#} m/(?[[[::]]{#}])/",
232  '/(?[[[:w:]]])/' => "Unexpected ']' with no following ')' in (?[... {#} m/(?[[[:w:]]{#}])/",
233  '/(?[a])/' =>  'Unexpected character {#} m/(?[a{#}])/',
234  '/(?[ + \t ])/' => 'Unexpected binary operator \'+\' with no preceding operand {#} m/(?[ +{#} \t ])/',
235  '/(?[ \cK - ( + \t ) ])/' => 'Unexpected binary operator \'+\' with no preceding operand {#} m/(?[ \cK - ( +{#} \t ) ])/',
236  '/(?[ \cK ( \t ) ])/' => 'Unexpected \'(\' with no preceding operator {#} m/(?[ \cK ({#} \t ) ])/',
237  '/(?[ \cK \t ])/' => 'Operand with no preceding operator {#} m/(?[ \cK \t{#} ])/',
238  '/(?[ \0004 ])/' => 'Need exactly 3 octal digits {#} m/(?[ \0004 {#}])/',
239  '/(?[ \05 ])/' => 'Need exactly 3 octal digits {#} m/(?[ \05 {#}])/',
240  '/(?[ \o{1038} ])/' => 'Non-octal character {#} m/(?[ \o{1038{#}} ])/',
241  '/(?[ \o{} ])/' => 'Empty \o{} {#} m/(?[ \o{}{#} ])/',
242  '/(?[ \x{defg} ])/' => 'Non-hex character {#} m/(?[ \x{defg{#}} ])/',
243  '/(?[ \xabcdef ])/' => 'Use \\x{...} for more than two hex characters {#} m/(?[ \xabc{#}def ])/',
244  '/(?[ \x{} ])/' => 'Empty \x{} {#} m/(?[ \x{}{#} ])/',
245  '/(?[ \cK + ) ])/' => 'Unexpected \')\' {#} m/(?[ \cK + ){#} ])/',
246  '/(?[ \cK + ])/' => 'Incomplete expression within \'(?[ ])\' {#} m/(?[ \cK + {#}])/',
247  '/(?[ ( ) ])/' => 'Incomplete expression within \'(?[ ])\' {#} m/(?[ ( ){#} ])/',
248  '/(?[[0]+()+])/' => 'Incomplete expression within \'(?[ ])\' {#} m/(?[[0]+(){#}+])/',
249  '/(?[ \p{foo} ])/' => 'Can\'t find Unicode property definition "foo" {#} m/(?[ \p{foo}{#} ])/',
250  '/(?[ \p{ foo = bar } ])/' => 'Can\'t find Unicode property definition "foo = bar" {#} m/(?[ \p{ foo = bar }{#} ])/',
251  '/(?[ \8 ])/' => 'Unrecognized escape \8 in character class {#} m/(?[ \8{#} ])/',
252  '/(?[ \t ]/' => "Unexpected ']' with no following ')' in (?[... {#} m/(?[ \\t ]{#}/",
253  '/(?[ [ \t ]/' => "Syntax error in (?[...]) {#} m/(?[ [ \\t ]{#}/",
254  '/(?[ \t ] ]/' => "Unexpected ']' with no following ')' in (?[... {#} m/(?[ \\t ]{#} ]/",
255  '/(?[ [ ] ]/' => "Syntax error in (?[...]) {#} m/(?[ [ ] ]{#}/",
256  '/(?[ \t + \e # This was supposed to be a comment ])/' =>
257     "Syntax error in (?[...]) {#} m/(?[ \\t + \\e # This was supposed to be a comment ]){#}/",
258  '/(?[ ])/' => 'Incomplete expression within \'(?[ ])\' {#} m/(?[ {#}])/',
259  'm/(?[[a-\d]])/' => 'False [] range "a-\d" {#} m/(?[[a-\d{#}]])/',
260  'm/(?[[\w-x]])/' => 'False [] range "\w-" {#} m/(?[[\w-{#}x]])/',
261  'm/(?[[a-\pM]])/' => 'False [] range "a-\pM" {#} m/(?[[a-\pM{#}]])/',
262  'm/(?[[\pM-x]])/' => 'False [] range "\pM-" {#} m/(?[[\pM-{#}x]])/',
263  'm/(?[[^\N{LATIN CAPITAL LETTER A WITH MACRON AND GRAVE}]])/' => '\N{} here is restricted to one character {#} m/(?[[^\N{U+100.300{#}}]])/',
264  'm/(?[ \p{Digit} & (?^(?[ \p{Thai} | \p{Lao} ]))])/' => 'Sequence (?^(...) not recognized {#} m/(?[ \p{Digit} & (?^({#}?[ \p{Thai} | \p{Lao} ]))])/',
265  'm/(?[ \p{Digit} & (?(?[ \p{Thai} | \p{Lao} ]))])/' => 'Unexpected character {#} m/(?[ \p{Digit} & (?{#}(?[ \p{Thai} | \p{Lao} ]))])/',
266  'm/\o{/' => 'Missing right brace on \o{ {#} m/\o{{#}/',
267  'm/\o/' => 'Missing braces on \o{} {#} m/\o{#}/',
268  'm/\o{}/' => 'Empty \o{} {#} m/\o{}{#}/',
269  'm/[\o{]/' => 'Missing right brace on \o{ {#} m/[\o{{#}]/',
270  'm/[\o]/' => 'Missing braces on \o{} {#} m/[\o{#}]/',
271  'm/[\o{}]/' => 'Empty \o{} {#} m/[\o{}{#}]/',
272  'm/(?^-i:foo)/' => 'Sequence (?^-...) not recognized {#} m/(?^-{#}i:foo)/',
273  'm/\87/' => 'Reference to nonexistent group {#} m/\87{#}/',
274  'm/a\87/' => 'Reference to nonexistent group {#} m/a\87{#}/',
275  'm/a\97/' => 'Reference to nonexistent group {#} m/a\97{#}/',
276  'm/(*DOOF)/' => 'Unknown verb pattern \'DOOF\' {#} m/(*DOOF){#}/',
277  'm/(?&a/'  => 'Sequence (?&... not terminated {#} m/(?&a{#}/',
278  'm/(?P=/' => 'Sequence ?P=... not terminated {#} m/(?P={#}/',
279  "m/(?'/"  => "Sequence (?'... not terminated {#} m/(?'{#}/",
280  "m/(?</"  => "Sequence (?<... not terminated {#} m/(?<{#}/",
281  'm/(?&/'  => 'Sequence (?&... not terminated {#} m/(?&{#}/',
282  'm/(?(</' => 'Sequence (?(<... not terminated {#} m/(?(<{#}/',
283  "m/(?('/" => "Sequence (?('... not terminated {#} m/(?('{#}/",
284  'm/\g{/'  => 'Sequence \g{... not terminated {#} m/\g{{#}/',
285  'm/\k</'  => 'Sequence \k<... not terminated {#} m/\k<{#}/',
286  'm/\cß/' => "Character following \"\\c\" must be printable ASCII",
287  '/((?# This is a comment in the middle of a token)?:foo)/' => 'In \'(?...)\', the \'(\' and \'?\' must be adjacent {#} m/((?# This is a comment in the middle of a token)?{#}:foo)/',
288  '/((?# This is a comment in the middle of a token)*FAIL)/' => 'In \'(*VERB...)\', the \'(\' and \'*\' must be adjacent {#} m/((?# This is a comment in the middle of a token)*{#}FAIL)/',
289  '/((?# This is a comment in the middle of a token)*script_run:foo)/' => 'In \'(*...)\', the \'(\' and \'*\' must be adjacent {#} m/((?# This is a comment in the middle of a token)*{#}script_run:foo)/',
290
291  '/(*script_runfoo)/' => 'Unknown \'(*...)\' construct \'script_runfoo\' {#} m/(*script_runfoo){#}/',
292  '/(*srfoo)/' => 'Unknown \'(*...)\' construct \'srfoo\' {#} m/(*srfoo){#}/',
293  '/(*script_run)/' => '\'(*script_run\' requires a terminating \':\' {#} m/(*script_run{#})/',
294  '/(*sr)/' => '\'(*sr\' requires a terminating \':\' {#} m/(*sr{#})/',
295  '/(*pla)/' => '\'(*pla\' requires a terminating \':\' {#} m/(*pla{#})/',
296  '/(*script_run/' => 'Unterminated \'(*...\' construct {#} m/(*script_run{#}/',
297  '/(*sr/' => 'Unterminated \'(*...\' construct {#} m/(*sr{#}/',
298  '/(*script_run:foo/' => 'Unterminated \'(*...\' argument {#} m/(*script_run:foo{#}/',
299  '/(*sr:foo/' => 'Unterminated \'(*...\' argument {#} m/(*sr:foo{#}/',
300  '/(?[\ &!])/' => 'Incomplete expression within \'(?[ ])\' {#} m/(?[\ &!{#}])/',    # [perl #126180]
301  '/(?[\ +!])/' => 'Incomplete expression within \'(?[ ])\' {#} m/(?[\ +!{#}])/',    # [perl #126180]
302  '/(?[\ -!])/' => 'Incomplete expression within \'(?[ ])\' {#} m/(?[\ -!{#}])/',    # [perl #126180]
303  '/(?[\ ^!])/' => 'Incomplete expression within \'(?[ ])\' {#} m/(?[\ ^!{#}])/',    # [perl #126180]
304  '/(?[\ |!])/' => 'Incomplete expression within \'(?[ ])\' {#} m/(?[\ |!{#}])/',    # [perl #126180]
305  '/(?[()-!])/' => 'Incomplete expression within \'(?[ ])\' {#} m/(?[(){#}-!])/',    # [perl #126204]
306  '/(?[!()])/' => 'Incomplete expression within \'(?[ ])\' {#} m/(?[!(){#}])/',      # [perl #126404]
307  '/\w{/' => 'Unescaped left brace in regex is illegal here {#} m/\w{{#}/',
308  '/\q{/' => 'Unescaped left brace in regex is illegal here {#} m/\q{{#}/',
309  '/\A{/' => 'Unescaped left brace in regex is illegal here {#} m/\A{{#}/',
310  '/.{, 4 }/' => 'Unescaped left brace in regex is illegal here {#} m/.{{#}, 4 }/',
311  '/[x]{, 4}/'       => 'Unescaped left brace in regex is illegal here {#} m/[x]{{#}, 4}/',
312  '/\p{Latin}{,4 }/' => 'Unescaped left brace in regex is illegal here {#} m/\p{Latin}{{#},4 }/',
313  '/(?<=/' => 'Sequence (?... not terminated {#} m/(?<={#}/',                        # [perl #128170]
314  '/\p{vertical \v tab}/' => 'Can\'t find Unicode property definition "vertical \v tab" {#} m/\\p{vertical \v tab}{#}/', # [perl #132055]
315  "/$bug133423/" => "Operand with no preceding operator {#} m/(?[(?^:(?[\\\0]))\\{#}]\0|2[^^]\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80])R.\\670/",
316  '/[^/' => 'Unmatched [ {#} m/[{#}^/', # [perl #133767]
317  '/\p{Is_Other_Alphabetic=F}/ ' => 'Can\'t find Unicode property definition "Is_Other_Alphabetic=F" {#} m/\p{Is_Other_Alphabetic=F}{#}/',
318  '/\p{Is_Other_Alphabetic=F}/ ' => 'Can\'t find Unicode property definition "Is_Other_Alphabetic=F" {#} m/\p{Is_Other_Alphabetic=F}{#}/',
319  '/\x{100}(?(/' => 'Unknown switch condition (?(...)) {#} m/\\x{100}(?({#}/', # [perl #133896]
320  '/(?[\N{KEYCAP DIGIT NINE}/' => '\N{} here is restricted to one character {#} m/(?[\\N{U+39.FE0F.20E3{#}}/', # [perl #133988]
321  '/0000000000000000[\N{U+0.00}0000/' => 'Unmatched [ {#} m/0000000000000000[{#}\N{U+0.00}0000/', # [perl #134059]
322 );
323
324 # These are messages that are death under 'use re "strict"', and may or may
325 # not warn otherwise.  See comment before @warning as to why some have a
326 # \x{100} in them.  This array has 3 elements per construct.  [0] is the regex
327 # to use; [1] is the message under no strict (empty to not warn), and [2] is
328 # under strict.
329 my @death_only_under_strict = (
330     'm/\xABC/' => "",
331                => 'Use \x{...} for more than two hex characters {#} m/\xABC{#}/',
332     'm/[\xABC]/' => "",
333                  => 'Use \x{...} for more than two hex characters {#} m/[\xABC{#}]/',
334
335     # XXX This is a confusing error message.  The G isn't ignored; it just
336     # terminates the \x.  Also some messages below are missing the <-- HERE,
337     # aren't all category 'regexp'.  (Hence we have to turn off 'digit'
338     # messages as well below)
339     'm/\xAG/' => 'Illegal hexadecimal digit \'G\' ignored',
340               => 'Non-hex character {#} m/\xAG{#}/',
341     'm/[\xAG]/' => 'Illegal hexadecimal digit \'G\' ignored',
342                 => 'Non-hex character {#} m/[\xAG{#}]/',
343     'm/\o{789}/' => 'Non-octal character \'8\'.  Resolved as "\o{7}"',
344                  => 'Non-octal character {#} m/\o{78{#}9}/',
345     'm/[\o{789}]/' => 'Non-octal character \'8\'.  Resolved as "\o{7}"',
346                    => 'Non-octal character {#} m/[\o{78{#}9}]/',
347     'm/\x{}/' => "",
348               => 'Empty \x{} {#} m/\x{}{#}/',
349     'm/[\x{}]/' => "",
350                 => 'Empty \x{} {#} m/[\x{}{#}]/',
351     'm/\x{ABCDEFG}/' => 'Illegal hexadecimal digit \'G\' ignored',
352                      => 'Non-hex character {#} m/\x{ABCDEFG{#}}/',
353     'm/[\x{ABCDEFG}]/' => 'Illegal hexadecimal digit \'G\' ignored',
354                        => 'Non-hex character {#} m/[\x{ABCDEFG{#}}]/',
355     "m'[\\y]\\x{100}'" => 'Unrecognized escape \y in character class passed through {#} m/[\y{#}]\x{100}/',
356                        => 'Unrecognized escape \y in character class {#} m/[\y{#}]\x{100}/',
357     'm/[a-\d]\x{100}/' => 'False [] range "a-\d" {#} m/[a-\d{#}]\x{100}/',
358                        => 'False [] range "a-\d" {#} m/[a-\d{#}]\x{100}/',
359     'm/[\w-x]\x{100}/' => 'False [] range "\w-" {#} m/[\w-{#}x]\x{100}/',
360                        => 'False [] range "\w-" {#} m/[\w-{#}x]\x{100}/',
361     'm/[a-\pM]\x{100}/' => 'False [] range "a-\pM" {#} m/[a-\pM{#}]\x{100}/',
362                         => 'False [] range "a-\pM" {#} m/[a-\pM{#}]\x{100}/',
363     'm/[\pM-x]\x{100}/' => 'False [] range "\pM-" {#} m/[\pM-{#}x]\x{100}/',
364                         => 'False [] range "\pM-" {#} m/[\pM-{#}x]\x{100}/',
365     'm/[^\N{LATIN CAPITAL LETTER A WITH MACRON AND GRAVE}]/' => 'Using just the first character returned by \N{} in character class {#} m/[^\N{U+100.300}{#}]/',
366                                        => '\N{} here is restricted to one character {#} m/[^\N{U+100.300{#}}]/',
367     'm/[\x03-\N{LATIN CAPITAL LETTER A WITH MACRON AND GRAVE}]/' => 'Using just the first character returned by \N{} in character class {#} m/[\x03-\N{U+100.300}{#}]/',
368                                             => '\N{} here is restricted to one character {#} m/[\x03-\N{U+100.300{#}}]/',
369     'm/[\N{LATIN CAPITAL LETTER A WITH MACRON AND GRAVE}-\x{10FFFF}]/' => 'Using just the first character returned by \N{} in character class {#} m/[\N{U+100.300}{#}-\x{10FFFF}]/',
370                                                   => '\N{} here is restricted to one character {#} m/[\N{U+100.300{#}}-\x{10FFFF}]/',
371     '/[\08]/'   => '\'\08\' resolved to \'\o{0}8\' {#} m/[\08{#}]/',
372                 => 'Need exactly 3 octal digits {#} m/[\08{#}]/',
373     '/[\018]/'  => '\'\018\' resolved to \'\o{1}8\' {#} m/[\018{#}]/',
374                 => 'Need exactly 3 octal digits {#} m/[\018{#}]/',
375     '/[\_\0]/'  => "",
376                 => 'Need exactly 3 octal digits {#} m/[\_\0]{#}/',
377     '/[\07]/'   => "",
378                 => 'Need exactly 3 octal digits {#} m/[\07]{#}/',
379     '/[\0005]/' => "",
380                 => 'Need exactly 3 octal digits {#} m/[\0005]{#}/',
381     '/[\8\9]\x{100}/' => ['Unrecognized escape \8 in character class passed through {#} m/[\8{#}\9]\x{100}/',
382                           'Unrecognized escape \9 in character class passed through {#} m/[\8\9{#}]\x{100}/',
383                          ],
384                       => 'Unrecognized escape \8 in character class {#} m/[\8{#}\9]\x{100}/',
385     '/[a-\d]\x{100}/' => 'False [] range "a-\d" {#} m/[a-\d{#}]\x{100}/',
386                       => 'False [] range "a-\d" {#} m/[a-\d{#}]\x{100}/',
387     '/[\d-b]\x{100}/' => 'False [] range "\d-" {#} m/[\d-{#}b]\x{100}/',
388                       => 'False [] range "\d-" {#} m/[\d-{#}b]\x{100}/',
389     '/[\s-\d]\x{100}/' => 'False [] range "\s-" {#} m/[\s-{#}\d]\x{100}/',
390                        => 'False [] range "\s-" {#} m/[\s-{#}\d]\x{100}/',
391     '/[\d-\s]\x{100}/' => 'False [] range "\d-" {#} m/[\d-{#}\s]\x{100}/',
392                        => 'False [] range "\d-" {#} m/[\d-{#}\s]\x{100}/',
393     '/[a-[:digit:]]\x{100}/' => 'False [] range "a-[:digit:]" {#} m/[a-[:digit:]{#}]\x{100}/',
394                              => 'False [] range "a-[:digit:]" {#} m/[a-[:digit:]{#}]\x{100}/',
395     '/[[:digit:]-b]\x{100}/' => 'False [] range "[:digit:]-" {#} m/[[:digit:]-{#}b]\x{100}/',
396                              => 'False [] range "[:digit:]-" {#} m/[[:digit:]-{#}b]\x{100}/',
397     '/[[:alpha:]-[:digit:]]\x{100}/' => 'False [] range "[:alpha:]-" {#} m/[[:alpha:]-{#}[:digit:]]\x{100}/',
398                                      => 'False [] range "[:alpha:]-" {#} m/[[:alpha:]-{#}[:digit:]]\x{100}/',
399     '/[[:digit:]-[:alpha:]]\x{100}/' => 'False [] range "[:digit:]-" {#} m/[[:digit:]-{#}[:alpha:]]\x{100}/',
400                                      => 'False [] range "[:digit:]-" {#} m/[[:digit:]-{#}[:alpha:]]\x{100}/',
401     '/[a\zb]\x{100}/' => 'Unrecognized escape \z in character class passed through {#} m/[a\z{#}b]\x{100}/',
402                       => 'Unrecognized escape \z in character class {#} m/[a\z{#}b]\x{100}/',
403     '/[a\fb]/'          => "",
404                         => 'Literal vertical space in [] is illegal except under /x {#} m/[a\f{#}b]/',
405     '/:{4,a}/'     => 'Unescaped left brace in regex is passed through {#} m/:{{#}4,a}/',
406                    => 'Unescaped left brace in regex is illegal here {#} m/:{{#}4,a}/',
407     '/xa{3\,4}y/'  => 'Unescaped left brace in regex is passed through {#} m/xa{{#}3\,4}y/',
408                    => 'Unescaped left brace in regex is illegal here {#} m/xa{{#}3\,4}y/',
409     '/\\${[^\\}]*}/' => 'Unescaped left brace in regex is passed through {#} m/\\${{#}[^\\}]*}/',
410                      => 'Unescaped left brace in regex is illegal here {#} m/\\${{#}[^\\}]*}/',
411     '/.{/'         => 'Unescaped left brace in regex is passed through {#} m/.{{#}/',
412                    => 'Unescaped left brace in regex is illegal here {#} m/.{{#}/',
413     '/[x]{/'       => 'Unescaped left brace in regex is passed through {#} m/[x]{{#}/',
414                    => 'Unescaped left brace in regex is illegal here {#} m/[x]{{#}/',
415     '/\p{Latin}{/' => 'Unescaped left brace in regex is passed through {#} m/\p{Latin}{{#}/',
416                    => 'Unescaped left brace in regex is illegal here {#} m/\p{Latin}{{#}/',
417     '/\x{100}\x/'  => "",
418                    => "Empty \\x {#} m/\\x{100}\\x{#}/",
419 );
420
421 # These need the character 'ネ' as a marker for mark_as_utf8()
422 my @death_utf8 = mark_as_utf8(
423  '/ネ(?<= .*)/' =>  'Lookbehind longer than 255 not implemented in regex m/ネ(?<= .*)/',
424
425  '/(?<= ネ{1000})/' => 'Lookbehind longer than 255 not implemented in regex m/(?<= ネ{1000})/',
426
427  '/ネ(?ネ)ネ/' => 'Sequence (?ネ...) not recognized {#} m/ネ(?ネ{#})ネ/',
428
429  '/ネ(?(1ネ))ネ/' => 'Switch condition not recognized {#} m/ネ(?(1ネ{#}))ネ/',
430
431  '/(?(1)ネ|y|ヌ)/' => 'Switch (?(condition)... contains too many branches {#} m/(?(1)ネ|y|{#}ヌ)/',
432
433  '/(?(ネ)y|ネ)/' => 'Unknown switch condition (?(...)) {#} m/(?(ネ{#})y|ネ)/',
434
435  '/ネ(?/' => 'Sequence (? incomplete {#} m/ネ(?{#}/',
436
437  '/ネ(?;ネ/' => 'Sequence (?;...) not recognized {#} m/ネ(?;{#}ネ/',
438  '/ネ(?<;ネ/' => 'Group name must start with a non-digit word character {#} m/ネ(?<;{#}ネ/',
439  '/ネ(?\ixネ/' => 'Sequence (?\...) not recognized {#} m/ネ(?\{#}ixネ/',
440  '/ネ(?^lu:ネ)/' => 'Regexp modifiers "l" and "u" are mutually exclusive {#} m/ネ(?^lu{#}:ネ)/',
441 '/ネ(?lil:ネ)/' => 'Regexp modifier "l" may not appear twice {#} m/ネ(?lil{#}:ネ)/',
442 '/ネ(?aaia:ネ)/' => 'Regexp modifier "a" may appear a maximum of twice {#} m/ネ(?aaia{#}:ネ)/',
443 '/ネ(?i-l:ネ)/' => 'Regexp modifier "l" may not appear after the "-" {#} m/ネ(?i-l{#}:ネ)/',
444
445  '/ネ((ネ)/' => 'Unmatched ( {#} m/ネ({#}(ネ)/',
446
447  "/ネ{$inf_p1}ネ/" => "Quantifier in {,} bigger than $inf_m1 {#} m/ネ{{#}$inf_p1}ネ/",
448
449
450  '/ネ**ネ/' => 'Nested quantifiers {#} m/ネ**{#}ネ/',
451
452  '/ネ[ネ/' => 'Unmatched [ {#} m/ネ[{#}ネ/',
453
454  '/*ネ/', => 'Quantifier follows nothing {#} m/*{#}ネ/',
455
456  '/ネ\p{ネ/' => 'Missing right brace on \p{} {#} m/ネ\p{{#}ネ/',
457
458  '/(ネ)\2ネ/' => 'Reference to nonexistent group {#} m/(ネ)\2{#}ネ/',
459
460  '/\g{ネ/; #no latin1' => 'Sequence \g{... not terminated {#} m/\g{ネ{#}/',
461
462  'my $m = "ネ\\\"; $m =~ $m', => 'Trailing \ in regex m/ネ\/',
463
464  '/\x{ネ/' => 'Missing right brace on \x{} {#} m/\x{{#}ネ/',
465  '/ネ[\x{ネ]ネ/' => 'Missing right brace on \x{} {#} m/ネ[\x{{#}ネ]ネ/',
466  '/ネ[\x{ネ]/' => 'Missing right brace on \x{} {#} m/ネ[\x{{#}ネ]/',
467
468  '/ネ\o{ネ/' => 'Missing right brace on \o{ {#} m/ネ\o{{#}ネ/',
469
470  '/[ネ-a]ネ/' => 'Invalid [] range "ネ-a" {#} m/[ネ-a{#}]ネ/',
471
472  '/ネ\p{}ネ/' => 'Empty \p{} {#} m/ネ\p{{#}}ネ/',
473
474  '/ネ(?[[[:ネ]]])ネ/' => "Unexpected ']' with no following ')' in (?[... {#} m/ネ(?[[[:ネ]]{#}])ネ/",
475  '/ネ(?[[[:ネ: ])ネ/' => "Syntax error in (?[...]) {#} m/ネ(?[[[:ネ: ])ネ{#}/",
476  '/ネ(?[[[::]]])ネ/' => "Unexpected ']' with no following ')' in (?[... {#} m/ネ(?[[[::]]{#}])ネ/",
477  '/ネ(?[[[:ネ:]]])ネ/' => "Unexpected ']' with no following ')' in (?[... {#} m/ネ(?[[[:ネ:]]{#}])ネ/",
478  '/ネ(?[ネ])ネ/' =>  'Unexpected character {#} m/ネ(?[ネ{#}])ネ/',
479  '/ネ(?[ + [ネ] ])/' => 'Unexpected binary operator \'+\' with no preceding operand {#} m/ネ(?[ +{#} [ネ] ])/',
480  '/ネ(?[ \cK - ( + [ネ] ) ])/' => 'Unexpected binary operator \'+\' with no preceding operand {#} m/ネ(?[ \cK - ( +{#} [ネ] ) ])/',
481  '/ネ(?[ \cK ( [ネ] ) ])/' => 'Unexpected \'(\' with no preceding operator {#} m/ネ(?[ \cK ({#} [ネ] ) ])/',
482  '/ネ(?[ \cK [ネ] ])ネ/' => 'Operand with no preceding operator {#} m/ネ(?[ \cK [ネ{#}] ])ネ/',
483  '/ネ(?[ \0004 ])ネ/' => 'Need exactly 3 octal digits {#} m/ネ(?[ \0004 {#}])ネ/',
484  '/(?[ \o{ネ} ])ネ/' => 'Non-octal character {#} m/(?[ \o{ネ{#}} ])ネ/',
485  '/ネ(?[ \o{} ])ネ/' => 'Empty \o{} {#} m/ネ(?[ \o{}{#} ])ネ/',
486  '/(?[ \x{ネ} ])ネ/' => 'Non-hex character {#} m/(?[ \x{ネ{#}} ])ネ/',
487  '/(?[ \p{ネ} ])/' => 'Can\'t find Unicode property definition "ネ" {#} m/(?[ \p{ネ}{#} ])/',
488  '/(?[ \p{ ネ = bar } ])/' => 'Can\'t find Unicode property definition "ネ = bar" {#} m/(?[ \p{ ネ = bar }{#} ])/',
489  '/ネ(?[ \t ]/' => "Unexpected ']' with no following ')' in (?[... {#} m/ネ(?[ \\t ]{#}/",
490  '/(?[ \t + \e # ネ This was supposed to be a comment ])/' =>
491     "Syntax error in (?[...]) {#} m/(?[ \\t + \\e # ネ This was supposed to be a comment ]){#}/",
492  'm/(*ネ)ネ/' => q<Unknown '(*...)' construct 'ネ' {#} m/(*ネ){#}ネ/>,
493  '/\cネ/' => "Character following \"\\c\" must be printable ASCII",
494  '/\b{ネ}/' => "'ネ' is an unknown bound type {#} m/\\b{ネ{#}}/",
495  '/\B{ネ}/' => "'ネ' is an unknown bound type {#} m/\\B{ネ{#}}/",
496 );
497 push @death, @death_utf8;
498
499 my @death_utf8_only_under_strict = (
500     "m'ネ[\\y]ネ'" => 'Unrecognized escape \y in character class passed through {#} m/ネ[\y{#}]ネ/',
501                    => 'Unrecognized escape \y in character class {#} m/ネ[\y{#}]ネ/',
502     'm/ネ[ネ-\d]ネ/' => 'False [] range "ネ-\d" {#} m/ネ[ネ-\d{#}]ネ/',
503                      => 'False [] range "ネ-\d" {#} m/ネ[ネ-\d{#}]ネ/',
504     'm/ネ[\w-ネ]ネ/' => 'False [] range "\w-" {#} m/ネ[\w-{#}ネ]ネ/',
505                      => 'False [] range "\w-" {#} m/ネ[\w-{#}ネ]ネ/',
506     'm/ネ[ネ-\pM]ネ/' => 'False [] range "ネ-\pM" {#} m/ネ[ネ-\pM{#}]ネ/',
507                       => 'False [] range "ネ-\pM" {#} m/ネ[ネ-\pM{#}]ネ/',
508     '/ネ[ネ-[:digit:]]ネ/' => 'False [] range "ネ-[:digit:]" {#} m/ネ[ネ-[:digit:]{#}]ネ/',
509                            => 'False [] range "ネ-[:digit:]" {#} m/ネ[ネ-[:digit:]{#}]ネ/',
510     '/ネ[\d-\s]ネ/' => 'False [] range "\d-" {#} m/ネ[\d-{#}\s]ネ/',
511                     => 'False [] range "\d-" {#} m/ネ[\d-{#}\s]ネ/',
512     '/ネ[a\zb]ネ/' => 'Unrecognized escape \z in character class passed through {#} m/ネ[a\z{#}b]ネ/',
513                    => 'Unrecognized escape \z in character class {#} m/ネ[a\z{#}b]ネ/',
514 );
515 # Tests involving a user-defined charnames translator are in pat_advanced.t
516
517 # In the following arrays of warnings, the value can be an array of things to
518 # expect.  If the empty string, it means no warning should be raised.
519
520
521 # Key-value pairs of code/error of code that should have non-fatal regexp
522 # warnings.  Most currently have \x{100} appended to them to force them to be
523 # upgraded to UTF-8, and the first pass restarted.  Previously this would
524 # cause some warnings to be output twice.  This tests that that behavior has
525 # been fixed.
526
527 my @warning = (
528     'm/\b*\x{100}/' => '\b* matches null string many times {#} m/\b*{#}\x{100}/',
529     '/\b{g}/a' => "Using /u for '\\b{g}' instead of /a {#} m/\\b{g}{#}/",
530     '/\B{gcb}/a' => "Using /u for '\\B{gcb}' instead of /a {#} m/\\B{gcb}{#}/",
531     'm/[:blank:]\x{100}/' => 'POSIX syntax [: :] belongs inside character classes {#} m/[:blank:]{#}\x{100}/',
532     'm/[[:cntrl:]][:^ascii:]\x{100}/' =>  'POSIX syntax [: :] belongs inside character classes {#} m/[[:cntrl:]][:^ascii:]{#}\x{100}/',
533     'm/[[:ascii]]\x{100}/' => "Assuming NOT a POSIX class since there is no terminating ':' {#} m/[[:ascii{#}]]\\x{100}/",
534     'm/(?[[:word]])\x{100}/' => "Assuming NOT a POSIX class since there is no terminating ':' {#} m/(?[[:word{#}]])\\x{100}/",
535     "m'\\y\\x{100}'"     => 'Unrecognized escape \y passed through {#} m/\y{#}\x{100}/',
536     '/x{3,1}/'   => 'Quantifier {n,m} with n > m can\'t match {#} m/x{3,1}{#}/',
537     '/\08/' => '\'\08\' resolved to \'\o{0}8\' {#} m/\08{#}/',
538     '/\018/' => '\'\018\' resolved to \'\o{1}8\' {#} m/\018{#}/',
539     '/(?=a)*/' => '(?=a)* matches null string many times {#} m/(?=a)*{#}/',
540     'my $x = \'\m\'; qr/a$x/' => 'Unrecognized escape \m passed through {#} m/a\m{#}/',
541     '/\q/' => 'Unrecognized escape \q passed through {#} m/\q{#}/',
542     '/\q\p{Any}/' => 'Unrecognized escape \q passed through {#} m/\q{#}\p{Any}/',
543
544     # These two tests do not include the marker, because regcomp.c no
545     # longer knows where it goes by the time this warning is emitted.
546     # See [perl #122680] regcomp warning gives wrong position of
547     # problem.
548     '/(?=a){1,3}\x{100}/' => 'Quantifier unexpected on zero-length expression in regex m/(?=a){1,3}\x{100}/',
549     '/(a|b)(?=a){3}\x{100}/' => 'Quantifier unexpected on zero-length expression in regex m/(a|b)(?=a){3}\x{100}/',
550
551     '/\_/' => "",
552     '/[\006]/' => "",
553     '/[:alpha:]\x{100}/' => 'POSIX syntax [: :] belongs inside character classes {#} m/[:alpha:]{#}\x{100}/',
554     '/[:zog:]\x{100}/' => 'POSIX syntax [: :] belongs inside character classes (but this one isn\'t fully valid) {#} m/[:zog:]{#}\x{100}/',
555     '/[.zog.]\x{100}/' => 'POSIX syntax [. .] belongs inside character classes (but this one isn\'t implemented) {#} m/[.zog.]{#}\x{100}/',
556     '/[a-b]/' => "",
557     '/(?c)\x{100}/' => 'Useless (?c) - use /gc modifier {#} m/(?c{#})\x{100}/',
558     '/(?-c)\x{100}/' => 'Useless (?-c) - don\'t use /gc modifier {#} m/(?-c{#})\x{100}/',
559     '/(?g)\x{100}/' => 'Useless (?g) - use /g modifier {#} m/(?g{#})\x{100}/',
560     '/(?-g)\x{100}/' => 'Useless (?-g) - don\'t use /g modifier {#} m/(?-g{#})\x{100}/',
561     '/(?o)\x{100}/' => 'Useless (?o) - use /o modifier {#} m/(?o{#})\x{100}/',
562     '/(?-o)\x{100}/' => 'Useless (?-o) - don\'t use /o modifier {#} m/(?-o{#})\x{100}/',
563     '/(?g-o)\x{100}/' => [ 'Useless (?g) - use /g modifier {#} m/(?g{#}-o)\x{100}/',
564                     'Useless (?-o) - don\'t use /o modifier {#} m/(?g-o{#})\x{100}/',
565                   ],
566     '/(?g-c)\x{100}/' => [ 'Useless (?g) - use /g modifier {#} m/(?g{#}-c)\x{100}/',
567                     'Useless (?-c) - don\'t use /gc modifier {#} m/(?g-c{#})\x{100}/',
568                   ],
569       # (?c) means (?g) error won't be thrown
570      '/(?o-cg)\x{100}/' => [ 'Useless (?o) - use /o modifier {#} m/(?o{#}-cg)\x{100}/',
571                       'Useless (?-c) - don\'t use /gc modifier {#} m/(?o-c{#}g)\x{100}/',
572                     ],
573     '/(?ogc)\x{100}/' => [ 'Useless (?o) - use /o modifier {#} m/(?o{#}gc)\x{100}/',
574                     'Useless (?g) - use /g modifier {#} m/(?og{#}c)\x{100}/',
575                     'Useless (?c) - use /gc modifier {#} m/(?ogc{#})\x{100}/',
576                   ],
577     '/a{1,1}?\x{100}/' => 'Useless use of greediness modifier \'?\' {#} m/a{1,1}?{#}\x{100}/',
578     "/(?[ [ % - % ] ])/" => "",
579     "/(?[ [ : - \\x$colon_hex ] ])\\x{100}/" => "\": - \\x$colon_hex \" is more clearly written simply as \":\" {#} m/(?[ [ : - \\x$colon_hex {#}] ])\\x{100}/",
580     "/(?[ [ \\x$colon_hex - : ] ])\\x{100}/" => "\"\\x$colon_hex\ - : \" is more clearly written simply as \":\" {#} m/(?[ [ \\x$colon_hex - : {#}] ])\\x{100}/",
581     "/(?[ [ \\t - \\x$tab_hex ] ])\\x{100}/" => "\"\\t - \\x$tab_hex \" is more clearly written simply as \"\\t\" {#} m/(?[ [ \\t - \\x$tab_hex {#}] ])\\x{100}/",
582     "/(?[ [ \\x$tab_hex - \\t ] ])\\x{100}/" => "\"\\x$tab_hex\ - \\t \" is more clearly written simply as \"\\t\" {#} m/(?[ [ \\x$tab_hex - \\t {#}] ])\\x{100}/",
583     "/(?[ [ $B_hex - C ] ])/" => "Ranges of ASCII printables should be some subset of \"0-9\", \"A-Z\", or \"a-z\" {#} m/(?[ [ $B_hex - C {#}] ])/",
584     "/(?[ [ A - $B_hex ] ])/" => "Ranges of ASCII printables should be some subset of \"0-9\", \"A-Z\", or \"a-z\" {#} m/(?[ [ A - $B_hex {#}] ])/",
585     "/(?[ [ $low_mixed_alpha - $high_mixed_alpha ] ])/" => "Ranges of ASCII printables should be some subset of \"0-9\", \"A-Z\", or \"a-z\" {#} m/(?[ [ $low_mixed_alpha - $high_mixed_alpha {#}] ])/",
586     "/(?[ [ $low_mixed_digit - $high_mixed_digit ] ])/" => "Ranges of ASCII printables should be some subset of \"0-9\", \"A-Z\", or \"a-z\" {#} m/(?[ [ $low_mixed_digit - $high_mixed_digit {#}] ])/",
587     "/[alnum]/" => "",
588     "/[^alnum]/" => "",
589     '/[:blank]\x{100}/' => 'POSIX syntax [: :] belongs inside character classes (but this one isn\'t fully valid) {#} m/[:blank{#}]\x{100}/',
590     '/[[:digit]]\x{100}/' => 'Assuming NOT a POSIX class since there is no terminating \':\' {#} m/[[:digit{#}]]\x{100}/', # [perl # 8904]
591     '/[[:digit:foo]\x{100}/' => 'Assuming NOT a POSIX class since there is no terminating \']\' {#} m/[[:digit:{#}foo]\x{100}/',
592     '/[[:di#it:foo]\x{100}/x' => 'Assuming NOT a POSIX class since there is no terminating \']\' {#} m/[[:di#it:{#}foo]\x{100}/',
593     '/[[:dgit]]\x{100}/' => 'Assuming NOT a POSIX class since there is no terminating \':\' {#} m/[[:dgit{#}]]\x{100}/',
594     '/[[:dgit:foo]\x{100}/' => 'Assuming NOT a POSIX class since there is no terminating \']\' {#} m/[[:dgit:{#}foo]\x{100}/',
595     '/[[:dgt]]\x{100}/' => "",      # Far enough away from a real class to not be recognized as one
596     '/[[:dgt:foo]\x{100}/' => "",
597     '/[[:DIGIT]]\x{100}/' => [ 'Assuming NOT a POSIX class since the name must be all lowercase letters {#} m/[[:DIGIT{#}]]\x{100}/',
598                                'Assuming NOT a POSIX class since there is no terminating \':\' {#} m/[[:DIGIT{#}]]\x{100}/',
599                            ],
600     '/[[digit]\x{100}/' => [ 'Assuming NOT a POSIX class since there must be a starting \':\' {#} m/[[{#}digit]\x{100}/',
601                              'Assuming NOT a POSIX class since there is no terminating \':\' {#} m/[[digit{#}]\x{100}/',
602                            ],
603     '/[[alpha]]\x{100}/' => [ 'Assuming NOT a POSIX class since there must be a starting \':\' {#} m/[[{#}alpha]]\x{100}/',
604                               'Assuming NOT a POSIX class since there is no terminating \':\' {#} m/[[alpha{#}]]\x{100}/',
605                            ],
606     '/[[^word]\x{100}/' => [ 'Assuming NOT a POSIX class since the \'^\' must come after the colon {#} m/[[^{#}word]\x{100}/',
607                               'Assuming NOT a POSIX class since there must be a starting \':\' {#} m/[[^{#}word]\x{100}/',
608                               'Assuming NOT a POSIX class since there is no terminating \':\' {#} m/[[^word{#}]\x{100}/',
609                             ],
610     '/[[   ^   :   x d i g i t   :   ]   ]\x{100}/' => [ 'Assuming NOT a POSIX class since no blanks are allowed in one {#} m/[[   {#}^   :   x d i g i t   :   ]   ]\x{100}/',
611                                                'Assuming NOT a POSIX class since the \'^\' must come after the colon {#} m/[[   ^{#}   :   x d i g i t   :   ]   ]\x{100}/',
612                                                'Assuming NOT a POSIX class since no blanks are allowed in one {#} m/[[   ^   {#}:   x d i g i t   :   ]   ]\x{100}/',
613                                                'Assuming NOT a POSIX class since no blanks are allowed in one {#} m/[[   ^   :   {#}x d i g i t   :   ]   ]\x{100}/',
614                                                'Assuming NOT a POSIX class since no blanks are allowed in one {#} m/[[   ^   :   x d i g i t   :   ]{#}   ]\x{100}/',
615                                                $only_strict_marker . 'Unescaped literal \']\' {#} m/[[   ^   :   x d i g i t   :   ]   ]{#}\x{100}/',
616                             ],
617     '/[foo:lower:]]\x{100}/' => 'Assuming NOT a POSIX class since it doesn\'t start with a \'[\' {#} m/[foo{#}:lower:]]\x{100}/',
618     '/[[;upper;]]\x{100}/' => [ 'Assuming NOT a POSIX class since a semi-colon was found instead of a colon {#} m/[[;{#}upper;]]\x{100}/',
619                                 'Assuming NOT a POSIX class since a semi-colon was found instead of a colon {#} m/[[;upper;]{#}]\x{100}/',
620                               ],
621     '/[foo;punct;]]\x{100}/' => [ 'Assuming NOT a POSIX class since it doesn\'t start with a \'[\' {#} m/[foo{#};punct;]]\x{100}/',
622                                   'Assuming NOT a POSIX class since a semi-colon was found instead of a colon {#} m/[foo;{#}punct;]]\x{100}/',
623                                   'Assuming NOT a POSIX class since a semi-colon was found instead of a colon {#} m/[foo;punct;]{#}]\x{100}/',
624                                 ],
625    '/[][[:alpha:]]/' => "",        # [perl #127581]
626    '/[][[:alpha:]\\@\\\\^_?]/' => "", # [perl #131522]
627     '/(?[[:w:]])/' => "",
628     '/([.].*)[.]/'   => "",    # [perl #127582]
629     '/[.].*[.]/'     => "",    # [perl #127604]
630     '/abc/xix' => "",
631     '/(?xmsixp:abc)/' => "",
632     '/(?xmsixp)abc/' => "",
633     '/(?xxxx:abc)/' => "",
634
635 ); # See comments before this for why '\x{100}' is generally needed
636
637 # These need the character 'ネ' as a marker for mark_as_utf8()
638 my @warnings_utf8 = mark_as_utf8(
639     'm/ネ\b*ネ/' => '\b* matches null string many times {#} m/ネ\b*{#}ネ/',
640     '/(?=ネ)*/' => '(?=ネ)* matches null string many times {#} m/(?=ネ)*{#}/',
641     'm/ネ[:foo:]ネ/' => 'POSIX syntax [: :] belongs inside character classes (but this one isn\'t fully valid) {#} m/ネ[:foo:]{#}ネ/',
642     '/ネ(?c)ネ/' => 'Useless (?c) - use /gc modifier {#} m/ネ(?c{#})ネ/',
643     '/utf8 ネ (?ogc) ネ/' => [
644         'Useless (?o) - use /o modifier {#} m/utf8 ネ (?o{#}gc) ネ/',
645         'Useless (?g) - use /g modifier {#} m/utf8 ネ (?og{#}c) ネ/',
646         'Useless (?c) - use /gc modifier {#} m/utf8 ネ (?ogc{#}) ネ/',
647     ],
648    '/ネ[[:ネ:]]ネ/' => "",
649    '/ネ(?[[:ネ:]])ネ/' => "",
650
651 );
652
653 push @warning, @warnings_utf8;
654
655 my @warning_only_under_strict = (
656     '/[\N{U+00}-\x01]\x{100}/' => 'Both or neither range ends should be Unicode {#} m/[\N{U+00}-\x01{#}]\x{100}/',
657     '/[\x00-\N{SOH}]\x{100}/' => 'Both or neither range ends should be Unicode {#} m/[\x00-\N{U+01}{#}]\x{100}/',
658     '/[\N{DEL}-\o{377}]\x{100}/' => 'Both or neither range ends should be Unicode {#} m/[\N{U+7F}-\o{377}{#}]\x{100}/',
659     '/[\o{0}-\N{U+01}]\x{100}/' => 'Both or neither range ends should be Unicode {#} m/[\o{0}-\N{U+01}{#}]\x{100}/',
660     '/[\000-\N{U+01}]\x{100}/' => 'Both or neither range ends should be Unicode {#} m/[\000-\N{U+01}{#}]\x{100}/',
661     '/[\N{DEL}-\377]\x{100}/' => 'Both or neither range ends should be Unicode {#} m/[\N{U+7F}-\377{#}]\x{100}/',
662     '/[\N{U+00}-A]\x{100}/' => 'Ranges of ASCII printables should be some subset of "0-9", "A-Z", or "a-z" {#} m/[\N{U+00}-A{#}]\x{100}/',
663     '/[a-\N{U+FF}]\x{100}/' => 'Ranges of ASCII printables should be some subset of "0-9", "A-Z", or "a-z" {#} m/[a-\N{U+FF}{#}]\x{100}/',
664     '/[\N{U+00}-\a]\x{100}/' => "",
665     '/[\a-\N{U+FF}]\x{100}/' => "",
666     '/[\N{U+FF}-\x{100}]/' => 'Both or neither range ends should be Unicode {#} m/[\N{U+FF}-\x{100}{#}]/',
667     '/[\N{U+100}-\x{101}]/' => "",
668     "/[%-%]/" => "",
669     "/[:-\\x$colon_hex]\\x{100}/" => "\":-\\x$colon_hex\" is more clearly written simply as \":\" {#} m/[:-\\x$colon_hex\{#}]\\x{100}/",
670     "/[\\x$colon_hex-:]\\x{100}/" => "\"\\x$colon_hex-:\" is more clearly written simply as \":\" {#} m/[\\x$colon_hex\-:{#}]\\x{100}/",
671     "/[\\t-\\x$tab_hex]\\x{100}/" => "\"\\t-\\x$tab_hex\" is more clearly written simply as \"\\t\" {#} m/[\\t-\\x$tab_hex\{#}]\\x{100}/",
672     "/[\\x$tab_hex-\\t]\\x{100}/" => "\"\\x$tab_hex-\\t\" is more clearly written simply as \"\\t\" {#} m/[\\x$tab_hex\-\\t{#}]\\x{100}/",
673     "/[$B_hex-C]/" => "Ranges of ASCII printables should be some subset of \"0-9\", \"A-Z\", or \"a-z\" {#} m/[$B_hex-C{#}]/",
674     "/[A-$B_hex]/" => "Ranges of ASCII printables should be some subset of \"0-9\", \"A-Z\", or \"a-z\" {#} m/[A-$B_hex\{#}]/",
675     "/[$low_mixed_alpha-$high_mixed_alpha]/" => "Ranges of ASCII printables should be some subset of \"0-9\", \"A-Z\", or \"a-z\" {#} m/[$low_mixed_alpha-$high_mixed_alpha\{#}]/",
676     "/[$low_mixed_digit-$high_mixed_digit]/" => "Ranges of ASCII printables should be some subset of \"0-9\", \"A-Z\", or \"a-z\" {#} m/[$low_mixed_digit-$high_mixed_digit\{#}]/",
677     '/\b<GCB}/' => 'Unescaped literal \'}\' {#} m/\b<GCB}{#}/',
678     '/[ ]def]/' => 'Unescaped literal \']\' {#} m/[ ]def]{#}/',
679     '/(?)/' => 'Empty (?) without any modifiers {#} m/(?){#}/', # [perl #132851]
680 );
681
682 my @warning_utf8_only_under_strict = mark_as_utf8(
683  '/ネ[᪉-᪐]/; #no latin1' => "Ranges of digits should be from the same group of 10 {#} m/ネ[᪉-᪐{#}]/",
684  '/ネ(?[ [ ᪉ - ᪐ ] ])/; #no latin1' => "Ranges of digits should be from the same group of 10 {#} m/ネ(?[ [ ᪉ - ᪐ {#}] ])/",
685  '/ネ[᧙-᧚]/; #no latin1' => "Ranges of digits should be from the same group of 10 {#} m/ネ[᧙-᧚{#}]/",
686  '/ネ(?[ [ ᧙ - ᧚ ] ])/; #no latin1' => "Ranges of digits should be from the same group of 10 {#} m/ネ(?[ [ ᧙ - ᧚ {#}] ])/",
687  '/ネ(?[ [ 𝟘 - 𝟡 ] ])/; #no latin1' => "",
688  '/ネ(?[ [ 𝟧 - 𝟱 ] ])/; #no latin1' => "Ranges of digits should be from the same group of 10 {#} m/ネ(?[ [ 𝟧 - 𝟱 {#}] ])/",
689  '/ネ(?[ [ 𝟧 - 𝟰 ] ])/; #no latin1' => "Ranges of digits should be from the same group of 10 {#} m/ネ(?[ [ 𝟧 - 𝟰 {#}] ])/",
690 );
691
692 push @warning_only_under_strict, @warning_utf8_only_under_strict;
693
694 my @experimental_regex_sets = (
695     '/(?[ \t ])/' => 'The regex_sets feature is experimental {#} m/(?[{#} \t ])/',
696     'use utf8; /utf8 ネ (?[ [\tネ] ])/' => do { use utf8; 'The regex_sets feature is experimental {#} m/utf8 ネ (?[{#} [\tネ] ])/' },
697     '/noutf8 ネ (?[ [\tネ] ])/' => 'The regex_sets feature is experimental {#} m/noutf8 ネ (?[{#} [\tネ] ])/',
698 );
699
700 my @deprecated = (
701  '/^{/'          => "",
702  '/foo|{/'       => "",
703  '/foo|^{/'      => "",
704  '/foo({bar)/'   => 'Unescaped left brace in regex is deprecated here (and will be fatal in Perl 5.32), passed through {#} m/foo({{#}bar)/',
705  '/foo(:?{bar)/' => "",
706  '/\s*{/'        => "",
707  '/a{3,4}{/'     => "",
708 );
709
710 for my $strict ("", "use re 'strict';") {
711
712     # First time just use @death; but under strict we add the things that fail
713     # there.  Doing it this way makes sure that 'strict' doesnt change the
714     # things that are already fatal when not under strict.
715     if ($strict) {
716         for (my $i = 0; $i < @death_only_under_strict; $i += 3) {
717             push @death, $death_only_under_strict[$i],    # The regex
718                          $death_only_under_strict[$i+2];  # The fatal msg
719         }
720         for (my $i = 0; $i < @death_utf8_only_under_strict; $i += 3) {
721
722             # Same with the utf8 versions
723             push @death, mark_as_utf8($death_utf8_only_under_strict[$i],
724                                       $death_utf8_only_under_strict[$i+2]);
725         }
726     }
727     for (my $i = 0; $i < @death; $i += 2) {
728         my $regex = $death[$i] =~ s/ default_ (on | off) //rx;
729         my $expect = fixup_expect($death[$i+1], $strict);
730         if ($expect eq "") {
731             fail("$0: Internal error: '$death[$i]' should have an error message");
732         }
733         else {
734             no warnings 'experimental::regex_sets';
735             no warnings 'experimental::re_strict';
736
737             warning_is(sub {
738                     my $meaning_of_life;
739                     my $eval_string = "$strict $regex";
740                     $_ = "x";
741                     eval "$eval_string; \$meaning_of_life = 42";
742                     ok (! defined $meaning_of_life, "$eval_string died");
743                     my $error= $@;
744                     if ($error =~ qr/\Q$expect/) {
745                         ok(1, "... and gave expected message");
746                     } else {
747                         ok(0,$eval_string);
748                         diag("Have: " . _q(add_markers($error)));
749                         diag("Want: " . _q($death[$i+1]));
750                     }
751                 }, undef, "... and no other warnings");
752         }
753     }
754 }
755
756 for my $strict ("",  "no warnings 'experimental::re_strict'; use re 'strict';") {
757     my @warning_tests = @warning;
758
759     # Build the tests for @warning.  Use the strict/non-strict versions
760     # appropriately.
761     if ($strict) {
762         push @warning_tests, @warning_only_under_strict;
763     }
764     else {
765         for (my $i = 0; $i < @warning_only_under_strict; $i += 2) {
766
767             # (?[ ]) are always under strict
768             if ($warning_only_under_strict[$i] =~ /\Q(?[/) {
769                 push @warning_tests, $warning_only_under_strict[$i],  # The regex
770                                     $warning_only_under_strict[$i+1];
771             }
772             else {
773                 push @warning_tests, $warning_only_under_strict[$i],  # The regex
774                                     "";    # No warning because not strict
775             }
776         }
777         for (my $i = 0; $i < @death_only_under_strict; $i += 3) {
778             push @warning_tests, $death_only_under_strict[$i],    # The regex
779                                  $death_only_under_strict[$i+1];  # The warning
780         }
781         for (my $i = 0; $i < @death_utf8_only_under_strict; $i += 3) {
782             push @warning_tests, mark_as_utf8($death_utf8_only_under_strict[$i],
783                                         $death_utf8_only_under_strict[$i+1]);
784         }
785     }
786
787     foreach my $ref (\@warning_tests,
788                      \@experimental_regex_sets,
789                      \@deprecated)
790     {
791         my $warning_type;
792         my $turn_off_warnings = "";
793         my $default_on;
794         if ($ref == \@warning_tests) {
795             $warning_type = 'regexp, digit';
796             $turn_off_warnings = "no warnings 'experimental::regex_sets';";
797             $default_on = $strict;
798         }
799         elsif ($ref == \@deprecated) {
800             $warning_type = 'regexp, deprecated';
801             $default_on = 1;
802         }
803         elsif ($ref == \@experimental_regex_sets) {
804             $warning_type = 'experimental::regex_sets';
805             $default_on = 1;
806         }
807         else {
808             fail("$0: Internal error: Unexpected loop variable");
809         }
810
811         for (my $i = 0; $i < @$ref; $i += 2) {
812             my $this_default_on = $default_on;
813             my $regex = $ref->[$i];
814             if ($regex =~ s/ default_ (on | off) //x) {
815                 $this_default_on = $1 eq 'on';
816             }
817             my @expect = fixup_expect($ref->[$i+1], $strict);
818
819             # A length-1 array with an empty warning means no warning gets
820             # generated at all.
821             undef @expect if @expect == 1 && $expect[0] eq "";
822
823             {
824                 $_ = "x";
825                 #use feature 'unicode_eval';
826                 #print STDERR __LINE__, ": ", "eval '$strict no warnings; $regex'", "\n";
827                 eval "$strict no warnings; $regex";
828             }
829             if (is($@, "", "$strict $regex did not die")) {
830                 my @got = capture_warnings(sub {
831                                         $_ = "x";
832                                         eval "$strict $turn_off_warnings $regex" });
833                 my $count = @expect;
834                 if (! is(scalar @got, scalar @expect,
835                             "... and gave expected number ($count) of warnings"))
836                 {
837                     if (@got < @expect) {
838                         $count = @got;
839                         note "Expected warnings not gotten:\n\t" . join "\n\t",
840                                                     @expect[$count .. $#expect];
841                     }
842                     else {
843                         note "Unexpected warnings gotten:\n\t" . join("\n\t",
844                                                          @got[$count .. $#got]);
845                     }
846                 }
847                 foreach my $i (0 .. $count - 1) {
848                     if (! like($got[$i], qr/\Q$expect[$i]/,
849                                                "... and gave expected warning"))
850                     {
851                         chomp($got[$i]);
852                         chomp($expect[$i]);
853                         diag("GOT\n'$got[$i]'\nEXPECT\n'$expect[$i]'");
854                     }
855                     else {
856                         ok (0 == capture_warnings(sub {
857                             $_ = "x";
858                             eval "$strict no warnings '$warning_type'; $regex;" }
859                            ),
860                            "... and turning off '$warning_type' warnings suppressed it");
861
862                         # Test that whether the warning is on by default is
863                         # correct.  This test relies on the fact that we
864                         # are outside the scope of any ‘use warnings’.
865                         local $^W;
866                         my @warns = capture_warnings(sub { $_ = "x";
867                                                         eval "$strict $regex" });
868                         # Warning should be on as well if is testing
869                         # '(?[...])' which turns on strict
870                         if ($this_default_on || grep { $_ =~ /\Q(?[/ } @expect ) {
871                            ok @warns > 0, "... and the warning is on by default";
872                         }
873                         else {
874                          ok @warns == 0, "... and the warning is off by default";
875                         }
876                     }
877                 }
878             }
879         }
880     }
881 }
882
883 done_testing();