diff --git a/compiler/src/dotty/tools/backend/jvm/BCodeSkelBuilder.scala b/compiler/src/dotty/tools/backend/jvm/BCodeSkelBuilder.scala index f67cd84ecdd0..9ed88e7d1ad6 100644 --- a/compiler/src/dotty/tools/backend/jvm/BCodeSkelBuilder.scala +++ b/compiler/src/dotty/tools/backend/jvm/BCodeSkelBuilder.scala @@ -139,6 +139,7 @@ trait BCodeSkelBuilder extends BCodeHelpers { // See `tests/run/given-var.scala` // + // !!! Part of this logic is duplicated in JSCodeGen.genCompilationUnit claszSymbol.info.decls.foreach { f => if f.isField && !f.name.is(LazyBitMapName) then f.setFlag(JavaStatic) diff --git a/compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala b/compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala index c533f2e15d9e..b4093de6de50 100644 --- a/compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala +++ b/compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala @@ -198,6 +198,23 @@ class JSCodeGen()(using genCtx: Context) { } val allTypeDefs = collectTypeDefs(cunit.tpdTree) + /* #13221 Set JavaStatic on all the Module fields of static module classes. + * This is necessary for `desugarIdent` not to crash in some obscure + * scenarios. + * + * !!! Part of this logic is duplicated in BCodeSkelBuilder.genPlainClass + * + * However, here we only do this for Module fields, not all fields. + */ + for (typeDef <- allTypeDefs) { + if (typeDef.symbol.is(ModuleClass)) { + typeDef.symbol.info.decls.foreach { f => + if (f.isField && f.is(Module)) + f.setFlag(JavaStatic) + } + } + } + val (anonJSClassTypeDefs, otherTypeDefs) = allTypeDefs.partition(td => td.symbol.isAnonymousClass && td.symbol.isJSType) @@ -1957,12 +1974,7 @@ class JSCodeGen()(using genCtx: Context) { val args = tree.args val sym = tree.fun.symbol - val fun = tree.fun match { - case fun: Ident => desugarIdent(fun) - case fun => fun - } - - fun match { + tree.fun match { case _ if sym.isJSDefaultParam => js.Transient(UndefinedParam) diff --git a/tests/sjs-junit/test/org/scalajs/testsuite/compiler/RegressionTestScala3.scala b/tests/sjs-junit/test/org/scalajs/testsuite/compiler/RegressionTestScala3.scala index 77a986470012..1399763297ba 100644 --- a/tests/sjs-junit/test/org/scalajs/testsuite/compiler/RegressionTestScala3.scala +++ b/tests/sjs-junit/test/org/scalajs/testsuite/compiler/RegressionTestScala3.scala @@ -39,6 +39,11 @@ class RegressionTestScala3 { @Test def defaultAccessorBridgesIssue12572(): Unit = { new MyPromiseIssue12572[Int](5) } + + @Test def desugarIdentCrashIssue13221(): Unit = { + assertEquals(1, X_Issue13221.I.i) + assertEquals(1, X_Issue13221.blah) + } } object RegressionTestScala3 { @@ -76,6 +81,17 @@ object RegressionTestScala3 { ??? } } + + object X_Issue13221 extends Y_Issue13221 { + object I { + def i = 1 + } + } + + abstract class Y_Issue13221 { self: X_Issue13221.type => + import I._ + def blah = i + } } // This class needs to be at the top-level, not in an object, to reproduce the issue