Skip to content

Commit

Permalink
Fix semfold handling of {.str/int/bool-define.} (#13964)
Browse files Browse the repository at this point in the history
  • Loading branch information
GULPF authored Apr 13, 2020
1 parent 40b64cc commit 255698d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions compiler/semfold.nim
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,13 @@ proc getConstExpr(m: PSym, n: PNode; g: ModuleGraph): PNode =
localError(g.config, s.info,
"{.intdefine.} const was set to an invalid integer: '" &
g.config.symbols[s.name.s] & "'")
else:
result = copyTree(s.ast)
of mStrDefine:
if isDefined(g.config, s.name.s):
result = newStrNodeT(g.config.symbols[s.name.s], n, g)
else:
result = copyTree(s.ast)
of mBoolDefine:
if isDefined(g.config, s.name.s):
try:
Expand All @@ -560,6 +564,8 @@ proc getConstExpr(m: PSym, n: PNode; g: ModuleGraph): PNode =
localError(g.config, s.info,
"{.booldefine.} const was set to an invalid bool: '" &
g.config.symbols[s.name.s] & "'")
else:
result = copyTree(s.ast)
else:
result = copyTree(s.ast)
of skProc, skFunc, skMethod:
Expand Down
12 changes: 12 additions & 0 deletions tests/misc/tdefine.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,15 @@ doAssert booldef
doAssert not booldef2
doAssert intdef == 2
doAssert strdef == "foobar"

# Intentionally not defined from command line
const booldef3 {.booldefine.} = true
const intdef2 {.intdefine.} = 1
const strdef2 {.strdefine.} = "abc"
type T = object
when booldef3:
field1: int
when intdef2 == 1:
field2: int
when strdef2 == "abc":
field3: int

0 comments on commit 255698d

Please sign in to comment.