Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion compiler/renderer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1454,7 +1454,13 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
put(g, tkSpaces, Space)
putWithSpace(g, tkEquals, "=")
gsub(g, n, 1)
of nkStmtList, nkStmtListExpr, nkStmtListType: gstmts(g, n, emptyContext)
of nkStmtList, nkStmtListExpr, nkStmtListType:
if n.len == 1 and n[0].kind == nkDiscardStmt:
put(g, tkParLe, "(")
gsub(g, n[0])
put(g, tkParRi, ")")
else:
gstmts(g, n, emptyContext)
of nkIfStmt:
putWithSpace(g, tkIf, "if")
gif(g, n)
Expand Down
50 changes: 38 additions & 12 deletions tests/stdlib/trepr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ discard """

# if excessive, could remove 'cpp' from targets

from strutils import endsWith, contains
from strutils import endsWith, contains, strip
from std/macros import newLit
macro deb(a): string = newLit a.repr

macro deb(a): string = newLit a.repr.strip

template main() =
doAssert repr({3,5}) == "{3, 5}"
Expand Down Expand Up @@ -67,17 +68,16 @@ template main() =
else:
doAssert reprOpenarray(arr) == "[1, 2, 3]"

block: # bug #17292
block: # bug #17292 repr with `do`
template foo(a, b, c, d) = discard
block:
let a = deb:
foo(1, 2, 3, 4)
doAssert a == "\nfoo(1, 2, 3, 4)"
doAssert a == "foo(1, 2, 3, 4)"
block:
let a = deb:
foo(1, 2, 3): 4
doAssert a == """

foo(1, 2, 3):
4"""

Expand All @@ -86,7 +86,6 @@ foo(1, 2, 3):
foo(1, 2): 3
do: 4
doAssert a == """

foo(1, 2):
3
do:
Expand All @@ -98,7 +97,6 @@ do:
do: 3
do: 4
doAssert a == """

foo(1):
3
do:
Expand All @@ -118,7 +116,6 @@ do:
4

doAssert a == """

foo(1):
3
do:
Expand All @@ -135,7 +132,6 @@ do:
do: 3
do: 4
doAssert a == """

foo:
1
do:
Expand All @@ -145,22 +141,52 @@ do:
do:
4"""

block: # bug #17292 repr with `(discard)` (`discard` would result in illegal code)
let a = deb:
let f {.inject.} = () => (discard)
doAssert a == """
let f {.inject.} = () =>
(discard )"""

let a2 = deb:
block:
discard
discard

block:
when true: discard

# let a = b => discard # illegal
discard b => (discard) # legal

block:
return
doAssert a2 == """
block:
discard
discard
block:
when true:
discard
discard b =>
(discard )
block:
return"""

block: # bug #17292 (bug 4)
let a = deb:
proc `=destroy`() = discard
proc `'foo`(): int = discard
proc `foo bar baz`(): int = discard
let a2 = """

proc `=destroy`() =
discard

proc `'foo`(): int =
discard

proc `foo bar baz`(): int =
discard
"""
discard"""
doAssert a2 == a

static: main()
Expand Down