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
commit 1b65b9cc19
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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) put(g, tkSpaces, Space)
putWithSpace(g, tkEquals, "=") putWithSpace(g, tkEquals, "=")
gsub(g, n, 1) 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: of nkIfStmt:
putWithSpace(g, tkIf, "if") putWithSpace(g, tkIf, "if")
gif(g, n) gif(g, n)

View file

@ -5,9 +5,10 @@ discard """
# if excessive, could remove 'cpp' from targets # if excessive, could remove 'cpp' from targets
from strutils import endsWith, contains from strutils import endsWith, contains, strip
from std/macros import newLit from std/macros import newLit
macro deb(a): string = newLit a.repr
macro deb(a): string = newLit a.repr.strip
template main() = template main() =
doAssert repr({3,5}) == "{3, 5}" doAssert repr({3,5}) == "{3, 5}"
@ -67,17 +68,16 @@ template main() =
else: else:
doAssert reprOpenarray(arr) == "[1, 2, 3]" doAssert reprOpenarray(arr) == "[1, 2, 3]"
block: # bug #17292 block: # bug #17292 repr with `do`
template foo(a, b, c, d) = discard template foo(a, b, c, d) = discard
block: block:
let a = deb: let a = deb:
foo(1, 2, 3, 4) foo(1, 2, 3, 4)
doAssert a == "\nfoo(1, 2, 3, 4)" doAssert a == "foo(1, 2, 3, 4)"
block: block:
let a = deb: let a = deb:
foo(1, 2, 3): 4 foo(1, 2, 3): 4
doAssert a == """ doAssert a == """
foo(1, 2, 3): foo(1, 2, 3):
4""" 4"""
@ -86,7 +86,6 @@ foo(1, 2, 3):
foo(1, 2): 3 foo(1, 2): 3
do: 4 do: 4
doAssert a == """ doAssert a == """
foo(1, 2): foo(1, 2):
3 3
do: do:
@ -98,7 +97,6 @@ do:
do: 3 do: 3
do: 4 do: 4
doAssert a == """ doAssert a == """
foo(1): foo(1):
3 3
do: do:
@ -118,7 +116,6 @@ do:
4 4
doAssert a == """ doAssert a == """
foo(1): foo(1):
3 3
do: do:
@ -135,7 +132,6 @@ do:
do: 3 do: 3
do: 4 do: 4
doAssert a == """ doAssert a == """
foo: foo:
1 1
do: do:
@ -145,13 +141,44 @@ do:
do: do:
4""" 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) block: # bug #17292 (bug 4)
let a = deb: let a = deb:
proc `=destroy`() = discard proc `=destroy`() = discard
proc `'foo`(): int = discard proc `'foo`(): int = discard
proc `foo bar baz`(): int = discard proc `foo bar baz`(): int = discard
let a2 = """ let a2 = """
proc `=destroy`() = proc `=destroy`() =
discard discard
@ -159,8 +186,7 @@ proc `'foo`(): int =
discard discard
proc `foo bar baz`(): int = proc `foo bar baz`(): int =
discard discard"""
"""
doAssert a2 == a doAssert a2 == a
block: # setters: `foo=` block: # setters: `foo=`