From 24316d315b561255e328ff0afc71d2a45e0a8b0f Mon Sep 17 00:00:00 2001 From: Devin Lehmacher Date: Tue, 4 Feb 2025 22:41:13 -0500 Subject: [PATCH] Fix handling of void parameters passed to functions --- inline-c/src/Language/C/Inline/Context.hs | 7 +++++++ inline-c/test/Language/C/Inline/ContextSpec.hs | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/inline-c/src/Language/C/Inline/Context.hs b/inline-c/src/Language/C/Inline/Context.hs index 42d12ed..9284477 100644 --- a/inline-c/src/Language/C/Inline/Context.hs +++ b/inline-c/src/Language/C/Inline/Context.hs @@ -320,10 +320,17 @@ convertType purity cTypes = runMaybeT . go -- We cannot convert standalone prototypes mzero + buildArr :: [TH.Type] -> TH.Type -> TH.Q TH.Type buildArr [] hsRetType = case purity of Pure -> [t| $(return hsRetType) |] IO -> [t| IO $(return hsRetType) |] + buildArr [TH.TupleT 0] hsRetType = + case purity of + Pure -> [t| $(return hsRetType) |] + IO -> [t| IO $(return hsRetType) |] + buildArr (TH.TupleT 0 : hsPars) hsRetType = + fail "C function can only have void parameter as the only parameter" buildArr (hsPar : hsPars) hsRetType = [t| $(return hsPar) -> $(buildArr hsPars hsRetType) |] diff --git a/inline-c/test/Language/C/Inline/ContextSpec.hs b/inline-c/test/Language/C/Inline/ContextSpec.hs index ef3c5f2..71aa754 100644 --- a/inline-c/test/Language/C/Inline/ContextSpec.hs +++ b/inline-c/test/Language/C/Inline/ContextSpec.hs @@ -74,6 +74,10 @@ spec = do shouldBeType (cty "int (*f)(unsigned char, float)") [t| FunPtr (CUChar -> CFloat -> IO CInt) |] + Hspec.it "converts void parameter function pointer" $ do + shouldBeType + (cty "void (*f)(void)") + [t| FunPtr (IO ()) |] Hspec.it "converts complicated function pointers (1)" $ do -- pointer to function returning pointer to function returning int shouldBeType