summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorringabout <43030857+ringabout@users.noreply.github.com>2024-01-26 15:04:16 +0800
committerGitHub <noreply@github.com>2024-01-26 08:04:16 +0100
commit24a606902a4e99ac3d7f58b89db3b023061bc8b0 (patch)
treea202199ec969eeee8873ec9758492d0c5ff84666 /tests
parent243f1e6cd5e147fdd96b5298e4df44546e5eea28 (diff)
downloadNim-24a606902a4e99ac3d7f58b89db3b023061bc8b0.tar.gz
fixes #23247; don't destroy openarray since it doesn't own the data (#23254)
fixes #23247
closes #23251 (which accounts for why the openarray type is lifted
because ops are lifted for openarray conversions)

related: https://github.com/nim-lang/Nim/pull/18713

It seems to me that openarray doesn't own the data, so it cannot destroy
itself. The same case should be applied to
https://github.com/nim-lang/Nim/issues/19435. It shouldn't be destroyed
even openarray can have a destructor. A cleanup will be followed for
https://github.com/nim-lang/Nim/pull/19723 if it makes sense.

According to https://github.com/nim-lang/Nim/pull/12073, it lifts
destructor for openarray when openarray is sunk into the function, when
means `sink openarray` owns the data and needs to destroy it. In other
cases, destructor shouldn't be lifted for `openarray` in the first place
and it shouldn't destroy the data if it doesn't own it.

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Diffstat (limited to 'tests')
-rw-r--r--tests/arc/t23247.nim52
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/arc/t23247.nim b/tests/arc/t23247.nim
new file mode 100644
index 000000000..0fadc50cd
--- /dev/null
+++ b/tests/arc/t23247.nim
@@ -0,0 +1,52 @@
+discard """
+  matrix: ";-d:useMalloc"
+"""
+
+# bug #23247
+import std/hashes
+
+func baseAddr[T](x: openArray[T]): ptr T =
+  # Return the address of the zero:th element of x or `nil` if x is empty
+  if x.len == 0: nil else: cast[ptr T](x)
+
+func makeUncheckedArray[T](p: ptr T): ptr UncheckedArray[T] =
+  cast[ptr UncheckedArray[T]](p)
+
+type
+  LabelKey = object
+    data: seq[string]
+    refs: ptr UncheckedArray[string]
+    refslen: int
+
+  Gauge = ref object
+    metrics: seq[seq[seq[string]]]
+
+template values(key: LabelKey): openArray[string] =
+  if key.refslen > 0:
+    key.refs.toOpenArray(0, key.refslen - 1)
+  else:
+    key.data
+
+proc hash(key: LabelKey): Hash =
+  hash(key.values)
+
+proc view(T: type LabelKey, values: openArray[string]): T =
+  # TODO some day, we might get view types - until then..
+  LabelKey(refs: baseAddr(values).makeUncheckedArray(), refslen: values.len())
+
+template withValue2(k: untyped) =
+  discard hash(k)
+
+proc setGauge(
+    collector: Gauge,
+    labelValues: openArray[string],
+) =
+  let v = LabelKey.view(labelValues)
+  withValue2(v)
+  collector.metrics.add @[@labelValues, @labelValues]
+  discard @labelValues
+
+var nim_gc_mem_bytes = Gauge()
+let threadID = $getThreadId()
+setGauge(nim_gc_mem_bytes, @[threadID])
+setGauge(nim_gc_mem_bytes, @[threadID])
\ No newline at end of file
evel&id=b01945a061f495099fa8bbcd1d481a88ae56fa9c'>^
74fdd5c2e ^
556f48808 ^
74fdd5c2e ^



4cd558bda ^
74fdd5c2e ^
07562d6b9 ^
74fdd5c2e ^
4cd558bda ^
4cd558bda ^
74fdd5c2e ^
4cd558bda ^
31bb4311e ^
74fdd5c2e ^
4cd558bda ^
31bb4311e ^
8c671d22d ^

4cd558bda ^
31bb4311e ^
74fdd5c2e ^
3ee048eaa ^
8c671d22d ^
e27e873b7 ^
571dbc8ef ^
74fdd5c2e ^
571dbc8ef ^
74fdd5c2e ^

571dbc8ef ^
6b3473ad1 ^
74fdd5c2e ^
571dbc8ef ^
74fdd5c2e ^
571dbc8ef ^
74fdd5c2e ^

571dbc8ef ^
74fdd5c2e ^

571dbc8ef ^
74fdd5c2e ^
571dbc8ef ^
74fdd5c2e ^

571dbc8ef ^
74fdd5c2e ^
8c671d22d ^
74fdd5c2e ^
c5d7f6d2b ^
e5e80d203 ^





571dbc8ef ^
74fdd5c2e ^
571dbc8ef ^
74be2ebe0 ^






7b2b61672 ^

8b76edfd6 ^
7b2b61672 ^



74fdd5c2e ^



8c671d22d ^

74fdd5c2e ^


















bd2832fa1 ^




8b76edfd6 ^
bd2832fa1 ^
74fdd5c2e ^





8b76edfd6 ^


74fdd5c2e ^

8b76edfd6 ^
74fdd5c2e ^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150