In perl.git, the branch blead has been updated <http://perl5.git.perl.org/perl.git/commitdiff/a539498ab05fd838aa1eaaebbe1f3528bb97d1d3?hp=af9838cc2fa3350e15e88a27008899ae3a3afdb6>
- Log ----------------------------------------------------------------- commit a539498ab05fd838aa1eaaebbe1f3528bb97d1d3 Author: Father Chrysostomos <[email protected]> Date: Mon Sep 20 22:05:39 2010 -0700 Let B::Deparse know about the [perl #20444] fix. With the previous commit: $ ./perl -Ilib -MO=Deparse -e'"foo" =~ (1?/foo/:/bar/)' 'foo' =~ /foo/; -e syntax OK So the Deparse output no longer matches what perl does. With this commit: $ ./perl -Ilib -MO=Deparse -e'"foo" =~ (1?/foo/:/bar/)' 'foo' =~ ($_ =~ /foo/); -e syntax OK M dist/B-Deparse/Deparse.pm M dist/B-Deparse/t/deparse.t commit 2474a784a94d8c70aea9c330d9f2a902b8a68b85 Author: Father Chrysostomos <[email protected]> Date: Mon Sep 20 22:05:34 2010 -0700 [perl #20444] regex not evaluated in constant ?: $text =~ ( 1 ? /phoo/ : /bear/) used to be constant-folded to $text =~ /phoo/ This patch solves the problem by marking match and subst ops as OPf_SPECIAL during constant folding, so the =~ operator can tell not to take possession of it. M op.c M op.h M t/comp/fold.t ----------------------------------------------------------------------- Summary of changes: dist/B-Deparse/Deparse.pm | 10 +++++++++- dist/B-Deparse/t/deparse.t | 11 ++++++++++- op.c | 16 +++++++++++++--- op.h | 4 ++++ t/comp/fold.t | 32 +++++++++++++++++++++++++++++++- 5 files changed, 67 insertions(+), 6 deletions(-) diff --git a/dist/B-Deparse/Deparse.pm b/dist/B-Deparse/Deparse.pm index f446f2a..92f2747 100644 --- a/dist/B-Deparse/Deparse.pm +++ b/dist/B-Deparse/Deparse.pm @@ -4221,6 +4221,7 @@ sub matchop { } my $quote = 1; my $extended = ($op->pmflags & PMf_EXTENDED); + my $rhs_bound_to_defsv; if (null $kid) { my $unbacked = re_unback($op->precomp); if ($extended) { @@ -4232,6 +4233,7 @@ sub matchop { carp("found ".$kid->name." where regcomp expected"); } else { ($re, $quote) = $self->regcomp($kid, 21, $extended); + $rhs_bound_to_defsv = 1 if $kid->first->first->flags & OPf_SPECIAL; } my $flags = ""; $flags .= "c" if $op->pmflags & PMf_CONTINUE; @@ -4250,7 +4252,13 @@ sub matchop { } $re = $re . $flags if $quote; if ($binop) { - return $self->maybe_parens("$var =~ $re", $cx, 20); + return + $self->maybe_parens( + $rhs_bound_to_defsv + ? "$var =~ (\$_ =~ $re)" + : "$var =~ $re", + $cx, 20 + ); } else { return $re; } diff --git a/dist/B-Deparse/t/deparse.t b/dist/B-Deparse/t/deparse.t index 3a7d2aa..0fc3b6c 100644 --- a/dist/B-Deparse/t/deparse.t +++ b/dist/B-Deparse/t/deparse.t @@ -17,7 +17,7 @@ BEGIN { require feature; feature->import(':5.10'); } -use Test::More tests => 89; +use Test::More tests => 90; use Config (); use B::Deparse; @@ -645,3 +645,12 @@ pop; pop(); #### pop @_; +#### +# 82 [perl #20444] +"foo" =~ (1 ? /foo/ : /bar/); +"foo" =~ (1 ? y/foo// : /bar/); +"foo" =~ (1 ? s/foo// : /bar/); +>>>> +'foo' =~ ($_ =~ /foo/); +'foo' =~ ($_ =~ tr/fo//); +'foo' =~ ($_ =~ s/foo//); diff --git a/op.c b/op.c index 75a52c3..db91cdb 100644 --- a/op.c +++ b/op.c @@ -2236,9 +2236,10 @@ Perl_bind_match(pTHX_ I32 type, OP *left, OP *right) type == OP_NOT) yyerror("Using !~ with s///r doesn't make sense"); - ismatchop = rtype == OP_MATCH || - rtype == OP_SUBST || - rtype == OP_TRANS; + ismatchop = (rtype == OP_MATCH || + rtype == OP_SUBST || + rtype == OP_TRANS) + && !(right->op_flags & OPf_SPECIAL); if (ismatchop && right->op_private & OPpTARGET_MY) { right->op_targ = 0; right->op_private &= ~OPpTARGET_MY; @@ -4876,6 +4877,11 @@ S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp) op_free(first); if (other->op_type == OP_LEAVE) other = newUNOP(OP_NULL, OPf_SPECIAL, other); + else if (other->op_type == OP_MATCH + || other->op_type == OP_SUBST + || other->op_type == OP_TRANS) + /* Mark the op as being unbindable with =~ */ + other->op_flags |= OPf_SPECIAL; return other; } else { @@ -5028,6 +5034,10 @@ Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop) } if (live->op_type == OP_LEAVE) live = newUNOP(OP_NULL, OPf_SPECIAL, live); + else if (live->op_type == OP_MATCH || live->op_type == OP_SUBST + || live->op_type == OP_TRANS) + /* Mark the op as being unbindable with =~ */ + live->op_flags |= OPf_SPECIAL; return live; } NewOp(1101, logop, 1, LOGOP); diff --git a/op.h b/op.h index 2ffd3e6..da280b8 100644 --- a/op.h +++ b/op.h @@ -142,6 +142,10 @@ Deprecated. Use C<GIMME_V> instead. /* On OP_HELEM and OP_HSLICE, localization will be followed by assignment, so do not wipe the target if it is special (e.g. a glob or a magic SV) */ + /* On OP_MATCH, OP_SUBST & OP_TRANS, the + operand of a logical or conditional + that was optimised away, so it should + not be bound via =~ */ /* old names; don't use in new code, but don't break them, either */ #define OPf_LIST OPf_WANT_LIST diff --git a/t/comp/fold.t b/t/comp/fold.t index 23e8e89..ec95f1a 100644 --- a/t/comp/fold.t +++ b/t/comp/fold.t @@ -4,7 +4,7 @@ # we've not yet verified that use works. # use strict; -print "1..13\n"; +print "1..19\n"; my $test = 0; # Historically constant folding was performed by evaluating the ops, and if @@ -52,6 +52,16 @@ sub is { failed($got, "'$expect'", $name); } +sub ok { + my ($got, $name) = @_; + $test = $test + 1; + if ($got) { + print "ok $test - $name\n"; + return 1; + } + failed($got, "a true value", $name); +} + my $a; $a = eval '$b = 0/0 if 0; 3'; is ($a, 3, 'constants in conditionals don\'t affect constant folding'); @@ -88,3 +98,23 @@ is ($@, '', 'no error'); like ($@, qr/division/, "eval caught division"); is($c, 2, "missing die hook"); } + +# [perl #20444] Constant folding should not change the meaning of match +# operators. +{ + local *_; + $_="foo"; my $jing = 1; + ok scalar $jing =~ (1 ? /foo/ : /bar/), + 'lone m// is not bound via =~ after ? : folding'; + ok scalar $jing =~ (0 || /foo/), + 'lone m// is not bound via =~ after || folding'; + ok scalar $jing =~ (1 ? s/foo/foo/ : /bar/), + 'lone s/// is not bound via =~ after ? : folding'; + ok scalar $jing =~ (0 || s/foo/foo/), + 'lone s/// is not bound via =~ after || folding'; + $jing = 3; + ok scalar $jing =~ (1 ? y/fo// : /bar/), + 'lone y/// is not bound via =~ after ? : folding'; + ok scalar $jing =~ (0 || y/fo//), + 'lone y/// is not bound via =~ after || folding'; +} -- Perl5 Master Repository
