Convert XException to ErrorCall - #1461
Conversation
|
We could also use Detailsdiff --git a/clash-prelude/src/Clash/XException.hs b/clash-prelude/src/Clash/XException.hs
index 7c702e3f..b5566331 100644
--- a/clash-prelude/src/Clash/XException.hs
+++ b/clash-prelude/src/Clash/XException.hs
@@ -125,9 +125,9 @@ errorX msg = throw (XException ("X: " ++ msg ++ "\n" ++ prettyCallStack callStac
-- > f (xToErrorCtx "a is X" -> !a) (xToErrorCtx "b is X" -> !b) = ...
--
-- __NB:__ Fully synthesisable, so doesn't have to be removed before synthesis
-xToErrorCtx :: String -> a -> a
+xToErrorCtx :: HasCallStack => String -> a -> a
xToErrorCtx ctx a = unsafeDupablePerformIO
- (catch (evaluate a >> return a) (\(XException msg) -> throw (ErrorCall (ctx <> "\n" <> msg))))
+ (catch (evaluate a >> return a) (\(XException msg) -> throw (ErrorCall (ctx <> "\n" <> prettyCallStack callStack <> "\n" <> msg))))
{-# NOINLINE xToErrorCtx #-} |
|
I doesn't have to be added to the primitive though |
|
Right, we can keep the primitive as it is and create a wrapper that passed the rendered callstack as a string |
|
The callstack can also be used to trace further back before f, g, h :: HasCallStack => Bool -> BitVector 8 -> BitVector 8
f = g
g = h
h (xToErrorCtx "a is X" -> a) (xToErrorCtx "b is X" -> b) = slice d7 d0 (pack a ++# b) |
martijnbastiaan
left a comment
There was a problem hiding this comment.
Looks good, have you tested it? Could be good to add a couple of tests testing the synthesizability / whether it actually displays usable error messages.
| -- > {-# LANGUAGE ViewPatterns, BangPatterns #-} | ||
| -- > f (xToErrorCtx "a is X" -> !a) (xToErrorCtx "b is X" -> !b) = ... | ||
| -- | ||
| -- __NB:__ Fully synthesisable, so doesn't have to be removed before synthesis |
|
@leonschoorl @martijnbastiaan could you review again to see whether it looks good now? |
| go "Clash.Magic.noDeDup" args | ||
| | [_aTy,f] <- args | ||
| = C.Tick C.NoDeDup <$> term f | ||
| go "Clash.XException.xToErrorCtx" args -- xToError :: forall a. String -> a -> a |
There was a problem hiding this comment.
Please add the callstack argument to the comment
There was a problem hiding this comment.
And the correct primitive name
|
@leonschoorl I think I've addressed all your points now |
| -- errorX, called at ... | ||
| -- <BLANKLINE> | ||
| xToError :: HasCallStack => a -> a | ||
| xToError = xToErrorCtx (prettyCallStack (popCallStack callStack)) |
There was a problem hiding this comment.
Why do you popCallStack?
Doesn't that remove the removes the call that will tell me whether it was the a or the b argument to h that contained the XException?
To make it easier to report XException when `pack` would normally hide them.
To make it easier to report
XExceptionwhenpackwould normally hide them.