Skip to content

Commit

Permalink
Some parser changes to handle erased as a soft keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
natsukagami committed Jan 29, 2023
1 parent 63d31a6 commit 2093b87
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
31 changes: 18 additions & 13 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ object Parsers {
def isPureArrow(name: Name): Boolean = isIdent(name) && Feature.pureFunsEnabled
def isPureArrow: Boolean = isPureArrow(nme.PUREARROW) || isPureArrow(nme.PURECTXARROW)
def isErased = isIdent(nme.erased) && in.erasedEnabled
// Are we seeing an `erased` soft keyword that will not be an identifier?
def isErasedKw = isErased && in.isSoftModifierInParamModifierPosition
def isSimpleLiteral =
simpleLiteralTokens.contains(in.token)
|| isIdent(nme.raw.MINUS) && numericLitTokens.contains(in.lookahead.token)
Expand Down Expand Up @@ -1545,8 +1547,8 @@ object Parsers {
else {
val paramStart = in.offset
def addErased() =
erasedArgs.addOne(isErased)
if isErased then { in.skipToken(); }
erasedArgs.addOne(isErasedKw)
if isErasedKw then { in.skipToken(); }
addErased()
val ts = in.currentRegion.withCommasExpected {
funArgType() match
Expand Down Expand Up @@ -2379,7 +2381,7 @@ object Parsers {
*/
def binding(mods: Modifiers): Tree =
atSpan(in.offset) {
val mods1 = if isErased then addModifier(mods) else mods
val mods1 = if isErasedKw then addModifier(mods) else mods
makeParameter(bindingName(), typedOpt(), mods1)
}

Expand Down Expand Up @@ -2578,7 +2580,7 @@ object Parsers {
else in.currentRegion.withCommasExpected {
var isFormalParams = false
def exprOrBinding() =
if isErased then isFormalParams = true
if isErasedKw then isFormalParams = true
if isFormalParams then binding(Modifiers())
else
val t = exprInParens()
Expand Down Expand Up @@ -3221,7 +3223,7 @@ object Parsers {
def param(): ValDef = {
val start = in.offset
var mods = impliedMods.withAnnotations(annotations())
if (isErased && in.isSoftModifierInParamModifierPosition)
if isErasedKw then
mods = addModifier(mods)
if (ofClass) {
mods = addFlag(modifiers(start = mods), ParamAccessor)
Expand Down Expand Up @@ -3279,16 +3281,19 @@ object Parsers {
if prefix && !isIdent(nme.using) && !isIdent(nme.erased) then param() :: Nil
else
paramMods()
val firstParamMod =
var mods = EmptyModifiers
if isErased then mods = addModifier(mods)
mods
if givenOnly && !impliedMods.is(Given) then
syntaxError(em"`using` expected")
val isParams =
!impliedMods.is(Given)
|| startParamTokens.contains(in.token)
|| isIdent && (in.name == nme.inline || in.lookahead.isColon)
val (firstParamMod, isParams) =
var mods = EmptyModifiers
if in.lookahead.isColon then
(mods, true)
else
if isErased then mods = addModifier(mods)
val isParams =
!impliedMods.is(Given)
|| startParamTokens.contains(in.token)
|| isIdent && (in.name == nme.inline || in.lookahead.isColon)
(mods, isParams)
(if isParams then commaSeparated(() => param())
else contextTypes(ofClass, nparams, impliedMods)) match {
case Nil => Nil
Expand Down
18 changes: 18 additions & 0 deletions tests/pos-custom-args/erased/erased-soft-keyword.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def f1(x: Int, erased y: Int) = 0
def f2(x: Int, erased: Int) = 0
inline def f3(x: Int, inline erased: Int) = 0
def f4(x: Int, erased inline: Int) = 0
// inline def f5(x: Int, erased inline y: Int) = 0 // should parse but rejected later

def f6(using erased y: Int) = 0
def f7(using erased: Int) = 0
inline def f8(using inline erased: Int) = 0
def f9(using erased inline: Int) = 0
// inline def f10(using erased inline x: Int) = 0 // should parse but rejected later
def f11(using erased Int) = 0

val v1 = (erased: Int) => 0
val v2: Int => Int = erased => 0
val v3 = (erased x: Int) => 0
val v4: (erased Int) => Int = (erased x) => 0
val v5: (erased: Int) => Int = x => 0

0 comments on commit 2093b87

Please sign in to comment.