Skip to content

Commit

Permalink
Fix inc. ranges with start == end and negative step (2) (#2195)
Browse files Browse the repository at this point in the history
This changes behavior of inclusive ranges for the case where
`start == end` and `start != second`, for compiled programs only.
In this case the result is now `[start]` rather than a runtime error.

Example: `1..0...1` now produces `[1]` rather than an "invalid range"
runtime error.

This makes behavior for compiled programs similar to that of the Futhark
interpreter (and e.g. Haskell ranges), for at least this particular
case (I have not looked for other discrepancies).
  • Loading branch information
sortraev authored Nov 27, 2024
1 parent a50b40b commit a30295f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/Futhark/Internalise/Exps.hs
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@ internaliseAppExp desc _ (E.Range start maybe_second end loc) = do
)
++ [ErrorVal int64 end'_i64, " is invalid."]

(it, le_op, lt_op) <-
(it, lt_op) <-
case E.typeOf start of
E.Scalar (E.Prim (E.Signed it)) -> pure (it, CmpSle it, CmpSlt it)
E.Scalar (E.Prim (E.Unsigned it)) -> pure (it, CmpUle it, CmpUlt it)
E.Scalar (E.Prim (E.Signed it)) -> pure (it, CmpSlt it)
E.Scalar (E.Prim (E.Unsigned it)) -> pure (it, CmpUlt it)
start_t -> error $ "Start value in range has type " ++ prettyString start_t

let one = intConst it 1
Expand All @@ -245,7 +245,7 @@ internaliseAppExp desc _ (E.Range start maybe_second end loc) = do
bounds_invalid_downwards <-
letSubExp "bounds_invalid_downwards" $
I.BasicOp $
I.CmpOp le_op start' end'
I.CmpOp lt_op start' end'
bounds_invalid_upwards <-
letSubExp "bounds_invalid_upwards" $
I.BasicOp $
Expand Down
7 changes: 3 additions & 4 deletions tests/range0.fut
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,20 @@
-- input { 1 5 } output { [1i32, 2i32, 3i32, 4i32, 5i32] }
-- input { 5 1 } error: 5...1
-- input { 5 0 } error: 5...0
-- input { 0 5 } output { [0i32, 1i32, 2i32,
-- 3i32, 4i32, 5i32] }

-- ==
-- entry: test1
-- input { 0 5 } output { [0i32, 1i32, 2i32, 3i32, 4i32] }
-- input { 1 5 } output { [1i32, 2i32, 3i32, 4i32] }
-- input { 5 1 } error: 5..<1
-- input { 5 0 } error: 5..<0
-- input { 0 5 } output { [0i32, 1i32, 2i32, 3i32, 4i32] }

-- ==
-- entry: test2
-- input { 0 5 } error: 0..>5
-- input { 1 5 } error: 1..>5
-- input { 5 1 } output { [5i32, 4i32, 3i32, 2i32] }
-- input { 5 0 } output { [5i32, 4i32, 3i32, 2i32, 1i32] }
-- input { 0 5 } error: 0..>5

-- ==
-- entry: test3
Expand All @@ -31,6 +27,9 @@
-- input { 5 4 1 } output { [5i32, 4i32, 3i32, 2i32, 1i32] }
-- input { 5 0 0 } output { [5i32, 0i32] }
-- input { 0 2 5 } output { [0i32, 2i32, 4i32] }
-- input { 1 1 5 } error: 1..1...5
-- input { 1 0 1 } output { [1i32] }
-- input { 1 2 1 } output { [1i32] }

-- ==
-- entry: test4
Expand Down

0 comments on commit a30295f

Please sign in to comment.