Skip to content

Commit c525a7e

Browse files
authored
Merge pull request #4639 from dotty-staging/fix-#4430
Fix #4430: Disallow access to Java bridge methods before erasure
2 parents df3c9b5 + bc4fa90 commit c525a7e

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

compiler/src/dotty/tools/dotc/core/Denotations.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ object Denotations {
488488
(!sym2.isAsConcrete(sym1) ||
489489
precedes(sym1.owner, sym2.owner) ||
490490
accessBoundary(sym2).isProperlyContainedIn(accessBoundary(sym1)) ||
491+
sym2.is(Bridge) && !sym1.is(Bridge) ||
491492
sym1.is(Method) && !sym2.is(Method)) ||
492493
sym1.info.isErroneous)
493494

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,27 @@ class TestBCode extends DottyBytecodeTest {
264264
}
265265
}
266266
}
267+
268+
// See #4430
269+
@Test def javaBridgesAreNotVisible = {
270+
val source =
271+
"""
272+
|class Test {
273+
| def test = (new java.lang.StringBuilder()).append(Array[Char](), 0, 0)
274+
|}
275+
""".stripMargin
276+
277+
checkBCode(source) { dir =>
278+
// We check the method call signature to make sure we don't call a Java bridge
279+
val clsIn = dir.lookupName("Test.class", directory = false).input
280+
val clsNode = loadClassNode(clsIn)
281+
val testMethod = getMethod(clsNode, "test")
282+
val instructions = instructionsFromMethod(testMethod)
283+
val containsExpectedCall = instructions.exists {
284+
case Invoke(_, "java/lang/StringBuilder", "append", "([CII)Ljava/lang/StringBuilder;", _) => true
285+
case _ => false
286+
}
287+
assertTrue(containsExpectedCall)
288+
}
289+
}
267290
}

tests/pickling/i4430.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
object Test {
2+
def main(args: Array[String]): Unit = {
3+
val sb = new java.lang.StringBuilder()
4+
sb.append(Array[Char](), 0, 0)
5+
sb.length
6+
}
7+
}

tests/run/i4430.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
object Test {
2+
def main(args: Array[String]): Unit = {
3+
val sb = new java.lang.StringBuilder()
4+
sb.append(Array[Char](), 0, 0)
5+
sb.length
6+
}
7+
}

0 commit comments

Comments
 (0)