Skip to content

Commit 4a3ff68

Browse files
committed
Move fix to typedBlock, eliminating special casing in typedApply
1 parent a2d0d4a commit 4a3ff68

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -573,20 +573,6 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
573573
else
574574
new ApplyToUntyped(tree, fun1, funRef, proto, pt)(argCtx(tree))
575575
convertNewGenericArray(ConstFold(app.result))
576-
case mt: MethodType =>
577-
/**
578-
* Fixes i1503:
579-
* {{{
580-
* (new Function0[Unit] {
581-
* def apply() = println("hello")
582-
* })()
583-
* }}}
584-
*
585-
* Will get a weird shape that results in a MethodType being
586-
* inferred instead of a TermRef, to solve this we'll try to type
587-
* it as an Apply with a Select.
588-
*/
589-
typedApply(untpd.Apply(untpd.Select(tree.fun, nme.apply), tree.args), pt)
590576
case _ =>
591577
handleUnexpectedFunType(tree, fun1)
592578
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,23 @@ 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+
/** i1503:
538+
* {{{
539+
* (new Function0[Unit] {
540+
* def apply(): Unit = ...
541+
* })()
542+
* }}}
543+
*
544+
* The code gets desugared into a block that is applied. In the case
545+
* above, the block ends with a `Select(_ apply)` in expression position
546+
* and fails compilation. To remedy this, if a block ends in a
547+
* `Select(_,apply)` we simply move the apply outside the block.
548+
*/
549+
val expr1 = typedExpr(tree.expr, pt)(exprCtx) match {
550+
case Select(x, nme.apply) => x
551+
case x => x
552+
}
537553
ensureNoLocalRefs(
538554
assignType(cpy.Block(tree)(stats1, expr1), stats1, expr1), pt, localSyms(stats1))
539555
}

0 commit comments

Comments
 (0)