Skip to content

Commit 3f50d5e

Browse files
committed
Fix #9339: Register doc of extension methods
1 parent 5cdfd31 commit 3f50d5e

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -530,28 +530,29 @@ class Namer { typer: Typer =>
530530
* enter them into symbol table
531531
*/
532532
def indexExpanded(origStat: Tree)(using Context): Context = {
533-
def recur(stat: Tree): Context = stat match {
533+
def recur(stat: Tree, idx: Int): Context = stat match {
534534
case pcl: PackageDef =>
535535
val pkg = createPackageSymbol(pcl.pid)
536536
index(pcl.stats)(using ctx.fresh.setOwner(pkg.moduleClass))
537537
invalidateCompanions(pkg, Trees.flatten(pcl.stats map expanded))
538-
setDocstring(pkg, stat)
538+
setDocstring(pkg, stat, idx)
539539
ctx
540540
case imp: Import =>
541541
ctx.importContext(imp, createSymbol(imp))
542542
case mdef: DefTree =>
543543
val sym = createSymbol(mdef)
544544
enterSymbol(sym)
545-
setDocstring(sym, origStat)
545+
setDocstring(sym, origStat, idx)
546546
addEnumConstants(mdef, sym)
547547
ctx
548548
case stats: Thicket =>
549-
stats.toList.foreach(recur)
549+
for (stat, i) <- stats.toList.zipWithIndex do
550+
recur(stat, i)
550551
ctx
551552
case _ =>
552553
ctx
553554
}
554-
recur(expanded(origStat))
555+
recur(expanded(origStat), 0)
555556
}
556557

557558
/** Determines whether this field holds an enum constant. */
@@ -590,9 +591,11 @@ class Namer { typer: Typer =>
590591
case _ =>
591592
}
592593

593-
def setDocstring(sym: Symbol, tree: Tree)(using Context): Unit = tree match {
594+
def setDocstring(sym: Symbol, tree: Tree, idx: Int)(using Context): Unit = tree match {
594595
case t: MemberDef if t.rawComment.isDefined =>
595596
ctx.docCtx.foreach(_.addDocstring(sym, t.rawComment))
597+
case t: ExtMethods if t.methods(idx).rawComment.isDefined =>
598+
ctx.docCtx.foreach(_.addDocstring(sym, t.methods(idx).rawComment))
596599
case _ => ()
597600
}
598601

compiler/test/dotty/tools/repl/DocTests.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ class DocTests extends ReplTest {
3131
assertEquals("doc", doc("new Foo"))
3232
}
3333

34+
@Test def docOfExtension1 =
35+
eval("/** doc */ extension (x: Int) def foo = 0").andThen { implicit s =>
36+
assertEquals("doc", doc("extension_foo"))
37+
}
38+
39+
@Test def docOfExtension2 =
40+
eval("extension (x: Int) /** doc */ def foo = 0").andThen { implicit s =>
41+
assertEquals("doc", doc("extension_foo"))
42+
}
43+
44+
@Test def docOfExtension3 =
45+
eval("/** doc0 */ extension (x: Int) { /** doc1 */ def foo = 0; /** doc2 */ def bar = 0; def baz = 0 }").andThen { implicit s =>
46+
assertEquals("doc1", doc("extension_foo"))
47+
assertEquals("doc2", doc("extension_bar"))
48+
assertEquals("doc0", doc("extension_baz"))
49+
}
50+
3451
@Test def docOfDefInObject =
3552
eval("object O { /** doc */ def foo = 0 }").andThen { implicit s =>
3653
assertEquals("doc", doc("O.foo"))

0 commit comments

Comments
 (0)