From efbf43d4a9b9ffdee5db95749d0d992bc16bb350 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Wed, 5 Jun 2019 14:55:47 +0200 Subject: [PATCH] intVal works now on enum field symbols (#11403) * intVal works now on enum field symbols * disable flakey titerators test --- compiler/vm.nim | 11 ++++++++--- lib/core/macros.nim | 2 ++ tests/coroutines/titerators.nim | 3 +++ tests/macros/tmacro1.nim | 11 +++++++++++ 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/compiler/vm.nim b/compiler/vm.nim index 8b36b461b9..4e8a344714 100644 --- a/compiler/vm.nim +++ b/compiler/vm.nim @@ -1418,9 +1418,12 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg = of opcNIntVal: decodeB(rkInt) let a = regs[rb].node - case a.kind - of nkCharLit..nkUInt64Lit: regs[ra].intVal = a.intVal - else: stackTrace(c, tos, pc, errFieldXNotFound & "intVal") + if a.kind in {nkCharLit..nkUInt64Lit}: + regs[ra].intVal = a.intVal + elif a.kind == nkSym and a.sym.kind == skEnumField: + regs[ra].intVal = a.sym.position + else: + stackTrace(c, tos, pc, errFieldXNotFound & "intVal") of opcNFloatVal: decodeB(rkFloat) let a = regs[rb].node @@ -1692,6 +1695,8 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg = if dest.kind in {nkCharLit..nkUInt64Lit} and regs[rb].kind in {rkInt}: dest.intVal = regs[rb].intVal + elif dest.kind == nkSym and dest.sym.kind == skEnumField: + stackTrace(c, tos, pc, "`intVal` cannot be changed for an enum symbol.") else: stackTrace(c, tos, pc, errFieldXNotFound & "intVal") of opcNSetFloatVal: diff --git a/lib/core/macros.nim b/lib/core/macros.nim index 77e7074646..87e9e4b948 100644 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -226,8 +226,10 @@ proc kind*(n: NimNode): NimNodeKind {.magic: "NKind", noSideEffect.} ## returns the `kind` of the node `n`. proc intVal*(n: NimNode): BiggestInt {.magic: "NIntVal", noSideEffect.} + ## Returns an integer value from any integer literal or enum field symbol. proc floatVal*(n: NimNode): BiggestFloat {.magic: "NFloatVal", noSideEffect.} + ## Returns a float from any floating point literal. {.push warnings: off.} diff --git a/tests/coroutines/titerators.nim b/tests/coroutines/titerators.nim index abcfbde43b..d12a5debec 100644 --- a/tests/coroutines/titerators.nim +++ b/tests/coroutines/titerators.nim @@ -1,7 +1,10 @@ discard """ target: "c" +disabled: true """ +# Timers are always flakey on the testing servers. + import coro include system/timers diff --git a/tests/macros/tmacro1.nim b/tests/macros/tmacro1.nim index 5388e861c0..844738c825 100644 --- a/tests/macros/tmacro1.nim +++ b/tests/macros/tmacro1.nim @@ -93,3 +93,14 @@ static: quit("may not be evaluated") assert( (myLit or bottom()) == myLit ) + +type + Fruit = enum + apple + banana + orange + +macro foo(x: typed) = + doAssert Fruit(x.intVal) == banana + +foo(banana)