Skip to content

Commit

Permalink
Numeric literal overflow is now an error.
Browse files Browse the repository at this point in the history
  • Loading branch information
athas committed Jun 10, 2020
1 parent 442eaa3 commit dc21049
Show file tree
Hide file tree
Showing 18 changed files with 61 additions and 30 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Changed

* Out-of-bounds literals are now an error rather than a warning.

### Fixed

* Fix bug in slice simplification (#992).
Expand Down
1 change: 0 additions & 1 deletion examples/rosettacode/md5.fut
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ let rs: [64]u32 =
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21 ]

let ks: [64]u32 =
map us32
[ 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee ,
0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501 ,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be ,
Expand Down
3 changes: 2 additions & 1 deletion src/Language/Futhark/TypeChecker/Terms.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2464,7 +2464,8 @@ literalOverflowCheck = void . check
inBoundsF x Float32 = not $ isInfinite (realToFrac x :: Float)
inBoundsF x Float64 = not $ isInfinite x
warnBounds inBounds x ty loc = unless inBounds
$ warn loc $ "Literal " <> show x <> " out of bounds for inferred type " <> pretty ty <> "."
$ typeError loc mempty $ "Literal " <> ppr x <>
" out of bounds for inferred type " <> ppr ty <> "."

-- | Type-check a top-level (or module-level) function definition.
-- Despite the name, this is also used for checking constant
Expand Down
26 changes: 13 additions & 13 deletions tests/bitwise.fut
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@
-- Originally distilled from MD5 sum calculation.
-- ==
-- input {
-- 1732584193
-- -271733879
-- -1732584194
-- 271733878
-- 1732584193u32
-- -271733879u32
-- -1732584194u32
-- 271733878u32
-- 1
-- }
-- output {
-- 271733878
-- 757607282
-- -271733879
-- -1732584194
-- 271733878u32
-- 757607282u32
-- -271733879u32
-- -1732584194u32
-- }

let funF(x: i32, y: i32, z: i32): i32 = x & y | !x & z
let funF(x: u32, y: u32, z: u32): u32 = x & y | !x & z

let rotateL (x: i32, i: i32): i32 =
let rotateL (x: u32, i: u32): u32 =
let post = x << i
let pre = (x >> i) & (!(0xFFFFFFFF << i)) in
post | pre

let frob(a: i32, b: i32, c: i32, d: i32): (i32, i32, i32, i32) =
let frob(a: u32, b: u32, c: u32, d: u32): (u32, u32, u32, u32) =
let w = 0x97989910
let f' = funF(b,c,d)
let a' = b + rotateL((a + f' + w + 0xd76aa478), 7) in
(d, a', b, c)

let main (a: i32) (b: i32) (c: i32) (d: i32) (n: i32): (i32, i32, i32, i32) =
loop (a',b',c',d') = (a,b,c,d) for i < n do
let main (a: u32) (b: u32) (c: u32) (d: u32) (n: i32): (u32, u32, u32, u32) =
loop (a',b',c',d') = (a,b,c,d) for _i < n do
frob(a',b',c',d')
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
-- Some edge cases of literals that don't overflow, but are close
--
-- ==
-- warning: ^$

entry main : (i8, i8, u16, f64) = (-128, 127, 65535, 1.79e308)
4 changes: 4 additions & 0 deletions tests/overflowing/f32high.fut
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- ==
-- error: (out of bounds.*)

let main : f32 = 9.7e42
4 changes: 4 additions & 0 deletions tests/overflowing/f32low.fut
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- ==
-- error: (out of bounds.*)

let main : f32 = -1e40
4 changes: 4 additions & 0 deletions tests/overflowing/f64low.fut
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- ==
-- error: (out of bounds.*)

let main : f64 = 1.8e308
4 changes: 4 additions & 0 deletions tests/overflowing/i16low.fut
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- ==
-- error: (out of bounds.*)

let main : i16 = -10000000
4 changes: 4 additions & 0 deletions tests/overflowing/i32high.fut
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- ==
-- error: (out of bounds.*)

let main : i32 = 1000000000000
4 changes: 4 additions & 0 deletions tests/overflowing/i8high.fut
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- ==
-- error: (out of bounds.*)

let main : i8 = 128
4 changes: 4 additions & 0 deletions tests/overflowing/i8low.fut
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- ==
-- error: (out of bounds.*)

let main : i8 = -129
4 changes: 4 additions & 0 deletions tests/overflowing/u16high.fut
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- ==
-- error: (out of bounds.*)

let main : u16 = 100000
4 changes: 4 additions & 0 deletions tests/overflowing/u32low.fut
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- ==
-- error: (out of bounds.*)

let main : u32 = -4
4 changes: 4 additions & 0 deletions tests/overflowing/u8high.fut
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- ==
-- error: (out of bounds.*)

let main : u8 = 256
4 changes: 4 additions & 0 deletions tests/overflowing/u8low.fut
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- ==
-- error: (out of bounds.*)

let main : u8 = -4
7 changes: 0 additions & 7 deletions tests/overflowing_lits0.fut

This file was deleted.

7 changes: 0 additions & 7 deletions tests/overflowing_lits1.fut

This file was deleted.

0 comments on commit dc21049

Please sign in to comment.