Improve the index checking for array accesses; fixes #2120 #2669
authorIan Lynagh <igloo@earth.li>
Sun, 19 Jul 2009 15:32:28 +0000 (15:32 +0000)
committerIan Lynagh <igloo@earth.li>
Sun, 19 Jul 2009 15:32:28 +0000 (15:32 +0000)
As well as checking that offset we are reading is actually inside the
array, we now also check that it is "in range" as defined by the Ix
instance. This fixes confusing behaviour (#2120) and improves some error
messages (#2669).

GHC/Arr.lhs

index dbd7975..7dd0e90 100644 (file)
@@ -435,10 +435,11 @@ safeRangeSize (l,u) = let r = rangeSize (l, u)
 
 {-# INLINE safeIndex #-}
 safeIndex :: Ix i => (i, i) -> Int -> i -> Int
-safeIndex (l,u) n i = let i' = unsafeIndex (l,u) i
+safeIndex (l,u) n i = let i' = index (l,u) i
                       in if (0 <= i') && (i' < n)
                          then i'
-                         else error "Error in array index"
+                         else error ("Error in array index; " ++ show i' ++
+                                     " not in range [0.." ++ show n ++ ")")
 
 {-# INLINE unsafeAt #-}
 unsafeAt :: Ix i => Array i e -> Int -> e