Skip to content

Commit 446bb6c

Browse files
committed
address review feedback
1 parent a3527a9 commit 446bb6c

File tree

3 files changed

+41
-50
lines changed

3 files changed

+41
-50
lines changed

src/dotty/tools/dotc/ast/untpd.scala

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,36 +95,36 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
9595

9696
// ----- Modifiers -----------------------------------------------------
9797
/** Mod is intended to record syntactic information about modifiers, it's
98-
* NOT a replacement of flagsets.
98+
* NOT a replacement of FlagSet.
9999
*
100100
* For any query about semantic information, check `flags` instead.
101101
*/
102-
sealed trait Mod extends Positioned
102+
sealed abstract class Mod(val flags: FlagSet) extends Positioned
103103

104104
object Mod {
105-
case class Private() extends Mod
105+
case class Private() extends Mod(Flags.Private)
106106

107-
case class Protected() extends Mod
107+
case class Protected() extends Mod(Flags.Protected)
108108

109-
case class Val() extends Mod
109+
case class Val() extends Mod(Flags.EmptyFlags)
110110

111-
case class Var() extends Mod
111+
case class Var() extends Mod(Flags.Mutable)
112112

113-
case class Implicit() extends Mod
113+
case class Implicit(flag: FlagSet = Flags.ImplicitCommon) extends Mod(flag)
114114

115-
case class Final() extends Mod
115+
case class Final() extends Mod(Flags.Final)
116116

117-
case class Sealed() extends Mod
117+
case class Sealed() extends Mod(Flags.Sealed)
118118

119-
case class Override() extends Mod
119+
case class Override() extends Mod(Flags.Override)
120120

121-
case class Abstract() extends Mod
121+
case class Abstract() extends Mod(Flags.Abstract)
122122

123-
case class Lazy() extends Mod
123+
case class Lazy() extends Mod(Flags.Lazy)
124124

125-
case class Inline() extends Mod
125+
case class Inline() extends Mod(Flags.Inline)
126126

127-
case class Type() extends Mod
127+
case class Type() extends Mod(Flags.EmptyFlags)
128128
}
129129

130130
/** Modifiers and annotations for definitions

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

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,22 +1465,10 @@ object Parsers {
14651465

14661466
/* -------- MODIFIERS and ANNOTATIONS ------------------------------------------- */
14671467

1468-
private def flagOfToken(tok: Int): FlagSet = tok match {
1469-
case ABSTRACT => Abstract
1470-
case FINAL => Final
1471-
case IMPLICIT => ImplicitCommon
1472-
case INLINE => Inline
1473-
case LAZY => Lazy
1474-
case OVERRIDE => Override
1475-
case PRIVATE => Private
1476-
case PROTECTED => Protected
1477-
case SEALED => Sealed
1478-
}
1479-
14801468
private def modOfToken(tok: Int): Mod = tok match {
14811469
case ABSTRACT => Mod.Abstract()
14821470
case FINAL => Mod.Final()
1483-
case IMPLICIT => Mod.Implicit()
1471+
case IMPLICIT => Mod.Implicit(ImplicitCommon)
14841472
case INLINE => Mod.Inline()
14851473
case LAZY => Mod.Lazy()
14861474
case OVERRIDE => Mod.Override()
@@ -1502,12 +1490,10 @@ object Parsers {
15021490

15031491
private def addModifier(mods: Modifiers): Modifiers = {
15041492
val tok = in.token
1505-
val flag = flagOfToken(tok)
1506-
val mod = atPos(in.offset) { in.nextToken(); modOfToken(tok) }
1493+
val mod = atPos(in.skipToken()) { modOfToken(tok) }
15071494

1508-
if (mods is flag) syntaxError(RepeatedModifier(flag.toString))
1509-
val res = addFlag(mods, flag).withAddedMod(mod)
1510-
res
1495+
if (mods is mod.flags) syntaxError(RepeatedModifier(flag.toString))
1496+
addMod(mods, mod)
15111497
}
15121498

15131499
private def compatible(flags1: FlagSet, flags2: FlagSet): Boolean = (
@@ -1533,6 +1519,11 @@ object Parsers {
15331519
}
15341520
}
15351521

1522+
/** Always add the syntactic `mod`, but check and conditionally add semantic `mod.flags`
1523+
*/
1524+
def addMod(mods: Modifiers, mod: Mod): Modifiers =
1525+
addFlag(mods, mod.flags).withAddedMod(mod)
1526+
15361527
/** AccessQualifier ::= "[" (Id | this) "]"
15371528
*/
15381529
def accessQualifierOpt(mods: Modifiers): Modifiers =
@@ -1629,7 +1620,7 @@ object Parsers {
16291620
mods =
16301621
atPos(start, in.offset) {
16311622
if (in.token == TYPE) {
1632-
val mod = atPos(in.offset) { in.nextToken(); Mod.Type() }
1623+
val mod = atPos(in.skipToken()) { Mod.Type() }
16331624
(mods | Param | ParamAccessor).withAddedMod(mod)
16341625
} else {
16351626
if (mods.hasFlags) syntaxError("`type' expected")
@@ -1674,7 +1665,6 @@ object Parsers {
16741665
* Param ::= id `:' ParamType [`=' Expr]
16751666
*/
16761667
def paramClauses(owner: Name, ofCaseClass: Boolean = false): List[List[ValDef]] = {
1677-
var implicitFlag = EmptyFlags
16781668
var implicitMod: Mod = null
16791669
var firstClauseOfCaseClass = ofCaseClass
16801670
var implicitOffset = -1 // use once
@@ -1686,11 +1676,11 @@ object Parsers {
16861676
mods =
16871677
atPos(start, in.offset) {
16881678
if (in.token == VAL) {
1689-
val mod = atPos(in.offset) { in.nextToken(); Mod.Val() }
1679+
val mod = atPos(in.skipToken()) { Mod.Val() }
16901680
mods.withAddedMod(mod)
16911681
} else if (in.token == VAR) {
1692-
val mod = atPos(in.offset) { in.nextToken(); Mod.Var() }
1693-
addFlag(mods, Mutable).withAddedMod(mod)
1682+
val mod = atPos(in.skipToken()) { Mod.Var() }
1683+
addMod(mods, mod)
16941684
} else {
16951685
if (!(mods.flags &~ (ParamAccessor | Inline)).isEmpty)
16961686
syntaxError("`val' or `var' expected")
@@ -1712,7 +1702,7 @@ object Parsers {
17121702
if (in.token == ARROW) {
17131703
if (owner.isTypeName && !(mods is Local))
17141704
syntaxError(s"${if (mods is Mutable) "`var'" else "`val'"} parameters may not be call-by-name")
1715-
else if (!implicitFlag.isEmpty)
1705+
else if (implicitMod != null)
17161706
syntaxError("implicit parameters may not be call-by-name")
17171707
}
17181708
paramType()
@@ -1724,17 +1714,16 @@ object Parsers {
17241714
mods = mods.withPos(mods.pos.union(Position(implicitOffset, implicitOffset)))
17251715
implicitOffset = -1
17261716
}
1727-
if (implicitMod != null) mods = mods.withAddedMod(implicitMod)
1728-
ValDef(name, tpt, default).withMods(addFlag(mods, implicitFlag))
1717+
if (implicitMod != null) mods = addMod(mods, implicitMod)
1718+
ValDef(name, tpt, default).withMods(mods)
17291719
}
17301720
}
17311721
def paramClause(): List[ValDef] = inParens {
17321722
if (in.token == RPAREN) Nil
17331723
else {
17341724
if (in.token == IMPLICIT) {
17351725
implicitOffset = in.offset
1736-
implicitMod = atPos(in.offset) { in.nextToken(); Mod.Implicit() }
1737-
implicitFlag = Implicit
1726+
implicitMod = atPos(in.skipToken()) { Mod.Implicit(Implicit) }
17381727
}
17391728
commaSeparated(param)
17401729
}
@@ -1744,7 +1733,7 @@ object Parsers {
17441733
if (in.token == LPAREN)
17451734
paramClause() :: {
17461735
firstClauseOfCaseClass = false
1747-
if (implicitFlag.isEmpty) clauses() else Nil
1736+
if (implicitMod == null) clauses() else Nil
17481737
}
17491738
else Nil
17501739
}
@@ -1837,12 +1826,12 @@ object Parsers {
18371826
*/
18381827
def defOrDcl(start: Int, mods: Modifiers): Tree = in.token match {
18391828
case VAL =>
1840-
val mod = atPos(in.offset) { in.nextToken(); Mod.Val() }
1829+
val mod = atPos(in.skipToken()) { Mod.Val() }
18411830
val modPos = atPos(start) { mods.withAddedMod(mod) }
18421831
patDefOrDcl(start, modPos, in.getDocComment(start))
18431832
case VAR =>
1844-
val mod = atPos(in.offset) { in.nextToken(); Mod.Var() }
1845-
val modPos = atPos(start) { addFlag(mods, Mutable).withAddedMod(mod) }
1833+
val mod = atPos(in.skipToken()) { Mod.Var() }
1834+
val modPos = atPos(start) { addMod(mods, mod) }
18461835
patDefOrDcl(start, modPos, in.getDocComment(start))
18471836
case DEF =>
18481837
defDefOrDcl(start, posMods(start, mods), in.getDocComment(start))
@@ -2231,7 +2220,7 @@ object Parsers {
22312220
else if (isDefIntro(localModifierTokens))
22322221
if (in.token == IMPLICIT) {
22332222
val start = in.offset
2234-
val mod = atPos(in.offset) { in.nextToken(); Mod.Implicit() }
2223+
val mod = atPos(in.skipToken()) { Mod.Implicit(ImplicitCommon) }
22352224
if (isIdent) stats += implicitClosure(start, Location.InBlock, Some(mod))
22362225
else stats += localDef(start, ImplicitCommon, Some(mod))
22372226
} else {

test/test/ModifiersParsingTest.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import dotty.tools.dotc.ast.{ Trees => d }
99
import dotty.tools.dotc.parsing.Parsers.Parser
1010
import dotty.tools.dotc.util.SourceFile
1111
import dotty.tools.dotc.core.Contexts.ContextBase
12+
import dotty.tools.dotc.core.Flags
1213

1314
object ModifiersParsingTest {
1415
implicit val ctx = (new ContextBase).initialCtx
@@ -136,12 +137,13 @@ class ModifiersParsingTest {
136137
assert(source.defParam(0).modifiers == List(Mod.Inline()))
137138

138139
source = "def f(implicit a: Int, b: Int) = ???"
139-
assert(source.defParam(0).modifiers == List(Mod.Implicit()))
140-
assert(source.defParam(1).modifiers == List(Mod.Implicit()))
140+
println(source.defParam(0).modifiers)
141+
assert(source.defParam(0).modifiers == List(Mod.Implicit(Flags.Implicit)))
142+
assert(source.defParam(1).modifiers == List(Mod.Implicit(Flags.Implicit)))
141143

142144
source = "def f(x: Int, y: Int)(implicit a: Int, b: Int) = ???"
143145
assert(source.defParam(0, 0).modifiers == List())
144-
assert(source.defParam(1, 0).modifiers == List(Mod.Implicit()))
146+
assert(source.defParam(1, 0).modifiers == List(Mod.Implicit(Flags.Implicit)))
145147
}
146148

147149
@Test def blockDef = {

0 commit comments

Comments
 (0)