Skip to content

Commit 2a6efc2

Browse files
committed
Make Modifiers not extend from Positioned
Modifiers was made a subclass of Positioned since it contains elements that are Positioned: source modifiers and annotations. That way, we got the navigation logic to get to the elements for free. However, a lot of modifiers are synthetic, and seems artificial to give these a position. With the addition of a source component to position the balance swings the other way. It now seems preferable to have Modifiers not extend Positions and to consider modifier as a as a separate case for navigation instead.
1 parent 8de82fc commit 2a6efc2

File tree

6 files changed

+38
-30
lines changed

6 files changed

+38
-30
lines changed

compiler/src/dotty/tools/dotc/ast/NavigateAST.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ object NavigateAST {
7272
while (it.hasNext) {
7373
val path1 = it.next() match {
7474
case p: Positioned => singlePath(p, path)
75+
case m: untpd.Modifiers => childPath(m.productIterator, path)
7576
case xs: List[_] => childPath(xs.iterator, path)
7677
case _ => path
7778
}

compiler/src/dotty/tools/dotc/ast/Positioned.scala

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ abstract class Positioned extends Product {
105105
end = p.pos.start
106106
}
107107
else outstanding = p :: outstanding
108+
case m: untpd.Modifiers =>
109+
elems = elems ::: m.mods.reverse ::: m.annotations.reverse
108110
case xs: List[_] =>
109111
elems = elems ::: xs.reverse
110112
case _ =>
@@ -121,6 +123,7 @@ abstract class Positioned extends Product {
121123
n -= 1
122124
productElement(n) match {
123125
case p: Positioned => pos = pos union p.pos
126+
case m: untpd.Modifiers => pos = unionPos(unionPos(pos, m.mods), m.annotations)
124127
case xs: List[_] => pos = unionPos(pos, xs)
125128
case _ =>
126129
}
@@ -138,9 +141,11 @@ abstract class Positioned extends Product {
138141
def contains(that: Positioned): Boolean = {
139142
def isParent(x: Any): Boolean = x match {
140143
case x: Positioned =>
141-
x contains that
144+
x.contains(that)
145+
case m: untpd.Modifiers =>
146+
m.mods.exists(isParent) || m.annotations.exists(isParent)
142147
case xs: List[_] =>
143-
xs exists isParent
148+
xs.exists(isParent)
144149
case _ =>
145150
false
146151
}
@@ -198,6 +203,9 @@ abstract class Positioned extends Product {
198203
lastPos = p.pos
199204
}
200205
p.checkPos(nonOverlapping)
206+
case m: untpd.Modifiers =>
207+
m.annotations.foreach(check)
208+
m.mods.foreach(check)
201209
case xs: List[_] =>
202210
xs.foreach(check)
203211
case _ =>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
151151
flags: FlagSet = EmptyFlags,
152152
privateWithin: TypeName = tpnme.EMPTY,
153153
annotations: List[Tree] = Nil,
154-
mods: List[Mod] = Nil) extends Positioned with Cloneable {
154+
mods: List[Mod] = Nil) extends Cloneable {
155155

156156
def is(fs: FlagSet): Boolean = flags is fs
157157
def is(fc: FlagConjunction): Boolean = flags is fc

compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ object JavaParsers {
732732
val members = new ListBuffer[Tree]
733733
while (in.token != RBRACE && in.token != EOF) {
734734
val start = in.offset
735-
var mods = atPos(start) { modifiers(inInterface) }
735+
var mods = modifiers(inInterface)
736736
if (in.token == LBRACE) {
737737
skipAhead() // skip init block, we just assume we have seen only static
738738
accept(RBRACE)
@@ -895,7 +895,7 @@ object JavaParsers {
895895
while (in.token == SEMI) in.nextToken()
896896
if (in.token != EOF) {
897897
val start = in.offset
898-
val mods = atPos(start) { modifiers(inInterface = false) }
898+
val mods = modifiers(inInterface = false)
899899
buf ++= typeDecl(start, mods)
900900
}
901901
}

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

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,7 +1927,7 @@ object Parsers {
19271927
def typeParam(): TypeDef = {
19281928
val isConcreteOwner = ownerKind == ParamOwner.Class || ownerKind == ParamOwner.Def
19291929
val start = in.offset
1930-
val mods = atPos(start) {
1930+
val mods =
19311931
annotsAsMods() | {
19321932
if (ownerKind == ParamOwner.Class) Param | PrivateLocal
19331933
else Param
@@ -1938,7 +1938,6 @@ object Parsers {
19381938
else EmptyFlags
19391939
else EmptyFlags
19401940
}
1941-
}
19421941
atPos(start, nameStart) {
19431942
val name =
19441943
if (isConcreteOwner || in.token != USCORE) ident().toTypeName
@@ -1984,27 +1983,25 @@ object Parsers {
19841983
if (ofClass) {
19851984
mods = addFlag(modifiers(start = mods), ParamAccessor)
19861985
mods =
1987-
atPos(start, in.offset) {
1988-
if (in.token == VAL) {
1989-
in.nextToken()
1990-
mods
1991-
}
1992-
else if (in.token == VAR) {
1993-
val mod = atPos(in.skipToken()) { Mod.Var() }
1994-
addMod(mods, mod)
1995-
}
1996-
else {
1997-
if (!(mods.flags &~ (ParamAccessor | Inline | impliedMods.flags)).isEmpty)
1998-
syntaxError("`val' or `var' expected")
1999-
if (firstClause && ofCaseClass) mods
2000-
else mods | PrivateLocal
2001-
}
1986+
if (in.token == VAL) {
1987+
in.nextToken()
1988+
mods
1989+
}
1990+
else if (in.token == VAR) {
1991+
val mod = atPos(in.skipToken()) { Mod.Var() }
1992+
addMod(mods, mod)
1993+
}
1994+
else {
1995+
if (!(mods.flags &~ (ParamAccessor | Inline | impliedMods.flags)).isEmpty)
1996+
syntaxError("`val' or `var' expected")
1997+
if (firstClause && ofCaseClass) mods
1998+
else mods | PrivateLocal
20021999
}
20032000
}
20042001
else {
20052002
if (isIdent(nme.inline) && in.isSoftModifierInParamModifierPosition)
20062003
mods = addModifier(mods)
2007-
mods = atPos(start) { mods | Param }
2004+
mods |= Param
20082005
}
20092006
atPos(start, nameStart) {
20102007
val name = ident()
@@ -2016,7 +2013,7 @@ object Parsers {
20162013
if (in.token == EQUALS) { in.nextToken(); expr() }
20172014
else EmptyTree
20182015
if (implicitOffset >= 0) {
2019-
mods = mods.withPos(mods.pos.union(Position(implicitOffset, implicitOffset)))
2016+
//mods = mods.withPos(mods.pos.union(Position(implicitOffset, implicitOffset)))
20202017
implicitOffset = -1
20212018
}
20222019
ValDef(name, tpt, default).withMods(mods)
@@ -2157,9 +2154,8 @@ object Parsers {
21572154
}
21582155

21592156
def posMods(start: Int, mods: Modifiers): Modifiers = {
2160-
val mods1 = atPos(start)(mods)
21612157
in.nextToken()
2162-
mods1
2158+
mods
21632159
}
21642160

21652161
/** Def ::= val PatDef
@@ -2549,8 +2545,10 @@ object Parsers {
25492545
setLastStatOffset()
25502546
if (in.token == PACKAGE) {
25512547
val start = in.skipToken()
2552-
if (in.token == OBJECT)
2553-
stats += objectDef(start, atPos(start, in.skipToken()) { Modifiers(Package) })
2548+
if (in.token == OBJECT) {
2549+
in.nextToken()
2550+
stats += objectDef(start, Modifiers(Package))
2551+
}
25542552
else stats += packaging(start)
25552553
}
25562554
else if (in.token == IMPORT)
@@ -2710,7 +2708,8 @@ object Parsers {
27102708
if (in.token == PACKAGE) {
27112709
in.nextToken()
27122710
if (in.token == OBJECT) {
2713-
ts += objectDef(start, atPos(start, in.skipToken()) { Modifiers(Package) })
2711+
in.nextToken()
2712+
ts += objectDef(start, Modifiers(Package))
27142713
if (in.token != EOF) {
27152714
acceptStatSep()
27162715
ts ++= topStatSeq()

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ class Namer { typer: Typer =>
266266
def createSymbol(tree: Tree)(implicit ctx: Context): Symbol = {
267267

268268
def privateWithinClass(mods: Modifiers) =
269-
enclosingClassNamed(mods.privateWithin, mods.pos)
269+
enclosingClassNamed(mods.privateWithin, tree.pos)
270270

271271
/** Check that flags are OK for symbol. This is done early to avoid
272272
* catastrophic failure when we create a TermSymbol with TypeFlags, or vice versa.

0 commit comments

Comments
 (0)