Skip to content

Commit 4cd870e

Browse files
AraqPMunch
authored andcommitted
fixes nim-lang#19198 [backport:1.6] (nim-lang#19209)
* fixes nim-lang#19198 [backport:1.6] * added a test case
1 parent b4a67ff commit 4cd870e

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

compiler/vm.nim

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,10 @@ proc opConv(c: PCtx; dest: var TFullReg, src: TFullReg, desttyp, srctyp: PType):
434434
of tyFloat..tyFloat64:
435435
dest.intVal = int(src.floatVal)
436436
else:
437-
let srcDist = (sizeof(src.intVal) - styp.size) * 8
438-
let destDist = (sizeof(dest.intVal) - desttyp.size) * 8
437+
let srcSize = getSize(c.config, styp)
438+
let destSize = getSize(c.config, desttyp)
439+
let srcDist = (sizeof(src.intVal) - srcSize) * 8
440+
let destDist = (sizeof(dest.intVal) - destSize) * 8
439441
var value = cast[BiggestUInt](src.intVal)
440442
value = (value shl srcDist) shr srcDist
441443
value = (value shl destDist) shr destDist

compiler/vmgen.nim

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -749,18 +749,20 @@ proc genNarrow(c: PCtx; n: PNode; dest: TDest) =
749749
let t = skipTypes(n.typ, abstractVar-{tyTypeDesc})
750750
# uint is uint64 in the VM, we we only need to mask the result for
751751
# other unsigned types:
752-
if t.kind in {tyUInt8..tyUInt32} or (t.kind == tyUInt and t.size < 8):
753-
c.gABC(n, opcNarrowU, dest, TRegister(t.size*8))
754-
elif t.kind in {tyInt8..tyInt32} or (t.kind == tyInt and t.size < 8):
755-
c.gABC(n, opcNarrowS, dest, TRegister(t.size*8))
752+
let size = getSize(c.config, t)
753+
if t.kind in {tyUInt8..tyUInt32} or (t.kind == tyUInt and size < 8):
754+
c.gABC(n, opcNarrowU, dest, TRegister(size*8))
755+
elif t.kind in {tyInt8..tyInt32} or (t.kind == tyInt and size < 8):
756+
c.gABC(n, opcNarrowS, dest, TRegister(size*8))
756757

757758
proc genNarrowU(c: PCtx; n: PNode; dest: TDest) =
758759
let t = skipTypes(n.typ, abstractVar-{tyTypeDesc})
759760
# uint is uint64 in the VM, we we only need to mask the result for
760761
# other unsigned types:
762+
let size = getSize(c.config, t)
761763
if t.kind in {tyUInt8..tyUInt32, tyInt8..tyInt32} or
762-
(t.kind in {tyUInt, tyInt} and t.size < 8):
763-
c.gABC(n, opcNarrowU, dest, TRegister(t.size*8))
764+
(t.kind in {tyUInt, tyInt} and size < 8):
765+
c.gABC(n, opcNarrowU, dest, TRegister(size*8))
764766

765767
proc genBinaryABCnarrow(c: PCtx; n: PNode; dest: var TDest; opc: TOpcode) =
766768
genBinaryABC(c, n, dest, opc)
@@ -1088,10 +1090,11 @@ proc genMagic(c: PCtx; n: PNode; dest: var TDest; m: TMagic) =
10881090
genBinaryABC(c, n, dest, opcShlInt)
10891091
# genNarrowU modified
10901092
let t = skipTypes(n.typ, abstractVar-{tyTypeDesc})
1091-
if t.kind in {tyUInt8..tyUInt32} or (t.kind == tyUInt and t.size < 8):
1092-
c.gABC(n, opcNarrowU, dest, TRegister(t.size*8))
1093-
elif t.kind in {tyInt8..tyInt32} or (t.kind == tyInt and t.size < 8):
1094-
c.gABC(n, opcSignExtend, dest, TRegister(t.size*8))
1093+
let size = getSize(c.config, t)
1094+
if t.kind in {tyUInt8..tyUInt32} or (t.kind == tyUInt and size < 8):
1095+
c.gABC(n, opcNarrowU, dest, TRegister(size*8))
1096+
elif t.kind in {tyInt8..tyInt32} or (t.kind == tyInt and size < 8):
1097+
c.gABC(n, opcSignExtend, dest, TRegister(size*8))
10951098
of mAshrI: genBinaryABC(c, n, dest, opcAshrInt)
10961099
of mBitandI: genBinaryABC(c, n, dest, opcBitandInt)
10971100
of mBitorI: genBinaryABC(c, n, dest, opcBitorInt)
@@ -1125,8 +1128,9 @@ proc genMagic(c: PCtx; n: PNode; dest: var TDest; m: TMagic) =
11251128
genUnaryABC(c, n, dest, opcBitnotInt)
11261129
#genNarrowU modified, do not narrow signed types
11271130
let t = skipTypes(n.typ, abstractVar-{tyTypeDesc})
1128-
if t.kind in {tyUInt8..tyUInt32} or (t.kind == tyUInt and t.size < 8):
1129-
c.gABC(n, opcNarrowU, dest, TRegister(t.size*8))
1131+
let size = getSize(c.config, t)
1132+
if t.kind in {tyUInt8..tyUInt32} or (t.kind == tyUInt and size < 8):
1133+
c.gABC(n, opcNarrowU, dest, TRegister(size*8))
11301134
of mCharToStr, mBoolToStr, mIntToStr, mInt64ToStr, mFloatToStr, mCStrToStr, mStrToStr, mEnumToStr:
11311135
genConv(c, n, n[1], dest)
11321136
of mEqStr, mEqCString: genBinaryABC(c, n, dest, opcEqStr)

tests/vm/tvmmisc.nim

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,10 @@ block: # bug #10815
290290

291291
const a = P()
292292
doAssert $a == ""
293-
293+
294294
when defined osx: # xxx bug https://github.com/nim-lang/Nim/issues/10815#issuecomment-476380734
295295
block:
296-
type CharSet {.union.} = object
296+
type CharSet {.union.} = object
297297
cs: set[char]
298298
vs: array[4, uint64]
299299
const a = Charset(cs: {'a'..'z'})
@@ -553,3 +553,22 @@ block: # bug #8015
553553
doAssert $viaProc.table[0] == "(kind: Fixed, cost: 999)"
554554
doAssert viaProc.table[1].handler() == 100
555555
doAssert viaProc.table[2].handler() == 200
556+
557+
558+
# bug #19198
559+
560+
block:
561+
type
562+
Foo[n: static int] = int
563+
564+
block:
565+
static:
566+
let x = int 1
567+
echo x.type # Foo
568+
569+
block:
570+
static:
571+
let x = int 1
572+
let y = x + 1
573+
# Error: unhandled exception: value out of range: -8 notin 0 .. 65535 [RangeDefect]
574+
echo y

0 commit comments

Comments
 (0)