Skip to content

Commit 035ea54

Browse files
benkobalogallanrenucci
authored andcommitted
Added error message for Symbol is not a value #1589
1 parent f029755 commit 035ea54

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,4 +2076,25 @@ 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 msg = hl"$symbol is not a value"
2082+
val kind = "Type Mismatch"
2083+
val explanation = {
2084+
val javaCodeExample = """class A {public static int a() {return 1;}}"""
2085+
2086+
val scalaCodeExample =
2087+
"""val objectA = A // This does not compile
2088+
|val aResult = A.a() // This does compile""".stripMargin
2089+
2090+
hl"""Java statics and packages cannot be used as a value.
2091+
|For Java statics consider the following Java example:
2092+
|
2093+
|$javaCodeExample
2094+
|
2095+
|When used from Scala:
2096+
|
2097+
|$scalaCodeExample"""
2098+
}
2099+
}
20792100
}

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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,4 +1278,18 @@ 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+
val JavaSymbolIsNotAValue(symbol) = messages.head
1293+
assertEquals(symbol.show, "package p")
1294+
}
12811295
}

0 commit comments

Comments
 (0)