Skip to content

Commit

Permalink
fix repr for infix expressions, object, symChoice
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Feb 11, 2020
1 parent 1f7c907 commit 79272a6
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
9 changes: 6 additions & 3 deletions compiler/renderer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ proc lsub(g: TSrcGen; n: PNode): int =
of nkTableConstr:
result = if n.len > 0: lcomma(g, n) + 2 else: len("{:}")
of nkClosedSymChoice, nkOpenSymChoice:
result = lsons(g, n) + len("()") + n.len - 1
if n.len > 0: result += lsub(g, n[0])
of nkTupleTy: result = lcomma(g, n) + len("tuple[]")
of nkTupleClassTy: result = len("tuple")
of nkDotExpr: result = lsons(g, n) + 1
Expand Down Expand Up @@ -529,10 +529,12 @@ proc lsub(g: TSrcGen; n: PNode): int =
if n[0].kind != nkEmpty: result = result + lsub(g, n[0]) + 2
of nkExceptBranch:
result = lcomma(g, n, 0, -2) + lsub(g, lastSon(n)) + len("except_:_")
of nkObjectTy:
result = len("object_")
else: result = MaxLineLen + 1

proc fits(g: TSrcGen, x: int): bool =
result = x + g.lineLen <= MaxLineLen
result = g.indent + x <= MaxLineLen

type
TSubFlag = enum
Expand Down Expand Up @@ -1142,7 +1144,8 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
infixArgument(g, n, 1)
put(g, tkSpaces, Space)
gsub(g, n, 0) # binary operator
if n.len == 3 and not fits(g, lsub(g, n[2]) + lsub(g, n[0]) + 1):
# will overfit for larger operators than "+", eg "<="
if n.len == 3 and not fits(g, lsub(g, n[1]) + lsub(g, n[2]) + len(" + ")):
optNL(g, g.indent + longIndentWid)
else:
put(g, tkSpaces, Space)
Expand Down
3 changes: 1 addition & 2 deletions tests/errmsgs/t8434.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
discard """
errormsg: "type mismatch: got <byte, int literal(0)>"
nimout: '''but expected one of:
proc fun0[T1: int | float |
object | array | seq](a1: T1; a2: int)
proc fun0[T1: int | float | object | array | seq](a1: T1; a2: int)
first type mismatch at position: 1
required type for a1: T1: int or float or object or array or seq[T]
but expression 'byte(1)' is of type: byte
Expand Down
4 changes: 1 addition & 3 deletions tests/errmsgs/tgcsafety.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ errormsg: "type mismatch: got <AsyncHttpServer, Port, proc (req: Request): Futur
nimout: '''
type mismatch: got <AsyncHttpServer, Port, proc (req: Request): Future[system.void]{.locks: <unknown>.}>
but expected one of:
proc serve(server: AsyncHttpServer; port: Port;
callback: proc (request: Request): Future[void] {.closure, gcsafe.};
address = ""): owned(Future[void])
proc serve(server: AsyncHttpServer; port: Port; callback: proc (request: Request): Future[void] {.closure, gcsafe.}; address = ""): owned(Future[void])
first type mismatch at position: 3
required type for callback: proc (request: Request): Future[system.void]{.closure, gcsafe.}
but expression 'cb' is of type: proc (req: Request): Future[system.void]{.locks: <unknown>.}
Expand Down
17 changes: 17 additions & 0 deletions tests/macros/tdumpast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,20 @@ dumpAST:

proc sub(x, y: int): int = return x - y

macro fun() =
let n = quote do:
1+1 == 2
doAssert n.repr == "1 + 1 == 2", n.repr
fun()

macro fun2(): untyped =
let n = quote do:
1 + 2 * 3 == 1 + 6
doAssert n.repr == "1 + 2 * 3 == 1 + 6", n.repr
fun2()

macro fun3(): untyped =
let n = quote do:
int | array | seq | object | ptr | pointer | ref
doAssert n.repr == "int | array | seq | object | ptr | pointer | ref", n.repr
fun3()
4 changes: 2 additions & 2 deletions tests/stdlib/tunittesttemplate.nim
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
discard """
exitcode: 1
outputsub: '''
tunittesttemplate.nim(20, 12): Check failed: a.b ==
2
tunittesttemplate.nim(20, 12): Check failed: a.b == 2
a.b was 0
[FAILED] 1
'''
"""


# bug #6736

import unittest
Expand Down

0 comments on commit 79272a6

Please sign in to comment.