mirror of
https://github.com/nim-lang/Nim.git
synced 2026-05-05 13:34:46 +00:00
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:
@@ -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)
|
||||
|
||||
|
||||
9
tests/arc/tinvalidenumtostr.nim
Normal file
9
tests/arc/tinvalidenumtostr.nim
Normal file
@@ -0,0 +1,9 @@
|
||||
# issue #24875
|
||||
|
||||
type
|
||||
MyEnum = enum
|
||||
One = 1
|
||||
|
||||
var x = cast[MyEnum](0)
|
||||
let s = $x
|
||||
doAssert s == ""
|
||||
Reference in New Issue
Block a user