Skip to content

Fix #4616: Proper static dependency tracking for jl.Class #4623

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

Merged
merged 2 commits into from
Feb 4, 2022
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
80 changes: 80 additions & 0 deletions linker/jvm/src/test/scala/org/scalajs/linker/RunTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Scala.js (https://www.scala-js.org/)
*
* Copyright EPFL.
*
* Licensed under Apache License 2.0
* (https://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/

package org.scalajs.linker

import scala.concurrent._
import scala.concurrent.duration._

import java.nio.file.Path

import org.junit.{Rule, Test}
import org.junit.Assert._
import org.junit.rules.TemporaryFolder

import org.scalajs.ir.Trees._

import org.scalajs.junit.async._

import org.scalajs.jsenv.nodejs.NodeJSEnv
import org.scalajs.jsenv.test.kit.TestKit

import org.scalajs.linker.interface._
import org.scalajs.linker.testutils.LinkingUtils._
import org.scalajs.linker.testutils.TestIRBuilder._

class RunTest {
import scala.concurrent.ExecutionContext.Implicits.global

@(Rule @scala.annotation.meta.getter)
val tempFolder = new TemporaryFolder()
Comment on lines +38 to +39
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this something standard that I should know about, or does this deserve a comment? (I feel like it could be the former.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a standard JUnit thing. A JUnit rule essentially encapsulates test fixtures (@Before and @After).


@Test
def jlClassIsCore_Issue4616(): AsyncResult = await {
// Check that if jl.Class is instantiated, it must be depended upon by jl.Object.

val classDefs = Seq(
mainTestClassDef({
consoleLog(ClassOf(T))
})
)

val linkerConfig = StandardConfig()
.withModuleKind(ModuleKind.ESModule)
.withModuleSplitStyle(ModuleSplitStyle.SmallestModules)
.withSourceMap(false)
.withOutputPatterns(OutputPatterns.fromJSFile("%s.mjs"))

testLinkAndRun(classDefs, MainTestModuleInitializers, linkerConfig,
TestKit.InputKind.ESModule)
}

private def testLinkAndRun(classDefs: Seq[ClassDef],
moduleInitializers: List[ModuleInitializer],
linkerConfig: StandardConfig, inputKind: TestKit.InputKind): Future[Unit] = {
val output = tempFolder.newFolder().toPath

val kit = new TestKit(new NodeJSEnv, timeout = 2.seconds, inputKind)

for {
report <- testLink(classDefs, moduleInitializers, linkerConfig, PathOutputDirectory(output))
} yield {
assertEquals(report.publicModules.size, 1)

val path = output.resolve(report.publicModules.head.jsFileName)

kit.withRun(Seq(kit.pathToInput(path))) {
_.succeeds()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,17 @@ private final class Analyzer(config: CommonPhaseConfig,
lookupClass(className)(_.accessData())
}

if (data.accessedClassClass) {
/* java.lang.Class is only ever instantiated in the CoreJSLib.
* Therefore, make java.lang.Object depend on it instead of the caller itself.
*/
objectClassInfo.staticDependencies += ClassClass
lookupClass(ClassClass) { clazz =>
clazz.instantiated()
clazz.callMethodStatically(MemberNamespace.Constructor, ObjectArgConstructorName)
}
}

for (className <- data.referencedClasses) {
/* No need to add to staticDependencies: The classes will not be
* referenced in the final JS code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ object Infos {
val usedInstanceTests: List[ClassName],
val accessedClassData: List[ClassName],
val referencedClasses: List[ClassName],
val accessedClassClass: Boolean,
val accessedNewTarget: Boolean,
val accessedImportMeta: Boolean
)
Expand All @@ -98,7 +99,7 @@ object Infos {
val Empty: ReachabilityInfo = {
new ReachabilityInfo(Map.empty, Map.empty, Map.empty, Map.empty,
Map.empty, Map.empty, Map.empty, Nil, Nil, Nil, Nil, Nil, false,
false)
false, false)
}
}

Expand Down Expand Up @@ -161,6 +162,7 @@ object Infos {
private val usedInstanceTests = mutable.Set.empty[ClassName]
private val accessedClassData = mutable.Set.empty[ClassName]
private val referencedClasses = mutable.Set.empty[ClassName]
private var accessedClassClass = false
private var accessedNewTarget = false
private var accessedImportMeta = false

Expand Down Expand Up @@ -311,6 +313,11 @@ object Infos {
this
}

def addAccessedClassClass(): this.type = {
accessedClassClass = true
this
}

def addAccessNewTarget(): this.type = {
accessedNewTarget = true
this
Expand Down Expand Up @@ -338,6 +345,7 @@ object Infos {
usedInstanceTests = usedInstanceTests.toList,
accessedClassData = accessedClassData.toList,
referencedClasses = referencedClasses.toList,
accessedClassClass = accessedClassClass,
accessedNewTarget = accessedNewTarget,
accessedImportMeta = accessedImportMeta
)
Expand Down Expand Up @@ -570,13 +578,10 @@ object Infos {

case ClassOf(cls) =>
builder.maybeAddAccessedClassData(cls)

// `ClassOf` instantiates a java.lang.Class.
builder.addInstantiatedClass(ClassClass, ObjectArgConstructorName)
builder.addAccessedClassClass()

case GetClass(_) =>
// `GetClass` instantiates a java.lang.Class.
builder.addInstantiatedClass(ClassClass, ObjectArgConstructorName)
builder.addAccessedClassClass()

case JSPrivateSelect(qualifier, className, field) =>
builder.addPrivateJSFieldUsed(className, field.name)
Expand Down
12 changes: 7 additions & 5 deletions project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,9 @@ object Build {
).settings(
libraryDependencies ++= Seq(
"com.google.javascript" % "closure-compiler" % "v20211201",
"com.google.jimfs" % "jimfs" % "1.1" % "test"
"com.google.jimfs" % "jimfs" % "1.1" % "test",
"org.scala-js" %% "scalajs-env-nodejs" % "1.3.0" % "test",
"org.scala-js" %% "scalajs-js-envs-test-kit" % "1.3.0" % "test"
) ++ (
parallelCollectionsDependencies(scalaVersion.value)
),
Expand Down Expand Up @@ -1072,7 +1074,7 @@ object Build {
name := "Scala.js sbt test adapter",
libraryDependencies ++= Seq(
"org.scala-sbt" % "test-interface" % "1.0",
"org.scala-js" %% "scalajs-js-envs" % "1.2.1",
"org.scala-js" %% "scalajs-js-envs" % "1.3.0",
"com.google.jimfs" % "jimfs" % "1.1" % "test",
),
libraryDependencies ++= JUnitDeps,
Expand Down Expand Up @@ -1101,8 +1103,8 @@ object Build {
mimaBinaryIssueFilters ++= BinaryIncompatibilities.SbtPlugin,

addSbtPlugin("org.portable-scala" % "sbt-platform-deps" % "1.0.1"),
libraryDependencies += "org.scala-js" %% "scalajs-js-envs" % "1.2.1",
libraryDependencies += "org.scala-js" %% "scalajs-env-nodejs" % "1.2.1",
libraryDependencies += "org.scala-js" %% "scalajs-js-envs" % "1.3.0",
libraryDependencies += "org.scala-js" %% "scalajs-env-nodejs" % "1.3.0",

scriptedLaunchOpts += "-Dplugin.version=" + version.value,

Expand Down Expand Up @@ -2335,7 +2337,7 @@ object Build {

resolvers += Resolver.typesafeIvyRepo("releases"),

libraryDependencies += "org.scala-js" %% "scalajs-env-nodejs" % "1.2.1",
libraryDependencies += "org.scala-js" %% "scalajs-env-nodejs" % "1.3.0",

artifactPath in fetchScalaSource :=
baseDirectory.value.getParentFile / "fetchedSources" / scalaVersion.value,
Expand Down
4 changes: 2 additions & 2 deletions project/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ libraryDependencies += "com.google.jimfs" % "jimfs" % "1.1"

libraryDependencies += "org.eclipse.jgit" % "org.eclipse.jgit.pgm" % "3.2.0.201312181205-r"

libraryDependencies += "org.scala-js" %% "scalajs-js-envs" % "1.2.1"
libraryDependencies += "org.scala-js" %% "scalajs-env-nodejs" % "1.2.1"
libraryDependencies += "org.scala-js" %% "scalajs-js-envs" % "1.3.0"
libraryDependencies += "org.scala-js" %% "scalajs-env-nodejs" % "1.3.0"

Compile / unmanagedSourceDirectories ++= {
val root = baseDirectory.value.getParentFile
Expand Down