Skip to content

Commit a830df2

Browse files
committed
Fix #5313: Don't allow selecting AnyRef methods on Java static companions
1 parent f548a18 commit a830df2

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
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

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)