From 9182c03d4786d6bf61845aaa9e5aa1e1c0d80f47 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Wed, 1 Apr 2026 19:15:00 +0800 Subject: [PATCH] refactor: replace elemSupportsCopyMem with supportsCopyMem for clarity and consistency --- compiler/liftdestructors.nim | 10 ++-------- compiler/semmagic.nim | 4 +--- compiler/types.nim | 5 +++++ 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/compiler/liftdestructors.nim b/compiler/liftdestructors.nim index e5a3cc6337..611f6f288a 100644 --- a/compiler/liftdestructors.nim +++ b/compiler/liftdestructors.nim @@ -620,12 +620,6 @@ proc checkSelfAssignment(c: var TLiftCtx; t: PType; body, x, y: PNode) = cond.typ = getSysType(c.g, c.info, tyBool) body.add genIf(c, cond, newTreeI(nkReturnStmt, c.info, newNodeI(nkEmpty, c.info))) -proc elemSupportsCopyMem(t: PType): bool = - ## Returns true if the element type of seq `t` supports bulk memory copy - ## (i.e., has no GC refs and no destructors). - let elemType = t.elementType.skipTypes({tyVar, tyLent, tyGenericInst, tyAlias, tySink, tyInferred}) - result = not containsGarbageCollectedRef(elemType) and not hasDestructor(elemType) - proc genBulkCopySeq(c: var TLiftCtx; t: PType; body, x, y: PNode) = ## Generates a call to nimCopySeqPayload for bulk memcpy of seq data. let elemType = t.elementType @@ -650,7 +644,7 @@ proc fillSeqOp(c: var TLiftCtx; t: PType; body, x, y: PNode) = case c.kind of attachedDup: body.add setLenSeqCall(c, t, x, y) - if elemSupportsCopyMem(t): + if supportsCopyMem(t): genBulkCopySeq(c, t, body, x, y) else: forallElements(c, t, body, x, y) @@ -665,7 +659,7 @@ proc fillSeqOp(c: var TLiftCtx; t: PType; body, x, y: PNode) = # For trivially copyable types, use bulk copyMem instead of element loop. checkSelfAssignment(c, t, body, x, y) body.add setLenSeqCall(c, t, x, y) - if elemSupportsCopyMem(t): + if supportsCopyMem(t): genBulkCopySeq(c, t, body, x, y) else: forallElements(c, t, body, x, y) diff --git a/compiler/semmagic.nim b/compiler/semmagic.nim index 87e085d4fd..542e5ab054 100644 --- a/compiler/semmagic.nim +++ b/compiler/semmagic.nim @@ -232,9 +232,7 @@ proc evalTypeTrait(c: PContext; traitCall: PNode, operand: PType, context: PSym) of "stripGenericParams": result = uninstantiate(operand).toNode(traitCall.info) of "supportsCopyMem": - let t = operand.skipTypes({tyVar, tyLent, tyGenericInst, tyAlias, tySink, tyInferred}) - let complexObj = containsGarbageCollectedRef(t) or - hasDestructor(t) + let complexObj = supportsCopyMem(operand) result = newIntNodeT(toInt128(ord(not complexObj)), traitCall, c.idgen, c.graph) of "canFormCycles": result = newIntNodeT(toInt128(ord(types.canFormAcycle(c.graph, operand))), traitCall, c.idgen, c.graph) diff --git a/compiler/types.nim b/compiler/types.nim index e18f97ff36..d38c2a4140 100644 --- a/compiler/types.nim +++ b/compiler/types.nim @@ -1779,3 +1779,8 @@ proc reduceToBase*(f: PType): PType = result = f.elementType else: result = f + +proc supportsCopyMem*(t: PType): bool = + let t = t.skipTypes({tyVar, tyLent, tyGenericInst, tyAlias, tySink, tyInferred}) + result = containsGarbageCollectedRef(t) or + hasDestructor(t)