]> git.donarmstrong.com Git - perltidy.git/commitdiff
Add warning when lexical sub names match some builtins
authorSteve Hancock <perltidy@users.sourceforge.net>
Sat, 5 Jun 2021 20:50:36 +0000 (13:50 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Sat, 5 Jun 2021 20:50:36 +0000 (13:50 -0700)
lib/Perl/Tidy/Tokenizer.pm
local-docs/BugLog.pod

index 3169e420d40820fa5aec58ad078a083e8e3e2c8f..9d60cd222fd3e058e641f16557017c1d3b7043da 100644 (file)
@@ -7116,6 +7116,15 @@ sub scan_identifier_do {
 
 {    ## closure for sub do_scan_sub
 
+    my %warn_if_lexical;
+
+    BEGIN {
+
+        # lexical subs with these names can cause parsing errors in this version
+        my @q = qw( m q qq qr qw qx s tr y );
+        @{warn_if_lexical}{@q} = (1) x scalar(@q);
+    }
+
     # saved package and subnames in case prototype is on separate line
     my ( $package_saved, $subname_saved );
 
@@ -7222,9 +7231,7 @@ sub scan_identifier_do {
             my $is_lexical_sub =
               $last_nonblank_type eq 'k' && $last_nonblank_token eq 'my';
             if ( $is_lexical_sub && $1 ) {
-                warning(
-                    "'my' sub $subname cannot be in package '$1'\n"
-                );
+                warning("'my' sub $subname cannot be in package '$1'\n");
                 $is_lexical_sub = 0;
             }
 
@@ -7235,6 +7242,11 @@ sub scan_identifier_do {
                   $current_sequence_number[BRACE][ $current_depth[BRACE] ];
                 $seqno   = 1 unless ( defined($seqno) );
                 $package = $seqno;
+                if ( $warn_if_lexical{$subname} ) {
+                    warning(
+"'my' sub '$subname' matches a builtin name and may not be handled correctly in this perltidy version.\n"
+                    );
+                }
             }
             else {
                 $package = ( defined($1) && $1 ) ? $1 : $current_package;
@@ -7369,10 +7381,10 @@ sub scan_identifier_do {
                     # Check for multiple definitions of a sub, but
                     # it is ok to have multiple sub BEGIN, etc,
                     # so we do not complain if name is all caps
-                    if (   $saw_function_definition{$package}{$subname}
+                    if (   $saw_function_definition{$subname}{$package}
                         && $subname !~ /^[A-Z]+$/ )
                     {
-                        my $lno = $saw_function_definition{$package}{$subname};
+                        my $lno = $saw_function_definition{$subname}{$package};
                         if ( $package =~ /^\d/ ) {
                             warning(
 "already saw definition of lexical 'sub $subname' at line $lno\n"
@@ -7385,7 +7397,7 @@ sub scan_identifier_do {
                             );
                         }
                     }
-                    $saw_function_definition{$package}{$subname} =
+                    $saw_function_definition{$subname}{$package} =
                       $tokenizer_self->[_last_line_number_];
                 }
             }
index d36b1e6ac4af15d3e06709b93b2b7868881d416c..c4ec023cf70359fbd643f7510233efa63fd8391b 100644 (file)
@@ -2,6 +2,22 @@
 
 =over 4
 
+=item B<Add warning when lexical sub names match some builtins>
+
+This update adds a warning when lexical subs have names which match some builtin
+names which will almost certainly cause a parsing error in the current version
+of perltidy.  For example, the following program is valid and will run, but
+perltidy will produce an error.
+
+    use feature qw(lexical_subs);
+    use warnings; no warnings "experimental::lexical_subs";
+    {
+      my sub y { print "Hello from y: $_[0]\n"; }
+      y(1);
+    }
+
+6 Jun 2021.
+
 =item B<Minor cleanups>
 
 This update fixes a case of formatting instability recently found with random testing.
@@ -9,8 +25,7 @@ It also does some minor coding cleanups.
 
 This fixes case b1139.
 
-5 Jun 2021.
-
+5 Jun 2021, b8527ab.
 
 =item B<Revised data structures for welding>
 
@@ -18,7 +33,7 @@ This update replaces the data structures used for the welding option with
 simpler but more general structures.  This cleans up the code and will
 simplify future coding.  No formatting changes should occur with this update.
 
-4 Jun 2021.
+4 Jun 2021, 4a886c8.
 
 =item B<improved treatment of lexical subs>