git.iko.soy/typeable/haskell-stack-trace-plugin
on master help sign in
7 files changed+137−40
since splitsnapshots
unifiedsplit
.github/workflows/cabal.yml+1−1 CHANGELOG.md+6 Readme.md+30−22 example/Main.hs+9−3 haskell-stack-trace-plugin.cabal+6−5 src/StackTrace/Plugin.hs+81−7 test/resource/ghc.output+4−2
.github/workflows/cabal.yml+1−1
@@ -13,7 +13,7 @@ jobs:
1313 runs-on: ubuntu-18.04
1414 strategy:
1515 matrix:
16− ghc: ["8.6", "8.8", "8.10"]
16+ ghc: ["8.6", "8.8", "8.10", "9.0"]
1717 cabal: ["3.2"]
1818 cache-version: ["2021-05-21"]
1919
CHANGELOG.md+6
@@ -2,9 +2,15 @@
22
33## Unreleased changes
44
5+## 0.1.3.0
6+
7+- Added support `where` clause [#11](https://github.com/waddlaw/haskell-stack-trace-plugin/pull/11) (@waddlaw)
8+- Avoid redundant-constraints warnings [#12](https://github.com/waddlaw/haskell-stack-trace-plugin/pull/12) (@waddlaw)
9+
510## 0.1.2.0 -- 2021-05-21
611
712- Added support for GHC 8.10 [#7](https://github.com/waddlaw/haskell-stack-trace-plugin/pull/7) (@etorreborre)
13+- Added support for GHC 9.0 @waddlaw
814
915## 0.1.1.1 -- 2020-01-18
1016
Readme.md+30−22
@@ -5,8 +5,10 @@
55
66This plugin allow implicitly add `HasCallStack` class to every top-level function for all module. Hence, we can to get completely continuous call stack.
77
8−1. (implicitly) Import [GHC.Stack](https://hackage.haskell.org/package/base-4.12.0.0/docs/GHC-Stack.html) for all modules.
9−2. Add [HasCallStack](https://hackage.haskell.org/package/base-4.12.0.0/docs/GHC-Stack.html#t:HasCallStack) constraint for all top-level functions.
8+1. (implicitly) Import [GHC.Stack](https://hackage.haskell.org/package/base/docs/GHC-Stack.html) for all modules.
9+2. Add [HasCallStack](https://hackage.haskell.org/package/base/docs/GHC-Stack.html#t:HasCallStack) constraint for all top-level functions.
10+3. Other supported syntaxes
11+ - [x] `where` clause
1012
1113Requirement: (8.6 <= on GHC)
1214
@@ -32,10 +34,10 @@ f3 = f4 0
3234
3335-- HsQualTy
3436f4 :: Show a => a -> Int
35−f4 _ = f5 0 0
37+f4 n = f5 (show n) 0
3638
3739-- HsFunTy
38−f5 :: Int -> Int -> Int
40+f5 :: String -> Int -> Int
3941f5 _ _ = head f6
4042
4143-- HsListTy
@@ -48,7 +50,13 @@ f7 = (fromJust f8, fromJust f8)
4850
4951-- HsAppTy
5052f8 :: Maybe Int
51−f8 = Just fError
53+f8 = Just f9
54+
55+f9 :: Int
56+f9 = f10
57+ where
58+ f10 :: Int
59+ f10 = fError
5260
5361-- HsTyVar
5462fError :: Int
@@ -58,7 +66,7 @@ fError = error "fError"
5866This example get error:
5967
6068```shell
61−$ cabal new-build
69+$ cabal build
6270example/Main.hs:15:7: error:
6371 Not in scope: type constructor or class ‘HasCallStack’
6472 |
@@ -71,10 +79,10 @@ Yes, add `import GHC.Stack` to above example.
7179Fix and rebuild!
7280
7381```shell
74−$ cabal new-run
82+$ cabal run example -v0
7583example: fError
7684CallStack (from HasCallStack):
77− error, called at example/Main.hs:41:10 in main:Main
85+ error, called at example/Main.hs:47:10 in main:Main
7886```
7987
8088Hmm, it is not useful. But, you will to be happy when enable this plugin.
@@ -85,22 +93,22 @@ Hmm, it is not useful. But, you will to be happy when enable this plugin.
8593```
8694
8795```shell
88−$ cabal new-run
89−...
90−
96+$ cabal run example -v0
9197example: fError
9298CallStack (from HasCallStack):
93− error, called at example/Main.hs:40:10 in main:Main
94− fError, called at example/Main.hs:36:11 in main:Main
95− f8, called at example/Main.hs:32:16 in main:Main
96− f7, called at example/Main.hs:28:11 in main:Main
97− f6, called at example/Main.hs:24:15 in main:Main
98− f5, called at example/Main.hs:20:8 in main:Main
99− f4, called at example/Main.hs:16:6 in main:Main
100− f3, called at example/Main.hs:12:6 in main:Main
101− f2, called at example/Main.hs:9:6 in main:Main
102− f1, called at example/Main.hs:6:14 in main:Main
103− main, called at example/Main.hs:6:1 in main:Main
99+ error, called at example/Main.hs:47:10 in main:Main
100+ fError, called at example/Main.hs:43:11 in main:Main
101+ f10, called at example/Main.hs:40:6 in main:Main
102+ f9, called at example/Main.hs:37:11 in main:Main
103+ f8, called at example/Main.hs:33:16 in main:Main
104+ f7, called at example/Main.hs:29:11 in main:Main
105+ f6, called at example/Main.hs:25:15 in main:Main
106+ f5, called at example/Main.hs:21:8 in main:Main
107+ f4, called at example/Main.hs:17:6 in main:Main
108+ f3, called at example/Main.hs:13:6 in main:Main
109+ f2, called at example/Main.hs:10:6 in main:Main
110+ f1, called at example/Main.hs:7:14 in main:Main
111+ main, called at example/Main.hs:7:1 in main:Main
104112```
105113
106114Great!!!
example/Main.hs+9−3
@@ -18,10 +18,10 @@ f3 = f4 0
1818
1919-- HsQualTy
2020f4 :: Show a => a -> Int
21−f4 _ = f5 0 0
21+f4 n = f5 (show n) 0
2222
2323-- HsFunTy
24−f5 :: Int -> Int -> Int
24+f5 :: String -> Int -> Int
2525f5 _ _ = head f6
2626
2727-- HsListTy
@@ -34,7 +34,13 @@ f7 = (fromJust f8, fromJust f8)
3434
3535-- HsAppTy
3636f8 :: Maybe Int
37−f8 = Just fError
37+f8 = Just f9
38+
39+f9 :: Int
40+f9 = f10
41+ where
42+ f10 :: Int
43+ f10 = fError
3844
3945-- HsTyVar
4046fError :: Int
haskell-stack-trace-plugin.cabal+6−5
@@ -1,6 +1,6 @@
11cabal-version: 2.4
22name: haskell-stack-trace-plugin
3−version: 0.1.2.0
3+version: 0.1.3.0
44synopsis: haskell-stack-trace-plugin
55description:
66 This plugin allow implicitly add HasCallStack class to every top-level function for all module. Hence, we can to get completely continuous call stack.
@@ -20,7 +20,7 @@ extra-source-files:
2020 CHANGELOG.md
2121 Readme.md
2222
23−tested-with: GHC ==8.6.5 || ==8.8.4 || ==8.10.4
23+tested-with: GHC ==8.6.5 || ==8.8.4 || ==8.10.4 || ==9.0.1
2424
2525source-repository head
2626 type: git
@@ -32,19 +32,20 @@ flag dev
3232 default: False
3333
3434common common-opts
35− build-depends: base >=4.12 && <4.15
35+ build-depends: base >=4.12 && <4.18
3636 default-language: Haskell2010
3737
3838library
3939 import: common-opts
4040 hs-source-dirs: src
41− build-depends: ghc ^>=8.6 || ^>=8.8 || ^>=8.10
41+ build-depends: ghc ^>=8.6 || ^>=8.8 || ^>=8.10 || ^>=9.0 || ^>=9.2 || ^>=9.4
4242 exposed-modules: StackTrace.Plugin
4343
4444 if flag(dev)
4545 ghc-options:
4646 -Wall -Werror -Wcompat -Wincomplete-uni-patterns
4747 -Wnoncanonical-monad-instances -Wno-missing-home-modules
48+
4849 else
4950 ghc-options: -Wall
5051
@@ -71,5 +72,5 @@ executable example
7172 import: common-opts
7273 main-is: Main.hs
7374 hs-source-dirs: example
74− ghc-options: -fplugin=StackTrace.Plugin
75+ ghc-options: -fplugin=StackTrace.Plugin -Wredundant-constraints
7576 build-depends: haskell-stack-trace-plugin
src/StackTrace/Plugin.hs+81−7
@@ -5,7 +5,13 @@ module StackTrace.Plugin (plugin) where
55
66import Control.Arrow (first)
77import Data.Monoid (Any(Any, getAny))
8+import GHC.Types.SrcLoc
9+#if __GLASGOW_HASKELL__ >= 900
10+import GHC.Plugins
11+#else
12+
813import GhcPlugins
14+#endif
915#if __GLASGOW_HASKELL__ >= 810
1016import GHC.Hs
1117#endif
@@ -44,11 +50,20 @@ importDeclQualified = QualifiedPre
4450
4551ghcStackImport :: Located (ImportDecl (GhcPass p))
4652ghcStackImport =
47− noLoc $
53+ L srcSpan $
4854 (simpleImportDecl $ mkModuleName "GHC.Stack")
49− {ideclQualified = importDeclQualified, ideclAs = Just $ noLoc ghcStackModuleName}
55+ { ideclQualified = importDeclQualified, ideclAs = Just $ noLoc ghcStackModuleName }
56+ where
57+ -- This is for GHC-9 related problems. @noLoc@ causes GHC to throw warnings
58+ -- about unused imports. Even if the import is used
59+ -- See: https://github.com/waddlaw/haskell-stack-trace-plugin/issues/16
60+ srcSpan = RealSrcSpan (realSrcLocSpan $ mkRealSrcLoc "haskell-stack-trace-plugin:very-unique-file-name-to-avoid-collision" 1 1) Nothing
5061
62+#if __GLASGOW_HASKELL__ >= 900
63+updateHsModule :: HsModule -> HsModule
64+#else
5165updateHsModule :: HsModule GhcPs -> HsModule GhcPs
66+#endif
5267updateHsModule hsm =
5368 hsm {hsmodImports = hsmodImports', hsmodDecls = hsmodDecls'}
5469 where
@@ -70,7 +85,6 @@ updateHsModule hsm =
7085 astTraversal :: Traversal' (LHsDecl GhcPs) (HsType GhcPs)
7186 astTraversal = updateHsmodDecl
7287 . updateHsDecl
73− . updateSig
7488 . updateLHsSigWsType
7589 . updateLHsSigType
7690 . updateLHsType
@@ -79,10 +93,56 @@ updateHsModule hsm =
7993updateHsmodDecl :: Traversal' (LHsDecl GhcPs) (HsDecl GhcPs)
8094updateHsmodDecl = traverse
8195
82−updateHsDecl :: Traversal' (HsDecl GhcPs) (Sig GhcPs)
83−updateHsDecl f (SigD xSig s) = SigD xSig <$> f s
96+updateHsDecl :: Traversal' (HsDecl GhcPs) (LHsSigWcType GhcPs)
97+updateHsDecl f (SigD xSig s) = SigD xSig <$> updateSig f s
98+updateHsDecl f (ValD xVal hsBind) = ValD xVal <$> updateHsBind f hsBind
8499updateHsDecl _ sig = pure sig
85100
101+updateHsBind :: Traversal' (HsBind GhcPs) (LHsSigWcType GhcPs)
102+updateHsBind f bind@FunBind {} = (\x -> bind {fun_matches = x}) <$> updateMatchGroup f (fun_matches bind)
103+updateHsBind _ bind = pure bind
104+
105+updateMatchGroup :: Traversal' (MatchGroup GhcPs (LHsExpr GhcPs)) (LHsSigWcType GhcPs)
106+updateMatchGroup f mg@MG {} = (\x -> mg {mg_alts = x}) <$> updateLLMatch f (mg_alts mg)
107+#if __GLASGOW_HASKELL__ < 900
108+updateMatchGroup _ mg = pure mg
109+#endif
110+
111+updateLocated :: Functor f => (a -> b -> f c) -> a -> Located b -> f (Located c)
112+updateLocated f g (L l e) = L l <$> f g e
113+
114+updateLLMatch :: Traversal' (Located [LMatch GhcPs (LHsExpr GhcPs)]) (LHsSigWcType GhcPs)
115+updateLLMatch = updateLocated updateLMatches
116+
117+updateLMatches :: Traversal' [LMatch GhcPs (LHsExpr GhcPs)] (LHsSigWcType GhcPs)
118+updateLMatches f = traverse (updateLocated updateMatch f)
119+
120+updateMatch :: Traversal' (Match GhcPs (LHsExpr GhcPs)) (LHsSigWcType GhcPs)
121+updateMatch f m@Match {} = (\x -> m {m_grhss = x}) <$> updateGrhss f (m_grhss m)
122+#if __GLASGOW_HASKELL__ < 900
123+updateMatch _ m = pure m
124+#endif
125+
126+updateGrhss :: Traversal' (GRHSs GhcPs (LHsExpr GhcPs)) (LHsSigWcType GhcPs)
127+updateGrhss f grhss@GRHSs {} = (\x -> grhss {grhssLocalBinds = x}) <$> updateLHsLocalBinds f (grhssLocalBinds grhss)
128+#if __GLASGOW_HASKELL__ < 900
129+updateGrhss _ grhss = pure grhss
130+#endif
131+
132+updateLHsLocalBinds :: Traversal' (LHsLocalBinds GhcPs) (LHsSigWcType GhcPs)
133+updateLHsLocalBinds = updateLocated updateLocalBinds
134+
135+updateLocalBinds :: Traversal' (HsLocalBinds GhcPs) (LHsSigWcType GhcPs)
136+updateLocalBinds f (HsValBinds xHsValBinds hsValBindsLR) = HsValBinds xHsValBinds <$> updateHsValBindsLR f hsValBindsLR
137+updateLocalBinds _ hsValBinds = pure hsValBinds
138+
139+updateHsValBindsLR :: Traversal' (HsValBindsLR GhcPs GhcPs) (LHsSigWcType GhcPs)
140+updateHsValBindsLR f (ValBinds xValBinds lHsBindsLR lSigs) = ValBinds xValBinds lHsBindsLR <$> updateLSigs f lSigs
141+updateHsValBindsLR _ valBinds = pure valBinds
142+
143+updateLSigs :: Traversal' [LSig GhcPs] (LHsSigWcType GhcPs)
144+updateLSigs f = traverse (updateLocated updateSig f)
145+
86146updateSig :: Traversal' (Sig GhcPs) (LHsSigWcType GhcPs)
87147updateSig f (TypeSig xSig ls t) = TypeSig xSig ls <$> f t
88148updateSig _ sig = pure sig
@@ -90,20 +150,26 @@ updateSig _ sig = pure sig
90150updateLHsSigWsType :: Traversal' (LHsSigWcType GhcPs) (LHsSigType GhcPs)
91151updateLHsSigWsType f lhs@HsWC {} =
92152 (\x -> lhs {hswc_body = x}) <$> f (hswc_body lhs)
153+#if __GLASGOW_HASKELL__ < 900
93154updateLHsSigWsType _ lhs = pure lhs
155+#endif
94156
95157updateLHsSigType :: Traversal' (LHsSigType GhcPs) (LHsType GhcPs)
96158updateLHsSigType f lhs@HsIB {} =
97159 (\x -> lhs {hsib_body = x}) <$> f (hsib_body lhs)
160+#if __GLASGOW_HASKELL__ < 900
98161updateLHsSigType _ lhs = pure lhs
162+#endif
99163
100164updateLHsType :: Traversal' (LHsType GhcPs) (HsType GhcPs)
101165updateLHsType = traverse
102166
103167-- Main process
104168updateHsType :: HsType GhcPs -> (Any, HsType GhcPs)
105−updateHsType (HsQualTy xty ctxt body) =
106− flagASTModified $ HsQualTy xty (fmap appendHSC ctxt) body
169+updateHsType ty@(HsQualTy xty ctxt body) =
170+ if hasHasCallStack (unLoc ctxt)
171+ then pure ty
172+ else flagASTModified $ HsQualTy xty (fmap appendHSC ctxt) body
107173updateHsType ty@HsTyVar {} =
108174 flagASTModified $ HsQualTy xQualTy (noLoc $ appendHSC []) (noLoc ty)
109175updateHsType ty@HsAppTy {} =
@@ -117,6 +183,7 @@ updateHsType ty@HsTupleTy {} =
117183updateHsType ty = pure ty
118184
119185#if __GLASGOW_HASKELL__ < 810
186+xQualTy :: NoExt
120187xQualTy = noExt
121188#else
122189xQualTy :: NoExtField
@@ -129,6 +196,13 @@ flagASTModified a = (Any True, a)
129196appendHSC :: HsContext GhcPs -> HsContext GhcPs
130197appendHSC cs = mkHSC : cs
131198
199+hasHasCallStack :: HsContext GhcPs -> Bool
200+hasHasCallStack = any (checkHsType . unLoc)
201+ where
202+ checkHsType :: HsType GhcPs -> Bool
203+ checkHsType (HsTyVar _ _ lid) = unLoc lid == (mkRdrUnqual $ mkClsOcc "HasCallStack")
204+ checkHsType _ = False
205+
132206-- make HasCallStack => constraint
133207mkHSC :: LHsType GhcPs
134208mkHSC = noLoc $ HsTyVar xQualTy NotPromoted lId
test/resource/ghc.output+4−2
@@ -1,7 +1,9 @@
11example: fError
22CallStack (from HasCallStack):
3− error, called at example/Main.hs:41:10 in main:Main
4− fError, called at example/Main.hs:37:11 in main:Main
3+ error, called at example/Main.hs:47:10 in main:Main
4+ fError, called at example/Main.hs:43:11 in main:Main
5+ f10, called at example/Main.hs:40:6 in main:Main
6+ f9, called at example/Main.hs:37:11 in main:Main
57 f8, called at example/Main.hs:33:16 in main:Main
68 f7, called at example/Main.hs:29:11 in main:Main
79 f6, called at example/Main.hs:25:15 in main:Main