-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve ranges in error messages (#2733)
- Loading branch information
1 parent
ebde652
commit 0fe5d49
Showing
9 changed files
with
46 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CHANGED: The error messages that mention the valid ranges for out-of-range inputs have been improved to be more intuitive: one of `<empty range>`, `[n]` or `[n..m]`. All _n..m_ ranges are now ordered with the lower bound on the left. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{-| | ||
Copyright : (C) 2013-2016, University of Twente | ||
2020, Myrtle Software Ltd | ||
2024, QBayLogic B.V. | ||
License : BSD2 (see the file LICENSE) | ||
Maintainer : QBayLogic B.V. <[email protected]> | ||
-} | ||
|
@@ -26,6 +27,8 @@ import Data.Proxy (Proxy(Proxy)) | |
import GHC.Stack (HasCallStack) | ||
import GHC.TypeLits (Nat, KnownNat, type (+)) | ||
|
||
import Clash.Sized.Internal (formatRange) | ||
|
||
-- | Coerce a value to be represented by a different number of bits | ||
class Resize (f :: Nat -> Type) where | ||
-- | A sign-preserving resize operation | ||
|
@@ -61,9 +64,8 @@ checkIntegral Proxy v = | |
if toInteger v > toInteger (maxBound @b) | ||
|| toInteger v < toInteger (minBound @b) then | ||
error $ "Given integral " <> show (toInteger v) <> " is out of bounds for" <> | ||
" target type. Bounds of target type are: [" <> | ||
show (toInteger (minBound @b)) <> ".." <> | ||
show (toInteger (maxBound @b)) <> "]." | ||
" target type. Bounds of target type are: " <> | ||
formatRange (toInteger (minBound @b)) (toInteger (maxBound @b)) <> "." | ||
else | ||
() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{-| | ||
Copyright : (C) 2024 , QBayLogic B.V. | ||
License : BSD2 (see the file LICENSE) | ||
Maintainer : QBayLogic B.V. <[email protected]> | ||
-} | ||
|
||
module Clash.Sized.Internal where | ||
|
||
-- | Format a range of numbers for use in error messages | ||
-- | ||
-- If the upper bound is below the lower bound, @"\<empty range\>"@ is returned. | ||
-- If the bounds are equal, @"[n]"@ is returned (for bounds equal to /n/). | ||
-- Otherwise, @formatRange n m@ returns @"[n..m]"@. | ||
formatRange :: | ||
(Ord a, Show a) => | ||
-- | Lower bound | ||
a -> | ||
-- | Upper bound | ||
a -> | ||
String | ||
formatRange n m | ||
| m < n = "<empty range>" | ||
| m == n = '[' : shows n "]" | ||
| otherwise = '[' : show n ++ ".." ++ shows m "]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{-| | ||
Copyright : (C) 2013-2016, University of Twente, | ||
2016-2019, Myrtle Software Ltd, | ||
2021-2023, QBayLogic B.V. | ||
2021-2024, QBayLogic B.V. | ||
License : BSD2 (see the file LICENSE) | ||
Maintainer : QBayLogic B.V. <[email protected]> | ||
-} | ||
|
@@ -108,6 +108,7 @@ import Clash.Class.Num (ExtendingNum (..), SaturatingNum (..), | |
import Clash.Class.Parity (Parity (..)) | ||
import Clash.Class.Resize (Resize (..)) | ||
import Clash.Class.BitPack.BitIndex (replaceBit) | ||
import Clash.Sized.Internal (formatRange) | ||
import {-# SOURCE #-} Clash.Sized.Internal.BitVector (BitVector (BV), high, low, undefError) | ||
import qualified Clash.Sized.Internal.BitVector as BV | ||
import Clash.Promoted.Nat (SNat(..), snatToNum, natToInteger, leToPlusKN) | ||
|
@@ -347,7 +348,7 @@ fromInteger_INLINE i = bound `seq` if i > (-1) && i < bound then I i else err | |
where | ||
bound = natToInteger @n | ||
err = errorX ("Clash.Sized.Index: result " ++ show i ++ | ||
" is out of bounds: [0.." ++ show (bound - 1) ++ "]") | ||
" is out of bounds: " ++ formatRange 0 (bound - 1)) | ||
|
||
instance ExtendingNum (Index m) (Index n) where | ||
type AResult (Index m) (Index n) = Index (m + n - 1) | ||
|
@@ -591,5 +592,5 @@ instance (KnownNat n) => Ix (Index n) where | |
range (a, b) = [a..b] | ||
index ab@(a, b) x | ||
| inRange ab x = fromIntegral $ x - a | ||
| otherwise = error $ printf "Index %d out of bounds (%d, %d)" x a b | ||
| otherwise = error $ printf "Index (%d) out of range ((%d, %d))" x a b | ||
inRange (a, b) x = a <= x && x <= b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{-| | ||
Copyright : (C) 2013-2016, University of Twente, | ||
2016 , Myrtle Software Ltd, | ||
2021-2023, QBayLogic B.V. | ||
2021-2024, QBayLogic B.V. | ||
License : BSD2 (see the file LICENSE) | ||
Maintainer : QBayLogic B.V. <[email protected]> | ||
-} | ||
|
@@ -881,7 +881,7 @@ instance (KnownNat n) => Ix (Signed n) where | |
range (a, b) = [a..b] | ||
index ab@(a, b) x | ||
| inRange ab x = fromIntegral $ x - a | ||
| otherwise = error $ printf "Index %d out of bounds (%d, %d) ab" x a b | ||
| otherwise = error $ printf "Index (%d) out of range ((%d, %d))" x a b | ||
inRange (a, b) x = a <= x && x <= b | ||
|
||
-- | Shift left that ties to zero on negative shifts | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{-| | ||
Copyright : (C) 2013-2016, University of Twente, | ||
2016 , Myrtle Software Ltd, | ||
2021-2023, QBayLogic B.V. | ||
2021-2024, QBayLogic B.V. | ||
License : BSD2 (see the file LICENSE) | ||
Maintainer : QBayLogic B.V. <[email protected]> | ||
-} | ||
|
@@ -794,7 +794,7 @@ instance (KnownNat n) => Ix (Unsigned n) where | |
range (a, b) = [a..b] | ||
index ab@(a, b) x | ||
| inRange ab x = fromIntegral $ x - a | ||
| otherwise = error $ printf "Index %d out of bounds (%d, %d) ab" x a b | ||
| otherwise = error $ printf "Index (%d) out of range ((%d, %d))" x a b | ||
inRange (a, b) x = a <= x && x <= b | ||
|
||
unsignedToWord :: Unsigned WORD_SIZE_IN_BITS -> Word | ||
|