mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 22:10:33 +00:00
fixes #7179 ```nim var f = 751.0 echo f.int8 ``` In this case, `int8(float)` yields different numbers for different optimization levels, since float to int conversions are undefined behaviors. In this PR, it mitigates this problem by conversions to same size integers before converting to the final type: i.e. `int8(int64(float))`, which has UB problems but is better than before
This commit is contained in:
@@ -245,3 +245,32 @@ proc bar2() =
|
||||
|
||||
static: bar2()
|
||||
bar2()
|
||||
|
||||
when not defined(js):
|
||||
proc foo =
|
||||
block:
|
||||
var s1:int = -10
|
||||
doAssertRaises(RangeDefect):
|
||||
var n2:Natural = s1.Natural
|
||||
|
||||
block:
|
||||
var f = 751.0
|
||||
let m = f.int8
|
||||
|
||||
block:
|
||||
var s2:float = -10
|
||||
doAssertRaises(RangeDefect):
|
||||
var n2:Natural = s2.Natural
|
||||
|
||||
|
||||
block:
|
||||
type A = range[0..10]
|
||||
|
||||
let f = 156.0
|
||||
|
||||
doAssertRaises(RangeDefect):
|
||||
let a = f.A
|
||||
|
||||
echo a # 156
|
||||
|
||||
foo()
|
||||
|
||||
Reference in New Issue
Block a user