-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9172a96
Fix #4058: reject sealed and lazy for class parameters in parser
Blaisorblade 8a79276
Add omitted @tailrec annotation (mostly for clarity)
Blaisorblade 6299785
Test a few extra variants
Blaisorblade 1d85ac7
Don't special-case `sealed`
Blaisorblade 6f2effe
Update grammar in comment to follow code
Blaisorblade f4b3538
Report forbidden modifiers from modifiers
Blaisorblade File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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] | ||
|
@@ -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 = { | ||
@tailrec | ||
def loop(mods: Modifiers): Modifiers = { | ||
if (allowed contains in.token) { | ||
|
@@ -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 | ||
} | ||
} | ||
|
@@ -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) { | ||
|
@@ -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() }) | ||
|
@@ -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 | ||
* | | ||
|
@@ -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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
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.