account for invalid data in enum $ on arc/orc (#24886)

closes #24875

Refc gives `0 (invalid data!)`, but since enum `$` procs on arc are
generated during enum declarations we might not have access to string
concatenation and integer `$`, so it generates a static string. Just
chose an empty string for this.

(cherry picked from commit 5aaba213d4)
This commit is contained in:
metagn
2025-04-18 06:32:49 +03:00
committed by narimiran
parent 403b24faeb
commit 545058a4ea
2 changed files with 13 additions and 0 deletions

View File

@@ -33,6 +33,10 @@ proc genEnumToStrProc*(t: PType; info: TLineInfo; g: ModuleGraph; idgen: IdGener
caseStmt.add newTree(nkOfBranch, newIntTypeNode(field.position, t),
newTree(nkStmtList, newTree(nkFastAsgn, newSymNode(res), newStrNode(val, info))))
#newIntTypeNode(nkIntLit, field.position, t)
# safety branch for invalid data:
caseStmt.add newTree(nkElse,
newTree(nkStmtList, newTree(nkFastAsgn, newSymNode(res),
newStrNode("", info))))
body.add(caseStmt)

View File

@@ -0,0 +1,9 @@
# issue #24875
type
MyEnum = enum
One = 1
var x = cast[MyEnum](0)
let s = $x
doAssert s == ""