Skip to content

Commit

Permalink
More graceful failure
Browse files Browse the repository at this point in the history
`error` is simply wrong in `IO`-ish contexts. Use fail or throwIO.
  • Loading branch information
phadej committed Dec 20, 2024
1 parent 2a16d32 commit f8131c5
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
6 changes: 3 additions & 3 deletions hs-bindgen/src/HsBindgen/C/Fold/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,11 @@ mkStructField unit path current = do
tokens <- HighLevel.clang_tokenize unit (multiLocExpansion <$> extent)
case reparseWith reparseFieldDecl tokens of
Left err ->
error $ "mkStructField: " ++ show err
fail $ "mkStructField: " ++ show err
Right (fieldType, fieldName) -> do
fieldOffset <- fromIntegral <$> clang_Cursor_getOffsetOfField current
unless (fieldOffset `mod` 8 == 0) $
error "bit-fields not supported yet"
fail "bit-fields not supported yet"
return $ Just StructField{fieldName, fieldOffset, fieldType}

else do
Expand All @@ -388,7 +388,7 @@ mkStructField unit path current = do
fieldType <- processTypeDeclRec (PathField fieldName path) unit ty
fieldOffset <- fromIntegral <$> liftIO (clang_Cursor_getOffsetOfField current)

unless (fieldOffset `mod` 8 == 0) $ error "bit-fields not supported yet"
unless (fieldOffset `mod` 8 == 0) $ fail "bit-fields not supported yet"

return $ Just StructField{fieldName, fieldOffset, fieldType}

Expand Down
2 changes: 1 addition & 1 deletion hs-bindgen/src/HsBindgen/Eff.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import HsBindgen.Imports
newtype Eff m a = Eff {
getEff :: ReaderT (Support m) IO a
}
deriving newtype (Functor, Applicative, Monad, MonadIO, MonadUnliftIO)
deriving newtype (Functor, Applicative, Monad, MonadFail, MonadIO, MonadUnliftIO)

wrapEff :: (Support m -> IO a) -> Eff m a
wrapEff = Eff . ReaderT
Expand Down
2 changes: 2 additions & 0 deletions hs-bindgen/tests/TastyGolden.hs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ runGoldenSteps (GoldenSteps getGolden getTested cmp update) progress opts = do
mbNew <- try $ getTested stepFn
msgs <- readIORef msgsRef <&> reverse

print $ either (const 'x') (const 'y') mbNew

case mbNew of
Left e
| Just _ <- fromException @AsyncException e -> throwIO e
Expand Down

0 comments on commit f8131c5

Please sign in to comment.