Skip to content

Commit 27c5540

Browse files
committed
Fix cheeky comment in nested scope
1 parent 7f365f4 commit 27c5540

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,21 @@ object Scanners {
178178
/** All doc comments kept by their end position in a `Map` */
179179
private[this] var docstringMap: SortedMap[Int, Comment] = SortedMap.empty
180180

181-
private[this] def addComment(comment: Comment): Unit =
182-
docstringMap = docstringMap + (comment.pos.end -> comment)
181+
private[this] def addComment(comment: Comment): Unit = {
182+
val lookahead = lookaheadReader
183+
def nextPos: Int = (lookahead.getc(): @switch) match {
184+
case ' ' | '\t' => nextPos
185+
case CR | LF | FF =>
186+
// if we encounter line delimitng whitespace we don't count it, since
187+
// it seems not to affect positions in source
188+
nextPos - 1
189+
case _ => lookahead.charOffset - 1
190+
}
191+
docstringMap = docstringMap + (nextPos -> comment)
192+
}
183193

184194
/** Returns the closest docstring preceding the position supplied */
185-
def getDocComment(pos: Int): Option[Comment] =
186-
docstringMap.to(pos).lastOption.map(_._2)
195+
def getDocComment(pos: Int): Option[Comment] = docstringMap.get(pos)
187196

188197
/** A buffer for comments */
189198
val commentBuf = new StringBuilder
@@ -589,10 +598,12 @@ object Scanners {
589598
val start = lastCharOffset
590599
def finishComment(): Boolean = {
591600
if (keepComments) {
592-
val pos = Position(start, charOffset, start)
601+
val pos = Position(start, charOffset - 1, start)
593602
val comment = Comment(pos, flushBuf(commentBuf))
594603

595-
if (comment.isDocComment) addComment(comment)
604+
if (comment.isDocComment) {
605+
addComment(comment)
606+
}
596607
}
597608

598609
true

compiler/test/dotty/tools/dotc/parsing/DocstringTests.scala

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,4 +488,34 @@ class DocstringTests extends DocstringTest {
488488
checkDocString(c.rawComment.map(_.raw), "/** Class1 */")
489489
}
490490
}
491+
492+
@Test def nestedComment = {
493+
val source =
494+
"""
495+
|trait T {
496+
| /** Cheeky comment */
497+
|}
498+
|class C
499+
""".stripMargin
500+
501+
import dotty.tools.dotc.ast.untpd._
502+
checkFrontend(source) {
503+
case p @ PackageDef(_, Seq(_, c: TypeDef)) =>
504+
assert(c.rawComment == None, s"class C is not supposed to have a docstring (${c.rawComment.get}) in:$source")
505+
}
506+
}
507+
508+
@Test def eofComment = {
509+
val source =
510+
"""
511+
|class C
512+
|/** Cheeky comment */
513+
""".stripMargin
514+
515+
import dotty.tools.dotc.ast.untpd._
516+
checkFrontend(source) {
517+
case p @ PackageDef(_, Seq(c: TypeDef)) =>
518+
assert(c.rawComment == None, s"class C is not supposed to have a docstring (${c.rawComment.get}) in:$source")
519+
}
520+
}
491521
} /* End class */

0 commit comments

Comments
 (0)