Files
Nim/tests/destructor/tdistinctseq.nim
ringabout 31effe8c75 fixes #24801; Invalid C codegen generated when destroying distinct seq types (#24835)
fixes #24801

Because distinct `seq` types match `proc `=destroy`*[T](x: var T)
{.inline, magic: "Destroy".}`. But the Nim compiler generates lifted seq
types for corresponding distinct types. So we skip the address for
distinct types.

Related to https://github.com/nim-lang/Nim/pull/22207 I had a hard time
finding the other place where generic destructors get replaced by
attachedDestructors

(cherry picked from commit 4352fa2ef0)
2025-04-04 10:08:06 +02:00

32 lines
635 B
Nim

discard """
matrix: "-u:nimPreviewNonVarDestructor;"
"""
type DistinctSeq* = distinct seq[int]
# `=destroy`(cast[ptr DistinctSeq](0)[])
var x = @[].DistinctSeq
`=destroy`(x)
import std/options
# bug #24801
type
B[T] = object
case r: bool
of false:
v: ref int
of true:
x: T
E = distinct seq[int]
U = ref object of RootObj
G = ref object of U
proc a(): E = default(E)
method c(_: U): seq[E] {.base.} = discard
proc p(): seq[E] = c(default(U))
method c(_: G): seq[E] = discard E(newSeq[seq[int]](1)[0])
method y(_: U) {.base.} =
let s = default(B[tuple[f: B[int], w: B[int]]])
discard some(s.x)