Skip to content

Commit 0cf569f

Browse files
authored
Merge pull request #3908 from benkobalog/ErrorMessage-SymbolIsNotAValue
Added error message for Symbol is not a value #1589
2 parents f029755 + 0a4dca6 commit 0cf569f

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public enum ErrorMessageID {
126126
MissingCompanionForStaticID,
127127
PolymorphicMethodMissingTypeInParentID,
128128
ParamsNoInlineID,
129+
JavaSymbolIsNotAValueID,
129130
;
130131

131132
public int errorNumber() {

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import ErrorMessageID._
2121
import Denotations.SingleDenotation
2222
import dotty.tools.dotc.ast.Trees
2323
import dotty.tools.dotc.config.ScalaVersion
24-
import dotty.tools.dotc.core.Flags.{FlagSet, Mutable}
24+
import dotty.tools.dotc.core.Flags._
2525
import dotty.tools.dotc.core.SymDenotations.SymDenotation
2626
import scala.util.control.NonFatal
2727

@@ -203,11 +203,11 @@ object messages {
203203
val explanation =
204204
hl"""|Anonymous functions must define a type. For example, if you define a function like this one:
205205
|
206-
|${"val f = { case xs @ List(1, 2, 3) => Some(xs) }"}
206+
|${"val f = { case x: Int => x + 1 }"}
207207
|
208208
|Make sure you give it a type of what you expect to match and help the type inference system:
209209
|
210-
|${"val f: Seq[Int] => Option[List[Int]] = { case xs @ List(1, 2, 3) => Some(xs) }"} """
210+
|${"val f: Any => Int = { case x: Int => x + 1 }"} """
211211
}
212212

213213
case class WildcardOnTypeArgumentNotAllowedOnNew()(implicit ctx: Context)
@@ -2076,4 +2076,16 @@ object messages {
20762076
val msg = hl"""${"inline"} modifier cannot be used for a ${owner.showKind} parameter"""
20772077
val explanation = ""
20782078
}
2079+
2080+
case class JavaSymbolIsNotAValue(symbol: Symbol)(implicit ctx: Context) extends Message(JavaSymbolIsNotAValueID) {
2081+
val kind = "Type Mismatch"
2082+
val msg = {
2083+
val kind =
2084+
if (symbol is Package) hl"$symbol"
2085+
else hl"Java defined ${"class " + symbol.name}"
2086+
2087+
s"$kind is not a value"
2088+
}
2089+
val explanation = ""
2090+
}
20792091
}

compiler/src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ trait Checking {
526526
val sym = tree.tpe.termSymbol
527527
// The check is avoided inside Java compilation units because it always fails
528528
// on the singleton type Module.type.
529-
if ((sym is Package) || ((sym is JavaModule) && !ctx.compilationUnit.isJava)) ctx.error(em"$sym is not a value", tree.pos)
529+
if ((sym is Package) || ((sym is JavaModule) && !ctx.compilationUnit.isJava)) ctx.error(JavaSymbolIsNotAValue(sym), tree.pos)
530530
}
531531
tree
532532
}

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -805,15 +805,15 @@ class ErrorMessagesTests extends ErrorMessagesTest {
805805
checkMessagesAfter("refchecks") {
806806
"""
807807
|object AnonymousF {
808-
| val f = { case l@List(1,2,3) => Some(l) }
808+
| val f = { case x: Int => x + 1 }
809809
|}""".stripMargin
810810
}
811811
.expect { (ictx, messages) =>
812812
implicit val ctx: Context = ictx
813813

814-
val AnonymousFunctionMissingParamType(param, args, _, pt) = messages.last
814+
assertMessageCount(1, messages)
815+
val AnonymousFunctionMissingParamType(param, args, _, pt) = messages.head
815816
assertEquals("x$1", param.show)
816-
assertEquals(s"List(ValDef(${param.show},TypeTree,EmptyTree))", args.toString)
817817
assertEquals("?", pt.show)
818818
}
819819

@@ -1278,4 +1278,20 @@ class ErrorMessagesTests extends ErrorMessagesTest {
12781278
assertEquals("method get", rsym.show)
12791279
assertEquals("class Object", parentSym.show)
12801280
}
1281+
1282+
@Test def javaSymbolIsNotAValue =
1283+
checkMessagesAfter("checkStatic") {
1284+
"""
1285+
|package p
1286+
|object O {
1287+
| val v = p
1288+
|}
1289+
""".stripMargin
1290+
}.expect { (itcx, messages) =>
1291+
implicit val ctx: Context = itcx
1292+
1293+
assertMessageCount(1, messages)
1294+
val JavaSymbolIsNotAValue(symbol) = messages.head
1295+
assertEquals(symbol.show, "package p")
1296+
}
12811297
}

0 commit comments

Comments
 (0)