-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert XException to ErrorCall #1461
Conversation
We could also use
diff --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)
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
synthesizable 🇺🇸
@leonschoorl @martijnbastiaan could you review again to see whether it looks good now? |
@@ -364,6 +364,9 @@ coreToTerm primMap unlocs = term | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the callstack argument to the comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
XException
whenpack
would normally hide them.