Skip to content

Commit 5aaba21

Browse files
authored
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.
1 parent af9219a commit 5aaba21

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

compiler/enumtostr.nim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ proc genEnumToStrProc*(t: PType; info: TLineInfo; g: ModuleGraph; idgen: IdGener
3333
caseStmt.add newTree(nkOfBranch, newIntTypeNode(field.position, t),
3434
newTree(nkStmtList, newTree(nkFastAsgn, newSymNode(res), newStrNode(val, info))))
3535
#newIntTypeNode(nkIntLit, field.position, t)
36+
# safety branch for invalid data:
37+
caseStmt.add newTree(nkElse,
38+
newTree(nkStmtList, newTree(nkFastAsgn, newSymNode(res),
39+
newStrNode("", info))))
3640

3741
body.add(caseStmt)
3842

tests/arc/tinvalidenumtostr.nim

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# issue #24875
2+
3+
type
4+
MyEnum = enum
5+
One = 1
6+
7+
var x = cast[MyEnum](0)
8+
let s = $x
9+
doAssert s == ""

0 commit comments

Comments
 (0)