In perl.git, the branch blead has been updated <http://perl5.git.perl.org/perl.git/commitdiff/2e6807b5b9d6bff0efdfbbed7b7731e04ea21273?hp=f15d05806fb7522031b75cb5a8784727ae03b98a>
- Log ----------------------------------------------------------------- commit 2e6807b5b9d6bff0efdfbbed7b7731e04ea21273 Author: Aaron Crane <[email protected]> Date: Sun May 15 19:00:53 2016 +0100 Make barewords constant-foldable Commit 11fa937b2766784f0f812687a7f81dd3761e605f, from August 1999, changed constant folding to treat barewords as unfoldable. Public mailing-list archives from that period are incomplete, unfortunately, and this change in particular is no longer well documented; the only message still available seems to be <[email protected]>, with subject "Re: Unwanted perl helpfullness". That message quotes a bug reporter pointing out that a constant-folded bareword would fail to trigger the "not allowed while strict subs in use" error under "use strict". However, there's no information available about why the bug was fixed in this way, rather than by merely ensuring that constant folding would produce that error when necessary. The no_bareword_allowed() routine did already exist at that point, for example. This change therefore adopts that approach. This causes one minor change in behaviour: since barewords now take part in constant folding, concatenating a bareword with another constant in void context now produces a warning for the concatenated string, rather than for the concatenation op. This seems like an acceptable change, given that non-bareword constants already behave in the same way. M op.c M t/lib/strict/subs M t/lib/warnings/2use M t/lib/warnings/op M t/perf/opcount.t commit 1ec2b028f490cff41b7f0c1131c900b58431c722 Author: Aaron Crane <[email protected]> Date: Sun May 15 18:44:16 2016 +0100 S_fold_constants(): refactor foldability detection M op.c ----------------------------------------------------------------------- Summary of changes: op.c | 24 +++++++++++++++++------- t/lib/strict/subs | 8 ++++++++ t/lib/warnings/2use | 2 +- t/lib/warnings/op | 8 ++++++++ t/perf/opcount.t | 8 +++++++- 5 files changed, 41 insertions(+), 9 deletions(-) diff --git a/op.c b/op.c index 9e643e0..81bd042 100644 --- a/op.c +++ b/op.c @@ -4342,13 +4342,23 @@ S_fold_constants(pTHX_ OP *o) goto nope; /* Don't try to run w/ errors */ for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) { - const OPCODE type = curop->op_type; - if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) && - type != OP_LIST && - type != OP_SCALAR && - type != OP_NULL && - type != OP_PUSHMARK) - { + switch (curop->op_type) { + case OP_CONST: + if ( (curop->op_private & OPpCONST_BARE) + && (curop->op_private & OPpCONST_STRICT)) { + no_bareword_allowed(curop); + goto nope; + } + /* FALLTHROUGH */ + case OP_LIST: + case OP_SCALAR: + case OP_NULL: + case OP_PUSHMARK: + /* Foldable; move to next op in list */ + break; + + default: + /* No other op types are considered foldable */ goto nope; } } diff --git a/t/lib/strict/subs b/t/lib/strict/subs index 246be0e..dff9282 100644 --- a/t/lib/strict/subs +++ b/t/lib/strict/subs @@ -467,3 +467,11 @@ my $v2 = $h->{+CONST_TYPO}; EXPECT Bareword "CONST_TYPO" not allowed while "strict subs" in use at - line 5. Execution of - aborted due to compilation errors. +######## +# NAME constant-folded barewords still trigger stricture +my $x = !BARE1; +use strict 'subs'; +my $y = !BARE2; +EXPECT +Bareword "BARE2" not allowed while "strict subs" in use at - line 3. +Execution of - aborted due to compilation errors. diff --git a/t/lib/warnings/2use b/t/lib/warnings/2use index c0d203a..4e10d4b 100644 --- a/t/lib/warnings/2use +++ b/t/lib/warnings/2use @@ -76,7 +76,7 @@ Reversed += operator at - line 3. no warnings 'reserved' ; foo.bar; EXPECT -Useless use of concatenation (.) or string in void context at - line 3. +Useless use of a constant ("foobar") in void context at - line 3. ######## --FILE-- abc diff --git a/t/lib/warnings/op b/t/lib/warnings/op index 528639e..d36d419 100644 --- a/t/lib/warnings/op +++ b/t/lib/warnings/op @@ -2057,3 +2057,11 @@ Array passed to stat will be coerced to a scalar (did you want stat $foo[0]?) at Array passed to stat will be coerced to a scalar (did you want stat $bar[0]?) at - line 9. Array passed to stat will be coerced to a scalar at - line 10. +######## +# NAME barewords and conditionals near constant folding +use warnings; +my $x1 = !a || !b; # no "in conditional" warnings +my $x2 = !A || !B; # warning-free, because upper-case won't clash +EXPECT +Unquoted string "a" may clash with future reserved word at - line 2. +Unquoted string "b" may clash with future reserved word at - line 2. diff --git a/t/perf/opcount.t b/t/perf/opcount.t index f3c0bad..13c916d 100644 --- a/t/perf/opcount.t +++ b/t/perf/opcount.t @@ -20,7 +20,7 @@ BEGIN { use warnings; use strict; -plan 2249; +plan 2250; use B (); @@ -260,3 +260,9 @@ test_opcount(0, 'multideref exists', multideref => 1, }, ); + +test_opcount(0, 'barewords can be constant-folded', + sub { no strict 'subs'; FOO . BAR }, + { + concat => 0, + }); -- Perl5 Master Repository
