Skip to content

Commit

Permalink
fix #20148 implicit compile time conversion int to ranged float cause… (
Browse files Browse the repository at this point in the history
#20698)

fix #20148 implicit compile time conversion int to ranged float causes compiler fatal error
  • Loading branch information
bung87 authored Oct 29, 2022
1 parent 534c97e commit a51ed90
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion compiler/vm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions tests/statictypes/t20148.nim
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a51ed90

Please sign in to comment.