Skip to content

Commit

Permalink
Optimize addDocstring
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Apr 11, 2024
1 parent cb531f7 commit 0dd8b02
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Comments.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ object Comments {
def registerLoader(sym: Symbol, loader: () => Option[Comment]): Unit =
docstrings.update(sym, CommentLoader(loader))

def addDocstring(sym: Symbol, doc: Option[Comment]): Unit =
doc.foreach(d => docstrings.update(sym, d))
def addDocstring(sym: Symbol, doc: Comment): Unit =
docstrings.update(sym, doc)
}


Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2169,7 +2169,7 @@ class Definitions {
@tu lazy val LazyValsControlState = requiredClass(s"$LazyValsModuleName.LazyValControlState")

def addSyntheticSymbolsComments(using Context): Unit =
def add(sym: Symbol, doc: String) = ctx.docCtx.foreach(_.addDocstring(sym, Some(Comment(NoSpan, doc))))
def add(sym: Symbol, doc: String) = ctx.docCtx.foreach(_.addDocstring(sym, Comment(NoSpan, doc)))

add(AnyClass,
"""/** Class `Any` is the root of the Scala class hierarchy. Every class in a Scala
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ class TreeUnpickler(reader: TastyReader,
commentUnpicklerOpt.foreach { commentUnpickler =>
def loadComment() = commentUnpickler.commentAt(start)
if ctx.settings.YreadDocsEagerly.value then
ctx.docCtx.get.addDocstring(tree.symbol, loadComment())
loadComment().foreach(doc => ctx.docCtx.get.addDocstring(tree.symbol, doc))
else
ctx.docCtx.get.registerLoader(tree.symbol, loadComment)
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Docstrings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ object Docstrings {
}

val commentWithUsecases = expanded.copy(usecases = typedUsecases)
docCtx.addDocstring(sym, Some(commentWithUsecases))
docCtx.addDocstring(sym, commentWithUsecases)
commentWithUsecases
}
}
Expand All @@ -52,7 +52,7 @@ object Docstrings {
val tplExp = docCtx.templateExpander
tplExp.defineVariables(sym)
val newComment = comment.expand(tplExp.expandedDocComment(sym, owner, _))
docCtx.addDocstring(sym, Some(newComment))
docCtx.addDocstring(sym, newComment)
newComment
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ class Namer { typer: Typer =>

def setDocstring(sym: Symbol, tree: Tree)(using Context): Unit = tree match {
case t: MemberDef if t.rawComment.isDefined =>
ctx.docCtx.foreach(_.addDocstring(sym, t.rawComment))
ctx.docCtx.foreach(_.addDocstring(sym, t.rawComment.get))
case t: ExtMethods =>
for meth <- t.methods.find(_.span.point == sym.span.point) do
setDocstring(sym, meth)
Expand Down

0 comments on commit 0dd8b02

Please sign in to comment.