Skip to content

Commit e1406ad

Browse files
committed
Drop runFold
The contract of `runFold` promised ```hs --- If the fold has an associated exception handler, it will get a chance to --- catch any exceptions thrown. See 'foldWithHandler' for detailed discussion. ``` but that is a contract it could not keep: after all, the whole point for introducing `foldWithHandler` in the first place was that we need to treat these handlers very carefully, simply using `handle` does not work. It turns out we don't actually _need_ `runFold`, so deleted it altogether; I think this clarifies the situation on the `hs-bindgen` side also (see discussion of `topLevelFold` there).
1 parent 1c9b619 commit e1406ad

2 files changed

Lines changed: 16 additions & 26 deletions

File tree

src/Clang/HighLevel/Fold.hs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ module Clang.HighLevel.Fold (
88
-- * Construction
99
, simpleFold
1010
, foldWithHandler
11-
, runFold
1211
-- * Fold-specific operations
1312
, foldBreak
1413
, foldBreakWith
@@ -177,23 +176,6 @@ foldWithHandler handler foldNext =
177176
Just e' -> handler curr e'
178177
Nothing -> liftIO $ throwIO e
179178

180-
-- | Run 'Fold'
181-
--
182-
-- If the fold has an associated exception handler, it will get a chance to
183-
-- catch any exceptions thrown. See 'foldWithHandler' for detailed discussion.
184-
runFold :: MonadUnliftIO m => Fold m a -> CXCursor -> m (Next m a)
185-
runFold fold curr = withRunInIO $ \runInIO -> runFoldUsing runInIO fold curr
186-
187-
-- | Internal generalization of 'runFold'
188-
runFoldUsing ::
189-
( MonadIO n
190-
, Functor m
191-
)
192-
=> RunInIO m -> Fold m a -> CXCursor -> n (Next m a)
193-
runFoldUsing runInIO Fold{foldNext, foldHandler} curr =
194-
handleUnliftUsing runInIO (fmap Continue . foldHandler curr) $
195-
foldNext curr
196-
197179
-- | Result of visiting one node
198180
--
199181
-- This is the equivalent of 'CXChildVisitResult'
@@ -487,25 +469,34 @@ clang_visitChildren root topLevelFold = withRunInIO $ \runInIO -> do
487469
-> IO (SimpleEnum CXChildVisitResult)
488470
visitor runInIO someStack current parent = do
489471
popUntil runInIO someStack parent
490-
SomeStack stack <- readIORef someStack
491-
let p = topProcessing stack
492-
previousException <- partialResultsIsException (partialResults p)
472+
SomeStack (stack :: Stack m x) <- readIORef someStack
473+
474+
let foldHandler :: CXCursor -> SomeException -> m (Maybe x)
475+
foldNext :: CXCursor -> m (Next m x)
476+
partialResults :: PartialResults x
477+
Processing{
478+
currentFold = Fold{foldHandler, foldNext}
479+
, partialResults
480+
} = topProcessing stack
493481

482+
previousException <- partialResultsIsException partialResults
494483
if previousException then
495484
return $ simpleEnum CXChildVisit_Continue
496485
else do
497-
next <- Base.try $ runFoldUsing runInIO (currentFold p) current
486+
next <- Base.try $
487+
handleUnliftUsing runInIO (fmap Continue . foldHandler current) $
488+
foldNext current
498489
case next of
499490
Right (Break ma) -> do
500-
forM_ ma $ addPartialResult (partialResults p)
491+
forM_ ma $ addPartialResult partialResults
501492
return $ simpleEnum CXChildVisit_Break
502493
Right (Continue ma) -> do
503-
forM_ ma $ addPartialResult (partialResults p)
494+
forM_ ma $ addPartialResult partialResults
504495
return $ simpleEnum CXChildVisit_Continue
505496
Right (Recurse fold summarize) -> do
506497
stack' <- push current fold summarize stack
507498
writeIORef someStack $ SomeStack stack'
508499
return $ simpleEnum CXChildVisit_Recurse
509500
Left ex -> do
510-
recordException (partialResults p) ex
501+
recordException partialResults ex
511502
return $ simpleEnum CXChildVisit_Continue

src/Clang/HighLevel/Types.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ module Clang.HighLevel.Types (
2828
-- ** Construction
2929
, simpleFold
3030
, foldWithHandler
31-
, runFold
3231
-- ** Fold-specific functionality
3332
, foldBreak
3433
, foldBreakWith

0 commit comments

Comments
 (0)