Skip to content

Commit bd13037

Browse files
committed
Fix #15419: Handle Unit parameters with default values in JS types.
Both at call site (for native and non-native JS types) and at definition sites (for non-native JS types). Forward port of scala-js/scala-js@1de54d7
1 parent e8d081c commit bd13037

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1829,7 +1829,15 @@ class JSCodeGen()(using genCtx: Context) {
18291829
}
18301830

18311831
case Block(stats, expr) =>
1832-
js.Block(stats.map(genStat) :+ genStatOrExpr(expr, isStat))
1832+
// #15419 Collapse { <undefined-param>; BoxedUnit } to <undefined-param>
1833+
val genStatsAndExpr0 = stats.map(genStat(_)) :+ genStatOrExpr(expr, isStat)
1834+
val genStatsAndExpr = genStatsAndExpr0 match {
1835+
case (undefParam @ js.Transient(UndefinedParam)) :: js.Undefined() :: Nil =>
1836+
undefParam :: Nil
1837+
case _ =>
1838+
genStatsAndExpr0
1839+
}
1840+
js.Block(genStatsAndExpr)
18331841

18341842
case Typed(expr, _) =>
18351843
expr match {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ final class JSExportsGen(jsCodeGen: JSCodeGen)(using Context) {
761761
// Pass previous arguments to defaultGetter
762762
val defaultGetterArgs = previousArgsValues(defaultGetter.info.paramInfoss.head.size)
763763

764-
if (targetSym.isJSType) {
764+
val callGetter = if (targetSym.isJSType) {
765765
if (defaultGetter.owner.isNonNativeJSClass) {
766766
if (defaultGetter.hasAnnotation(jsdefn.JSOptionalAnnot))
767767
js.Undefined()
@@ -784,6 +784,12 @@ final class JSExportsGen(jsCodeGen: JSCodeGen)(using Context) {
784784
} else {
785785
genApplyMethod(targetTree, defaultGetter, defaultGetterArgs)
786786
}
787+
788+
// #15419 If the getter returns void, we must "box" it by returning undefined
789+
if (callGetter.tpe == jstpe.NoType)
790+
js.Block(callGetter, js.Undefined())
791+
else
792+
callGetter
787793
}
788794

789795
private def targetSymForDefaultGetter(sym: Symbol): Symbol =

0 commit comments

Comments
 (0)