From 035ea54761ba9a7d05d04f1a35f61ee9dad5837c Mon Sep 17 00:00:00 2001 From: benkobalog Date: Wed, 24 Jan 2018 16:13:39 +0100 Subject: [PATCH 1/2] Added error message for Symbol is not a value #1589 --- .../reporting/diagnostic/ErrorMessageID.java | 1 + .../dotc/reporting/diagnostic/messages.scala | 21 +++++++++++++++++++ .../src/dotty/tools/dotc/typer/Checking.scala | 2 +- .../dotc/reporting/ErrorMessagesTests.scala | 14 +++++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java b/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java index 8cc9fc1fc63f..cf529724c028 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java @@ -126,6 +126,7 @@ public enum ErrorMessageID { MissingCompanionForStaticID, PolymorphicMethodMissingTypeInParentID, ParamsNoInlineID, + JavaSymbolIsNotAValueID, ; public int errorNumber() { diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index f9f75a7d4882..fbc7ed90d4ca 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -2076,4 +2076,25 @@ object messages { val msg = hl"""${"inline"} modifier cannot be used for a ${owner.showKind} parameter""" val explanation = "" } + + case class JavaSymbolIsNotAValue(symbol: Symbol)(implicit ctx: Context) extends Message(JavaSymbolIsNotAValueID) { + val msg = hl"$symbol is not a value" + val kind = "Type Mismatch" + val explanation = { + val javaCodeExample = """class A {public static int a() {return 1;}}""" + + val scalaCodeExample = + """val objectA = A // This does not compile + |val aResult = A.a() // This does compile""".stripMargin + + hl"""Java statics and packages cannot be used as a value. + |For Java statics consider the following Java example: + | + |$javaCodeExample + | + |When used from Scala: + | + |$scalaCodeExample""" + } + } } diff --git a/compiler/src/dotty/tools/dotc/typer/Checking.scala b/compiler/src/dotty/tools/dotc/typer/Checking.scala index 9a9e196000ad..e14081207e23 100644 --- a/compiler/src/dotty/tools/dotc/typer/Checking.scala +++ b/compiler/src/dotty/tools/dotc/typer/Checking.scala @@ -526,7 +526,7 @@ trait Checking { 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 Package) || ((sym is JavaModule) && !ctx.compilationUnit.isJava)) ctx.error(em"$sym is not a value", tree.pos) + if ((sym is Package) || ((sym is JavaModule) && !ctx.compilationUnit.isJava)) ctx.error(JavaSymbolIsNotAValue(sym), tree.pos) } tree } diff --git a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala index 57f6638fccb0..3502b174af08 100644 --- a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala +++ b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala @@ -1278,4 +1278,18 @@ class ErrorMessagesTests extends ErrorMessagesTest { assertEquals("method get", rsym.show) assertEquals("class Object", parentSym.show) } + + @Test def javaSymbolIsNotAValue = + checkMessagesAfter("checkStatic") { + """ + |package p + |object O { + | val v = p + |} + """.stripMargin + }.expect { (itcx, messages) => + implicit val ctx: Context = itcx + val JavaSymbolIsNotAValue(symbol) = messages.head + assertEquals(symbol.show, "package p") + } } From 0a4dca6b6c7124ed685d9b4b943e9a920782fd56 Mon Sep 17 00:00:00 2001 From: Allan Renucci Date: Mon, 29 Jan 2018 16:37:27 +0100 Subject: [PATCH 2/2] Polishing --- .../dotc/reporting/diagnostic/messages.scala | 27 +++++++------------ .../dotc/reporting/ErrorMessagesTests.scala | 8 +++--- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index fbc7ed90d4ca..3c7720b65aa9 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -21,7 +21,7 @@ import ErrorMessageID._ import Denotations.SingleDenotation import dotty.tools.dotc.ast.Trees import dotty.tools.dotc.config.ScalaVersion -import dotty.tools.dotc.core.Flags.{FlagSet, Mutable} +import dotty.tools.dotc.core.Flags._ import dotty.tools.dotc.core.SymDenotations.SymDenotation import scala.util.control.NonFatal @@ -203,11 +203,11 @@ object messages { val explanation = hl"""|Anonymous functions must define a type. For example, if you define a function like this one: | - |${"val f = { case xs @ List(1, 2, 3) => Some(xs) }"} + |${"val f = { case x: Int => x + 1 }"} | |Make sure you give it a type of what you expect to match and help the type inference system: | - |${"val f: Seq[Int] => Option[List[Int]] = { case xs @ List(1, 2, 3) => Some(xs) }"} """ + |${"val f: Any => Int = { case x: Int => x + 1 }"} """ } case class WildcardOnTypeArgumentNotAllowedOnNew()(implicit ctx: Context) @@ -2078,23 +2078,14 @@ object messages { } case class JavaSymbolIsNotAValue(symbol: Symbol)(implicit ctx: Context) extends Message(JavaSymbolIsNotAValueID) { - val msg = hl"$symbol is not a value" val kind = "Type Mismatch" - val explanation = { - val javaCodeExample = """class A {public static int a() {return 1;}}""" - - val scalaCodeExample = - """val objectA = A // This does not compile - |val aResult = A.a() // This does compile""".stripMargin + val msg = { + val kind = + if (symbol is Package) hl"$symbol" + else hl"Java defined ${"class " + symbol.name}" - hl"""Java statics and packages cannot be used as a value. - |For Java statics consider the following Java example: - | - |$javaCodeExample - | - |When used from Scala: - | - |$scalaCodeExample""" + s"$kind is not a value" } + val explanation = "" } } diff --git a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala index 3502b174af08..9d950953f550 100644 --- a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala +++ b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala @@ -805,15 +805,15 @@ class ErrorMessagesTests extends ErrorMessagesTest { checkMessagesAfter("refchecks") { """ |object AnonymousF { - | val f = { case l@List(1,2,3) => Some(l) } + | val f = { case x: Int => x + 1 } |}""".stripMargin } .expect { (ictx, messages) => implicit val ctx: Context = ictx - val AnonymousFunctionMissingParamType(param, args, _, pt) = messages.last + assertMessageCount(1, messages) + val AnonymousFunctionMissingParamType(param, args, _, pt) = messages.head assertEquals("x$1", param.show) - assertEquals(s"List(ValDef(${param.show},TypeTree,EmptyTree))", args.toString) assertEquals("?", pt.show) } @@ -1289,6 +1289,8 @@ class ErrorMessagesTests extends ErrorMessagesTest { """.stripMargin }.expect { (itcx, messages) => implicit val ctx: Context = itcx + + assertMessageCount(1, messages) val JavaSymbolIsNotAValue(symbol) = messages.head assertEquals(symbol.show, "package p") }