Skip to content

Fix #4058: reject sealed and lazy for class parameters in parser #4070

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

Closed
wants to merge 6 commits into from
Closed
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
25 changes: 19 additions & 6 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1650,6 +1650,7 @@ object Parsers {
case PROTECTED => Mod.Protected()
case SEALED => Mod.Sealed()
}
private def flagsOfModToken(tok: Int): FlagSet = modOfToken(tok).flags

/** Drop `private' modifier when followed by a qualifier.
* Contract `abstract' and `override' to ABSOVERRIDE
Expand Down Expand Up @@ -1705,7 +1706,14 @@ object Parsers {
}
} else mods

/** {Annotation} {Modifier}
/**
* Extend the start Modifiers by parsing modifier tokens in the allowed BitSet, appearing in any order.
* Ignore modifier tokens in the parsedSeparately BitSet, so that the caller can parse them after modifier returns
* (suitable to parse `AllowedModifiers* lazy`).
* Report modifier tokens that appear neither in allowed nor in parsedSeparately.
*
* Grammar:
* {Annotation} {Modifier}
* Modifiers ::= {Modifier}
* LocalModifiers ::= {LocalModifier}
* AccessModifier ::= (private | protected) [AccessQualifier]
Expand All @@ -1714,7 +1722,7 @@ object Parsers {
* | override
* LocalModifier ::= abstract | final | sealed | implicit | lazy
*/
def modifiers(allowed: BitSet = modifierTokens, start: Modifiers = Modifiers()): Modifiers = {
def modifiers(allowed: BitSet = modifierTokens, start: Modifiers = Modifiers(), parsedSeparately: BitSet = BitSet.empty): Modifiers = {
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not convinced by this change of API. If the caller wants to parse some modifier separately, then he can exclude them from the allowed modifiers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To get the nicer error I need to distinguish "not allowed" (which gives an error) from "parsed separately" (which doesn't). I had to use parsedSeparately only once.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the API is too hard for the error, I can revert/separate these refactorings.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think a better API would something along the line of:

/** ...
 *  @param errorOn emit a syntax error when a parsed modifier is in errorOn
 */
def modifiers(allowed: BitSet, start: Modifiers, errorOn: BitSet) = {
  ...
  if (allowed.contains(in.token) { ...; loop() }
  else if (errorOn.contains(in.token) { syntaxError; loop() }
  else ...
}

You can keep parsing modifiers after encountering an invalid modifier.

But since it is used only once, I think we should no complicate this API.

@tailrec
def loop(mods: Modifiers): Modifiers = {
if (allowed contains in.token) {
Expand All @@ -1725,6 +1733,8 @@ object Parsers {
in.nextToken()
loop(mods)
} else {
if ((modifierTokens contains in.token) && !(parsedSeparately contains in.token))
syntaxError(hl"Modifier `${flagsOfModToken(in.token)}' not allowed at this position")
mods
}
}
Expand Down Expand Up @@ -1839,7 +1849,10 @@ object Parsers {
val start = in.offset
var mods = annotsAsMods()
if (owner.isTypeName) {
mods = modifiers(start = mods) | ParamAccessor
// Adding ParamAccessor would crash
mods = modifiers(start = mods, allowed = modifierTokens - SEALED) | ParamAccessor
if (mods.is(Lazy))
syntaxError("`lazy' modifier not allowed here. Use call-by-name parameters instead")
mods =
atPos(start, in.offset) {
if (in.token == VAL) {
Expand Down Expand Up @@ -1879,7 +1892,7 @@ object Parsers {
def paramClause(): List[ValDef] = inParens {
if (in.token == RPAREN) Nil
else {
def funArgMods(): Unit = {
@tailrec def funArgMods(): Unit = {
if (in.token == IMPLICIT) {
implicitOffset = in.offset
imods = addMod(imods, atPos(accept(IMPLICIT)) { Mod.Implicit() })
Expand Down Expand Up @@ -2451,7 +2464,7 @@ object Parsers {

/** BlockStatSeq ::= { BlockStat semi } [ResultExpr]
* BlockStat ::= Import
* | Annotations [implicit] [lazy] Def
* | Annotations [implicit] [erased] [lazy] Def
* | Annotations LocalModifiers TmplDef
* | Expr1
* |
Expand All @@ -2468,7 +2481,7 @@ object Parsers {
else if (isDefIntro(localModifierTokens))
if (in.token == IMPLICIT || in.token == ERASED) {
val start = in.offset
var imods = modifiers(funArgMods)
var imods = modifiers(funArgMods, parsedSeparately = BitSet(LAZY))
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need this, the current error message is good:

72 |  lazy def bar = 1
   |           ^
   |           modifier `lazy` is not allowed for this definition

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That’s not the error message I’m fixing. Will separate the PR and add examples.

if (isBindingIntro) stats += implicitClosure(start, Location.InBlock, imods)
else stats +++= localDef(start, imods)
} else {
Expand Down
9 changes: 9 additions & 0 deletions tests/neg/i4058.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class A(sealed val a: Int) // error
class B(lazy val a: Int) // error
class C(abstract val a: Int) // error
class D {
def f(sealed a: Int) = 0 // error
def g(lazy a: Int) = 0 // error
def g(override a: Int) = 0 // error
def g(abstract a: Int) = 0 // error
}