Skip to content

Commit 321290a

Browse files
committed
Revert "op_scope/newCONDOP: Optimise away empty else blocks in OP_COND_EXPR"
This reverts commit 7015de1. See BBC issue #22866 for details. Reverting in case there is a bug in the commit.
1 parent 1282565 commit 321290a

File tree

3 files changed

+11
-39
lines changed

3 files changed

+11
-39
lines changed

lib/B/Deparse.pm

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3966,22 +3966,13 @@ sub pp_cond_expr {
39663966
my $true = $cond->sibling;
39673967
my $false = $true->sibling;
39683968
my $cuddle = $self->{'cuddle'};
3969-
3970-
if (class($false) eq "NULL") { # Empty else {} block was optimised away
3971-
unless ($cx < 1 and (is_scope($true) and $true->name ne "null")) {
3972-
$cond = $self->deparse($cond, 8);
3973-
$true = $self->deparse($true, 6);
3974-
return $self->maybe_parens("$cond ? $true : ()", $cx, 8);
3975-
}
3976-
} else { # Both true and false branches are present
3977-
unless ($cx < 1 and (is_scope($true) and $true->name ne "null")
3978-
and (is_scope($false) || is_ifelse_cont($false))
3979-
and $self->{'expand'} < 7) {
3980-
$cond = $self->deparse($cond, 8);
3981-
$true = $self->deparse($true, 6);
3982-
$false = $self->deparse($false, 8);
3983-
return $self->maybe_parens("$cond ? $true : $false", $cx, 8);
3984-
}
3969+
unless ($cx < 1 and (is_scope($true) and $true->name ne "null") and
3970+
(is_scope($false) || is_ifelse_cont($false))
3971+
and $self->{'expand'} < 7) {
3972+
$cond = $self->deparse($cond, 8);
3973+
$true = $self->deparse($true, 6);
3974+
$false = $self->deparse($false, 8);
3975+
return $self->maybe_parens("$cond ? $true : $false", $cx, 8);
39853976
}
39863977

39873978
$cond = $self->deparse($cond, 1);

lib/B/Deparse.t

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3378,7 +3378,3 @@ $_ = (!$p) isa 'Some::Class';
33783378
$_ = (!$p) =~ tr/1//;
33793379
$_ = (!$p) =~ /1/;
33803380
$_ = (!$p) =~ s/1//r;
3381-
####
3382-
# Else block of a ternary is optimised away
3383-
my $x;
3384-
my(@y) = $x ? [1, 2] : ();

op.c

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4517,12 +4517,6 @@ Perl_op_scope(pTHX_ OP *o)
45174517
{
45184518
if (o) {
45194519
if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || TAINTING_get) {
4520-
4521-
/* There's no need to wrap an empty stub in an enter/leave.
4522-
* This also makes eliding empty if/else blocks simpler. */
4523-
if (OP_TYPE_IS(o, OP_STUB) && (o->op_flags & OPf_PARENS))
4524-
return o;
4525-
45264520
o = op_prepend_elem(OP_LINESEQ,
45274521
newOP(OP_ENTER, (o->op_flags & OPf_WANT)), o);
45284522
OpTYPE_set(o, OP_LEAVE);
@@ -9274,6 +9268,7 @@ Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
92749268
logop = alloc_LOGOP(OP_COND_EXPR, first, LINKLIST(trueop));
92759269
logop->op_flags |= (U8)flags;
92769270
logop->op_private = (U8)(1 | (flags >> 8));
9271+
logop->op_next = LINKLIST(falseop);
92779272

92789273
CHECKOP(OP_COND_EXPR, /* that's logop->op_type */
92799274
logop);
@@ -9282,23 +9277,13 @@ Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
92829277
start = LINKLIST(first);
92839278
first->op_next = (OP*)logop;
92849279

9285-
/* make first & trueop siblings */
9286-
/* falseop may also be a sibling, if not optimised away */
9280+
/* make first, trueop, falseop siblings */
92879281
op_sibling_splice((OP*)logop, first, 0, trueop);
9282+
op_sibling_splice((OP*)logop, trueop, 0, falseop);
92889283

92899284
o = newUNOP(OP_NULL, 0, (OP*)logop);
92909285

9291-
if (OP_TYPE_IS(falseop, OP_STUB) && (falseop->op_flags & OPf_PARENS)) {
9292-
/* The `else` block is empty. Optimise it away. */
9293-
logop->op_next = o;
9294-
op_free(falseop);
9295-
} else {
9296-
op_sibling_splice((OP*)logop, trueop, 0, falseop);
9297-
logop->op_next = LINKLIST(falseop);
9298-
falseop->op_next = o;
9299-
}
9300-
9301-
trueop->op_next = o;
9286+
trueop->op_next = falseop->op_next = o;
93029287

93039288
o->op_next = start;
93049289
return o;

0 commit comments

Comments
 (0)