Skip to content

Commit

Permalink
fix #820 translate switch with exhausitve match correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
peq committed Mar 20, 2019
1 parent 9448120 commit e187e50
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
Expand Up @@ -315,6 +315,12 @@ public static ImStmt translate(SwitchStmt switchStmt, ImTranslator t, ImFunction
lastIf = ImIf(switchStmt, translateSwitchCase(cse, tempVar, f, t), ImStmts(t
.translateStatements(f, cse.getStmts())), ImStmts());
result.add(lastIf);
} else if (i == switchStmt.getCases().size() - 1
&& switchStmt.getSwitchDefault() instanceof NoDefaultCase
&& switchStmt.calculateHandlesAllCases()) {
// if this is the last case and all cases are covered, then just add
// the code to the else statement without checking the condition:
lastIf.setElseBlock(ImStmts(t.translateStatements(f, cse.getStmts())));
} else {
ImIf tmp = ImIf(switchStmt, translateSwitchCase(cse, tempVar, f, t), ImStmts
(t.translateStatements(f, cse.getStmts())), ImStmts());
Expand Down
Expand Up @@ -235,6 +235,72 @@ public void testSwitchEnumAll2() {
);
}

@Test
public void testSwitchEnumAll3() {
testAssertOkLines(true,
"package Test",
"native testSuccess()",
"enum Blub",
" A",
" B",
" C",
"function foo(Blub blub) returns int",
" switch blub",
" case A | B",
" return 1",
" case C",
" return 2",
"init",
" if foo(Blub.C) == 2",
" testSuccess()"
);
}

@Test
public void testSwitchEnumAll4() {
testAssertOkLines(true,
"package Test",
"native testSuccess()",
"enum Blub",
" A",
" B",
" C",
"function foo(Blub blub) returns int",
" switch blub",
" case A | B",
" return 1",
" case C",
" return 2",
" default",
" return 3",
"init",
" if foo(Blub.C) == 2",
" testSuccess()"
);
}

@Test
public void testSwitchEnumAll5() {
testAssertOkLines(true,
"package Test",
"native testSuccess()",
"enum Blub",
" A",
" B",
" C",
"function foo(Blub blub) returns int",
" switch blub",
" case A | B",
" return 1",
" case C",
" return 2",
" return 3",
"init",
" if foo(Blub.C) == 2",
" testSuccess()"
);
}

@Test
public void testSwitchEnumDuplicate() {
testAssertErrorsLines(false, "The case B is already handled in line 9",
Expand Down

0 comments on commit e187e50

Please sign in to comment.