Skip to content

Commit 844683e

Browse files
committed
Implement comments as attachments instead of tree members
1 parent 8513dd6 commit 844683e

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
@@ -1782,9 +1782,9 @@ object Parsers {
17821782
}
17831783
} else EmptyTree
17841784
lhs match {
1785-
case (id @ Ident(name: TermName)) :: Nil =>
1786-
cpy.ValDef(id)(name, tpt, rhs).withMods(mods).withComment(docstring)
1787-
case _ =>
1785+
case (id @ Ident(name: TermName)) :: Nil => {
1786+
cpy.ValDef(id)(name, tpt, rhs).withMods(mods).setComment(docstring)
1787+
} case _ =>
17881788
PatDef(mods, lhs, tpt, rhs)
17891789
}
17901790
}
@@ -1835,7 +1835,7 @@ object Parsers {
18351835
accept(EQUALS)
18361836
expr()
18371837
}
1838-
DefDef(name, tparams, vparamss, tpt, rhs).withMods(mods1).withComment(docstring)
1838+
DefDef(name, tparams, vparamss, tpt, rhs).withMods(mods1).setComment(docstring)
18391839
}
18401840
}
18411841

@@ -1877,7 +1877,7 @@ object Parsers {
18771877
in.token match {
18781878
case EQUALS =>
18791879
in.nextToken()
1880-
TypeDef(name, tparams, typ()).withMods(mods).withComment(docstring)
1880+
TypeDef(name, tparams, typ()).withMods(mods).setComment(docstring)
18811881
case SUPERTYPE | SUBTYPE | SEMI | NEWLINE | NEWLINES | COMMA | RBRACE | EOF =>
18821882
TypeDef(name, tparams, typeBounds()).withMods(mods)
18831883
case _ =>
@@ -1923,9 +1923,7 @@ object Parsers {
19231923
}
19241924
val templ = templateOpt(constr)
19251925

1926-
TypeDef(name, templ)
1927-
.withMods(mods)
1928-
.withComment(docstring)
1926+
TypeDef(name, templ).withMods(mods).setComment(docstring)
19291927
}
19301928

19311929
/** ConstrMods ::= AccessModifier
@@ -1945,9 +1943,7 @@ object Parsers {
19451943
val name = ident()
19461944
val template = templateOpt(emptyConstructor())
19471945

1948-
ModuleDef(name, template)
1949-
.withMods(mods)
1950-
.withComment(docstring)
1946+
ModuleDef(name, template).withMods(mods).setComment(docstring)
19511947
}
19521948

19531949
/* -------- 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)