Skip to content

Commit 5dcc31d

Browse files
committed
Test final object is indeed redundant: objects must be JVM-final
Scalac sometimes *forgets* the final bytecode flag, and sometimes one cares, so people even *recommend* always writing *final object* instead of object. That's bad, since we started warning about it in #4973. scala/bug#11094 (comment) https://nrinaudo.github.io/scala-best-practices/adts/final_case_objects.html Why care? ========= If we forget the final flag in bytecode, Java code might then extend the class. Performance might or might not be affected - some in scala/contributors debated this for ~20 minutes (starting at https://gitter.im/scala/contributors?at=5c423c449bfa375aab21f2af).
1 parent 4ecbb60 commit 5dcc31d

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,4 +565,19 @@ class TestBCode extends DottyBytecodeTest {
565565
assertEquals(14, instructionsFromMethod(method).size)
566566
}
567567
}
568+
569+
@Test def objectsAreFinal = {
570+
val source =
571+
"""class Test {
572+
| object Foo
573+
|}
574+
""".stripMargin
575+
576+
checkBCode(source) {
577+
dir =>
578+
val moduleIn = dir.lookupName("Test$Foo$.class", directory = false)
579+
val moduleNode = loadClassNode(moduleIn.input)
580+
assert((moduleNode.access & Opcodes.ACC_FINAL) != 0)
581+
}
582+
}
568583
}

0 commit comments

Comments
 (0)