Skip to content

Commit

Permalink
Bugfix: return does not produce a value.
Browse files Browse the repository at this point in the history
  • Loading branch information
DSouzaM committed May 28, 2024
1 parent f903a1e commit f676e4f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ public void testUnreachableWhile1() {
@Test
public void testUnreachableConditional1() {
// @formatter:off
// true ? return 42 : return 41;
// true ? { return 42; true } : { return 41; false }
// <dead>
// @formatter:on
DeadCodeTestRootNode node = (DeadCodeTestRootNode) parse(b -> {
Expand All @@ -481,13 +481,19 @@ public void testUnreachableConditional1() {
b.beginConditional();
b.emitLoadConstant(true);

b.beginBlock();
b.beginReturn();
b.emitLoadConstant(42);
b.endReturn();
b.emitLoadConstant(true);
b.endBlock();

b.beginBlock();
b.beginReturn();
b.emitLoadConstant(41);
b.endReturn();
b.emitLoadConstant(false);
b.endBlock();

b.endConditional();

Expand Down Expand Up @@ -567,16 +573,19 @@ public void testUnreachableBranch() {
@Test
public void testUnreachableConditionConditional() {
// @formatter:off
// (return 42) ? 41: 41;
// (return 42; true) ? 41: 41;
// <dead>
// @formatter:on
DeadCodeTestRootNode node = (DeadCodeTestRootNode) parse(b -> {
b.beginRoot(LANGUAGE);

b.beginConditional();
b.beginBlock();
b.beginReturn();
b.emitLoadConstant(42);
b.endReturn();
b.emitLoadConstant(true);
b.endBlock();

b.emitLoadConstant(41);

Expand All @@ -598,7 +607,7 @@ public void testUnreachableConditionConditional() {
@Test
public void testUnreachableConditionIfThen() {
// @formatter:off
// if (return 42) {
// if (return 42; true) {
// false;
// }
// return 41;
Expand All @@ -607,9 +616,13 @@ public void testUnreachableConditionIfThen() {
b.beginRoot(LANGUAGE);

b.beginIfThen();
b.beginBlock();
b.beginReturn();
b.emitLoadConstant(42);
b.endReturn();
b.emitLoadConstant(true);
b.endBlock();

b.emitLoadConstant(false);
b.endIfThen();

Expand All @@ -630,7 +643,7 @@ public void testUnreachableConditionIfThen() {
@Test
public void testUnreachableConditionWhile() {
// @formatter:off
// while (return 42) {
// while (return 42; true) {
// false;
// }
// return 41;
Expand All @@ -639,9 +652,12 @@ public void testUnreachableConditionWhile() {
b.beginRoot(LANGUAGE);

b.beginWhile();
b.beginBlock();
b.beginReturn();
b.emitLoadConstant(42);
b.endReturn();
b.emitLoadConstant(true);
b.endBlock();

b.emitLoadConstant(false);
b.endWhile();
Expand All @@ -663,7 +679,7 @@ public void testUnreachableConditionWhile() {
@Test
public void testUnreachableConditionIfThenElse() {
// @formatter:off
// if (return 42) {
// if (return 42; true) {
// 41;
// } else {
// 41;
Expand All @@ -673,9 +689,12 @@ public void testUnreachableConditionIfThenElse() {
b.beginRoot(LANGUAGE);

b.beginIfThenElse();
b.beginBlock();
b.beginReturn();
b.emitLoadConstant(42);
b.endReturn();
b.emitLoadConstant(true);
b.endBlock();

b.emitLoadConstant(41);
b.emitLoadConstant(41);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6400,9 +6400,9 @@ private void buildEmitInstruction(CodeTreeBuilder b, InstructionModel instr, Str
case LOAD_LOCAL_MATERIALIZED:
case THROW:
case YIELD:
case RETURN:
case CLEAR_LOCAL:
break;
case RETURN:
case BRANCH_FALSE:
case POP:
case STORE_LOCAL:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,7 @@ Conditional implements a conditional expression (e.g., {@code condition ? thens
m.signature(void.class, Object.class, Object.class)) //
.addImmediate(ImmediateKind.LOCAL_OFFSET, "localOffset"));
m.returnOperation = m.operation(OperationKind.RETURN, "Return", "Return returns the value produced by {@code result}.") //
/**
* NB: return doesn't produce a value, but it is convenient to treat it as
* non-void for bytecode validation (e.g., an operation expecting a value
* will accept a return as its child, which is okay because the return ends
* execution before the parent runs).
*/
.setVoid(false) //
.setVoid(true) //
.setDynamicOperands(child("result")) //
.setInstruction(m.returnInstruction);
if (m.enableYield) {
Expand Down

0 comments on commit f676e4f

Please sign in to comment.