diff --git a/compiler/vm.nim b/compiler/vm.nim index 08ac142f37..251017c208 100644 --- a/compiler/vm.nim +++ b/compiler/vm.nim @@ -1723,6 +1723,12 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg = let max = (1.BiggestInt shl (rb-1))-1 if regs[ra].intVal < min or regs[ra].intVal > max: stackTrace(c, tos, pc, "unhandled exception: value out of range") + of opcNarrowR: + decodeBC(rkInt) + let min = regs[rb].intVal + let max = regs[rc].intVal + if regs[ra].intVal < min or regs[ra].intVal > max: + stackTrace(c, tos, pc, "unhandled exception: value out of range") of opcNarrowU: decodeB(rkInt) regs[ra].intVal = regs[ra].intVal and ((1'i64 shl rb)-1) diff --git a/compiler/vmdef.nim b/compiler/vmdef.nim index e8336aaba4..a3ac120f99 100644 --- a/compiler/vmdef.nim +++ b/compiler/vmdef.nim @@ -105,7 +105,7 @@ type opcIsNil, opcOf, opcIs, opcParseFloat, opcConv, opcCast, opcQuit, opcInvalidField, - opcNarrowS, opcNarrowU, + opcNarrowS, opcNarrowU, opcNarrowR opcSignExtend, opcAddStrCh, diff --git a/compiler/vmgen.nim b/compiler/vmgen.nim index 8c5460b330..11b7b27fe7 100644 --- a/compiler/vmgen.nim +++ b/compiler/vmgen.nim @@ -798,6 +798,11 @@ proc genNarrow(c: PCtx; n: PNode; dest: TDest) = c.gABC(n, opcNarrowU, dest, TRegister(size*8)) elif t.kind in {tyInt8..tyInt32} or (t.kind == tyInt and size < 8): c.gABC(n, opcNarrowS, dest, TRegister(size*8)) + elif t.kind in {tyEnum, tyRange}: + let intType = getSysType(c.graph, n.info, tyInt) + let first = c.genx(newIntTypeNode(firstOrd(c.config, t), intType)) + let last = c.genx(newIntTypeNode(lastOrd(c.config, t), intType)) + c.gABC(n, opcNarrowR, dest, first, last) proc genNarrowU(c: PCtx; n: PNode; dest: TDest) = let t = skipTypes(n.typ, abstractVar-{tyTypeDesc}) diff --git a/tests/errmsgs/tvmranges.nim b/tests/errmsgs/tvmranges.nim new file mode 100644 index 0000000000..236da34148 --- /dev/null +++ b/tests/errmsgs/tvmranges.nim @@ -0,0 +1,17 @@ +discard """ + action: reject + nimout: ''' +stack trace: (most recent call last) +tvmranges.nim(14, 10) +tvmranges.nim(14, 10) Error: unhandled exception: value out of range +''' +""" + +type X = enum + a + b + +when pred(a) == b: + echo "a" +else: + echo "b" \ No newline at end of file