diff --git a/compiler/parser.nim b/compiler/parser.nim
index d00a17e9fe..b089614b2e 100644
--- a/compiler/parser.nim
+++ b/compiler/parser.nim
@@ -1795,6 +1795,13 @@ proc parseRoutine(p: var Parser, kind: TNodeKind): PNode =
else:
result.add(p.emptyNode)
indAndComment(p, result, maybeMissEquals)
+ let body = result[^1]
+ if body.kind == nkStmtList and body.len > 0 and body[0].comment.len > 0 and body[0].kind != nkCommentStmt:
+ if result.comment.len == 0:
+ # proc fn*(a: int): int = a ## foo
+ # => moves comment `foo` to `fn`
+ swap(result.comment, body[0].comment)
+ else: discard # xxx either `assert false` or issue a warning (otherwise we'll never know of this edge case)
proc newCommentStmt(p: var Parser): PNode =
#| commentStmt = COMMENT
diff --git a/nimdoc/testproject/expected/subdir/subdir_b/utils.html b/nimdoc/testproject/expected/subdir/subdir_b/utils.html
index 939495ad12..533cb5b9d8 100644
--- a/nimdoc/testproject/expected/subdir/subdir_b/utils.html
+++ b/nimdoc/testproject/expected/subdir/subdir_b/utils.html
@@ -115,7 +115,52 @@ window.addEventListener('DOMContentLoaded', main);
Procs
- someType
+
+
+
+
+
+
+
+
+
+ someType
- someType(): SomeType
@@ -181,6 +226,69 @@ window.addEventListener('DOMContentLoaded', main);
+
+proc fn2() {....raises: [], tags: [].}
+-
+
+comment
+
+
+
+proc fn3(): auto {....raises: [], tags: [].}
+-
+
+comment
+
+
+
+proc fn4(): auto {....raises: [], tags: [].}
+-
+
+comment
+
+
+
+proc fn5() {....raises: [], tags: [].}
+-
+
+comment
+
+
+
+proc fn6() {....raises: [], tags: [].}
+-
+
+comment
+
+
+
+proc fn7() {....raises: [], tags: [].}
+-
+
+comment
+
+
+
+proc fn8(): auto {....raises: [], tags: [].}
+-
+
+comment
+
+
+
+func fn9(a: int): int {....raises: [], tags: [].}
+-
+
+comment
+
+
+
+func fn10(a: int): int {....raises: [], tags: [].}
+-
+
+comment
+
+
proc someType(): SomeType {....raises: [], tags: [].}
-
diff --git a/nimdoc/testproject/expected/subdir/subdir_b/utils.idx b/nimdoc/testproject/expected/subdir/subdir_b/utils.idx
index 6dc3953af0..a87393f095 100644
--- a/nimdoc/testproject/expected/subdir/subdir_b/utils.idx
+++ b/nimdoc/testproject/expected/subdir/subdir_b/utils.idx
@@ -3,6 +3,15 @@ enumValueB subdir/subdir_b/utils.html#enumValueB SomeType.enumValueB
enumValueC subdir/subdir_b/utils.html#enumValueC SomeType.enumValueC
SomeType subdir/subdir_b/utils.html#SomeType utils: SomeType
someType subdir/subdir_b/utils.html#someType_2 utils: someType(): SomeType
+fn2 subdir/subdir_b/utils.html#fn2 utils: fn2()
+fn3 subdir/subdir_b/utils.html#fn3 utils: fn3(): auto
+fn4 subdir/subdir_b/utils.html#fn4 utils: fn4(): auto
+fn5 subdir/subdir_b/utils.html#fn5 utils: fn5()
+fn6 subdir/subdir_b/utils.html#fn6 utils: fn6()
+fn7 subdir/subdir_b/utils.html#fn7 utils: fn7()
+fn8 subdir/subdir_b/utils.html#fn8 utils: fn8(): auto
+fn9 subdir/subdir_b/utils.html#fn9,int utils: fn9(a: int): int
+fn10 subdir/subdir_b/utils.html#fn10,int utils: fn10(a: int): int
aEnum subdir/subdir_b/utils.html#aEnum.t utils: aEnum(): untyped
bEnum subdir/subdir_b/utils.html#bEnum.t utils: bEnum(): untyped
fromUtilsGen subdir/subdir_b/utils.html#fromUtilsGen.t utils: fromUtilsGen(): untyped
diff --git a/nimdoc/testproject/expected/theindex.html b/nimdoc/testproject/expected/theindex.html
index cfaf6856d4..f72f6c37e1 100644
--- a/nimdoc/testproject/expected/theindex.html
+++ b/nimdoc/testproject/expected/theindex.html
@@ -171,6 +171,42 @@ window.addEventListener('DOMContentLoaded', main);
- SomeType.enumValueC
+- fn10:
+- fn2:
+- fn3:
+- fn4:
+- fn5:
+- fn6:
+- fn7:
+- fn8:
+- fn9:
- Foo:
- testproject: Foo
diff --git a/nimdoc/testproject/subdir/subdir_b/utils.nim b/nimdoc/testproject/subdir/subdir_b/utils.nim
index 4e3aa1f108..37c91dd3c7 100644
--- a/nimdoc/testproject/subdir/subdir_b/utils.nim
+++ b/nimdoc/testproject/subdir/subdir_b/utils.nim
@@ -31,6 +31,23 @@ proc someType*(): SomeType =
## constructor.
SomeType(2)
+
+proc fn2*() = discard ## comment
+proc fn3*(): auto = 1 ## comment
+proc fn4*(): auto = 2 * 3 + 4 ## comment
+proc fn5*() ## comment
+proc fn5*() = discard
+proc fn6*() =
+ ## comment
+proc fn7*() =
+ ## comment
+ discard
+proc fn8*(): auto =
+ ## comment
+ 1+1
+func fn9*(a: int): int = 42 ## comment
+func fn10*(a: int): int = a ## comment
+
# bug #9235
template aEnum*(): untyped =
diff --git a/tests/stdlib/trepr.nim b/tests/stdlib/trepr.nim
index 3567649037..83ae7b119d 100644
--- a/tests/stdlib/trepr.nim
+++ b/tests/stdlib/trepr.nim
@@ -9,6 +9,7 @@ from strutils import endsWith, contains, strip
from std/macros import newLit
macro deb(a): string = newLit a.repr.strip
+macro debTyped(a: typed): string = newLit a.repr.strip
template main() =
doAssert repr({3,5}) == "{3, 5}"
@@ -243,5 +244,31 @@ template bar(): untyped =
echo "baz"
4)"""
+ block: # one liner doc comments
+ let a1 = deb:
+ func fn1(): int = 1 ## comment
+ func fn2(): int = 1
+ ## comment
+ let a2 = debTyped:
+ func fn1(): int = 1 ## comment
+ func fn2(): int = 1
+ ## comment
+ doAssert a1 == """
+func fn1(): int =
+ ## comment
+ 1
+
+func fn2(): int =
+ ## comment
+ 1"""
+ doAssert a2 == """
+func fn1(): int =
+ ## comment
+ result = 1
+
+func fn2(): int =
+ ## comment
+ result = 1"""
+
static: main()
main()