Skip to content

Commit

Permalink
Don't merge in output contexts from branches that are guaranteed to r…
Browse files Browse the repository at this point in the history
…eturn, since the code AFTER them will never execute after the branch's code.
  • Loading branch information
mcoblenz committed Jan 23, 2020
1 parent d482a60 commit 829e7b1
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/main/scala/edu/cmu/cs/obsidian/typecheck/Checker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2188,11 +2188,18 @@ class Checker(globalTable: SymbolTable, verbose: Boolean = false) {
val (mergedContext, newCases) = cases.headOption match {
case None => (contextPrime, cases)
case Some(switchCase) =>
val (initialContext, newCase) = checkSwitchCase(switchCase)
// If a given switch case is guaranteed to return, then if it DID run, then the code BELOW this will NEVER run, so
// we need not merge the case's output context in.
val (firstCaseOutputContext, newCase) = checkSwitchCase(switchCase)

val initialContextToMerge = if (hasReturnStatementDontLog(switchCase.body)) contextPrime else firstCaseOutputContext
val restCases = cases.tail
restCases.foldLeft((initialContext, Seq(newCase)))((prev: (Context, Seq[SwitchCase]), sc: SwitchCase) => {
restCases.foldLeft((initialContextToMerge, Seq(newCase)))((prev: (Context, Seq[SwitchCase]), sc: SwitchCase) => {
val (newContext, newCase) = checkSwitchCase(sc)
(mergeContext(s, prev._1, newContext), newCase +: prev._2)
if (hasReturnStatementDontLog(sc.body))
(prev._1, newCase +: prev._2)
else
(mergeContext(s, prev._1, newContext), newCase +: prev._2)
})
}

Expand Down

0 comments on commit 829e7b1

Please sign in to comment.