From a51ed90c5ddc49d96dd96a16716f91fd543c518e Mon Sep 17 00:00:00 2001 From: Bung Date: Sun, 30 Oct 2022 00:04:05 +0800 Subject: [PATCH] =?UTF-8?q?fix=20#20148=20implicit=20compile=20time=20conv?= =?UTF-8?q?ersion=20int=20to=20ranged=20float=20cause=E2=80=A6=20(#20698)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix #20148 implicit compile time conversion int to ranged float causes compiler fatal error --- compiler/vm.nim | 5 ++++- tests/statictypes/t20148.nim | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 tests/statictypes/t20148.nim diff --git a/compiler/vm.nim b/compiler/vm.nim index 865ee1ea6e..1a41ce8a1f 100644 --- a/compiler/vm.nim +++ b/compiler/vm.nim @@ -460,9 +460,12 @@ proc opConv(c: PCtx; dest: var TFullReg, src: TFullReg, desttyp, srctyp: PType): else: int(src.intVal != 0) of tyFloat..tyFloat64: dest.ensureKind(rkFloat) - case skipTypes(srctyp, abstractRange).kind + let srcKind = skipTypes(srctyp, abstractRange).kind + case srcKind of tyInt..tyInt64, tyUInt..tyUInt64, tyEnum, tyBool, tyChar: dest.floatVal = toBiggestFloat(src.intVal) + elif src.kind == rkInt: + dest.floatVal = toBiggestFloat(src.intVal) else: dest.floatVal = src.floatVal of tyObject: diff --git a/tests/statictypes/t20148.nim b/tests/statictypes/t20148.nim new file mode 100644 index 0000000000..d74a7755e1 --- /dev/null +++ b/tests/statictypes/t20148.nim @@ -0,0 +1,8 @@ +type Percent = range[0.0 .. 1.0] +# type Percent = float # using unlimited `float` works fine + +proc initColor*(alpha: Percent): bool = + echo alpha + +const moduleInstanceStyle = initColor(1) +# let moduleInstanceStyle = initColor(1) # using runtime conversion works fine