Skip to content

Commit 1b65b9c

Browse files
authored
refs #17292 fix repr: (discard) now does't render as discard which gave illegal code (#17455)
* refs #17292 fix `repr` with (discard) * add tests * add more tests
1 parent fdd4391 commit 1b65b9c

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

compiler/renderer.nim

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1461,7 +1461,13 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext, fromStmtList = false) =
14611461
put(g, tkSpaces, Space)
14621462
putWithSpace(g, tkEquals, "=")
14631463
gsub(g, n, 1)
1464-
of nkStmtList, nkStmtListExpr, nkStmtListType: gstmts(g, n, emptyContext)
1464+
of nkStmtList, nkStmtListExpr, nkStmtListType:
1465+
if n.len == 1 and n[0].kind == nkDiscardStmt:
1466+
put(g, tkParLe, "(")
1467+
gsub(g, n[0])
1468+
put(g, tkParRi, ")")
1469+
else:
1470+
gstmts(g, n, emptyContext)
14651471
of nkIfStmt:
14661472
putWithSpace(g, tkIf, "if")
14671473
gif(g, n)

tests/stdlib/trepr.nim

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ discard """
55

66
# if excessive, could remove 'cpp' from targets
77

8-
from strutils import endsWith, contains
8+
from strutils import endsWith, contains, strip
99
from std/macros import newLit
10-
macro deb(a): string = newLit a.repr
10+
11+
macro deb(a): string = newLit a.repr.strip
1112

1213
template main() =
1314
doAssert repr({3,5}) == "{3, 5}"
@@ -67,17 +68,16 @@ template main() =
6768
else:
6869
doAssert reprOpenarray(arr) == "[1, 2, 3]"
6970

70-
block: # bug #17292
71+
block: # bug #17292 repr with `do`
7172
template foo(a, b, c, d) = discard
7273
block:
7374
let a = deb:
7475
foo(1, 2, 3, 4)
75-
doAssert a == "\nfoo(1, 2, 3, 4)"
76+
doAssert a == "foo(1, 2, 3, 4)"
7677
block:
7778
let a = deb:
7879
foo(1, 2, 3): 4
7980
doAssert a == """
80-
8181
foo(1, 2, 3):
8282
4"""
8383

@@ -86,7 +86,6 @@ foo(1, 2, 3):
8686
foo(1, 2): 3
8787
do: 4
8888
doAssert a == """
89-
9089
foo(1, 2):
9190
3
9291
do:
@@ -98,7 +97,6 @@ do:
9897
do: 3
9998
do: 4
10099
doAssert a == """
101-
102100
foo(1):
103101
3
104102
do:
@@ -118,7 +116,6 @@ do:
118116
4
119117

120118
doAssert a == """
121-
122119
foo(1):
123120
3
124121
do:
@@ -135,7 +132,6 @@ do:
135132
do: 3
136133
do: 4
137134
doAssert a == """
138-
139135
foo:
140136
1
141137
do:
@@ -145,22 +141,52 @@ do:
145141
do:
146142
4"""
147143

144+
block: # bug #17292 repr with `(discard)` (`discard` would result in illegal code)
145+
let a = deb:
146+
let f {.inject.} = () => (discard)
147+
doAssert a == """
148+
let f {.inject.} = () =>
149+
(discard )"""
150+
151+
let a2 = deb:
152+
block:
153+
discard
154+
discard
155+
156+
block:
157+
when true: discard
158+
159+
# let a = b => discard # illegal
160+
discard b => (discard) # legal
161+
162+
block:
163+
return
164+
doAssert a2 == """
165+
block:
166+
discard
167+
discard
168+
block:
169+
when true:
170+
discard
171+
discard b =>
172+
(discard )
173+
block:
174+
return"""
175+
148176
block: # bug #17292 (bug 4)
149177
let a = deb:
150178
proc `=destroy`() = discard
151179
proc `'foo`(): int = discard
152180
proc `foo bar baz`(): int = discard
153181
let a2 = """
154-
155182
proc `=destroy`() =
156183
discard
157184
158185
proc `'foo`(): int =
159186
discard
160187
161188
proc `foo bar baz`(): int =
162-
discard
163-
"""
189+
discard"""
164190
doAssert a2 == a
165191

166192
block: # setters: `foo=`

0 commit comments

Comments
 (0)