From b363051343654da7291db1361c408d79a1cb0648 Mon Sep 17 00:00:00 2001 From: David Feuer Date: Mon, 12 Feb 2024 13:47:06 -0500 Subject: [PATCH] Use fail instead of error `error` in Template Haskell splices should be used to report bugs in the Template Haskell code, and ideally not to report bugs in the user code calling it. When GHC encounters `error`, it gives a rather more verbose (and ugly) error report, based on that assumption. The usual way to report a problem in this context is to use `fail`. --- src/Debug/Dump.hs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/Debug/Dump.hs b/src/Debug/Dump.hs index 4f9936e..fa9ffdd 100644 --- a/src/Debug/Dump.hs +++ b/src/Debug/Dump.hs @@ -31,7 +31,14 @@ import qualified Internal.Parser as Parser -- | This is the main `QuasiQuoter`. dump :: QuasiQuoter -dump = QuasiQuoter {quoteExp = process} +dump = QuasiQuoter + { quoteExp = process + , quoteType = \_ -> fl "use in types." + , quotePat = \_ -> fl "use in patterns." + , quoteDec = \_ -> fl "generating declarations." + } + where + fl m = fail $ "This quasiquoter is not for " ++ m -- | Shorthand for `dump`. d :: QuasiQuoter @@ -61,7 +68,6 @@ process = id .> joinAsColumns .> (fmap (++ "\n")) .> wrapInParens .> parseHsStrToQQExp str - .> return removeLineComments = id .> lines @@ -90,15 +96,15 @@ joinAsColumns = sequenceA .> fmap (intercalate [qc|{nl} ++ "\t " ++ |]) wrapInParens :: HsExp String -> HsExp String wrapInParens = fmap Utils.wrapInParens -parseHsStrToQQExp :: String -> HsExp String -> Exp +parseHsStrToQQExp :: String -> HsExp String -> Q Exp parseHsStrToQQExp original = id .> unHsExp .> \s -> s $> id .> parseExp .> let e = [qc|Debug.Dump: parseHsStrToQQExp:{indent 2 original}{indent 10 s})|] in id - .> let ef = (e ++) .> error in id - .> either ef id + .> let ef = (e ++) .> fail in id + .> either ef pure nl = "\n"