Skip to content

Commit 174edf4

Browse files
committed
Fix #1503: remove .apply on blocks in function position
1 parent eb22d15 commit 174edf4

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,16 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
533533
def typedBlock(tree: untpd.Block, pt: Type)(implicit ctx: Context) = track("typedBlock") {
534534
val exprCtx = index(tree.stats)
535535
val stats1 = typedStats(tree.stats, ctx.owner)
536-
val expr1 = typedExpr(tree.expr, pt)(exprCtx)
536+
537+
// A block might appear in function position after desugaring (see
538+
// i1503.scala), so adaptation may insert a `.apply` at the end of expr1.
539+
// When this happens we simply remove the select to make expr1 a valid
540+
// block expression. The `.apply` will be added back around the block when
541+
// the block itself is adapted later.
542+
val expr1 = typedExpr(tree.expr, pt)(exprCtx) match {
543+
case Select(x, nme.apply) => x
544+
case x => x
545+
}
537546
ensureNoLocalRefs(
538547
assignType(cpy.Block(tree)(stats1, expr1), stats1, expr1), pt, localSyms(stats1))
539548
}

tests/run/i1503.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
working

tests/run/i1503.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
object Test {
2+
def main(args: Array[String]): Unit = {
3+
(new Function0[Unit] {
4+
def apply() = println("working")
5+
})()
6+
}
7+
}

0 commit comments

Comments
 (0)