summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--changelog.md2
-rw-r--r--lib/system.nim3
-rw-r--r--lib/system/repr.nim3
-rw-r--r--lib/system/reprjs.nim3
-rw-r--r--tests/arc/trepr.nim3
-rw-r--r--tests/array/tarray.nim3
-rw-r--r--tests/js/trepr.nim73
-rw-r--r--tests/metatype/tmetatype_various.nim3
-rw-r--r--tests/misc/tunsignedconv.nim3
-rw-r--r--tests/stdlib/trepr.nim99
-rw-r--r--tests/stdlib/tstreams.nim1
-rw-r--r--tests/vm/tmisc_vm.nim5
12 files changed, 111 insertions, 90 deletions
diff --git a/changelog.md b/changelog.md
index d53f5b038..4c18ddf1e 100644
--- a/changelog.md
+++ b/changelog.md
@@ -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
 
diff --git a/lib/system.nim b/lib/system.nim
index 1104cb17f..716eb296a 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -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.
diff --git a/lib/system/repr.nim b/lib/system/repr.nim
index 318e95ebb..ae51a4aab 100644
--- a/lib/system/repr.nim
+++ b/lib/system/repr.nim
@@ -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)
diff --git a/lib/system/reprjs.nim b/lib/system/reprjs.nim
index 9c27a4721..36972024a 100644
--- a/lib/system/reprjs.nim
+++ b/lib/system/reprjs.nim
@@ -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"
diff --git a/tests/arc/trepr.nim b/tests/arc/trepr.nim
index 9eb2ed43f..3c1e4129c 100644
--- a/tests/arc/trepr.nim
+++ b/tests/arc/trepr.nim
@@ -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
diff --git a/tests/array/tarray.nim b/tests/array/tarray.nim
index 27288bab3..eadb53ac1 100644
--- a/tests/array/tarray.nim
+++ b/tests/array/tarray.nim
@@ -1,11 +1,8 @@
 discard """
 output: '''
 [4, 5, 6]
-
 [16, 25, 36]
-
 [16, 25, 36]
-
 apple
 banana
 Fruit
diff --git a/tests/js/trepr.nim b/tests/js/trepr.nim
index eb285f70d..c63b64d3a 100644
--- a/tests/js/trepr.nim
+++ b/tests/js/trepr.nim
@@ -1,6 +1,4 @@
-discard """
-  action: run
-"""
+# xxx consider merging with `tests/stdlib/trepr.nim` to increase overall test coverage
 
 block ints:
   let
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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 =
@@ -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
@@ -322,8 +312,7 @@ o = [Field0 = [a = "",
 b = @[]],
 Field1 = ""],
 p = nil,
-q = nil]
-""")
+q = nil]""")
   doAssert(repr(cc) == """
 [a = 12,
 b = 1,
@@ -346,8 +335,7 @@ o = [Field0 = [a = "inner",
 b = @['o', 'b', 'j']],
 Field1 = "tuple!"],
 p = 0,
-q = "cstringtest"]
-""")
+q = "cstringtest"]""")
 
 block another:
   type
@@ -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:
 
@@ -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,
@@ -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'}")
diff --git a/tests/metatype/tmetatype_various.nim b/tests/metatype/tmetatype_various.nim
index 0dd948293..9faeeec4a 100644
--- a/tests/metatype/tmetatype_various.nim
+++ b/tests/metatype/tmetatype_various.nim
@@ -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:
diff --git a/tests/misc/tunsignedconv.nim b/tests/misc/tunsignedconv.nim
index e9a2a2189..b9463b5f0 100644
--- a/tests/misc/tunsignedconv.nim
+++ b/tests/misc/tunsignedconv.nim
@@ -67,8 +67,7 @@ let limit = 1'u64
 let rangeVar = 0'u64 ..< limit
 
 doAssert repr(rangeVar) == """[a = 0,
-b = 0]
-"""
+b = 0]"""
 
 # bug #15210
 
diff --git a/tests/stdlib/trepr.nim b/tests/stdlib/trepr.nim
index c1941bd38..1d89d37b2 100644
--- a/tests/stdlib/trepr.nim
+++ b/tests/stdlib/trepr.nim
@@ -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()
diff --git a/tests/stdlib/tstreams.nim b/tests/stdlib/tstreams.nim
index 6979bae38..0fe6b6933 100644
--- a/tests/stdlib/tstreams.nim
+++ b/tests/stdlib/tstreams.nim
@@ -4,7 +4,6 @@ discard """
 Hello! What is your name?
 Nice name: Arne
 fs is: nil
-
 threw exception
 _heh_
 '''
diff --git a/tests/vm/tmisc_vm.nim b/tests/vm/tmisc_vm.nim
index 6eeecb869..2d3e30c5e 100644
--- a/tests/vm/tmisc_vm.nim
+++ b/tests/vm/tmisc_vm.nim
@@ -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)
 '''