Skip to content

Commit fbd7228

Browse files
Merge pull request #8609 from dotty-staging/optimize-DottyPredef-assert
Elide assert message def binding if not used
2 parents 82d76e8 + f147b23 commit fbd7228

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

compiler/test/dotty/tools/backend/jvm/InlineBytecodeTests.scala

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,37 @@ class InlineBytecodeTests extends DottyBytecodeTest {
4242
}
4343
}
4444

45+
@Test def inlineAssert = {
46+
def mkSource(original: String, expected: String) =
47+
s"""
48+
|class Foo {
49+
| def meth1: Unit = $original
50+
| def meth2: Unit = $expected
51+
|}
52+
""".stripMargin
53+
54+
val sources = List(
55+
mkSource("assert(true)", "()"),
56+
mkSource("assert(true, ???)", "()"),
57+
mkSource("assert(false)", "assertFail()")
58+
)
59+
for (source <- sources)
60+
checkBCode(source) { dir =>
61+
val clsIn = dir.lookupName("Foo.class", directory = false).input
62+
val clsNode = loadClassNode(clsIn)
63+
val meth1 = getMethod(clsNode, "meth1")
64+
val meth2 = getMethod(clsNode, "meth2")
65+
66+
val instructions1 = instructionsFromMethod(meth1)
67+
val instructions2 = instructionsFromMethod(meth2)
68+
69+
assert(instructions1 == instructions2,
70+
"`assert` was not properly inlined in `meth1`\n" +
71+
diffInstructions(instructions1, instructions2))
72+
73+
}
74+
}
75+
4576
@Test def i4947 = {
4677
val source = """class Foo {
4778
| inline def track[T](inline f: T) <: T = {

library/src/dotty/DottyPredef.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package dotty
33
object DottyPredef {
44
import compiletime.summonFrom
55

6-
inline final def assert(assertion: => Boolean, message: => Any): Unit = {
6+
inline final def assert(inline assertion: Boolean, inline message: => Any): Unit = {
77
if (!assertion)
88
assertFail(message)
99
}
1010

11-
inline final def assert(inline assertion: => Boolean) <: Unit = {
11+
inline final def assert(inline assertion: Boolean) <: Unit = {
1212
if (!assertion)
1313
assertFail()
1414
}

0 commit comments

Comments
 (0)