Skip to content

Fix #13518: Scala.js: fix primitive + string when the string is a TermRef. #13537

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
merged 1 commit into from
Sep 16, 2021
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: 1 addition & 21 deletions compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2790,27 +2790,7 @@ class JSCodeGen()(using genCtx: Context) {
args: List[Tree]): js.Tree = {
implicit val pos = tree.span

val arg = args.head

/* Primitive number types such as scala.Int have a
* def +(s: String): String
* method, which is why we have to box the lhs sometimes.
* Otherwise, both lhs and rhs are already reference types (Any or String)
* so boxing is not necessary (in particular, rhs is never a primitive).
*/
assert(!isPrimitiveValueType(receiver.tpe) || arg.tpe.isRef(defn.StringClass))
assert(!isPrimitiveValueType(arg.tpe))

val genLhs = {
val genLhs0 = genExpr(receiver)
// Box the receiver if it is a primitive value
if (!isPrimitiveValueType(receiver.tpe)) genLhs0
else makePrimitiveBox(genLhs0, receiver.tpe)
}

val genRhs = genExpr(arg)

js.BinaryOp(js.BinaryOp.String_+, genLhs, genRhs)
js.BinaryOp(js.BinaryOp.String_+, genExpr(receiver), genExpr(args.head))
}

/** Gen JS code for a call to Any.## */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ class RegressionTestScala3 {
assertEquals(1, X_Issue13221.I.i)
assertEquals(1, X_Issue13221.blah)
}

@Test def primitivePlusStringThatIsATermRefIssue13518(): Unit = {
def charPlusString(x: String): String = 'a' + x
assertEquals("abc", charPlusString("bc"))

def intPlusString(x: String): String = 5 + x
assertEquals("5bc", intPlusString("bc"))
}
}

object RegressionTestScala3 {
Expand Down