Skip to content

Commit 0c597f0

Browse files
committed
Merge pull request scala#4809 from wpopielarski/delambdafy-multiple-outputs
Multi output problem with delambdafied compilation
2 parents 9cdfc45 + b237fb3 commit 0c597f0

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed

src/compiler/scala/tools/nsc/backend/jvm/BCodeHelpers.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,14 @@ abstract class BCodeHelpers extends BCodeIdiomatic with BytecodeWriters {
3535
/*
3636
* must-single-thread
3737
*/
38-
def getOutFolder(csym: Symbol, cName: String, cunit: CompilationUnit): _root_.scala.tools.nsc.io.AbstractFile = {
39-
try {
38+
def getOutFolder(csym: Symbol, cName: String, cunit: CompilationUnit): _root_.scala.tools.nsc.io.AbstractFile =
39+
_root_.scala.util.Try {
4040
outputDirectory(csym)
41-
} catch {
41+
}.recover {
4242
case ex: Throwable =>
4343
reporter.error(cunit.body.pos, s"Couldn't create file for class $cName\n${ex.getMessage}")
4444
null
45-
}
46-
}
45+
}.get
4746

4847
var pickledBytes = 0 // statistics
4948

src/compiler/scala/tools/nsc/transform/Delambdafy.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ abstract class Delambdafy extends Transform with TypingTransformers with ast.Tre
294294
val name = unit.freshTypeName(s"$oldClassPart$suffix".replace("$anon", "$nestedInAnon"))
295295

296296
val lambdaClass = pkg newClassSymbol(name, originalFunction.pos, FINAL | SYNTHETIC) addAnnotation SerialVersionUIDAnnotation
297+
lambdaClass.associatedFile = unit.source.file
297298
// make sure currentRun.compiles(lambdaClass) is true (AddInterfaces does the same for trait impl classes)
298299
currentRun.symSource(lambdaClass) = funOwner.sourceFile
299300
lambdaClass setInfo ClassInfoType(parents, newScope, lambdaClass)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package scala.tools.nsc.transform.delambdafy
2+
3+
import scala.reflect.io.Path.jfile2path
4+
import scala.tools.nsc.backend.jvm.CodeGenTools.getGeneratedClassfiles
5+
import scala.tools.nsc.backend.jvm.CodeGenTools.makeSourceFile
6+
import scala.tools.nsc.backend.jvm.CodeGenTools.newCompilerWithoutVirtualOutdir
7+
import scala.tools.nsc.io.AbstractFile
8+
import scala.tools.testing.TempDir
9+
10+
import org.junit.Assert.assertTrue
11+
import org.junit.Test
12+
import org.junit.runner.RunWith
13+
import org.junit.runners.JUnit4
14+
15+
@RunWith(classOf[JUnit4])
16+
class DelambdafyTest {
17+
def compileToMultipleOutputWithDelamdbafyMethod(): List[(String, Array[Byte])] = {
18+
val codeForMultiOutput = """
19+
object Delambdafy {
20+
type -->[D, I] = PartialFunction[D, I]
21+
22+
def main(args: Array[String]): Unit = {
23+
val result = List(1, 2, 4).map { a =>
24+
val list = List("1", "2", "3").map { _ + "test" }
25+
list.find { _ == a.toString + "test" }
26+
}
27+
println(result)
28+
lazy val _foo = foo(result) {
29+
case x :: xs if x isDefined => x.get.length
30+
case _ => 0
31+
}
32+
println(_foo)
33+
lazy val bar: Int => Int = {
34+
case 2 => 23
35+
case _ =>
36+
val v = List(1).map { _ + 42 }.head
37+
v + 31
38+
}
39+
bar(3)
40+
lazy val _baz = baz {
41+
case 1 =>
42+
val local = List(1).map(_ + 1)
43+
local.head
44+
}
45+
}
46+
47+
def baz[T](f: Any --> Any): Any => Any = f
48+
49+
def foo(b: List[Option[String]])(a: List[Option[String]] => Int): Int = a(b)
50+
}
51+
"""
52+
val srcFile = makeSourceFile(codeForMultiOutput, "delambdafyTest.scala")
53+
val outDir = AbstractFile.getDirectory(TempDir.createTempDir())
54+
val outDirPath = outDir.canonicalPath
55+
val extraArgs = "-Ybackend:GenBCode -Ydelambdafy:method"
56+
val argsWithOutDir = extraArgs + s" -d $outDirPath -cp $outDirPath"
57+
val compiler = newCompilerWithoutVirtualOutdir(extraArgs = argsWithOutDir)
58+
compiler.settings.outputDirs.add(srcFile.file, outDir)
59+
60+
new compiler.Run().compileSources(List(srcFile))
61+
62+
val classfiles = getGeneratedClassfiles(outDir)
63+
outDir.delete()
64+
classfiles
65+
}
66+
67+
@Test
68+
def shouldFindOutputFoldersForAllPromotedLambdasAsMethod(): Unit = {
69+
val actual = compileToMultipleOutputWithDelamdbafyMethod()
70+
71+
assertTrue(actual.length > 0)
72+
}
73+
}

0 commit comments

Comments
 (0)