Skip to content

Commit

Permalink
make parseEnum skip type aliases for enum type sym
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Sep 19, 2023
1 parent 5f9038a commit 4181618
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/std/enumutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ macro genEnumCaseStmt*(typ: typedesc, argSym: typed, default: typed,
# a normalized string comparison to the `argSym` input.
# string normalization is done using passed normalizer.
let typ = typ.getTypeInst[1]
let impl = typ.getImpl[2]
let typSym = typ.getTypeImpl.getTypeInst # skip aliases etc to get type sym
let impl = typSym.getImpl[2]
expectKind impl, nnkEnumTy
let normalizerNode = quote: `normalizer`
expectKind normalizerNode, nnkSym
Expand Down
19 changes: 15 additions & 4 deletions tests/stdlib/tstrutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -679,11 +679,22 @@ template main() =
doAssert b == f2
doAssert c == f3

block: # parseEnum TODO: merge above
type MyEnum = enum enA, enB, enC, enuD, enE
doAssert parseEnum[MyEnum]("enu_D") == enuD
block:
type MyEnum = enum enA, enB, enC, enuD, enE
doAssert parseEnum[MyEnum]("enu_D") == enuD

doAssert parseEnum("invalid enum value", enC) == enC
doAssert parseEnum("invalid enum value", enC) == enC

block: # issue #22726
type SomeEnum = enum A, B, C

proc assignEnum(dest: var enum, s: string) =
type ty = typeof(dest)
dest = parseEnum[ty](s)

var v: SomeEnum
v.assignEnum("A")
doAssert v == A

block: # indentation
doAssert 0 == indentation """
Expand Down

0 comments on commit 4181618

Please sign in to comment.