Skip to content

Commit

Permalink
Add error checking for array dimensions when using interpreter fix #1682
Browse files Browse the repository at this point in the history
 (#2012)

* Add error checking for array dimensions when using interpreter fix #1682

* Fix style based on hlint info
  • Loading branch information
dominicmkennedy authored Aug 28, 2023
1 parent c56ccca commit 185a84c
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Language/Futhark/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2028,12 +2028,18 @@ interpretFunction ctx fname vs = do
updateType _ t =
Right t

-- FIXME: we don't check array sizes.
checkInput :: ValueType -> StructType -> Either T.Text ()
checkInput (Scalar (Prim vt)) (Scalar (Prim pt))
| vt /= pt = badPrim vt pt
checkInput (Array _ _ (Prim vt)) (Array _ _ (Prim pt))
| vt /= pt = badPrim vt pt
checkInput vArr@(Array _ (F.Shape vd) _) pArr@(Array _ (F.Shape pd) _)
| length vd /= length pd = badDim vArr pArr
| not . and $ zipWith sameShape vd pd = badDim vArr pArr
where
sameShape :: Int64 -> Size -> Bool
sameShape shape0 (IntLit shape1 _ _) = fromIntegral shape0 == shape1
sameShape _ _ = True
checkInput _ _ =
Right ()

Expand All @@ -2044,3 +2050,11 @@ interpretFunction ctx fname vs = do
<+> align (pretty pt)
</> "Got: "
<+> align (pretty vt)

badDim vd pd =
Left . docText $
"Invalid argument dimensions."
</> "Expected:"
<+> align (pretty pd)
</> "Got: "
<+> align (pretty vd)

0 comments on commit 185a84c

Please sign in to comment.