Skip to content

Commit d425b2f

Browse files
committed
Test and simplify ANF transform of Nothing-typed if/match
1 parent a5465fc commit d425b2f

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

src/compiler/scala/tools/nsc/transform/async/AnfTransform.scala

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,18 +176,10 @@ private[async] trait AnfTransform extends TransformUtils {
176176
}
177177
}
178178

179-
@tailrec
180179
private def transformMatchOrIf[T <: Tree](tree: Tree, needsResultVar: Boolean, nameSource: asyncNames.NameSource[TermName])(core: Symbol => T): Tree = {
181-
// if type of if/match is Unit don't introduce assignment,
182-
// but add Unit value to bring it into form expected by async transform
183-
if (isUnitType(tree.tpe)) {
184-
assignUnitType(core(NoSymbol))
185-
} else if (tree.tpe =:= definitions.NothingTpe) {
186-
currentStats += assignUnitType(core(NoSymbol))
187-
localTyper.typedPos(tree.pos)(Throw(New(IllegalStateExceptionClass)))
188-
} else if (isPatMatGeneratedJump(tree)) {
189-
transformMatchOrIf(assignUnitType(tree), needsResultVar, nameSource)(core)
190-
} else if (!needsResultVar) {
180+
if (isPatMatGeneratedJump(tree)) assignUnitType(tree)
181+
182+
if (!needsResultVar || isUnitType(tree.tpe) || (tree.tpe =:= definitions.NothingTpe)) {
191183
core(NoSymbol)
192184
} else {
193185
val varDef = defineVar(nameSource(), tree.tpe, tree.pos)

test/junit/scala/tools/nsc/async/AnnotationDrivenAsync.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,24 @@ class AnnotationDrivenAsync {
442442
assertEquals(100, run(code))
443443
}
444444

445+
@Test
446+
def testNothingTypedExpr(): Unit = {
447+
val code =
448+
"""
449+
|import scala.concurrent._, duration.Duration, ExecutionContext.Implicits.global
450+
|import scala.tools.partest.async.Async.{async, await}
451+
|import Future.{successful => f}
452+
|
453+
|object Test {
454+
| def test: Future[Throwable] = async { if ("".isEmpty) {await(f("")); throw new RuntimeException("boo!")} else ??? }.failed
455+
|}
456+
|""".stripMargin
457+
run(code) match {
458+
case re: RuntimeException => assert(re.getMessage == "boo!")
459+
case _ => Assert.fail()
460+
}
461+
}
462+
445463
// Handy to debug the compiler or to collect code coverage statistics in IntelliJ.
446464
@Test
447465
@Ignore

0 commit comments

Comments
 (0)