diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2023-03-28 13:27:17 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-28 13:27:17 +0200 |
commit | 115cec17452dc13aed7a1300f7786177f886e9c7 (patch) | |
tree | 9881043858679be331b9e3d2232725db9c5e71eb /tests/arc | |
parent | 0630c649c6b4fca4abfa157c5bc6f9f9e50e9c65 (diff) | |
download | Nim-115cec17452dc13aed7a1300f7786177f886e9c7.tar.gz |
fixes #20993 [backport:1.6] (#21574)
* fixes #20993 [backport:1.6] * proper line endings for the test file
Diffstat (limited to 'tests/arc')
-rw-r--r-- | tests/arc/taliased_reassign.nim | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/tests/arc/taliased_reassign.nim b/tests/arc/taliased_reassign.nim new file mode 100644 index 000000000..5563fae8c --- /dev/null +++ b/tests/arc/taliased_reassign.nim @@ -0,0 +1,41 @@ +discard """ + matrix: "--mm:orc" +""" + +# bug #20993 + +type + Dual[int] = object # must be generic (even if fully specified) + p: int +proc D(p: int): Dual[int] = Dual[int](p: p) +proc `+`(x: Dual[int], y: Dual[int]): Dual[int] = D(x.p + y.p) + +type + Tensor[T] = object + buf: seq[T] +proc newTensor*[T](s: int): Tensor[T] = Tensor[T](buf: newSeq[T](s)) +proc `[]`*[T](t: Tensor[T], idx: int): T = t.buf[idx] +proc `[]=`*[T](t: var Tensor[T], idx: int, val: T) = t.buf[idx] = val + +proc `+.`[T](t1, t2: Tensor[T]): Tensor[T] = + let n = t1.buf.len + result = newTensor[T](n) + for i in 0 ..< n: + result[i] = t1[i] + t2[i] + +proc toTensor*[T](a: sink seq[T]): Tensor[T] = + ## This breaks it: Using `T` instead makes it work + type U = typeof(a[0]) + var t: Tensor[U] # Tensor[T] works + t.buf = a + result = t + +proc loss() = + var B = toTensor(@[D(123)]) + let a = toTensor(@[D(-10)]) + B = B +. a + doAssert B[0].p == 113, "I want to be 113, but I am " & $B[0].p + +loss() + + |