[Bug #19611] Remove never-reachable branch in logical expression
This commit is contained in:
parent
b0cb54a378
commit
c7bacf84f0
Notes:
git
2023-04-27 05:52:43 +00:00
61
compile.c
61
compile.c
@ -4252,6 +4252,37 @@ compile_flip_flop(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const nod
|
|||||||
return COMPILE_OK;
|
return COMPILE_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *cond,
|
||||||
|
LABEL *then_label, LABEL *else_label);
|
||||||
|
|
||||||
|
static int
|
||||||
|
compile_logical(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *cond,
|
||||||
|
LABEL *then_label, LABEL *else_label)
|
||||||
|
{
|
||||||
|
DECL_ANCHOR(seq);
|
||||||
|
INIT_ANCHOR(seq);
|
||||||
|
LABEL *label = NEW_LABEL(nd_line(cond));
|
||||||
|
if (!then_label) then_label = label;
|
||||||
|
else if (!else_label) else_label = label;
|
||||||
|
|
||||||
|
CHECK(compile_branch_condition(iseq, seq, cond, then_label, else_label));
|
||||||
|
|
||||||
|
if (LIST_INSN_SIZE_ONE(seq)) {
|
||||||
|
INSN *insn = (INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(seq));
|
||||||
|
if (insn->insn_id == BIN(jump) && (LABEL *)(insn->operands[0]) == label)
|
||||||
|
return COMPILE_OK;
|
||||||
|
}
|
||||||
|
if (!label->refcnt) {
|
||||||
|
ADD_INSN(seq, cond, putnil);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ADD_LABEL(seq, label);
|
||||||
|
}
|
||||||
|
ADD_SEQ(ret, seq);
|
||||||
|
return COMPILE_OK;
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *cond,
|
compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *cond,
|
||||||
LABEL *then_label, LABEL *else_label)
|
LABEL *then_label, LABEL *else_label)
|
||||||
@ -4259,31 +4290,13 @@ compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *co
|
|||||||
again:
|
again:
|
||||||
switch (nd_type(cond)) {
|
switch (nd_type(cond)) {
|
||||||
case NODE_AND:
|
case NODE_AND:
|
||||||
{
|
CHECK(compile_logical(iseq, ret, cond->nd_1st, NULL, else_label));
|
||||||
LABEL *label = NEW_LABEL(nd_line(cond));
|
cond = cond->nd_2nd;
|
||||||
CHECK(compile_branch_condition(iseq, ret, cond->nd_1st, label,
|
goto again;
|
||||||
else_label));
|
|
||||||
if (!label->refcnt) {
|
|
||||||
ADD_INSN(ret, cond, putnil);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
ADD_LABEL(ret, label);
|
|
||||||
cond = cond->nd_2nd;
|
|
||||||
goto again;
|
|
||||||
}
|
|
||||||
case NODE_OR:
|
case NODE_OR:
|
||||||
{
|
CHECK(compile_logical(iseq, ret, cond->nd_1st, then_label, NULL));
|
||||||
LABEL *label = NEW_LABEL(nd_line(cond));
|
cond = cond->nd_2nd;
|
||||||
CHECK(compile_branch_condition(iseq, ret, cond->nd_1st, then_label,
|
goto again;
|
||||||
label));
|
|
||||||
if (!label->refcnt) {
|
|
||||||
ADD_INSN(ret, cond, putnil);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
ADD_LABEL(ret, label);
|
|
||||||
cond = cond->nd_2nd;
|
|
||||||
goto again;
|
|
||||||
}
|
|
||||||
case NODE_LIT: /* NODE_LIT is always true */
|
case NODE_LIT: /* NODE_LIT is always true */
|
||||||
case NODE_TRUE:
|
case NODE_TRUE:
|
||||||
case NODE_STR:
|
case NODE_STR:
|
||||||
|
@ -759,4 +759,14 @@ class TestISeq < Test::Unit::TestCase
|
|||||||
assert_equal :new, r.take
|
assert_equal :new, r.take
|
||||||
RUBY
|
RUBY
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_ever_condition_loop
|
||||||
|
assert_ruby_status([], "BEGIN {exit}; while true && true; end")
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_unreachable_syntax_error
|
||||||
|
mesg = /Invalid break/
|
||||||
|
assert_syntax_error("false and break", mesg)
|
||||||
|
assert_syntax_error("if false and break; end", mesg)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user