summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2020-02-11 12:33:06 -0800
committerGitHub <noreply@github.com>2020-02-11 21:33:06 +0100
commiteec07b4e84d3418d171cf08aa8a02f380a39b78e (patch)
tree6c240454769f5d82e9febb3e79e13586d56a3319
parentf6d45b40a51d8b336acaae4cddcbca825e11c0ba (diff)
downloadNim-eec07b4e84d3418d171cf08aa8a02f380a39b78e.tar.gz
fix several bugs with `repr` (#13386)
-rw-r--r--compiler/renderer.nim12
-rw-r--r--tests/errmsgs/t8434.nim3
-rw-r--r--tests/macros/tdumpast.nim17
-rw-r--r--tests/stdlib/tunittesttemplate.nim4
4 files changed, 28 insertions, 8 deletions
diff --git a/compiler/renderer.nim b/compiler/renderer.nim
index d9d4a9bc9..8840e08be 100644
--- a/compiler/renderer.nim
+++ b/compiler/renderer.nim
@@ -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
@@ -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 = x <= MaxLineLen
 
 type
   TSubFlag = enum
@@ -572,7 +574,7 @@ proc gcommaAux(g: var TSrcGen, n: PNode, ind: int, start: int = 0,
   for i in start..n.len + theEnd:
     var c = i < n.len + theEnd
     var sublen = lsub(g, n[i]) + ord(c)
-    if not fits(g, sublen) and (ind + sublen < MaxLineLen): optNL(g, ind)
+    if not fits(g, g.lineLen + sublen) and (ind + sublen < MaxLineLen): optNL(g, ind)
     let oldLen = g.tokens.len
     gsub(g, n[i])
     if c:
@@ -1139,10 +1141,12 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
     putWithSpace(g, tkColon, ":")
     gsub(g, n, 1)
   of nkInfix:
+    let oldLineLen = g.lineLen # we cache this because lineLen gets updated below
     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):
+    # eg: `n1 == n2` decompses as following sum:
+    if n.len == 3 and not fits(g, oldLineLen + lsub(g, n[1]) + lsub(g, n[2]) + lsub(g, n[0]) + len("  ")):
       optNL(g, g.indent + longIndentWid)
     else:
       put(g, tkSpaces, Space)
diff --git a/tests/errmsgs/t8434.nim b/tests/errmsgs/t8434.nim
index ada38e9c0..c8bc1193b 100644
--- a/tests/errmsgs/t8434.nim
+++ b/tests/errmsgs/t8434.nim
@@ -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
diff --git a/tests/macros/tdumpast.nim b/tests/macros/tdumpast.nim
index 3a26ffd2f..b9d224ab8 100644
--- a/tests/macros/tdumpast.nim
+++ b/tests/macros/tdumpast.nim
@@ -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 | float | array | seq | object | ptr | pointer | float32
+  doAssert n.repr == "int | float | array | seq | object | ptr | pointer | float32", n.repr
+fun3()
diff --git a/tests/stdlib/tunittesttemplate.nim b/tests/stdlib/tunittesttemplate.nim
index 5ac323a36..2ca50a18b 100644
--- a/tests/stdlib/tunittesttemplate.nim
+++ b/tests/stdlib/tunittesttemplate.nim
@@ -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
e='author genotrance <dev@genotrance.com> 2018-04-10 11:50:23 -0500 committer Andreas Rumpf <rumpf_a@web.de> 2018-04-10 18:50:23 +0200 Add a few useful os calls to nimscript (#7442)' href='/ahoang/Nim/commit/compiler/scriptconfig.nim?h=devel&id=f6c8f97fe8bd22e7a2c1b9463f52747e98693682'>f6c8f97fe ^
69b32637b ^
a480bebfc ^


178275f49 ^
089506498 ^
178275f49 ^

4790b6d63 ^

178275f49 ^




69b32637b ^


342e50e26 ^
69b32637b ^
342e50e26 ^
69b32637b ^
342e50e26 ^
69b32637b ^
342e50e26 ^
69b32637b ^





178275f49 ^

69b32637b ^
342e50e26 ^
1b7d8246c ^

342e50e26 ^
4c4f6541d ^
342e50e26 ^

1b7d8246c ^
342e50e26 ^
1b7d8246c ^
342e50e26 ^
69b32637b ^
342e50e26 ^
69b32637b ^
342e50e26 ^
e2267ef5c ^
54bd728c1 ^
342e50e26 ^
e2267ef5c ^
54bd728c1 ^
342e50e26 ^
5e82ffc8d ^


0867c62de ^
342e50e26 ^
0867c62de ^

342e50e26 ^
8882b062e ^

f04d21f27 ^
342e50e26 ^
69b32637b ^
9e6fb3f69 ^
342e50e26 ^

b92fcacb9 ^
40ec7be45 ^
826c1e2d7 ^
342e50e26 ^
69b32637b ^
342e50e26 ^

b92fcacb9 ^

69b32637b ^
342e50e26 ^
69b32637b ^
9e6fb3f69 ^
69b32637b ^
382bc34f9 ^
69b32637b ^
826c1e2d7 ^

69b32637b ^

342e50e26 ^
d80f16338 ^

342e50e26 ^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179