Skip to content

Added error message for Symbol is not a value #1589 #3908

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
Jan 29, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public enum ErrorMessageID {
MissingCompanionForStaticID,
PolymorphicMethodMissingTypeInParentID,
ParamsNoInlineID,
JavaSymbolIsNotAValueID,
;

public int errorNumber() {
Expand Down
18 changes: 15 additions & 3 deletions compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -2076,4 +2076,16 @@ 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 kind = "Type Mismatch"
val msg = {
val kind =
if (symbol is Package) hl"$symbol"
else hl"Java defined ${"class " + symbol.name}"

s"$kind is not a value"
}
val explanation = ""
}
}
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
22 changes: 19 additions & 3 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -1278,4 +1278,20 @@ 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

assertMessageCount(1, messages)
val JavaSymbolIsNotAValue(symbol) = messages.head
assertEquals(symbol.show, "package p")
}
}