Skip to content

Commit b8dc58d

Browse files
authored
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
1 parent 9d9ecc3 commit b8dc58d

File tree

9 files changed

+84
-40
lines changed

9 files changed

+84
-40
lines changed

compiler/ast.nim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,9 @@ type
747747
module*: int32
748748
item*: int32
749749

750+
proc `$`*(x: ItemId): string =
751+
"(module: " & $x.module & ", item: " & $x.item & ")"
752+
750753
proc `==`*(a, b: ItemId): bool {.inline.} =
751754
a.item == b.item and a.module == b.module
752755

compiler/passaux.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ proc verboseOpen(graph: ModuleGraph; s: PSym; idgen: IdGenerator): PPassContext
2222
# xxx consider either removing this or keeping for documentation for how to add a pass
2323
result = VerboseRef(config: graph.config, idgen: idgen)
2424

25+
import std/objectdollar
26+
2527
proc verboseProcess(context: PPassContext, n: PNode): PNode =
2628
# called from `process` in `processTopLevelStmt`.
2729
result = n

lib/pure/asyncfutures.nim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import os, tables, strutils, times, heapqueue, options, deques, cstrutils
1111

1212
import system/stacktraces
1313

14+
when defined(nimPreviewSlimSystem):
15+
import std/objectdollar # for StackTraceEntry
16+
1417
# TODO: This shouldn't need to be included, but should ideally be exported.
1518
type
1619
CallbackFunc = proc () {.closure, gcsafe.}

lib/std/objectdollar.nim

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import std/private/miscdollars
2+
3+
proc `$`*[T: object](x: T): string =
4+
## Generic `$` operator for objects with similar output to
5+
## `$` for named tuples.
6+
runnableExamples:
7+
type Foo = object
8+
a, b: int
9+
let x = Foo(a: 23, b: 45)
10+
assert $x == "(a: 23, b: 45)"
11+
tupleObjectDollar(result, x)

lib/std/private/miscdollars.nim

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,42 @@ template toLocation*(result: var string, file: string | cstring, line: int, col:
1212
result.add ", "
1313
addInt(result, col)
1414
result.add ")"
15+
16+
when defined(nimHasIsNamedTuple):
17+
proc isNamedTuple(T: typedesc): bool {.magic: "TypeTrait".}
18+
else:
19+
# for bootstrap; remove after release 1.2
20+
proc isNamedTuple(T: typedesc): bool =
21+
# Taken from typetraits.
22+
when T isnot tuple: result = false
23+
else:
24+
var t: T
25+
for name, _ in t.fieldPairs:
26+
when name == "Field0":
27+
return compiles(t.Field0)
28+
else:
29+
return true
30+
return false
31+
32+
template tupleObjectDollar*[T: tuple | object](result: var string, x: T) =
33+
result = "("
34+
const isNamed = T is object or isNamedTuple(typeof(T))
35+
var count {.used.} = 0
36+
for name, value in fieldPairs(x):
37+
if count > 0: result.add(", ")
38+
when isNamed:
39+
result.add(name)
40+
result.add(": ")
41+
count.inc
42+
when compiles($value):
43+
when value isnot string and value isnot seq and compiles(value.isNil):
44+
if value.isNil: result.add "nil"
45+
else: result.addQuoted(value)
46+
else:
47+
result.addQuoted(value)
48+
else:
49+
result.add("...")
50+
when not isNamed:
51+
if count == 1:
52+
result.add(",") # $(1,) should print as the semantically legal (1,)
53+
result.add(")")

lib/system/dollars.nim

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ runnableExamples:
33
assert $0.1 == "0.1"
44
assert $(-2*3) == "-6"
55

6-
import std/private/digitsutils
6+
import std/private/[digitsutils, miscdollars]
77
import system/formatfloat
88
export addFloat
99

@@ -69,53 +69,19 @@ proc `$`*(t: typedesc): string {.magic: "TypeTrait".}
6969
## doAssert $(typeof("Foo")) == "string"
7070
## static: doAssert $(typeof(@['A', 'B'])) == "seq[char]"
7171

72-
when defined(nimHasIsNamedTuple):
73-
proc isNamedTuple(T: typedesc): bool {.magic: "TypeTrait".}
74-
else:
75-
# for bootstrap; remove after release 1.2
76-
proc isNamedTuple(T: typedesc): bool =
77-
# Taken from typetraits.
78-
when T isnot tuple: result = false
79-
else:
80-
var t: T
81-
for name, _ in t.fieldPairs:
82-
when name == "Field0":
83-
return compiles(t.Field0)
84-
else:
85-
return true
86-
return false
87-
88-
89-
proc `$`*[T: tuple|object](x: T): string =
72+
proc `$`*[T: tuple](x: T): string =
9073
## Generic `$` operator for tuples that is lifted from the components
9174
## of `x`. Example:
9275
##
9376
## .. code-block:: Nim
9477
## $(23, 45) == "(23, 45)"
9578
## $(a: 23, b: 45) == "(a: 23, b: 45)"
9679
## $() == "()"
97-
result = "("
98-
const isNamed = T is object or isNamedTuple(T)
99-
var count {.used.} = 0
100-
for name, value in fieldPairs(x):
101-
if count > 0: result.add(", ")
102-
when isNamed:
103-
result.add(name)
104-
result.add(": ")
105-
count.inc
106-
when compiles($value):
107-
when value isnot string and value isnot seq and compiles(value.isNil):
108-
if value.isNil: result.add "nil"
109-
else: result.addQuoted(value)
110-
else:
111-
result.addQuoted(value)
112-
else:
113-
result.add("...")
114-
when not isNamed:
115-
if count == 1:
116-
result.add(",") # $(1,) should print as the semantically legal (1,)
117-
result.add(")")
80+
tupleObjectDollar(result, x)
11881

82+
when not defined(nimPreviewSlimSystem):
83+
import std/objectdollar
84+
export objectdollar
11985

12086
proc collectionToString[T](x: T, prefix, separator, suffix: string): string =
12187
result = prefix

tests/stdlib/tobjectdollar.nim

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
discard """
2+
matrix: "-d:nimPreviewSlimSystem"
3+
"""
4+
5+
import std/assertions
6+
7+
type Foo = object
8+
a, b: int
9+
10+
let x = Foo(a: 23, b: 45)
11+
doAssert not compiles($x)
12+
import std/objectdollar
13+
doAssert compiles($x)
14+
doAssert $x == "(a: 23, b: 45)"

tests/stdlib/toptions.nim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ discard """
44

55
import std/[json, options]
66

7+
when defined(nimPreviewSlimSystem):
8+
import std/objectdollar
9+
710

811
# RefPerson is used to test that overloaded `==` operator is not called by
912
# options. It is defined here in the global scope, because otherwise the test

tests/stdlib/tstrformat.nim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import genericstrformat
44
import std/[strformat, strutils, times, tables, json]
55

6+
when defined(nimPreviewSlimSystem):
7+
import std/objectdollar
8+
69
proc main() =
710
block: # issue #7632
811
doAssert works(5) == "formatted 5"

0 commit comments

Comments
 (0)