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
This commit is contained in:
Timothee Cour
2021-04-16 05:55:51 -07:00
committed by GitHub
parent fdd4391534
commit 1b65b9cc19
2 changed files with 45 additions and 13 deletions

View File

@@ -1461,7 +1461,13 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext, fromStmtList = false) =
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)

View File

@@ -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}"
@@ -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"""
@@ -86,7 +86,6 @@ foo(1, 2, 3):
foo(1, 2): 3
do: 4
doAssert a == """
foo(1, 2):
3
do:
@@ -98,7 +97,6 @@ do:
do: 3
do: 4
doAssert a == """
foo(1):
3
do:
@@ -118,7 +116,6 @@ do:
4
doAssert a == """
foo(1):
3
do:
@@ -135,7 +132,6 @@ do:
do: 3
do: 4
doAssert a == """
foo:
1
do:
@@ -145,13 +141,44 @@ 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
@@ -159,8 +186,7 @@ proc `'foo`(): int =
discard
proc `foo bar baz`(): int =
discard
"""
discard"""
doAssert a2 == a
block: # setters: `foo=`