Skip to content

Commit 280c9c7

Browse files
authored
Merge pull request #8455 from dotty-staging/fix-#5313
Fix #5313: Don't allow selecting AnyRef methods on Java static compan…
2 parents 492cae7 + e11c378 commit 280c9c7

File tree

4 files changed

+19
-25
lines changed

4 files changed

+19
-25
lines changed

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
181181
case tp: ErrorType =>
182182
s"<error ${tp.msg.msg}>"
183183
case tp: WildcardType =>
184-
if (tp.optBounds.exists) "(?" ~ toTextRHS(tp.bounds) ~ ")" else "?"
184+
if (tp.optBounds.exists) "<?" ~ toTextRHS(tp.bounds) ~ ">" else "<?>"
185185
case NoType =>
186186
"<notype>"
187187
case NoPrefix =>

compiler/src/dotty/tools/dotc/transform/Erasure.scala

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -419,15 +419,17 @@ object Erasure {
419419

420420
/** Check that Java statics and packages can only be used in selections.
421421
*/
422-
private def checkValue(tree: Tree, proto: Type)(implicit ctx: Context): tree.type = {
423-
if (!proto.isInstanceOf[SelectionProto] && !proto.isInstanceOf[ApplyingProto]) {
424-
val sym = tree.tpe.termSymbol
425-
// The check is avoided inside Java compilation units because it always fails
426-
// on the singleton type Module.type.
427-
if ((sym is Flags.Package) || (sym.isAllOf(Flags.JavaModule) && !ctx.compilationUnit.isJava)) ctx.error(reporting.diagnostic.messages.JavaSymbolIsNotAValue(sym), tree.sourcePos)
428-
}
422+
private def checkValue(tree: Tree, proto: Type)(implicit ctx: Context): tree.type =
423+
if (!proto.isInstanceOf[SelectionProto] && !proto.isInstanceOf[ApplyingProto]) then
424+
checkValue(tree)
429425
tree
430-
}
426+
427+
private def checkValue(tree: Tree)(using ctx: Context): Unit =
428+
val sym = tree.tpe.termSymbol
429+
if (sym is Flags.Package)
430+
|| (sym.isAllOf(Flags.JavaModule) && !ctx.compilationUnit.isJava)
431+
then
432+
ctx.error(reporting.diagnostic.messages.JavaSymbolIsNotAValue(sym), tree.sourcePos)
431433

432434
private def checkNotErased(tree: Tree)(implicit ctx: Context): tree.type = {
433435
if (!ctx.mode.is(Mode.Type)) {
@@ -551,6 +553,8 @@ object Erasure {
551553
val sym = if (owner eq origSym.maybeOwner) origSym else owner.info.decl(tree.name).symbol
552554
assert(sym.exists, origSym.showLocated)
553555

556+
if owner == defn.ObjectClass then checkValue(qual1)
557+
554558
def select(qual: Tree, sym: Symbol): Tree =
555559
untpd.cpy.Select(tree)(qual, sym.name).withType(NamedType(qual.tpe, sym))
556560

compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -984,22 +984,6 @@ class ErrorMessagesTests extends ErrorMessagesTest {
984984
assertEquals(err, ImplicitClassPrimaryConstructorArity())
985985
}
986986

987-
@Test def anonymousFunctionMissingParamType =
988-
checkMessagesAfter(RefChecks.name) {
989-
"""
990-
|object AnonymousF {
991-
| val f = { case x: Int => x + 1 }
992-
|}""".stripMargin
993-
}
994-
.expect { (ictx, messages) =>
995-
implicit val ctx: Context = ictx
996-
997-
assertMessageCount(1, messages)
998-
val AnonymousFunctionMissingParamType(param, args, _, pt) = messages.head
999-
assertEquals("x$1", param.show)
1000-
assertEquals("?", pt.show)
1001-
}
1002-
1003987
@Test def superCallsNotAllowedInline =
1004988
checkMessagesAfter(RefChecks.name) {
1005989
"""

tests/neg/i5313.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def test =
2+
System.getClass // error
3+
System.toString // error
4+
System == null // error
5+
System eq null // error
6+
System.wait() // error

0 commit comments

Comments
 (0)