Skip to content

Commit eec07b4

Browse files
authored
fix several bugs with repr (#13386)
1 parent f6d45b4 commit eec07b4

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

compiler/renderer.nim

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ proc lsub(g: TSrcGen; n: PNode): int =
437437
of nkTableConstr:
438438
result = if n.len > 0: lcomma(g, n) + 2 else: len("{:}")
439439
of nkClosedSymChoice, nkOpenSymChoice:
440-
result = lsons(g, n) + len("()") + n.len - 1
440+
if n.len > 0: result += lsub(g, n[0])
441441
of nkTupleTy: result = lcomma(g, n) + len("tuple[]")
442442
of nkTupleClassTy: result = len("tuple")
443443
of nkDotExpr: result = lsons(g, n) + 1
@@ -529,10 +529,12 @@ proc lsub(g: TSrcGen; n: PNode): int =
529529
if n[0].kind != nkEmpty: result = result + lsub(g, n[0]) + 2
530530
of nkExceptBranch:
531531
result = lcomma(g, n, 0, -2) + lsub(g, lastSon(n)) + len("except_:_")
532+
of nkObjectTy:
533+
result = len("object_")
532534
else: result = MaxLineLen + 1
533535

534536
proc fits(g: TSrcGen, x: int): bool =
535-
result = x + g.lineLen <= MaxLineLen
537+
result = x <= MaxLineLen
536538

537539
type
538540
TSubFlag = enum
@@ -572,7 +574,7 @@ proc gcommaAux(g: var TSrcGen, n: PNode, ind: int, start: int = 0,
572574
for i in start..n.len + theEnd:
573575
var c = i < n.len + theEnd
574576
var sublen = lsub(g, n[i]) + ord(c)
575-
if not fits(g, sublen) and (ind + sublen < MaxLineLen): optNL(g, ind)
577+
if not fits(g, g.lineLen + sublen) and (ind + sublen < MaxLineLen): optNL(g, ind)
576578
let oldLen = g.tokens.len
577579
gsub(g, n[i])
578580
if c:
@@ -1139,10 +1141,12 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
11391141
putWithSpace(g, tkColon, ":")
11401142
gsub(g, n, 1)
11411143
of nkInfix:
1144+
let oldLineLen = g.lineLen # we cache this because lineLen gets updated below
11421145
infixArgument(g, n, 1)
11431146
put(g, tkSpaces, Space)
11441147
gsub(g, n, 0) # binary operator
1145-
if n.len == 3 and not fits(g, lsub(g, n[2]) + lsub(g, n[0]) + 1):
1148+
# eg: `n1 == n2` decompses as following sum:
1149+
if n.len == 3 and not fits(g, oldLineLen + lsub(g, n[1]) + lsub(g, n[2]) + lsub(g, n[0]) + len(" ")):
11461150
optNL(g, g.indent + longIndentWid)
11471151
else:
11481152
put(g, tkSpaces, Space)

tests/errmsgs/t8434.nim

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
discard """
22
errormsg: "type mismatch: got <byte, int literal(0)>"
33
nimout: '''but expected one of:
4-
proc fun0[T1: int | float |
5-
object | array | seq](a1: T1; a2: int)
4+
proc fun0[T1: int | float | object | array | seq](a1: T1; a2: int)
65
first type mismatch at position: 1
76
required type for a1: T1: int or float or object or array or seq[T]
87
but expression 'byte(1)' is of type: byte

tests/macros/tdumpast.nim

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,20 @@ dumpAST:
3131

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

34+
macro fun() =
35+
let n = quote do:
36+
1+1 == 2
37+
doAssert n.repr == "1 + 1 == 2", n.repr
38+
fun()
39+
40+
macro fun2(): untyped =
41+
let n = quote do:
42+
1 + 2 * 3 == 1 + 6
43+
doAssert n.repr == "1 + 2 * 3 == 1 + 6", n.repr
44+
fun2()
45+
46+
macro fun3(): untyped =
47+
let n = quote do:
48+
int | float | array | seq | object | ptr | pointer | float32
49+
doAssert n.repr == "int | float | array | seq | object | ptr | pointer | float32", n.repr
50+
fun3()

tests/stdlib/tunittesttemplate.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
discard """
22
exitcode: 1
33
outputsub: '''
4-
tunittesttemplate.nim(20, 12): Check failed: a.b ==
5-
2
4+
tunittesttemplate.nim(20, 12): Check failed: a.b == 2
65
a.b was 0
76
[FAILED] 1
87
'''
98
"""
109

10+
1111
# bug #6736
1212

1313
import unittest

0 commit comments

Comments
 (0)