From: Steve Hancock Date: Sat, 5 Jun 2021 20:50:36 +0000 (-0700) Subject: Add warning when lexical sub names match some builtins X-Git-Tag: 20210402.01~17 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=32729fb6998c5556edec44eaa57d83966713f27a;p=perltidy.git Add warning when lexical sub names match some builtins --- diff --git a/lib/Perl/Tidy/Tokenizer.pm b/lib/Perl/Tidy/Tokenizer.pm index 3169e420..9d60cd22 100644 --- a/lib/Perl/Tidy/Tokenizer.pm +++ b/lib/Perl/Tidy/Tokenizer.pm @@ -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_]; } } diff --git a/local-docs/BugLog.pod b/local-docs/BugLog.pod index d36b1e6a..c4ec023c 100644 --- a/local-docs/BugLog.pod +++ b/local-docs/BugLog.pod @@ -2,6 +2,22 @@ =over 4 +=item B + +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 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 @@ -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