Skip to content

Fix #11592: Scala.js: Handle default params of native JS classes. #11857

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
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
22 changes: 11 additions & 11 deletions compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,12 @@ class JSCodeGen()(using genCtx: Context) {
"genScalaClass() must be called only for normal classes: "+sym)
assert(sym.superClass != NoSymbol, sym)

/*if (hasDefaultCtorArgsAndRawJSModule(sym)) {
reporter.error(pos,
"Implementation restriction: constructors of " +
"Scala classes cannot have default parameters " +
"if their companion module is JS native.")
}*/
if (hasDefaultCtorArgsAndJSModule(sym)) {
report.error(
"Implementation restriction: " +
"constructors of Scala classes cannot have default parameters if their companion module is JS native.",
td)
}

val classIdent = encodeClassNameIdent(sym)
val originalName = originalNameOfClass(sym)
Expand Down Expand Up @@ -965,9 +965,8 @@ class JSCodeGen()(using genCtx: Context) {

if (hasDefaultCtorArgsAndJSModule(classSym)) {
report.error(
"Implementation restriction: constructors of " +
"non-native JS classes cannot have default parameters " +
"if their companion module is JS native.",
"Implementation restriction: " +
"constructors of non-native JS classes cannot have default parameters if their companion module is JS native.",
classSym.srcPos)
val ctorDef = js.JSMethodDef(js.MemberFlags.empty,
js.StringLiteral("constructor"), Nil, None, js.Skip())(
Expand Down Expand Up @@ -1067,9 +1066,10 @@ class JSCodeGen()(using genCtx: Context) {
Some(js.MethodDef(js.MemberFlags.empty, methodName, originalName,
jsParams, toIRType(patchedResultType(sym)), None)(
OptimizerHints.empty, None))
} else /*if (isJSNativeCtorDefaultParam(sym)) {
} else if (sym.isJSNativeCtorDefaultParam) {
// #11592
None
} else if (sym.isClassConstructor && isHijackedBoxedClass(sym.owner)) {
} else /*if (sym.isClassConstructor && isHijackedBoxedClass(sym.owner)) {
None
} else*/ {
/*def isTraitImplForwarder = dd.rhs match {
Expand Down
7 changes: 7 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/sjs/JSSymUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ object JSSymUtils {
}
}

/** Is this symbol a default param accessor for the constructor of a native JS class? */
def isJSNativeCtorDefaultParam(using Context): Boolean = {
sym.name.is(DefaultGetterName)
&& sym.name.exclude(DefaultGetterName) == nme.CONSTRUCTOR
&& sym.owner.linkedClass.hasAnnotation(jsdefn.JSNativeAnnot)
}

def jsCallingConvention(using Context): JSCallingConvention =
JSCallingConvention.of(sym)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.junit.Assert.*
import org.junit.Test

import scala.scalajs.js
import scala.scalajs.js.annotation._

class RegressionTestScala3 {
import RegressionTestScala3.*
Expand All @@ -21,6 +22,11 @@ class RegressionTestScala3 {
assertEquals(-1, obj2.y)
assertEquals(4, obj2.foo(5))
}

@Test def testJSNativeDefaultCtorParamIssue11592(): Unit = {
assertEquals("foo", new RangeErrorIssue11592("foo").message)
assertEquals("", new RangeErrorIssue11592().message)
}
}

object RegressionTestScala3 {
Expand All @@ -33,6 +39,12 @@ object RegressionTestScala3 {

def foo(x: Int): Int = new ChildClass().concreteMethod(x)
}

@js.native
@JSGlobal("RangeError")
class RangeErrorIssue11592(msg: String = js.native) extends js.Object {
val message: String = js.native
}
}

// This class needs to be at the top-level, not in an object, to reproduce the issue
Expand Down