Skip to content

Commit e9fb094

Browse files
authored
Merge pull request #13537 from dotty-staging/sjs-fix-prim-plus-string
Fix #13518: Scala.js: fix primitive + string when the string is a TermRef.
2 parents ea871c2 + 33c335e commit e9fb094

File tree

2 files changed

+9
-21
lines changed

2 files changed

+9
-21
lines changed

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

+1-21
Original file line numberDiff line numberDiff line change
@@ -2790,27 +2790,7 @@ class JSCodeGen()(using genCtx: Context) {
27902790
args: List[Tree]): js.Tree = {
27912791
implicit val pos = tree.span
27922792

2793-
val arg = args.head
2794-
2795-
/* Primitive number types such as scala.Int have a
2796-
* def +(s: String): String
2797-
* method, which is why we have to box the lhs sometimes.
2798-
* Otherwise, both lhs and rhs are already reference types (Any or String)
2799-
* so boxing is not necessary (in particular, rhs is never a primitive).
2800-
*/
2801-
assert(!isPrimitiveValueType(receiver.tpe) || arg.tpe.isRef(defn.StringClass))
2802-
assert(!isPrimitiveValueType(arg.tpe))
2803-
2804-
val genLhs = {
2805-
val genLhs0 = genExpr(receiver)
2806-
// Box the receiver if it is a primitive value
2807-
if (!isPrimitiveValueType(receiver.tpe)) genLhs0
2808-
else makePrimitiveBox(genLhs0, receiver.tpe)
2809-
}
2810-
2811-
val genRhs = genExpr(arg)
2812-
2813-
js.BinaryOp(js.BinaryOp.String_+, genLhs, genRhs)
2793+
js.BinaryOp(js.BinaryOp.String_+, genExpr(receiver), genExpr(args.head))
28142794
}
28152795

28162796
/** Gen JS code for a call to Any.## */

tests/sjs-junit/test/org/scalajs/testsuite/compiler/RegressionTestScala3.scala

+8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ class RegressionTestScala3 {
4444
assertEquals(1, X_Issue13221.I.i)
4545
assertEquals(1, X_Issue13221.blah)
4646
}
47+
48+
@Test def primitivePlusStringThatIsATermRefIssue13518(): Unit = {
49+
def charPlusString(x: String): String = 'a' + x
50+
assertEquals("abc", charPlusString("bc"))
51+
52+
def intPlusString(x: String): String = 5 + x
53+
assertEquals("5bc", intPlusString("bc"))
54+
}
4755
}
4856

4957
object RegressionTestScala3 {

0 commit comments

Comments
 (0)