@@ -1,23 +1,11 @@
11{-# LANGUAGE RankNTypes #-}
22{-# LANGUAGE OverloadedStrings #-}
3−{-# LANGUAGE CPP #-}
43module StackTrace.Plugin (plugin) where
54
65import Control.Arrow (first)
76import Data.Monoid (Any(Any, getAny))
8−import GHC.Types.SrcLoc
9−#if __GLASGOW_HASKELL__ >= 900
10−import GHC.Plugins
11−#else
12−
137import GhcPlugins
14−#endif
15−#if __GLASGOW_HASKELL__ >= 810
16−import GHC.Hs
17−#endif
18−#if __GLASGOW_HASKELL__ < 810
198import HsSyn
20−#endif
219
2210type Traversal s t a b
2311 = forall f. Applicative f =>
@@ -40,30 +28,13 @@ parsedPlugin _ _ pm = do
4028ghcStackModuleName :: ModuleName
4129ghcStackModuleName = mkModuleName "AutoImported.GHC.Stack"
4230
43−#if __GLASGOW_HASKELL__ < 810
44−importDeclQualified :: Bool
45−importDeclQualified = True
46−#else
47−importDeclQualified :: ImportDeclQualifiedStyle
48−importDeclQualified = QualifiedPre
49−#endif
50−
5131ghcStackImport :: Located (ImportDecl (GhcPass p))
5232ghcStackImport =
53− L srcSpan $
33+ noLoc $
5434 (simpleImportDecl $ mkModuleName "GHC.Stack")
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
61−
62−#if __GLASGOW_HASKELL__ >= 900
63−updateHsModule :: HsModule -> HsModule
64−#else
35+ {ideclQualified = True, ideclAs = Just $ noLoc ghcStackModuleName}
36+
6537updateHsModule :: HsModule GhcPs -> HsModule GhcPs
66−#endif
6738updateHsModule hsm =
6839 hsm {hsmodImports = hsmodImports', hsmodDecls = hsmodDecls'}
6940 where
@@ -85,6 +56,7 @@ updateHsModule hsm =
8556 astTraversal :: Traversal' (LHsDecl GhcPs) (HsType GhcPs)
8657 astTraversal = updateHsmodDecl
8758 . updateHsDecl
59+ . updateSig
8860 . updateLHsSigWsType
8961 . updateLHsSigType
9062 . updateLHsType
@@ -93,56 +65,10 @@ updateHsModule hsm =
9365updateHsmodDecl :: Traversal' (LHsDecl GhcPs) (HsDecl GhcPs)
9466updateHsmodDecl = traverse
9567
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
68+updateHsDecl :: Traversal' (HsDecl GhcPs) (Sig GhcPs)
69+updateHsDecl f (SigD xSig s) = SigD xSig <$> f s
9970updateHsDecl _ sig = pure sig
10071
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−
14672updateSig :: Traversal' (Sig GhcPs) (LHsSigWcType GhcPs)
14773updateSig f (TypeSig xSig ls t) = TypeSig xSig ls <$> f t
14874updateSig _ sig = pure sig
@@ -150,62 +76,41 @@ updateSig _ sig = pure sig
15076updateLHsSigWsType :: Traversal' (LHsSigWcType GhcPs) (LHsSigType GhcPs)
15177updateLHsSigWsType f lhs@HsWC {} =
15278 (\x -> lhs {hswc_body = x}) <$> f (hswc_body lhs)
153−#if __GLASGOW_HASKELL__ < 900
15479updateLHsSigWsType _ lhs = pure lhs
155−#endif
15680
15781updateLHsSigType :: Traversal' (LHsSigType GhcPs) (LHsType GhcPs)
15882updateLHsSigType f lhs@HsIB {} =
15983 (\x -> lhs {hsib_body = x}) <$> f (hsib_body lhs)
160−#if __GLASGOW_HASKELL__ < 900
16184updateLHsSigType _ lhs = pure lhs
162−#endif
16385
16486updateLHsType :: Traversal' (LHsType GhcPs) (HsType GhcPs)
16587updateLHsType = traverse
16688
16789-- Main process
16890updateHsType :: HsType GhcPs -> (Any, HsType GhcPs)
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
91+updateHsType (HsQualTy xty ctxt body) =
92+ flagASTModified $ HsQualTy xty (fmap appendHSC ctxt) body
17393updateHsType ty@HsTyVar {} =
174− flagASTModified $ HsQualTy xQualTy (noLoc $ appendHSC []) (noLoc ty)
94+ flagASTModified $ HsQualTy noExt (noLoc $ appendHSC []) (noLoc ty)
17595updateHsType ty@HsAppTy {} =
176− flagASTModified $ HsQualTy xQualTy (noLoc $ appendHSC []) (noLoc ty)
96+ flagASTModified $ HsQualTy noExt (noLoc $ appendHSC []) (noLoc ty)
17797updateHsType ty@HsFunTy {} =
178− flagASTModified $ HsQualTy xQualTy (noLoc $ appendHSC []) (noLoc ty)
98+ flagASTModified $ HsQualTy noExt (noLoc $ appendHSC []) (noLoc ty)
17999updateHsType ty@HsListTy {} =
180− flagASTModified $ HsQualTy xQualTy (noLoc $ appendHSC []) (noLoc ty)
100+ flagASTModified $ HsQualTy noExt (noLoc $ appendHSC []) (noLoc ty)
181101updateHsType ty@HsTupleTy {} =
182− flagASTModified $ HsQualTy xQualTy (noLoc $ appendHSC []) (noLoc ty)
102+ flagASTModified $ HsQualTy noExt (noLoc $ appendHSC []) (noLoc ty)
183103updateHsType ty = pure ty
184104
185−#if __GLASGOW_HASKELL__ < 810
186−xQualTy :: NoExt
187−xQualTy = noExt
188−#else
189−xQualTy :: NoExtField
190−xQualTy = NoExtField
191−#endif
192−
193105flagASTModified :: a -> (Any, a)
194106flagASTModified a = (Any True, a)
195107
196108appendHSC :: HsContext GhcPs -> HsContext GhcPs
197109appendHSC cs = mkHSC : cs
198110
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−
206111-- make HasCallStack => constraint
207112mkHSC :: LHsType GhcPs
208−mkHSC = noLoc $ HsTyVar xQualTy NotPromoted lId
113+mkHSC = noLoc $ HsTyVar noExt NotPromoted lId
209114
210115lId :: Located (IdP GhcPs)
211116lId = noLoc $ mkRdrQual ghcStackModuleName $ mkClsOcc "HasCallStack"