summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authormetagn <metagngn@gmail.com>2022-08-24 10:44:16 +0300
committerGitHub <noreply@github.com>2022-08-24 09:44:16 +0200
commitb8dc58d8843af7dbd0ee2f3274e0d4cf18391dd2 (patch)
tree78c2cf31185875c855de1f1457daaefbaadd930e /lib
parent9d9ecc3c1d774a71b4b866d7e503b25c307e7cbf (diff)
downloadNim-b8dc58d8843af7dbd0ee2f3274e0d4cf18391dd2.tar.gz
test removing dollar for objects out of system (#20242)
* test removing dollar for objects out of system

* test & fixes

* fix bootstrap

* use nimPreviewSlimSystem, test stdlib category

* fix test
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/asyncfutures.nim3
-rw-r--r--lib/std/objectdollar.nim11
-rw-r--r--lib/std/private/miscdollars.nim39
-rw-r--r--lib/system/dollars.nim46
4 files changed, 59 insertions, 40 deletions
diff --git a/lib/pure/asyncfutures.nim b/lib/pure/asyncfutures.nim
index 4782f0a73..e91b99c3a 100644
--- a/lib/pure/asyncfutures.nim
+++ b/lib/pure/asyncfutures.nim
@@ -11,6 +11,9 @@ import os, tables, strutils, times, heapqueue, options, deques, cstrutils
 
 import system/stacktraces
 
+when defined(nimPreviewSlimSystem):
+  import std/objectdollar # for StackTraceEntry
+
 # TODO: This shouldn't need to be included, but should ideally be exported.
 type
   CallbackFunc = proc () {.closure, gcsafe.}
diff --git a/lib/std/objectdollar.nim b/lib/std/objectdollar.nim
new file mode 100644
index 000000000..f413bbc46
--- /dev/null
+++ b/lib/std/objectdollar.nim
@@ -0,0 +1,11 @@
+import std/private/miscdollars
+
+proc `$`*[T: object](x: T): string =
+  ## Generic `$` operator for objects with similar output to
+  ## `$` for named tuples.
+  runnableExamples:
+    type Foo = object
+      a, b: int
+    let x = Foo(a: 23, b: 45)
+    assert $x == "(a: 23, b: 45)"
+  tupleObjectDollar(result, x)
diff --git a/lib/std/private/miscdollars.nim b/lib/std/private/miscdollars.nim
index 840fedf54..47b788ee9 100644
--- a/lib/std/private/miscdollars.nim
+++ b/lib/std/private/miscdollars.nim
@@ -12,3 +12,42 @@ template toLocation*(result: var string, file: string | cstring, line: int, col:
       result.add ", "
       addInt(result, col)
     result.add ")"
+
+when defined(nimHasIsNamedTuple):
+  proc isNamedTuple(T: typedesc): bool {.magic: "TypeTrait".}
+else:
+  # for bootstrap; remove after release 1.2
+  proc isNamedTuple(T: typedesc): bool =
+    # Taken from typetraits.
+    when T isnot tuple: result = false
+    else:
+      var t: T
+      for name, _ in t.fieldPairs:
+        when name == "Field0":
+          return compiles(t.Field0)
+        else:
+          return true
+      return false
+
+template tupleObjectDollar*[T: tuple | object](result: var string, x: T) =
+  result = "("
+  const isNamed = T is object or isNamedTuple(typeof(T))
+  var count {.used.} = 0
+  for name, value in fieldPairs(x):
+    if count > 0: result.add(", ")
+    when isNamed:
+      result.add(name)
+      result.add(": ")
+    count.inc
+    when compiles($value):
+      when value isnot string and value isnot seq and compiles(value.isNil):
+        if value.isNil: result.add "nil"
+        else: result.addQuoted(value)
+      else:
+        result.addQuoted(value)
+    else:
+      result.add("...")
+  when not isNamed:
+    if count == 1:
+      result.add(",") # $(1,) should print as the semantically legal (1,)
+  result.add(")")
diff --git a/lib/system/dollars.nim b/lib/system/dollars.nim
index 46085d2aa..b060aedb0 100644
--- a/lib/system/dollars.nim
+++ b/lib/system/dollars.nim
@@ -3,7 +3,7 @@ runnableExamples:
   assert $0.1 == "0.1"
   assert $(-2*3) == "-6"
 
-import std/private/digitsutils
+import std/private/[digitsutils, miscdollars]
 import system/formatfloat
 export addFloat
 
@@ -69,24 +69,7 @@ proc `$`*(t: typedesc): string {.magic: "TypeTrait".}
   ##   doAssert $(typeof("Foo")) == "string"
   ##   static: doAssert $(typeof(@['A', 'B'])) == "seq[char]"
 
-when defined(nimHasIsNamedTuple):
-  proc isNamedTuple(T: typedesc): bool {.magic: "TypeTrait".}
-else:
-  # for bootstrap; remove after release 1.2
-  proc isNamedTuple(T: typedesc): bool =
-    # Taken from typetraits.
-    when T isnot tuple: result = false
-    else:
-      var t: T
-      for name, _ in t.fieldPairs:
-        when name == "Field0":
-          return compiles(t.Field0)
-        else:
-          return true
-      return false
-
-
-proc `$`*[T: tuple|object](x: T): string =
+proc `$`*[T: tuple](x: T): string =
   ## Generic `$` operator for tuples that is lifted from the components
   ## of `x`. Example:
   ##
@@ -94,28 +77,11 @@ proc `$`*[T: tuple|object](x: T): string =
   ##   $(23, 45) == "(23, 45)"
   ##   $(a: 23, b: 45) == "(a: 23, b: 45)"
   ##   $() == "()"
-  result = "("
-  const isNamed = T is object or isNamedTuple(T)
-  var count {.used.} = 0
-  for name, value in fieldPairs(x):
-    if count > 0: result.add(", ")
-    when isNamed:
-      result.add(name)
-      result.add(": ")
-    count.inc
-    when compiles($value):
-      when value isnot string and value isnot seq and compiles(value.isNil):
-        if value.isNil: result.add "nil"
-        else: result.addQuoted(value)
-      else:
-        result.addQuoted(value)
-    else:
-      result.add("...")
-  when not isNamed:
-    if count == 1:
-      result.add(",") # $(1,) should print as the semantically legal (1,)
-  result.add(")")
+  tupleObjectDollar(result, x)
 
+when not defined(nimPreviewSlimSystem):
+  import std/objectdollar
+  export objectdollar
 
 proc collectionToString[T](x: T, prefix, separator, suffix: string): string =
   result = prefix