Skip to content

Commit

Permalink
Export mkThunkInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
coot committed Jul 29, 2024
1 parent 1af8bc8 commit 238fc82
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/NoThunks/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module NoThunks.Class (
-- * Check a value for unexpected thunks
NoThunks(..)
, ThunkInfo(..)
, mkThunkInfo
, Context
, Info
, unsafeNoThunks
Expand Down Expand Up @@ -139,7 +140,7 @@ class NoThunks a where
noThunks ctxt x = do
isThunk <- checkIsThunk x
let ctxt' = showTypeOf (Proxy @a) : ctxt
thunkInfo <- getThunkInfo ctxt' x
thunkInfo <- mkThunkInfo ctxt' x
if isThunk
then return $ Just thunkInfo
else wNoThunks ctxt' x
Expand Down Expand Up @@ -229,17 +230,20 @@ type Info = String
newtype ThunkInfo = ThunkInfo { thunkInfo :: Either Context Info }
deriving Show

getThunkInfo :: Context -> a -> IO ThunkInfo
-- | Construct `ThunkInfo` either from `Context` or information provided by
-- `GHC` about `a` (see `whereFrom`).
--
mkThunkInfo :: Context -> a -> IO ThunkInfo
#if MIN_VERSION_base(4,16,0)
getThunkInfo ctxt a = ThunkInfo . maybe (Left ctxt) (Right . fmt) <$> whereFrom a
mkThunkInfo ctxt a = ThunkInfo . maybe (Left ctxt) (Right . fmt) <$> whereFrom a
where
fmt :: InfoProv -> Info
fmt InfoProv { ipSrcFile, ipSrcSpan,
ipLabel, ipTyDesc } =
ipLabel ++ " :: " ++ ipTyDesc
++ " @ " ++ ipSrcFile ++ ":" ++ ipSrcSpan
#else
getThunkInfo ctxt _ = return (ThunkInfo (Left ctxt))
mkThunkInfo ctxt _ = return (ThunkInfo (Left ctxt))
#endif


Expand Down Expand Up @@ -388,7 +392,7 @@ instance KnownSymbol name => NoThunks (InspectHeapNamed name a) where
inspectHeap :: Context -> a -> IO (Maybe ThunkInfo)
inspectHeap ctxt x = do
containsThunks <- checkContainsThunks x
thunkInfo <- getThunkInfo ("..." : ctxt) x
thunkInfo <- mkThunkInfo ("..." : ctxt) x
return $ if containsThunks
then Just thunkInfo
else Nothing
Expand Down

0 comments on commit 238fc82

Please sign in to comment.