Skip to content

Commit 7853ce6

Browse files
committed
Implement comments as attachments instead of tree members
1 parent 19c5ff5 commit 7853ce6

File tree

4 files changed

+16
-19
lines changed

4 files changed

+16
-19
lines changed

project/Build.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ object DottyBuild extends Build {
182182
}
183183
).
184184
settings(
185-
addCommandAlias("partest", ";test:package;package;test:runMain dotc.build;lockPartestFile;test:test;runPartestRunner;test:runMain test.DottyDocTests") ++
185+
addCommandAlias("partest", ";test:package;package;test:runMain dotc.build;lockPartestFile;test:test;runPartestRunner") ++
186186
addCommandAlias("partest-only", ";test:package;package;test:runMain dotc.build;lockPartestFile;test:test-only dotc.tests;runPartestRunner") ++
187187
addCommandAlias("partest-only-no-bootstrap", ";test:package;package; lockPartestFile;test:test-only dotc.tests;runPartestRunner")
188188
)

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ object Trees {
2929
/** The total number of created tree nodes, maintained if Stats.enabled */
3030
@sharable var ntrees = 0
3131

32+
/** Attachment key for trees with documentation strings attached */
33+
val DocComment = new Attachment.Key[String]
34+
3235
/** Modifiers and annotations for definitions
3336
* @param flags The set flags
3437
* @param privateWithin If a private or protected has is followed by a
@@ -321,9 +324,7 @@ object Trees {
321324
private[ast] def rawMods: Modifiers[T] =
322325
if (myMods == null) genericEmptyModifiers else myMods
323326

324-
private[this] var myComment: Option[String] = None
325-
326-
def rawComment: Option[String] = myComment
327+
def rawComment: Option[String] = getAttachment(DocComment)
327328

328329
def withMods(mods: Modifiers[Untyped]): ThisTree[Untyped] = {
329330
val tree = if (myMods == null || (myMods == mods)) this else clone.asInstanceOf[MemberDef[Untyped]]
@@ -333,8 +334,8 @@ object Trees {
333334

334335
def withFlags(flags: FlagSet): ThisTree[Untyped] = withMods(Modifiers(flags))
335336

336-
def withComment(cmt: Option[String]): ThisTree[Untyped] = {
337-
myComment = cmt
337+
def setComment(comment: Option[String]): ThisTree[Untyped] = {
338+
comment.map(putAttachment(DocComment, _))
338339
asInstanceOf[ThisTree[Untyped]]
339340
}
340341

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,9 +1780,9 @@ object Parsers {
17801780
}
17811781
} else EmptyTree
17821782
lhs match {
1783-
case (id @ Ident(name: TermName)) :: Nil =>
1784-
cpy.ValDef(id)(name, tpt, rhs).withMods(mods).withComment(docstring)
1785-
case _ =>
1783+
case (id @ Ident(name: TermName)) :: Nil => {
1784+
cpy.ValDef(id)(name, tpt, rhs).withMods(mods).setComment(docstring)
1785+
} case _ =>
17861786
PatDef(mods, lhs, tpt, rhs)
17871787
}
17881788
}
@@ -1833,7 +1833,7 @@ object Parsers {
18331833
accept(EQUALS)
18341834
expr()
18351835
}
1836-
DefDef(name, tparams, vparamss, tpt, rhs).withMods(mods1).withComment(docstring)
1836+
DefDef(name, tparams, vparamss, tpt, rhs).withMods(mods1).setComment(docstring)
18371837
}
18381838
}
18391839

@@ -1875,7 +1875,7 @@ object Parsers {
18751875
in.token match {
18761876
case EQUALS =>
18771877
in.nextToken()
1878-
TypeDef(name, tparams, typ()).withMods(mods).withComment(docstring)
1878+
TypeDef(name, tparams, typ()).withMods(mods).setComment(docstring)
18791879
case SUPERTYPE | SUBTYPE | SEMI | NEWLINE | NEWLINES | COMMA | RBRACE | EOF =>
18801880
TypeDef(name, tparams, typeBounds()).withMods(mods)
18811881
case _ =>
@@ -1921,9 +1921,7 @@ object Parsers {
19211921
}
19221922
val templ = templateOpt(constr)
19231923

1924-
TypeDef(name, templ)
1925-
.withMods(mods)
1926-
.withComment(docstring)
1924+
TypeDef(name, templ).withMods(mods).setComment(docstring)
19271925
}
19281926

19291927
/** ConstrMods ::= AccessModifier
@@ -1943,9 +1941,7 @@ object Parsers {
19431941
val name = ident()
19441942
val template = templateOpt(emptyConstructor())
19451943

1946-
ModuleDef(name, template)
1947-
.withMods(mods)
1948-
.withComment(docstring)
1944+
ModuleDef(name, template).withMods(mods).setComment(docstring)
19491945
}
19501946

19511947
/* -------- TEMPLATES ------------------------------------------- */

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,12 @@ object Scanners {
180180
/** All comments in the reverse order of their position in the source.
181181
* set only when `keepComments` is true.
182182
*/
183-
var revComments: List[Comment] = Nil
183+
private[this] var revComments: List[Comment] = Nil
184184

185185
/** All doc comments as encountered, each list contains doc comments from
186186
* the same block level. Starting with the deepest level and going upward
187187
*/
188-
var docsPerBlockStack: List[List[Comment]] = List(List())
188+
private[this] var docsPerBlockStack: List[List[Comment]] = List(List())
189189

190190
/** Adds level of nesting to docstrings */
191191
def enterBlock(): Unit =

0 commit comments

Comments
 (0)