Skip to content

Commit 1716ff6

Browse files
committed
Move native companion check to top-level genNonNativeJSClass
This is consistent with genClass. Forward port of scala-js/scala-js@6022605
1 parent c84c102 commit 1716ff6

File tree

2 files changed

+34
-31
lines changed

2 files changed

+34
-31
lines changed

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

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,13 @@ class JSCodeGen()(using genCtx: Context) {
456456
i"genNonNativeJSClass() must be called only for non-native JS classes: $sym")
457457
assert(sym.superClass != NoSymbol, sym)
458458

459+
if (hasDefaultCtorArgsAndJSModule(sym)) {
460+
report.error(
461+
"Implementation restriction: " +
462+
"constructors of non-native JS classes cannot have default parameters if their companion module is JS native.",
463+
td)
464+
}
465+
459466
val classIdent = encodeClassNameIdent(sym)
460467
val originalName = originalNameOfClass(sym)
461468

@@ -964,42 +971,31 @@ class JSCodeGen()(using genCtx: Context) {
964971
constructorTrees: List[DefDef]): (Option[List[js.ParamDef]], js.JSMethodDef) = {
965972
implicit val pos = classSym.span
966973

967-
if (hasDefaultCtorArgsAndJSModule(classSym)) {
968-
report.error(
969-
"Implementation restriction: " +
970-
"constructors of non-native JS classes cannot have default parameters if their companion module is JS native.",
971-
classSym.srcPos)
972-
val ctorDef = js.JSMethodDef(js.MemberFlags.empty,
973-
js.StringLiteral("constructor"), Nil, None, js.Skip())(
974-
OptimizerHints.empty, None)
975-
(None, ctorDef)
976-
} else {
977-
withNewLocalNameScope {
978-
localNames.reserveLocalName(JSSuperClassParamName)
979-
980-
val ctors: List[js.MethodDef] = constructorTrees.flatMap { tree =>
981-
genMethodWithCurrentLocalNameScope(tree)
982-
}
974+
withNewLocalNameScope {
975+
localNames.reserveLocalName(JSSuperClassParamName)
983976

984-
val (captureParams, dispatch) =
985-
jsExportsGen.genJSConstructorDispatch(constructorTrees.map(_.symbol))
977+
val ctors: List[js.MethodDef] = constructorTrees.flatMap { tree =>
978+
genMethodWithCurrentLocalNameScope(tree)
979+
}
986980

987-
/* Ensure that the first JS class capture is a reference to the JS super class value.
988-
* genNonNativeJSClass and genNewAnonJSClass rely on this.
989-
*/
990-
val captureParamsWithJSSuperClass = captureParams.map { params =>
991-
val jsSuperClassParam = js.ParamDef(
992-
js.LocalIdent(JSSuperClassParamName), NoOriginalName,
993-
jstpe.AnyType, mutable = false)
994-
jsSuperClassParam :: params
995-
}
981+
val (captureParams, dispatch) =
982+
jsExportsGen.genJSConstructorDispatch(constructorTrees.map(_.symbol))
996983

997-
val ctorDef = JSConstructorGen.buildJSConstructorDef(dispatch, ctors, freshLocalIdent("overload")) {
998-
msg => report.error(msg, classSym.srcPos)
999-
}
984+
/* Ensure that the first JS class capture is a reference to the JS super class value.
985+
* genNonNativeJSClass and genNewAnonJSClass rely on this.
986+
*/
987+
val captureParamsWithJSSuperClass = captureParams.map { params =>
988+
val jsSuperClassParam = js.ParamDef(
989+
js.LocalIdent(JSSuperClassParamName), NoOriginalName,
990+
jstpe.AnyType, mutable = false)
991+
jsSuperClassParam :: params
992+
}
1000993

1001-
(captureParamsWithJSSuperClass, ctorDef)
994+
val ctorDef = JSConstructorGen.buildJSConstructorDef(dispatch, ctors, freshLocalIdent("overload")) {
995+
msg => report.error(msg, classSym.srcPos)
1002996
}
997+
998+
(captureParamsWithJSSuperClass, ctorDef)
1003999
}
10041000
}
10051001

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,13 @@ final class JSExportsGen(jsCodeGen: JSCodeGen)(using Context) {
772772
js.Undefined()
773773
else
774774
genApplyJSClassMethod(targetTree, defaultGetter, defaultGetterArgs)
775+
} else if (defaultGetter.owner == targetSym) {
776+
/* We get here if a non-native constructor has a native companion.
777+
* This is reported on a per-class level.
778+
*/
779+
assert(sym.isClassConstructor,
780+
s"got non-constructor method $sym with default method in JS native companion")
781+
js.Undefined()
775782
} else {
776783
report.error(
777784
"When overriding a native method with default arguments, " +

0 commit comments

Comments
 (0)