Skip to content

Commit 386b87f

Browse files
committed
Fix #5107: disallow block as Apply.fun
1 parent 2b73f9d commit 386b87f

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

compiler/src/dotty/tools/dotc/transform/TreeChecker.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ class TreeChecker extends Phase with SymTransformer {
279279
case _ if tree.isType =>
280280
promote(tree)
281281
case _ =>
282+
checkApplyNonBlock(tree)
282283
val tree1 = super.typedUnadapted(tree, pt, locked)
283284
def isSubType(tp1: Type, tp2: Type) =
284285
(tp1 eq tp2) || // accept NoType / NoType
@@ -300,6 +301,12 @@ class TreeChecker extends Phase with SymTransformer {
300301
res
301302
}
302303

304+
def checkApplyNonBlock(tree: untpd.Tree)(implicit ctx: Context) = tree match {
305+
case tree: untpd.Apply => assert(!tree.fun.isInstanceOf[untpd.Block])
306+
case tree: untpd.UnApply => assert(!tree.fun.isInstanceOf[untpd.Block])
307+
case _ =>
308+
}
309+
303310
def checkNotRepeated(tree: Tree)(implicit ctx: Context): tree.type = {
304311
def allowedRepeated = tree.tpe.widen.isRepeatedParam
305312

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2378,7 +2378,12 @@ class Typer extends Namer
23782378
}
23792379
} else issueErrors()
23802380
}
2381-
else readaptSimplified(tpd.Apply(tree, args))
2381+
else tree match {
2382+
case tree: Block =>
2383+
readaptSimplified(tpd.Block(tree.stats, tpd.Apply(tree.expr, args)))
2384+
case _ =>
2385+
readaptSimplified(tpd.Apply(tree, args))
2386+
}
23822387
}
23832388
addImplicitArgs(argCtx(tree))
23842389
}

tests/pos/i5107.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Test {
2+
inline def foo(x: Int = 5)(implicit y: Int): Int =
3+
if (x > 0) y * y
4+
else y + y
5+
6+
implicit val m: Int = 7
7+
8+
(new Test).foo()
9+
}

0 commit comments

Comments
 (0)