Skip to content
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

Fix #1589: Reworks with explanation error:modifiers(s) not allowed for 'item' #2747

Merged
merged 6 commits into from
Jun 15, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1681,7 +1681,7 @@ object Parsers {

def addFlag(mods: Modifiers, flag: FlagSet): Modifiers = {
def incompatible(kind: String) = {
syntaxError(s"modifier(s) `${mods.flags}' not allowed for $kind")
syntaxError(ModifiersNotAllowed(mods.flags, kind))
Modifiers(flag)
}
if (compatible(mods.flags, flag)) mods | flag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public enum ErrorMessageID {
OnlyCaseClassOrCaseObjectAllowedID,
ExpectedClassOrObjectDefID,
AnonymousFunctionMissingParamTypeID,
SuperCallsNotAllowedInlineID
SuperCallsNotAllowedInlineID,
ModifiersNotAllowedID,
;

public int errorNumber() {
Expand Down
17 changes: 17 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import printing.Formatting
import ErrorMessageID._
import Denotations.SingleDenotation
import dotty.tools.dotc.ast.Trees
import dotty.tools.dotc.ast.untpd.Modifiers
import dotty.tools.dotc.core.Flags.{FlagSet, Mutable}
import dotty.tools.dotc.core.SymDenotations.SymDenotation

Expand Down Expand Up @@ -1614,4 +1615,20 @@ object messages {
val msg = s"super call not allowed in inline $symbol"
val explanation = "Method inlining prohibits calling superclass methods, as it may lead to confusion about which super is being called."
}

case class ModifiersNotAllowed(flags: FlagSet, sort: String)(implicit ctx: Context)
extends Message(ModifiersNotAllowedID) {
val kind = "Syntax"
val msg = s"modifier(s) `$flags' not allowed for $sort"
val explanation = {
val code = "sealed def y: Int = 1"
hl"""You tried to use a modifier that is inapplicable for the type of item under modification
|
|
|Consider the following example:
|$code
|In this instance, the modifier 'sealed' is not applicable to the item type 'def' (method)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also possibly try to identify which flags have been applied to what and make a suggestion. That would be cool 😄

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@felixmulder not quite sure how to identify all modifiers for traits,methods,variables. Should this not be handled instead by the next error,
1693 syntaxError(s"illegal modifier combination: ${mods.flags} and $flag")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think these two should be harmonized. I.e. the same error message for both. You could make sort: Option[String] to discern between the case _ and the rest of them.

Otherwise this LGTM now :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, I can take that one next. Can we agree that it will be handled with the 1693 consolidation refactor and get this one through? :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure - that's fine by me - assigning you in #1589

"""
}
}
}
10 changes: 10 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -839,4 +839,14 @@ class ErrorMessagesTests extends ErrorMessagesTest {
val SuperCallsNotAllowedInline(symbol) = err
assertEquals("method bar", symbol.show)
}

@Test def modifiersNotAllowed =
checkMessagesAfter("refchecks")("""lazy trait T""")
.expect { (ictx, messages) =>
implicit val ctx: Context = ictx
assertMessageCount(1, messages)
val ModifiersNotAllowed(flags, sort) :: Nil = messages
assertEquals("lazy", flags.toString)
assertEquals("trait", sort)
}
}