Skip to content

Fix #5313: Don't allow selecting AnyRef methods on Java static compan… #8455

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 2 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
case tp: ErrorType =>
s"<error ${tp.msg.msg}>"
case tp: WildcardType =>
if (tp.optBounds.exists) "(?" ~ toTextRHS(tp.bounds) ~ ")" else "?"
if (tp.optBounds.exists) "<?" ~ toTextRHS(tp.bounds) ~ ">" else "<?>"
case NoType =>
"<notype>"
case NoPrefix =>
Expand Down
20 changes: 12 additions & 8 deletions compiler/src/dotty/tools/dotc/transform/Erasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -419,15 +419,17 @@ object Erasure {

/** Check that Java statics and packages can only be used in selections.
*/
private def checkValue(tree: Tree, proto: Type)(implicit ctx: Context): tree.type = {
if (!proto.isInstanceOf[SelectionProto] && !proto.isInstanceOf[ApplyingProto]) {
val sym = tree.tpe.termSymbol
// The check is avoided inside Java compilation units because it always fails
// on the singleton type Module.type.
if ((sym is Flags.Package) || (sym.isAllOf(Flags.JavaModule) && !ctx.compilationUnit.isJava)) ctx.error(reporting.diagnostic.messages.JavaSymbolIsNotAValue(sym), tree.sourcePos)
}
private def checkValue(tree: Tree, proto: Type)(implicit ctx: Context): tree.type =
if (!proto.isInstanceOf[SelectionProto] && !proto.isInstanceOf[ApplyingProto]) then
checkValue(tree)
tree
}

private def checkValue(tree: Tree)(using ctx: Context): Unit =
val sym = tree.tpe.termSymbol
if (sym is Flags.Package)
|| (sym.isAllOf(Flags.JavaModule) && !ctx.compilationUnit.isJava)
then
ctx.error(reporting.diagnostic.messages.JavaSymbolIsNotAValue(sym), tree.sourcePos)

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

if owner == defn.ObjectClass then checkValue(qual1)

def select(qual: Tree, sym: Symbol): Tree =
untpd.cpy.Select(tree)(qual, sym.name).withType(NamedType(qual.tpe, sym))

Expand Down
16 changes: 0 additions & 16 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -984,22 +984,6 @@ class ErrorMessagesTests extends ErrorMessagesTest {
assertEquals(err, ImplicitClassPrimaryConstructorArity())
}

@Test def anonymousFunctionMissingParamType =
checkMessagesAfter(RefChecks.name) {
"""
|object AnonymousF {
| val f = { case x: Int => x + 1 }
|}""".stripMargin
}
.expect { (ictx, messages) =>
implicit val ctx: Context = ictx

assertMessageCount(1, messages)
val AnonymousFunctionMissingParamType(param, args, _, pt) = messages.head
assertEquals("x$1", param.show)
assertEquals("?", pt.show)
}

@Test def superCallsNotAllowedInline =
checkMessagesAfter(RefChecks.name) {
"""
Expand Down
6 changes: 6 additions & 0 deletions tests/neg/i5313.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def test =
System.getClass // error
System.toString // error
System == null // error
System eq null // error
System.wait() // error