From 855f5c8b81707c2f769814fa9cee1deb127cb684 Mon Sep 17 00:00:00 2001 From: metagn Date: Tue, 29 Aug 2023 15:59:49 +0300 Subject: [PATCH] clearer error for different size int/float cast in VM (#22582) refs #16547 (cherry picked from commit b6cea7b599b81db675b95d2f84a8e3cda071cb0d) --- compiler/vmgen.nim | 14 +++++++++++--- tests/vm/tunsupportedintfloatcast.nim | 3 +++ 2 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 tests/vm/tunsupportedintfloatcast.nim diff --git a/compiler/vmgen.nim b/compiler/vmgen.nim index cf89f30940..0ce90a4158 100644 --- a/compiler/vmgen.nim +++ b/compiler/vmgen.nim @@ -882,6 +882,8 @@ proc genCastIntFloat(c: PCtx; n: PNode; dest: var TDest) = let dst = n[0].typ.skipTypes(abstractRange)#.kind let srcSize = getSize(c.config, src) let dstSize = getSize(c.config, dst) + const unsupportedCastDifferentSize = + "VM does not support 'cast' from $1 with size $2 to $3 with size $4 due to different sizes" if src.kind in allowedIntegers and dst.kind in allowedIntegers: let tmp = c.genx(n[1]) if dest < 0: dest = c.getTemp(n[0].typ) @@ -898,8 +900,11 @@ proc genCastIntFloat(c: PCtx; n: PNode; dest: var TDest) = # is smaller than source. c.gABC(n, opcNarrowU, dest, TRegister(dstSize*8)) c.freeTemp(tmp) - elif srcSize == dstSize and src.kind in allowedIntegers and - dst.kind in {tyFloat, tyFloat32, tyFloat64}: + elif src.kind in allowedIntegers and + dst.kind in {tyFloat, tyFloat32, tyFloat64}: + if srcSize != dstSize: + globalError(c.config, n.info, unsupportedCastDifferentSize % + [$src.kind, $srcSize, $dst.kind, $dstSize]) let tmp = c.genx(n[1]) if dest < 0: dest = c.getTemp(n[0].typ) if dst.kind == tyFloat32: @@ -908,8 +913,11 @@ proc genCastIntFloat(c: PCtx; n: PNode; dest: var TDest) = c.gABC(n, opcCastIntToFloat64, dest, tmp) c.freeTemp(tmp) - elif srcSize == dstSize and src.kind in {tyFloat, tyFloat32, tyFloat64} and + elif src.kind in {tyFloat, tyFloat32, tyFloat64} and dst.kind in allowedIntegers: + if srcSize != dstSize: + globalError(c.config, n.info, unsupportedCastDifferentSize % + [$src.kind, $srcSize, $dst.kind, $dstSize]) let tmp = c.genx(n[1]) if dest < 0: dest = c.getTemp(n[0].typ) if src.kind == tyFloat32: diff --git a/tests/vm/tunsupportedintfloatcast.nim b/tests/vm/tunsupportedintfloatcast.nim new file mode 100644 index 0000000000..d65f10d867 --- /dev/null +++ b/tests/vm/tunsupportedintfloatcast.nim @@ -0,0 +1,3 @@ +static: + echo cast[int32](12.0) #[tt.Error + ^ VM does not support 'cast' from tyFloat with size 8 to tyInt32 with size 4 due to different sizes]#