summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBung <crc32@qq.com>2022-10-30 00:04:05 +0800
committerGitHub <noreply@github.com>2022-10-29 18:04:05 +0200
commita51ed90c5ddc49d96dd96a16716f91fd543c518e (patch)
tree116225b5d8598fb603339e6c072a2e4d8c4c3898
parent534c97edc54ffb6d55baa5084f8c426a451c9244 (diff)
downloadNim-a51ed90c5ddc49d96dd96a16716f91fd543c518e.tar.gz
fix #20148 implicit compile time conversion int to ranged float cause… (#20698)
fix #20148 implicit compile time conversion int to ranged float causes compiler fatal error
-rw-r--r--compiler/vm.nim5
-rw-r--r--tests/statictypes/t20148.nim8
2 files changed, 12 insertions, 1 deletions
diff --git a/compiler/vm.nim b/compiler/vm.nim
index 865ee1ea6..1a41ce8a1 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 000000000..d74a7755e
--- /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