From: Steve Hancock Date: Wed, 13 Jan 2021 16:34:48 +0000 (-0800) Subject: Fix bad guess of divide vs pattern X-Git-Tag: 20210402~88 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=9171e2a486debb2abe3c39c0506409b01cfa1621;p=perltidy.git Fix bad guess of divide vs pattern --- diff --git a/lib/Perl/Tidy/Tokenizer.pm b/lib/Perl/Tidy/Tokenizer.pm index 30b2c30f..63ca4710 100644 --- a/lib/Perl/Tidy/Tokenizer.pm +++ b/lib/Perl/Tidy/Tokenizer.pm @@ -5797,6 +5797,20 @@ sub guess_if_pattern_or_conditional { return ( $is_pattern, $msg ); } +my %is_known_constant; +my %is_known_function; + +BEGIN { + + # Constants like 'pi' in Trig.pm are common + my @q = qw(pi pi2 pi4 pip2 pip4); + @{is_known_constant}{@q} = (1) x scalar(@q); + + # parenless calls of 'ok' are common + @q = qw( ok ); + @{is_known_function}{@q} = (1) x scalar(@q); +} + sub guess_if_pattern_or_division { # this routine is called when we have encountered a / following an @@ -5881,15 +5895,20 @@ sub guess_if_pattern_or_division { # Both pattern and divide can work here... - # A very common bare word in math expressions is 'pi' - if ( $last_nonblank_token eq 'pi' ) { - $msg .= "division (pattern works too but saw 'pi')\n"; + # Increase weight of divide if a pure number follows + $divide_expected += $next_token =~ /^\d+$/; + + # Check for known constants in the numerator, like 'pi' + if ( $is_known_constant{$last_nonblank_token} ) { + $msg .= +"division (pattern works too but saw known constant '$last_nonblank_token')\n"; $is_pattern = 0; } # A very common bare word in pattern expressions is 'ok' - elsif ( $last_nonblank_token eq 'ok' ) { - $msg .= "pattern (division works too but saw 'ok')\n"; + elsif ( $is_known_function{$last_nonblank_token} ) { + $msg .= +"pattern (division works too but saw '$last_nonblank_token')\n"; $is_pattern = 1; } diff --git a/local-docs/BugLog.pod b/local-docs/BugLog.pod index f653ce96..3fa7aca5 100644 --- a/local-docs/BugLog.pod +++ b/local-docs/BugLog.pod @@ -1,7 +1,30 @@ -=head1 Issues fixed after release 20201207 +=head1 Issues fixed after release 20210111 =over 4 +=item B + +A syntax error was produced in random testing when perltidy was fed +the following line: + + sub _DR () { pi2 /360 } sub _RD () { 360 /pi2 } + +The bareword 'pi2' was not recognized and the text between the two slashes +was a taken as a possible pattern argument in a parenless call to pi2. Two +fixes were made to fix this. Perltidy looks for 'pi' but not 'pi2', so the +first fix was to expand its table to include all variations of 'pi' in Trig.pm. +Second, the fact that the first slash was followed by a number should have +tipped the guess to favor division, so this was fixed. +As it was, a backup spacing rule was used, which favored a pattern. + +The formatted result is now + + sub _DR () { pi2 / 360 } + sub _RD () { 360 / pi2 } + +This update was made 13 Jan 2021. + + =item B A formula used to estimating maximum line length when the -wn option is set was @@ -28,6 +51,11 @@ caused an oscillation between two states. An unusual feature which contributed to the problem is the very large ci value. This is fixed in a patch made 12 Jan 2021. +=back + +=head1 Issues fixed after release 20201207 + +=over 4 =item B