From 255698deee403ae80b26baa567043a07dc3c7404 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Nihlg=C3=A5rd?= Date: Mon, 13 Apr 2020 14:22:33 +0200 Subject: [PATCH] Fix semfold handling of {.str/int/bool-define.} (#13964) --- compiler/semfold.nim | 6 ++++++ tests/misc/tdefine.nim | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/compiler/semfold.nim b/compiler/semfold.nim index f0971bf099e9..5e2c2bccc13e 100644 --- a/compiler/semfold.nim +++ b/compiler/semfold.nim @@ -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: @@ -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: diff --git a/tests/misc/tdefine.nim b/tests/misc/tdefine.nim index 1378b890147a..f1c6e7a96dfa 100644 --- a/tests/misc/tdefine.nim +++ b/tests/misc/tdefine.nim @@ -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 \ No newline at end of file