Skip to content

Commit 78c42c7

Browse files
johnreganfelixmulder
authored andcommitted
Rework with explanation error:modifiers(s) not allowed for 'item' (#2747)
* Applies dotty error message standard - modifier(s) 'modifiers' not allowed for 'item' * restore trailing semicolon * add trailing ,
1 parent 7cf6409 commit 78c42c7

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,7 @@ object Parsers {
16811681

16821682
def addFlag(mods: Modifiers, flag: FlagSet): Modifiers = {
16831683
def incompatible(kind: String) = {
1684-
syntaxError(s"modifier(s) `${mods.flags}' not allowed for $kind")
1684+
syntaxError(ModifiersNotAllowed(mods.flags, kind))
16851685
Modifiers(flag)
16861686
}
16871687
if (compatible(mods.flags, flag)) mods | flag

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ public enum ErrorMessageID {
8989
OnlyCaseClassOrCaseObjectAllowedID,
9090
ExpectedClassOrObjectDefID,
9191
AnonymousFunctionMissingParamTypeID,
92-
SuperCallsNotAllowedInlineID
92+
SuperCallsNotAllowedInlineID,
93+
ModifiersNotAllowedID,
9394
;
9495

9596
public int errorNumber() {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import printing.Formatting
2020
import ErrorMessageID._
2121
import Denotations.SingleDenotation
2222
import dotty.tools.dotc.ast.Trees
23+
import dotty.tools.dotc.ast.untpd.Modifiers
2324
import dotty.tools.dotc.core.Flags.{FlagSet, Mutable}
2425
import dotty.tools.dotc.core.SymDenotations.SymDenotation
2526

@@ -1614,4 +1615,20 @@ object messages {
16141615
val msg = s"super call not allowed in inline $symbol"
16151616
val explanation = "Method inlining prohibits calling superclass methods, as it may lead to confusion about which super is being called."
16161617
}
1618+
1619+
case class ModifiersNotAllowed(flags: FlagSet, sort: String)(implicit ctx: Context)
1620+
extends Message(ModifiersNotAllowedID) {
1621+
val kind = "Syntax"
1622+
val msg = s"modifier(s) `$flags' not allowed for $sort"
1623+
val explanation = {
1624+
val code = "sealed def y: Int = 1"
1625+
hl"""You tried to use a modifier that is inapplicable for the type of item under modification
1626+
|
1627+
|
1628+
|Consider the following example:
1629+
|$code
1630+
|In this instance, the modifier 'sealed' is not applicable to the item type 'def' (method)
1631+
"""
1632+
}
1633+
}
16171634
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,4 +839,14 @@ class ErrorMessagesTests extends ErrorMessagesTest {
839839
val SuperCallsNotAllowedInline(symbol) = err
840840
assertEquals("method bar", symbol.show)
841841
}
842+
843+
@Test def modifiersNotAllowed =
844+
checkMessagesAfter("refchecks")("""lazy trait T""")
845+
.expect { (ictx, messages) =>
846+
implicit val ctx: Context = ictx
847+
assertMessageCount(1, messages)
848+
val ModifiersNotAllowed(flags, sort) :: Nil = messages
849+
assertEquals("lazy", flags.toString)
850+
assertEquals("trait", sort)
851+
}
842852
}

0 commit comments

Comments
 (0)