Fix #14270 and add testcases (#14276)

This commit is contained in:
Clyybber 2020-05-08 15:54:34 +02:00 • committed by GitHub
commit 120ea906fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 2 deletions

View file

@ -106,8 +106,12 @@ proc repr*[T: tuple|object](x: T): string =
proc repr*[T](x: ref T | ptr T): string = proc repr*[T](x: ref T | ptr T): string =
if isNil(x): return "nil" if isNil(x): return "nil"
result = $typeof(x) when T is object:
reprObject(result, x[]) result = $typeof(x)
reprObject(result, x[])
else:
result = when typeof(x) is ref: "ref " else: "ptr "
result.add repr(x[])
proc collectionToRepr[T](x: T, prefix, separator, suffix: string): string = proc collectionToRepr[T](x: T, prefix, separator, suffix: string): string =
result = prefix result = prefix

View file

@ -3,6 +3,12 @@ discard """
nimout: '''(a: true, n: doAssert) nimout: '''(a: true, n: doAssert)
Table[system.string, trepr.MyType](data: @[], counter: 0) Table[system.string, trepr.MyType](data: @[], counter: 0)
nil nil
'''
output: '''
nil
2
Obj(member: ref @[hello])
ref (member: ref @[hello])
''' '''
""" """
import tables import tables
@ -32,3 +38,27 @@ macro dumpSym(a: typed) =
dumpSym(doAssert) dumpSym(doAssert)
# bug 13731
import os
var a: File
echo repr a
# bug 13872
echo repr(2'u16)
# bug 14270
type
Obj = ref object
member: ref seq[string]
var c = Obj(member: new seq[string])
c.member[] = @["hello"]
echo c.repr
var c2 = new tuple[member: ref seq[string]]
c2.member = new seq[string]
c2.member[] = @["hello"]
echo c2.repr