fix several bugs with repr (#13386)

This commit is contained in:
Timothee Cour 2020-02-11 12:33:06 -08:00 • committed by GitHub
commit eec07b4e84
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 8 deletions

View file

@ -437,7 +437,7 @@ proc lsub(g: TSrcGen; n: PNode): int =
of nkTableConstr: of nkTableConstr:
result = if n.len > 0: lcomma(g, n) + 2 else: len("{:}") result = if n.len > 0: lcomma(g, n) + 2 else: len("{:}")
of nkClosedSymChoice, nkOpenSymChoice: 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 nkTupleTy: result = lcomma(g, n) + len("tuple[]")
of nkTupleClassTy: result = len("tuple") of nkTupleClassTy: result = len("tuple")
of nkDotExpr: result = lsons(g, n) + 1 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 if n[0].kind != nkEmpty: result = result + lsub(g, n[0]) + 2
of nkExceptBranch: of nkExceptBranch:
result = lcomma(g, n, 0, -2) + lsub(g, lastSon(n)) + len("except_:_") result = lcomma(g, n, 0, -2) + lsub(g, lastSon(n)) + len("except_:_")
of nkObjectTy:
result = len("object_")
else: result = MaxLineLen + 1 else: result = MaxLineLen + 1
proc fits(g: TSrcGen, x: int): bool = proc fits(g: TSrcGen, x: int): bool =
result = x + g.lineLen <= MaxLineLen result = x <= MaxLineLen
type type
TSubFlag = enum 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: for i in start..n.len + theEnd:
var c = i < n.len + theEnd var c = i < n.len + theEnd
var sublen = lsub(g, n[i]) + ord(c) 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 let oldLen = g.tokens.len
gsub(g, n[i]) gsub(g, n[i])
if c: if c:
@ -1139,10 +1141,12 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
putWithSpace(g, tkColon, ":") putWithSpace(g, tkColon, ":")
gsub(g, n, 1) gsub(g, n, 1)
of nkInfix: of nkInfix:
let oldLineLen = g.lineLen # we cache this because lineLen gets updated below
infixArgument(g, n, 1) infixArgument(g, n, 1)
put(g, tkSpaces, Space) put(g, tkSpaces, Space)
gsub(g, n, 0) # binary operator 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) optNL(g, g.indent + longIndentWid)
else: else:
put(g, tkSpaces, Space) put(g, tkSpaces, Space)

View file

@ -1,8 +1,7 @@
discard """ discard """
errormsg: "type mismatch: got <byte, int literal(0)>" errormsg: "type mismatch: got <byte, int literal(0)>"
nimout: '''but expected one of: nimout: '''but expected one of:
proc fun0[T1: int | float | proc fun0[T1: int | float | object | array | seq](a1: T1; a2: int)
object | array | seq](a1: T1; a2: int)
first type mismatch at position: 1 first type mismatch at position: 1
required type for a1: T1: int or float or object or array or seq[T] required type for a1: T1: int or float or object or array or seq[T]
but expression 'byte(1)' is of type: byte but expression 'byte(1)' is of type: byte

View file

@ -31,3 +31,20 @@ dumpAST:
proc sub(x, y: int): int = return x - y 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()

View file

@ -1,13 +1,13 @@
discard """ discard """
exitcode: 1 exitcode: 1
outputsub: ''' outputsub: '''
tunittesttemplate.nim(20, 12): Check failed: a.b == tunittesttemplate.nim(20, 12): Check failed: a.b == 2
2
a.b was 0 a.b was 0
[FAILED] 1 [FAILED] 1
''' '''
""" """
# bug #6736 # bug #6736
import unittest import unittest