Skip to content

Commit 9344b1e

Browse files
authored
Fix rewrite logic for old <function> _ syntax (#21715)
A rewrite would previously produce uncompilable code if the access path to the eta-expanded function goes through at least one `def`. Fixes #21394
2 parents 6fa81cf + e42d883 commit 9344b1e

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,13 @@ trait Migrations:
7171
}
7272
nestedCtx.typerState.commit()
7373

74+
def functionPrefixSuffix(arity: Int) = if (arity > 0) ("", "") else ("(() => ", "())")
75+
7476
lazy val (prefix, suffix) = res match {
75-
case Block(mdef @ DefDef(_, vparams :: Nil, _, _) :: Nil, _: Closure) =>
76-
val arity = vparams.length
77-
if (arity > 0) ("", "") else ("(() => ", "())")
77+
case Block(DefDef(_, vparams :: Nil, _, _) :: Nil, _: Closure) =>
78+
functionPrefixSuffix(vparams.length)
79+
case Block(ValDef(_, _, _) :: Nil, Block(DefDef(_, vparams :: Nil, _, _) :: Nil, _: Closure)) =>
80+
functionPrefixSuffix(vparams.length)
7881
case _ =>
7982
("(() => ", ")")
8083
}

compiler/test/dotty/tools/dotc/CompilationTests.scala

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class CompilationTests {
6363
compileFile("tests/rewrites/rewrites.scala", defaultOptions.and("-source", "3.0-migration").and("-rewrite", "-indent")),
6464
compileFile("tests/rewrites/rewrites3x.scala", defaultOptions.and("-rewrite", "-source", "future-migration")),
6565
compileFile("tests/rewrites/rewrites3x-fatal-warnings.scala", defaultOptions.and("-rewrite", "-source", "future-migration", "-Xfatal-warnings")),
66+
compileFile("tests/rewrites/i21394.scala", defaultOptions.and("-rewrite", "-source", "future-migration")),
6667
compileFile("tests/rewrites/uninitialized-var.scala", defaultOptions.and("-rewrite", "-source", "future-migration")),
6768
compileFile("tests/rewrites/with-type-operator.scala", defaultOptions.and("-rewrite", "-source", "future-migration")),
6869
compileFile("tests/rewrites/private-this.scala", defaultOptions.and("-rewrite", "-source", "future-migration")),

tests/rewrites/i21394.check

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
trait Container:
2+
def loopDef: Container
3+
val loopVal: Container
4+
def fun(x: Int)(y: Int): Int
5+
6+
def test(c: Container): Int =
7+
use(c.fun)
8+
+ use(c.loopDef.fun)
9+
+ use(c.loopVal.fun)
10+
+ use(c.loopDef.loopDef.fun)
11+
+ use(c.loopVal.loopVal.fun)
12+
+ use(c.loopVal.loopDef.fun)
13+
+ use(c.loopDef.loopVal.fun)
14+
+ use(c.loopVal.loopDef.loopVal.fun)
15+
+ use(c.loopVal.loopDef.loopVal.loopDef.fun)
16+
17+
def use(f: Int => Int => Int): Int = ???

tests/rewrites/i21394.scala

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
trait Container:
2+
def loopDef: Container
3+
val loopVal: Container
4+
def fun(x: Int)(y: Int): Int
5+
6+
def test(c: Container): Int =
7+
use(c.fun _)
8+
+ use(c.loopDef.fun _)
9+
+ use(c.loopVal.fun _)
10+
+ use(c.loopDef.loopDef.fun _)
11+
+ use(c.loopVal.loopVal.fun _)
12+
+ use(c.loopVal.loopDef.fun _)
13+
+ use(c.loopDef.loopVal.fun _)
14+
+ use(c.loopVal.loopDef.loopVal.fun _)
15+
+ use(c.loopVal.loopDef.loopVal.loopDef.fun _)
16+
17+
def use(f: Int => Int => Int): Int = ???

0 commit comments

Comments
 (0)