Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix parseEnum cannot parse enums with constant fields #19466

Merged
merged 1 commit into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/std/enumutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ from typetraits import OrdinalEnum, HoleyEnum

macro genEnumCaseStmt*(typ: typedesc, argSym: typed, default: typed,
userMin, userMax: static[int], normalizer: static[proc(s :string): string]): untyped =
# generates a case stmt, which assigns the correct enum field given
# Generates a case stmt, which assigns the correct enum field given
# a normalized string comparison to the `argSym` input.
# string normalization is done using passed normalizer.
# NOTE: for an enum with fields Foo, Bar, ... we cannot generate
Expand Down Expand Up @@ -49,7 +49,12 @@ macro genEnumCaseStmt*(typ: typedesc, argSym: typed, default: typed,
of nnkIntLit:
fStr = f[0].strVal
fNum = f[1].intVal
else: error("Invalid tuple syntax!", f[1])
else:
let fAst = f[0].getImpl
if fAst.kind == nnkStrLit:
fStr = fAst.strVal
else:
error("Invalid tuple syntax!", f[1])
else: error("Invalid node for enum type `" & $f.kind & "`!", f)
# add field if string not already added
if fNum >= userMin and fNum <= userMax:
Expand Down
11 changes: 11 additions & 0 deletions tests/stdlib/tstrutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,17 @@ template main() =
let g = parseEnum[Foo]("Bar", A)
doAssert g == A

block: # bug #19463
const CAMPAIGN_TABLE = "wikientries_campaign"
const CHARACTER_TABLE = "wikientries_character"

type Tables = enum
a = CAMPAIGN_TABLE,
b = CHARACTER_TABLE,

let myA = CAMPAIGN_TABLE
doAssert $parseEnum[Tables](myA) == "wikientries_campaign"

block: # check enum defined in block
type
Bar = enum
Expand Down