From 5aaba213d426e67a0761c6dcb7a37d8663026d92 Mon Sep 17 00:00:00 2001 From: metagn Date: Fri, 18 Apr 2025 06:32:49 +0300 Subject: [PATCH] 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. --- compiler/enumtostr.nim | 4 ++++ tests/arc/tinvalidenumtostr.nim | 9 +++++++++ 2 files changed, 13 insertions(+) create mode 100644 tests/arc/tinvalidenumtostr.nim diff --git a/compiler/enumtostr.nim b/compiler/enumtostr.nim index dc516d2e52..2223be2ffb 100644 --- a/compiler/enumtostr.nim +++ b/compiler/enumtostr.nim @@ -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) diff --git a/tests/arc/tinvalidenumtostr.nim b/tests/arc/tinvalidenumtostr.nim new file mode 100644 index 0000000000..b053e30993 --- /dev/null +++ b/tests/arc/tinvalidenumtostr.nim @@ -0,0 +1,9 @@ +# issue #24875 + +type + MyEnum = enum + One = 1 + +var x = cast[MyEnum](0) +let s = $x +doAssert s == ""