Skip to content

Commit

Permalink
Backport "emit generatedNonLocalClass in backend when callback is not…
Browse files Browse the repository at this point in the history
… enabled" to LTS 3.3 (scala#21997)

Backports scala#21186 to the LTS 3.3 branch.

PR submitted by the release tooling.
[skip ci]
  • Loading branch information
WojciechMazur authored Dec 2, 2024
2 parents 85e2d1a + f548a6c commit 8e7690b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
10 changes: 8 additions & 2 deletions compiler/src/dotty/tools/backend/jvm/CodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,14 @@ class CodeGen(val int: DottyBackendInterface, val primitives: DottyPrimitives)(
ctx.compilerCallback.onClassGenerated(sourceFile, convertAbstractFile(clsFile), className)

ctx.withIncCallback: cb =>
if (isLocal) cb.generatedLocalClass(sourceFile, clsFile.jpath)
else cb.generatedNonLocalClass(sourceFile, clsFile.jpath, className, fullClassName)
if isLocal then
cb.generatedLocalClass(sourceFile, clsFile.jpath)
else if !cb.enabled() then
// callback is not enabled, so nonLocalClasses were not reported in ExtractAPI
val fullClassName = atPhase(sbtExtractDependenciesPhase) {
ExtractDependencies.classNameAsString(claszSymbol)
}
cb.generatedNonLocalClass(sourceFile, clsFile.jpath, className, fullClassName)
}
}

Expand Down
31 changes: 29 additions & 2 deletions sbt-bridge/test/xsbt/ProductsSpecification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,39 @@ class ProductsSpecification {
val output = compiler.compileSrcsToJar(src)
val srcFile = output.srcFiles.head
val products = output.analysis.productClassesToSources.filter(_._2 == srcFile).keys.toSet

def toPathInJar(className: String): Path =
Paths.get(s"${output.classesOutput}!${className.replace('.', File.separatorChar)}.class")
val expected = Set("example.A", "example.A$B", "example.A$C$1").map(toPathInJar)
assertEquals(products, expected)
}

@Test
def extractNonLocalClassesNoInc = {
val src =
"""package example
|
|class A {
| class B
| def foo =
| class C
|}""".stripMargin
val output = compiler.compileSrcsNoInc(src)
val srcFile = output.srcFiles.head
val (srcNames, binaryNames) = output.analysis.classNames(srcFile).unzip // non local class names

assertFalse(output.analysis.enabled()) // inc phases are disabled
assertTrue(output.analysis.apis.isEmpty) // extract-api did not run
assertTrue(output.analysis.usedNamesAndScopes.isEmpty) // extract-dependencies did not run

// note that local class C is not included, classNames only records non local classes
val expectedBinary = Set("example.A", "example.A$B")
assertEquals(expectedBinary, binaryNames.toSet)

// note that local class C is not included, classNames only records non local classes
val expectedSrc = Set("example.A", "example.A.B")
assertEquals(expectedSrc, srcNames.toSet)
}

private def compiler = new ScalaCompilerForUnitTesting
}
}
15 changes: 11 additions & 4 deletions sbt-bridge/test/xsbt/ScalaCompilerForUnitTesting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,13 @@ class ScalaCompilerForUnitTesting {
* The sequence of temporary files corresponding to passed snippets and analysis
* callback is returned as a result.
*/
def compileSrcs(groupedSrcs: List[List[String]], sourcePath: List[String] = Nil, compileToJar: Boolean = false): CompileOutput = {
def compileSrcs(groupedSrcs: List[List[String]], sourcePath: List[String] = Nil, compileToJar: Boolean = false, incEnabled: Boolean = true): CompileOutput = {
val temp = IO.createTemporaryDirectory
val analysisCallback = new TestCallback
val (forceSbtArgs, analysisCallback) =
if (incEnabled)
(Seq("-Yforce-sbt-phases"), new TestCallback)
else
(Seq.empty, new TestCallbackNoInc)
val testProgress = new TestCompileProgress
val classesOutput =
if (compileToJar) {
Expand Down Expand Up @@ -174,7 +178,7 @@ class ScalaCompilerForUnitTesting {
bridge.run(
virtualSrcFiles,
new TestDependencyChanges,
Array("-Yforce-sbt-phases", "-classpath", classesOutputPath, "-usejavacp", "-d", classesOutputPath) ++ maybeSourcePath,
(forceSbtArgs ++: Array("-classpath", classesOutputPath, "-usejavacp", "-d", classesOutputPath)) ++ maybeSourcePath,
output,
analysisCallback,
new TestReporter,
Expand All @@ -193,6 +197,10 @@ class ScalaCompilerForUnitTesting {
compileSrcs(List(srcs.toList))
}

def compileSrcsNoInc(srcs: String*): CompileOutput = {
compileSrcs(List(srcs.toList), incEnabled = false)
}

def compileSrcsToJar(srcs: String*): CompileOutput =
compileSrcs(List(srcs.toList), compileToJar = true)

Expand All @@ -202,4 +210,3 @@ class ScalaCompilerForUnitTesting {
new TestVirtualFile(srcFile.toPath)
}
}

4 changes: 4 additions & 0 deletions sbt-bridge/test/xsbti/TestCallback.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import DependencyContext._
import java.{util => ju}
import ju.Optional

class TestCallbackNoInc extends TestCallback {
override def enabled(): Boolean = false
}

class TestCallback extends AnalysisCallback2 {
case class TestUsedName(name: String, scopes: ju.EnumSet[UseScope])

Expand Down

0 comments on commit 8e7690b

Please sign in to comment.