Skip to content

Commit c0ae291

Browse files
committed
Implement comments as attachments instead of tree members
1 parent 77a5a78 commit c0ae291

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
@@ -317,9 +320,7 @@ object Trees {
317320
private[ast] def rawMods: Modifiers[T] =
318321
if (myMods == null) genericEmptyModifiers else myMods
319322

320-
private[this] var myComment: Option[String] = None
321-
322-
def rawComment: Option[String] = myComment
323+
def rawComment: Option[String] = getAttachment(DocComment)
323324

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

330331
def withFlags(flags: FlagSet): ThisTree[Untyped] = withMods(Modifiers(flags))
331332

332-
def withComment(cmt: Option[String]): ThisTree[Untyped] = {
333-
myComment = cmt
333+
def setComment(comment: Option[String]): ThisTree[Untyped] = {
334+
comment.map(putAttachment(DocComment, _))
334335
asInstanceOf[ThisTree[Untyped]]
335336
}
336337

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,9 +1750,9 @@ object Parsers {
17501750
}
17511751
} else EmptyTree
17521752
lhs match {
1753-
case (id @ Ident(name: TermName)) :: Nil =>
1754-
cpy.ValDef(id)(name, tpt, rhs).withMods(mods).withComment(docstring)
1755-
case _ =>
1753+
case (id @ Ident(name: TermName)) :: Nil => {
1754+
cpy.ValDef(id)(name, tpt, rhs).withMods(mods).setComment(docstring)
1755+
} case _ =>
17561756
PatDef(mods, lhs, tpt, rhs)
17571757
}
17581758
}
@@ -1796,7 +1796,7 @@ object Parsers {
17961796
accept(EQUALS)
17971797
expr()
17981798
}
1799-
DefDef(name, tparams, vparamss, tpt, rhs).withMods(mods1).withComment(docstring)
1799+
DefDef(name, tparams, vparamss, tpt, rhs).withMods(mods1).setComment(docstring)
18001800
}
18011801
}
18021802

@@ -1838,7 +1838,7 @@ object Parsers {
18381838
in.token match {
18391839
case EQUALS =>
18401840
in.nextToken()
1841-
TypeDef(name, tparams, typ()).withMods(mods).withComment(docstring)
1841+
TypeDef(name, tparams, typ()).withMods(mods).setComment(docstring)
18421842
case SUPERTYPE | SUBTYPE | SEMI | NEWLINE | NEWLINES | COMMA | RBRACE | EOF =>
18431843
TypeDef(name, tparams, typeBounds()).withMods(mods)
18441844
case _ =>
@@ -1884,9 +1884,7 @@ object Parsers {
18841884
}
18851885
val templ = templateOpt(constr)
18861886

1887-
TypeDef(name, templ)
1888-
.withMods(mods)
1889-
.withComment(docstring)
1887+
TypeDef(name, templ).withMods(mods).setComment(docstring)
18901888
}
18911889

18921890
/** ConstrMods ::= AccessModifier
@@ -1906,9 +1904,7 @@ object Parsers {
19061904
val name = ident()
19071905
val template = templateOpt(emptyConstructor())
19081906

1909-
ModuleDef(name, template)
1910-
.withMods(mods)
1911-
.withComment(docstring)
1907+
ModuleDef(name, template).withMods(mods).setComment(docstring)
19121908
}
19131909

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