Skip to content

Commit

Permalink
Merge pull request #366 from scala-native/topic/fix-365
Browse files Browse the repository at this point in the history
Fix #365
  • Loading branch information
densh committed Oct 29, 2016
2 parents 071bcce + 89e786c commit 399b21b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1199,13 +1199,25 @@ abstract class NirCodeGen
}

def genStringConcat(leftp: Tree, rightp: Tree, focus: Focus): Focus = {
def stringify(sym: Symbol, focus: Focus) =
if (sym == StringClass) {
focus
} else {
genMethodCall(Object_toString, statically = false, focus.value, Seq(), focus)
}

val left = {
val typesym = leftp.tpe.typeSymbol
val unboxed = genExpr(leftp, focus)
val boxed = boxValue(leftp.tpe.typeSymbol, unboxed)
genMethodCall(
Object_toString, statically = false, boxed.value, Seq(), boxed)
val boxed = boxValue(typesym, unboxed)
stringify(typesym, boxed)
}

val right = {
val typesym = rightp.tpe.typeSymbol
val boxed = genExpr(rightp, left)
stringify(typesym, boxed)
}
val right = genExpr(rightp, left)

genMethodCall(String_+,
statically = true,
Expand Down
21 changes: 21 additions & 0 deletions unit-tests/src/main/scala/java/lang/StringSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package java.lang

object StringSuite extends tests.Suite {
test("concat primitive") {
assert("big 5" == "big " + 5.toByte)
assert("big 5" == "big " + 5.toShort)
assert("big 5" == "big " + 5)
assert("big 5" == "big " + 5L)
assert("5 big" == 5.toByte + " big")
assert("5 big" == 5.toShort + " big")
assert("5 big" == 5 + " big")
assert("5 big" == 5L + " big")
}

test("concat string") {
assert("foo" == "foo" + "")
assert("foo" == "" + "foo")
assert("foobar" == "foo" + "bar")
assert("foobarbaz" == "foo" + "bar" + "baz")
}
}
8 changes: 7 additions & 1 deletion unit-tests/src/main/scala/tests/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import java.lang.System.exit
object Main {
def main(args: Array[String]): Unit = {
// tests.Discover object is code-generated in the sbt build
if (!tests.Discover.suites.forall(_.run)) exit(1) else exit(0)
val suites = tests.Discover.suites
var failed = false
suites.foreach { suite =>
val result = suite.run()
failed = failed || result
}
exit(if (failed) 1 else 0)
}
}
11 changes: 7 additions & 4 deletions unit-tests/src/main/scala/tests/Suite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ abstract class Suite {

def run(): Boolean = {
println("* " + this.getClass.getName)
tests.forall { test =>
val res = test.run()
println((if (res) " [ok] " else " [fail] ") + test.name)
res
var failed = false
tests.foreach { test =>
val result = test.run()
val status = if (result) " [ok] " else " [fail] "
println(status + test.name)
failed = failed || result
}
!failed
}
}

0 comments on commit 399b21b

Please sign in to comment.