Skip to content

Commit 4eed3f1

Browse files
committed
Address review comments
1 parent 236b931 commit 4eed3f1

File tree

5 files changed

+26
-39
lines changed

5 files changed

+26
-39
lines changed

compiler/src/dotty/tools/dotc/Driver.scala

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,14 @@ class Driver extends DotClass {
4242

4343
protected def sourcesRequired = true
4444

45-
/**
46-
* Should the `ContextDocstrings` be set for this context? The `ContextDocstrings` is used
47-
* to store doc comments unless `-Ydrop-comments` is set, or when TASTY is configured to
48-
* unpickle the doc comments.
49-
*/
50-
protected def shouldAddDocContext(implicit ctx: Context): Boolean = {
51-
!ctx.settings.YdropComments.value || ctx.mode.is(Mode.ReadComments)
52-
}
53-
5445
def setup(args: Array[String], rootCtx: Context): (List[String], Context) = {
5546
val ctx = rootCtx.fresh
5647
val summary = CompilerCommand.distill(args)(ctx)
5748
ctx.setSettings(summary.sstate)
5849

59-
if (shouldAddDocContext(ctx)) ctx.setProperty(ContextDoc, new ContextDocstrings)
50+
if (!ctx.settings.YdropComments.value(ctx) || ctx.mode.is(Mode.ReadComments)) {
51+
ctx.setProperty(ContextDoc, new ContextDocstrings)
52+
}
6053

6154
val fileNames = CompilerCommand.checkUsage(summary, sourcesRequired)(ctx)
6255
(fileNames, ctx)

compiler/src/dotty/tools/dotc/core/quoted/TastyUnpickler.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import dotty.tools.dotc.core.tasty._
44
import dotty.tools.dotc.core.tasty.TastyUnpickler.NameTable
55

66
object TastyUnpickler {
7-
class QuotedTreeSectionUnpickler(posUnpickler: Option[PositionUnpickler], commentUnpickler: Option[CommentUnpickler], splices: Seq[Any])
8-
extends DottyUnpickler.TreeSectionUnpickler(posUnpickler, commentUnpickler) {
7+
class QuotedTreeSectionUnpickler(posUnpickler: Option[PositionUnpickler], splices: Seq[Any])
8+
extends DottyUnpickler.TreeSectionUnpickler(posUnpickler, None) {
99
override def unpickle(reader: TastyReader, nameAtRef: NameTable) =
10-
new TreeUnpickler(reader, nameAtRef, posUnpickler, commentUnpickler, splices)
10+
new TreeUnpickler(reader, nameAtRef, posUnpickler, None, splices)
1111
}
1212
}
1313

14-
/** A class for unpickling quoted Tasty trees and symbols.
14+
/** A class for unpickling quoted Tasty trees and symbols. Comments are never unpickled.
1515
* @param bytes the bytearray containing the Tasty file from which we unpickle
1616
* @param splices splices that will fill the holes in the quote
1717
*/
@@ -20,5 +20,5 @@ class TastyUnpickler(bytes: Array[Byte], splices: Seq[Any]) extends DottyUnpickl
2020
import TastyUnpickler._
2121

2222
protected override def treeSectionUnpickler(posUnpicklerOpt: Option[PositionUnpickler], commentUnpicklerOpt: Option[CommentUnpickler]): TreeSectionUnpickler =
23-
new QuotedTreeSectionUnpickler(posUnpicklerOpt, commentUnpicklerOpt, splices)
23+
new QuotedTreeSectionUnpickler(posUnpicklerOpt, splices)
2424
}

compiler/src/dotty/tools/dotc/core/tasty/CommentPickler.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dotty.tools.dotc.core.tasty
22

33
import dotty.tools.dotc.ast.tpd
4-
import dotty.tools.dotc.core.Comments.{Comment, CommentsContext}
4+
import dotty.tools.dotc.core.Comments.{Comment, CommentsContext, ContextDocstrings}
55
import dotty.tools.dotc.core.Contexts.Context
66
import dotty.tools.dotc.core.tasty.TastyBuffer.Addr
77

@@ -11,8 +11,10 @@ class CommentPickler(pickler: TastyPickler, addrOfTree: tpd.Tree => Option[Addr]
1111
private[this] val buf = new TastyBuffer(5000)
1212
pickler.newSection("Comments", buf)
1313

14-
def pickleComment(root: tpd.Tree): Unit =
15-
new Traverser().traverse(root)
14+
def pickleComment(root: tpd.Tree): Unit = {
15+
assert(ctx.docCtx.isDefined, "Trying to pickle comments, but there's no `docCtx`.")
16+
new Traverser(ctx.docCtx.get).traverse(root)
17+
}
1618

1719
def pickleComment(addrOfTree: Option[Addr], comment: Option[Comment]): Unit = (addrOfTree, comment) match {
1820
case (Some(addr), Some(cmt)) =>
@@ -26,14 +28,12 @@ class CommentPickler(pickler: TastyPickler, addrOfTree: tpd.Tree => Option[Addr]
2628
()
2729
}
2830

29-
private class Traverser extends tpd.TreeTraverser {
31+
private class Traverser(docCtx: ContextDocstrings) extends tpd.TreeTraverser {
3032
override def traverse(tree: tpd.Tree)(implicit ctx: Context): Unit =
3133
tree match {
3234
case md: tpd.MemberDef =>
33-
ctx.docCtx.foreach { docCtx =>
34-
val comment = docCtx.docstring(md.symbol)
35-
pickleComment(addrOfTree(md), comment)
36-
}
35+
val comment = docCtx.docstring(md.symbol)
36+
pickleComment(addrOfTree(md), comment)
3737
traverseChildren(md)
3838
case _ =>
3939
traverseChildren(tree)

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -825,10 +825,10 @@ class TreeUnpickler(reader: TastyReader,
825825
sym.registerIfChild(late = true)
826826

827827
if (ctx.mode.is(Mode.ReadComments)) {
828-
for { docCtx <- ctx.docCtx
829-
commentUnpickler <- commentUnpicklerOpt } {
828+
assert(ctx.docCtx.isDefined, "Mode is `ReadComments`, but no `docCtx` is set.")
829+
commentUnpicklerOpt.foreach { commentUnpickler =>
830830
val comment = commentUnpickler.commentAt(start)
831-
docCtx.addDocstring(tree.symbol, comment)
831+
ctx.docCtx.get.addDocstring(tree.symbol, comment)
832832
tree.setComment(comment)
833833
}
834834
}

compiler/test/dotty/tools/dotc/core/tasty/CommentPicklingTest.scala

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package dotty.tools.dotc.core.tasty
22

33
import dotty.tools.dotc.ast.tpd
44
import dotty.tools.dotc.ast.tpd.TreeOps
5-
import dotty.tools.dotc.Driver
5+
import dotty.tools.dotc.{Driver, Main}
66
import dotty.tools.dotc.core.Comments.CommentsContext
77
import dotty.tools.dotc.core.Contexts.Context
88
import dotty.tools.dotc.core.Decorators.PreNamedString
@@ -78,16 +78,11 @@ class CommentPicklingTest {
7878
}
7979
}
8080

81-
private def findTreeNamed(name: Name)(trees: List[tpd.Tree], ctx: Context): Option[tpd.NameTree] = {
82-
val acc = new tpd.TreeAccumulator[Option[tpd.NameTree]] {
83-
override def apply(x: Option[tpd.NameTree], tree: tpd.Tree)(implicit ctx: Context): Option[tpd.NameTree] = {
84-
x.orElse(tree match {
85-
case md: tpd.NameTree if md.name == name => Some(md)
86-
case other => foldOver(None, other)
87-
})
88-
}
89-
}
90-
acc(None, trees)(ctx)
81+
private def findTreeNamed(name: Name)(trees: List[tpd.Tree], ctx: Context): Option[tpd.MemberDef] = {
82+
implicit val _ctx: Context = ctx
83+
trees.flatMap { _.find { case md: tpd.MemberDef => md.name == name; case _ => false }
84+
.map(_.asInstanceOf[tpd.MemberDef]).toList
85+
}.headOption
9186
}
9287

9388
private def compileAndUnpickle[T](sources: List[String])(fn: (List[tpd.Tree], Context) => T) = {
@@ -103,9 +98,8 @@ class CommentPicklingTest {
10398
Files.createDirectories(out)
10499

105100
val options = compileOptions.and("-d", out.toAbsolutePath.toString).and(sourceFiles: _*)
106-
val driver = new Driver
107101
val reporter = TestReporter.reporter(System.out, logLevel = ERROR)
108-
driver.process(options.all, reporter)
102+
Main.process(options.all, reporter)
109103
assertFalse("Compilation failed.", reporter.hasErrors)
110104

111105
val tastyFiles = getAll(tmp, "glob:**.tasty")

0 commit comments

Comments
 (0)