Skip to content

Commit

Permalink
fix #16025 repr now consistent: does not insert trailing newline (#16034
Browse files Browse the repository at this point in the history
)
  • Loading branch information
timotheecour authored Nov 19, 2020
1 parent 1efd11e commit b3c3557
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 90 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

- `os.FileInfo` (returned by `getFileInfo`) now contains `blockSize`,
determining preferred I/O block size for this file object.
- `repr` now doesn't insert trailing newline; previous behavior was very inconsistent,
see #16034. Use `-d:nimLegacyReprWithNewline` for previous behavior.

## Language changes

Expand Down
3 changes: 3 additions & 0 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,9 @@ proc insert*[T](x: var seq[T], item: sink T, i = 0.Natural) {.noSideEffect.} =
when not defined(nimV2):
proc repr*[T](x: T): string {.magic: "Repr", noSideEffect.}
## Takes any Nim variable and returns its string representation.
## No trailing newline is inserted (so `echo` won't add an empty newline).
## Use `-d:nimLegacyReprWithNewline` to revert to old behavior where newlines
## were added in some cases.
##
## It works even for complex data graphs with cycles. This is a great
## debugging tool.
Expand Down
3 changes: 2 additions & 1 deletion lib/system/repr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -325,5 +325,6 @@ when not defined(useNimRtl):
else:
var p = p
reprAux(result, addr(p), typ, cl)
add result, "\n"
when defined(nimLegacyReprWithNewline): # see PR #16034
add result, "\n"
deinitReprClosure(cl)
3 changes: 2 additions & 1 deletion lib/system/reprjs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,5 @@ proc reprAny(p: pointer, typ: PNimType): string {.compilerRtl.} =
var cl: ReprClosure
initReprClosure(cl)
reprAux(result, p, typ, cl)
add(result, "\n")
when defined(nimLegacyReprWithNewline): # see PR #16034
add result, "\n"
3 changes: 3 additions & 0 deletions tests/arc/trepr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Obj(member: ref @["hello"])
ref (member: ref @["hello"])
'''
"""

# xxx consider merging with `tests/stdlib/trepr.nim` to increase overall test coverage

import tables

type
Expand Down
3 changes: 0 additions & 3 deletions tests/array/tarray.nim
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
discard """
output: '''
[4, 5, 6]
[16, 25, 36]
[16, 25, 36]
apple
banana
Fruit
Expand Down
73 changes: 29 additions & 44 deletions tests/js/trepr.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
discard """
action: run
"""
# xxx consider merging with `tests/stdlib/trepr.nim` to increase overall test coverage

block ints:
let
Expand Down Expand Up @@ -137,15 +135,13 @@ block tuples:
when defined js:
doAssert(repr(ot) == """
[Field0 = true,
Field1 = 120]
""")
Field1 = 120]""")
doAssert(repr(t) == """
[Field0 = 42,
Field1 = 12.34,
Field2 = "tuple",
Field3 = [Field0 = true,
Field1 = 120]]
""")
Field1 = 120]]""")

block objects:
type
Expand All @@ -162,14 +158,12 @@ block objects:

doAssert(repr(oo) == """
[a = true,
b = 120]
""")
b = 120]""")
doAssert(repr(o) == """
[a = 42,
b = 12.34,
c = [a = true,
b = 120]]
""")
b = 120]]""")

block arrays:
type
Expand All @@ -183,15 +177,14 @@ block arrays:
c = [o, o, o]
d = ["hi", "array", "!"]

doAssert(repr(a) == "[0.0, 1.0, 2.0]\n")
doAssert(repr(b) == "[[0.0, 1.0, 2.0], [0.0, 1.0, 2.0], [0.0, 1.0, 2.0]]\n")
doAssert(repr(a) == "[0.0, 1.0, 2.0]")
doAssert(repr(b) == "[[0.0, 1.0, 2.0], [0.0, 1.0, 2.0], [0.0, 1.0, 2.0]]")
doAssert(repr(c) == """
[[x = 42,
y = [0.0, 1.0, 2.0]], [x = 42,
y = [0.0, 1.0, 2.0]], [x = 42,
y = [0.0, 1.0, 2.0]]]
""")
doAssert(repr(d) == "[\"hi\", \"array\", \"!\"]\n")
y = [0.0, 1.0, 2.0]]]""")
doAssert(repr(d) == "[\"hi\", \"array\", \"!\"]")

block seqs:
type
Expand All @@ -205,15 +198,14 @@ block seqs:
c = @[o, o, o]
d = @["hi", "array", "!"]

doAssert(repr(a) == "@[0.0, 1.0, 2.0]\n")
doAssert(repr(b) == "@[@[0.0, 1.0, 2.0], @[0.0, 1.0, 2.0], @[0.0, 1.0, 2.0]]\n")
doAssert(repr(a) == "@[0.0, 1.0, 2.0]")
doAssert(repr(b) == "@[@[0.0, 1.0, 2.0], @[0.0, 1.0, 2.0], @[0.0, 1.0, 2.0]]")
doAssert(repr(c) == """
@[[x = 42,
y = @[0.0, 1.0, 2.0]], [x = 42,
y = @[0.0, 1.0, 2.0]], [x = 42,
y = @[0.0, 1.0, 2.0]]]
""")
doAssert(repr(d) == "@[\"hi\", \"array\", \"!\"]\n")
y = @[0.0, 1.0, 2.0]]]""")
doAssert(repr(d) == "@[\"hi\", \"array\", \"!\"]")

block ptrs:
type
Expand All @@ -226,13 +218,12 @@ block ptrs:
c = addr a[2]
d = AObj()

doAssert(repr(a) == "[12.0, 13.0, 14.0]\n")
doAssert(repr(b) == "ref 0 --> 12.0\n")
doAssert(repr(c) == "ref 2 --> 14.0\n")
doAssert(repr(a) == "[12.0, 13.0, 14.0]")
doAssert(repr(b) == "ref 0 --> 12.0")
doAssert(repr(c) == "ref 2 --> 14.0")
doAssert(repr(d) == """
[x = nil,
y = 0]
""")
y = 0]""")

block ptrs:
type
Expand All @@ -248,8 +239,7 @@ block ptrs:
[x = ref 0 --> [[x = nil,
y = 0], [x = nil,
y = 0]],
y = 0]
""")
y = 0]""")

block procs:
proc test(): int =
Expand All @@ -258,9 +248,9 @@ block procs:
ptest = test
nilproc: proc(): int

doAssert(repr(test) == "0\n")
doAssert(repr(ptest) == "0\n")
doAssert(repr(nilproc) == "nil\n")
doAssert(repr(test) == "0")
doAssert(repr(ptest) == "0")
doAssert(repr(nilproc) == "nil")

block bunch:
type
Expand Down Expand Up @@ -322,8 +312,7 @@ o = [Field0 = [a = "",
b = @[]],
Field1 = ""],
p = nil,
q = nil]
""")
q = nil]""")
doAssert(repr(cc) == """
[a = 12,
b = 1,
Expand All @@ -346,8 +335,7 @@ o = [Field0 = [a = "inner",
b = @['o', 'b', 'j']],
Field1 = "tuple!"],
p = 0,
q = "cstringtest"]
""")
q = "cstringtest"]""")

block another:
type
Expand All @@ -358,9 +346,9 @@ block another:
Size3 = enum
s3e=0, s3f=2000000000

doAssert(repr([s1a, s1b]) == "[s1a, s1b]\n")
doAssert(repr([s2c, s2d]) == "[s2c, s2d]\n")
doAssert(repr([s3e, s3f]) == "[s3e, s3f]\n")
doAssert(repr([s1a, s1b]) == "[s1a, s1b]")
doAssert(repr([s2c, s2d]) == "[s2c, s2d]")
doAssert(repr([s3e, s3f]) == "[s3e, s3f]")

block another2:

Expand Down Expand Up @@ -395,15 +383,13 @@ block another2:
y = 13,
z = 45,
s = ["abc", "xyz"],
e = en6]
""")
e = en6]""")
doAssert(repr(q) == """
ref 0 --> [x = 0,
y = 13,
z = 45,
s = ["abc", "xyz"],
e = en6]
""")
e = en6]""")
doAssert(repr(s) == """
@[ref 0 --> [x = 0,
y = 13,
Expand All @@ -421,8 +407,7 @@ e = en6], ref 3 --> [x = 0,
y = 13,
z = 45,
s = ["abc", "xyz"],
e = en6]]
""")
e = en6]]""")
doAssert(repr(en4) == "en4")

doAssert(repr({'a'..'p'}) == "{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'}")
3 changes: 1 addition & 2 deletions tests/metatype/tmetatype_various.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
discard """
output: '''[1, 0, 0, 0, 0, 0, 0, 0]
CTBool[Ct[system.uint32]]'''
output: '''[1, 0, 0, 0, 0, 0, 0, 0] CTBool[Ct[system.uint32]]'''
"""

block tconstraints:
Expand Down
3 changes: 1 addition & 2 deletions tests/misc/tunsignedconv.nim
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ let limit = 1'u64
let rangeVar = 0'u64 ..< limit

doAssert repr(rangeVar) == """[a = 0,
b = 0]
"""
b = 0]"""

# bug #15210

Expand Down
99 changes: 66 additions & 33 deletions tests/stdlib/trepr.nim
Original file line number Diff line number Diff line change
@@ -1,36 +1,69 @@
discard """
output: '''{a, b}{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}
[1, 2, 3, 4, 5, 6]'''
targets: "c cpp js"
matrix: ";--gc:arc"
"""

type
TEnum = enum
a, b

var val = {a, b}
stdout.write(repr(val))
stdout.writeLine(repr({'a'..'z', 'A'..'Z'}))

type
TObj {.pure, inheritable.} = object
data: int
TFoo = ref object of TObj
d2: float
var foo: TFoo
new(foo)

when false:
# cannot capture this output as it contains a memory address :-/
echo foo.repr
#var testseq: seq[string] = @[
# "a", "b", "c", "d", "e"
#]
#echo(repr(testseq))

# bug #7878
proc test(variable: var openarray[int]) =
echo repr(variable)

var arr = [1, 2, 3, 4, 5, 6]

test(arr)
# if excessive, could remove 'cpp' from targets

from strutils import endsWith, contains

template main() =
doAssert repr({3,5}) == "{3, 5}"

block:
type TEnum = enum a, b
var val = {a, b}
when nimvm:
discard
#[
# BUG:
{0, 1}
{97..99, 65..67}
]#
else:
doAssert repr(val) == "{a, b}"
doAssert repr({'a'..'c', 'A'..'C'}) == "{'A', 'B', 'C', 'a', 'b', 'c'}"

type
TObj {.pure, inheritable.} = object
data: int
TFoo = ref object of TObj
d2: float
var foo: TFoo
new(foo)

#[
BUG:
--gc:arc returns `"abc"`
regular gc returns with address, e.g. 0x1068aae60"abc", but only
for c,cpp backends (not js, vm)
]#
block:
doAssert repr("abc").endsWith "\"abc\""
var b: cstring = "def"
doAssert repr(b).endsWith "\"def\""

block:
var c = @[1,2]
when nimvm:
discard # BUG: this shows [1, 2] instead of @[1, 2]
else:
# BUG (already mentioned above): some backends / gc show address, others don't
doAssert repr(c).endsWith "@[1, 2]"

let d = @["foo", "bar"]
let s = repr(d)
# depending on backend/gc, we get 0x106a1c350@[0x106a1c390"foo", 0x106a1c3c0"bar"]
doAssert "\"foo\"," in s

var arr = [1, 2, 3]
doAssert repr(arr) == "[1, 2, 3]"

block: # bug #7878
proc reprOpenarray(variable: var openarray[int]): string = repr(variable)
when defined(js): discard # BUG: doesn't work
else:
doAssert reprOpenarray(arr) == "[1, 2, 3]"

static: main()
main()
1 change: 0 additions & 1 deletion tests/stdlib/tstreams.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ discard """
Hello! What is your name?
Nice name: Arne
fs is: nil
threw exception
_heh_
'''
Expand Down
5 changes: 2 additions & 3 deletions tests/vm/tmisc_vm.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
discard """
output: '''[127, 127, 0, 255]
[127, 127, 0, 255]
output: '''
[127, 127, 0, 255][127, 127, 0, 255]
(data: 1)
'''
Expand Down

0 comments on commit b3c3557

Please sign in to comment.