Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport "emit generatedNonLocalClass in backend when callback is not enabled" to LTS 3.3 #21997

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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