Skip to content

Commit 60a8b72

Browse files
committed
Fix issues with attaching doc comments in parser
1 parent 79092cb commit 60a8b72

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,7 @@ object Scanners {
190190
private def addComment(comment: Comment): Unit = {
191191
val lookahead = lookaheadReader()
192192
def nextPos: Int = (lookahead.getc(): @switch) match {
193-
case ' ' | '\t' => nextPos
194-
case CR | LF | FF =>
195-
// if we encounter line delimiting whitespace we don't count it, since
196-
// it seems not to affect positions in source
197-
nextPos - 1
193+
case ' ' | '\t' | CR | LF | FF => nextPos
198194
case _ => lookahead.charOffset - 1
199195
}
200196
docstringMap = docstringMap + (nextPos -> comment)
@@ -854,6 +850,9 @@ object Scanners {
854850

855851
if (comment.isDocComment)
856852
addComment(comment)
853+
else
854+
// "forward" doc comments over normal ones
855+
getDocComment(start).foreach(addComment)
857856
}
858857

859858
true

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,37 @@ class DocstringTests extends DocstringTest {
474474
}
475475
}
476476

477+
478+
@Test def overNL = {
479+
val source =
480+
"""
481+
|/** Class1 */
482+
|
483+
|class Class1
484+
""".stripMargin
485+
486+
import dotty.tools.dotc.ast.untpd._
487+
checkFrontend(source) {
488+
case p @ PackageDef(_, Seq(c: TypeDef)) =>
489+
checkDocString(c.rawComment.map(_.raw), "/** Class1 */")
490+
}
491+
}
492+
493+
@Test def overComment = {
494+
val source =
495+
"""
496+
|/** Class1 */
497+
|// foo
498+
|class Class1
499+
""".stripMargin
500+
501+
import dotty.tools.dotc.ast.untpd._
502+
checkFrontend(source) {
503+
case p @ PackageDef(_, Seq(c: TypeDef)) =>
504+
checkDocString(c.rawComment.map(_.raw), "/** Class1 */")
505+
}
506+
}
507+
477508
@Test def withAnnotation = {
478509
val source =
479510
"""
@@ -489,6 +520,22 @@ class DocstringTests extends DocstringTest {
489520
}
490521
}
491522

523+
@Test def withAnnotationOverComment = {
524+
val source =
525+
"""
526+
|/** Class1 */
527+
|// foo
528+
|@SerialVersionUID(1)
529+
|class Class1
530+
""".stripMargin
531+
532+
import dotty.tools.dotc.ast.untpd._
533+
checkFrontend(source) {
534+
case p @ PackageDef(_, Seq(c: TypeDef)) =>
535+
checkDocString(c.rawComment.map(_.raw), "/** Class1 */")
536+
}
537+
}
538+
492539
@Test def nestedComment = {
493540
val source =
494541
"""

scala3doc-testcases/src/tests/tests.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,11 @@ class Methods:
210210
def primitives(a: Int, b: Double, c: Short): Byte = 0
211211
def strings(a: String): String = ""
212212
def arrays(a: Array[String], b: Array[Int]): Array[Double] = ???
213+
214+
/** @define foo O's foo.
215+
*/
216+
object O:
217+
218+
/** This is foo: $foo
219+
*/
220+
def method(s: String) = s

0 commit comments

Comments
 (0)