Skip to content

Test final object is indeed redundant: objects must be JVM-final #5745

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
Jan 19, 2019
Merged
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
58 changes: 58 additions & 0 deletions compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -565,4 +565,62 @@ class TestBCode extends DottyBytecodeTest {
assertEquals(14, instructionsFromMethod(method).size)
}
}

/* Test that objects compile to *final* classes. */

def checkFinalClass(outputClassName: String, source: String) = {
checkBCode(source) {
dir =>
val moduleIn = dir.lookupName(outputClassName, directory = false)
val moduleNode = loadClassNode(moduleIn.input)
assert((moduleNode.access & Opcodes.ACC_FINAL) != 0)
}
}

@Test def objectsAreFinal =
checkFinalClass("Foo$.class", "object Foo")

@Test def objectsInClassAreFinal =
checkFinalClass("Test$Foo$.class",
"""class Test {
| object Foo
|}
""".stripMargin)

@Test def objectsInObjsAreFinal =
checkFinalClass("Test$Foo$.class",
"""object Test {
| object Foo
|}
""".stripMargin)

@Test def objectsInObjDefAreFinal =
checkFinalClass("Test$Foo$1$.class",
"""
|object Test {
| def bar() = {
| object Foo
| }
|}
""".stripMargin)

@Test def objectsInClassDefAreFinal =
checkFinalClass("Test$Foo$1$.class",
"""
|class Test {
| def bar() = {
| object Foo
| }
|}
""".stripMargin)

@Test def objectsInObjValAreFinal =
checkFinalClass("Test$Foo$1$.class",
"""
|class Test {
| val bar = {
| object Foo
| }
|}
""".stripMargin)
}