Skip to content

Commit 8d4babb

Browse files
committed
Do not emit static forwarders for non-top-level objects by default.
This is a forward port of the upstream commit scala-js/scala-js@3c3b100 We now require the option `-scalajs-genStaticForwardersForNonTopLevelObjects` to emit static forwarders for non-top-level objects. In addition, we take `-XnoForwarders` into account, which we were erroneously not doing before.
1 parent ce48f5a commit 8d4babb

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,14 +523,33 @@ class JSCodeGen()(using genCtx: Context) {
523523
* `def hashCode(): Int` and a `static def hashCode(Int): Int`. The JVM
524524
* back-end considers them as colliding because they have the same name,
525525
* but we must not.
526+
*
527+
* By default, we only emit forwarders for top-level objects, like the JVM
528+
* back-end. However, if requested via a compiler option, we enable them
529+
* for all static objects. This is important so we can implement static
530+
* methods of nested static classes of JDK APIs (see scala-js/#3950).
526531
*/
527532

528533
/** Is the given Scala class, interface or module class a candidate for
529534
* static forwarders?
535+
*
536+
* - the flag `-XnoForwarders` is not set to true, and
537+
* - the symbol is static, and
538+
* - either of both of the following is true:
539+
* - the flag `-scalajsGenStaticForwardersForNonTopLevelObjects` is set to true, or
540+
* - the symbol was originally at the package level
541+
*
542+
* Other than the Scala.js-specific flag, and the fact that we also consider
543+
* interfaces, this performs the same tests as the JVM back-end.
530544
*/
531545
def isCandidateForForwarders(sym: Symbol): Boolean = {
532-
// it must be a top level class
533-
sym.isStatic
546+
!ctx.settings.XnoForwarders.value && sym.isStatic && {
547+
ctx.settings.scalajsGenStaticForwardersForNonTopLevelObjects.value || {
548+
atPhase(flattenPhase) {
549+
toDenot(sym).owner.is(PackageClass)
550+
}
551+
}
552+
}
534553
}
535554

536555
/** Gen the static forwarders to the members of a class or interface for

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ class ScalaSettings extends Settings.SettingGroup {
6565
val pluginsDir: Setting[String] = StringSetting ("-Xpluginsdir", "path", "Path to search for plugin archives.", Defaults.scalaPluginPath)
6666
val pluginOptions: Setting[List[String]] = MultiStringSetting ("-P", "plugin:opt", "Pass an option to a plugin, e.g. -P:<plugin>:<opt>")
6767

68+
/** Scala.js-related settings */
69+
val scalajsGenStaticForwardersForNonTopLevelObjects: Setting[Boolean] = BooleanSetting("-scalajs-genStaticForwardersForNonTopLevelObjects", "Generate static forwarders even for non-top-level objects (Scala.js only)")
70+
6871
/** -X "Advanced" settings
6972
*/
7073
val Xhelp: Setting[Boolean] = BooleanSetting("-X", "Print a synopsis of advanced options.")

project/Build.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,8 @@ object Build {
10041004
)
10051005
}.taskValue,
10061006

1007+
scalacOptions in Test += "-scalajs-genStaticForwardersForNonTopLevelObjects",
1008+
10071009
scalaJSLinkerConfig ~= { _.withSemantics(build.TestSuiteLinkerOptions.semantics _) },
10081010
scalaJSModuleInitializers in Test ++= build.TestSuiteLinkerOptions.moduleInitializers,
10091011

0 commit comments

Comments
 (0)