Fixes #25341; Invalid C code for lifecycle hooks for distinct types based on generics (#25342)

This commit is contained in:
elijahr
2025-12-07 05:59:42 -06:00
committed by GitHub
parent c3a20fa890
commit 099ee1ce4a
5 changed files with 35 additions and 2 deletions

View File

@@ -1212,8 +1212,10 @@ proc produceSym(g: ModuleGraph; c: PContext; typ: PType; kind: TTypeAttachedOp;
result.ast[bodyPos].add newAsgnStmt(d, src)
else:
var tk: TTypeKind
var skipped: PType = nil
if g.config.selectedGC in {gcArc, gcOrc, gcHooks, gcAtomicArc}:
tk = skipTypes(typ, {tyOrdinal, tyRange, tyInferred, tyGenericInst, tyStatic, tyAlias, tySink}).kind
skipped = skipTypes(typ, {tyOrdinal, tyRange, tyInferred, tyGenericInst, tyStatic, tyAlias, tySink})
tk = skipped.kind
else:
tk = tyNone # no special casing for strings and seqs
case tk
@@ -1223,7 +1225,7 @@ proc produceSym(g: ModuleGraph; c: PContext; typ: PType; kind: TTypeAttachedOp;
fillStrOp(a, typ, result.ast[bodyPos], d, src)
else:
fillBody(a, typ, result.ast[bodyPos], d, src)
if tk == tyObject and a.kind in {attachedAsgn, attachedSink, attachedDeepCopy, attachedDup} and not isObjLackingTypeField(typ):
if tk == tyObject and a.kind in {attachedAsgn, attachedSink, attachedDeepCopy, attachedDup} and not isObjLackingTypeField(skipped):
# bug #19205: Do not forget to also copy the hidden type field:
genTypeFieldCopy(a, typ, result.ast[bodyPos], d, src)

View File

@@ -0,0 +1,7 @@
discard """
cmd: "nim c --mm:orc $file"
output: ""
"""
import ./t25341_aux/a, ./t25341_aux/b
a()
b()

View File

@@ -0,0 +1,4 @@
import ./module
proc a*() =
discard make1[4]().make2()

View File

@@ -0,0 +1,6 @@
import ./module
var globalObj: Distinct2[4]
proc b*() =
globalObj = make1[4]().make2()

View File

@@ -0,0 +1,14 @@
type
BaseObject*[N: static int] = object
value*: int
Distinct1*[N: static int] = distinct BaseObject[N]
Distinct2*[N: static int] = distinct BaseObject[N]
proc `=copy`*[N: static int](dest: var Distinct2[N], src: Distinct2[N]) {.error: "no".}
proc make1*[N: static int](): Distinct1[N] =
Distinct1[N](BaseObject[N](value: 0))
proc make2*[N: static int](u: sink Distinct1[N]): Distinct2[N] =
Distinct2[N](BaseObject[N](u))