[Git][ghc/ghc][master] 2 commits: Revert "CApiFFI: add ConstPtr for encoding const-qualified pointer return types (#22043)"
Marge Bot (@marge-bot)
gitlab at gitlab.haskell.org
Sat Jan 28 07:58:21 UTC 2023
Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC
Commits:
56c1bd98 by Ben Gamari at 2023-01-28T02:57:59-05:00
Revert "CApiFFI: add ConstPtr for encoding const-qualified pointer return types (#22043)"
This reverts commit 99aca26b652603bc62953157a48e419f737d352d.
- - - - -
b3a3534b by nineonine at 2023-01-28T02:57:59-05:00
CApiFFI: add ConstPtr for encoding const-qualified pointer return types
Previously, when using `capi` calling convention in foreign declarations,
code generator failed to handle const-cualified pointer return types.
This resulted in CC toolchain throwing `-Wincompatible-pointer-types-discards-qualifiers`
warning.
`Foreign.C.Types.ConstPtr` newtype was introduced to handle these cases -
special treatment was put in place to generate appropritetly qualified C
wrapper that no longer triggers the above mentioned warning.
Fixes #22043.
- - - - -
13 changed files:
- compiler/GHC/Builtin/Names.hs
- compiler/GHC/HsToCore/Foreign/C.hs
- docs/users_guide/exts/ffi.rst
- libraries/base/Data/Data.hs
- + libraries/base/Foreign/C/ConstPtr.hs
- libraries/base/Foreign/C/Types.hs
- libraries/base/Foreign/Storable.hs
- libraries/base/base.cabal
- libraries/base/changelog.md
- testsuite/tests/ffi/should_compile/T22034.h → testsuite/tests/ffi/should_compile/T22043.h
- testsuite/tests/ffi/should_compile/T22034.hs → testsuite/tests/ffi/should_compile/T22043.hs
- testsuite/tests/ffi/should_compile/T22034_c.c → testsuite/tests/ffi/should_compile/T22043_c.c
- testsuite/tests/ffi/should_compile/all.T
Changes:
=====================================
compiler/GHC/Builtin/Names.hs
=====================================
@@ -557,7 +557,7 @@ gHC_PRIM, gHC_PRIM_PANIC,
aRROW, gHC_DESUGAR, rANDOM, gHC_EXTS, gHC_IS_LIST,
cONTROL_EXCEPTION_BASE, gHC_TYPEERROR, gHC_TYPELITS, gHC_TYPELITS_INTERNAL,
gHC_TYPENATS, gHC_TYPENATS_INTERNAL,
- dATA_COERCE, dEBUG_TRACE, uNSAFE_COERCE, fOREIGN_C_TYPES :: Module
+ dATA_COERCE, dEBUG_TRACE, uNSAFE_COERCE, fOREIGN_C_CONSTPTR :: Module
gHC_PRIM = mkPrimModule (fsLit "GHC.Prim") -- Primitive types and values
gHC_PRIM_PANIC = mkPrimModule (fsLit "GHC.Prim.Panic")
@@ -626,7 +626,7 @@ gHC_TYPENATS_INTERNAL = mkBaseModule (fsLit "GHC.TypeNats.Internal")
dATA_COERCE = mkBaseModule (fsLit "Data.Coerce")
dEBUG_TRACE = mkBaseModule (fsLit "Debug.Trace")
uNSAFE_COERCE = mkBaseModule (fsLit "Unsafe.Coerce")
-fOREIGN_C_TYPES = mkBaseModule (fsLit "Foreign.C.Types")
+fOREIGN_C_CONSTPTR = mkBaseModule (fsLit "Foreign.C.ConstPtr")
gHC_SRCLOC :: Module
gHC_SRCLOC = mkBaseModule (fsLit "GHC.SrcLoc")
@@ -1667,7 +1667,7 @@ fingerprintDataConName =
constPtrConName :: Name
constPtrConName =
- tcQual fOREIGN_C_TYPES (fsLit "ConstPtr") constPtrTyConKey
+ tcQual fOREIGN_C_CONSTPTR (fsLit "ConstPtr") constPtrTyConKey
{-
************************************************************************
=====================================
compiler/GHC/HsToCore/Foreign/C.hs
=====================================
@@ -250,11 +250,11 @@ dsFCall fn_id co fcall mDeclHeader = do
(tv_bndrs, rho) = tcSplitForAllTyVarBinders ty
(arg_tys, io_res_ty) = tcSplitFunTys rho
- let constQual -- provide 'const' qualifier (#22034)
+ let constQual -- provide 'const' qualifier (#22043)
| (_, res_ty1) <- tcSplitFunTys ty1
, newty <- maybe res_ty1 snd (tcSplitIOType_maybe res_ty1)
, Just (ptr, _) <- splitTyConApp_maybe newty
- , tyConName ptr `elem` [constPtrConName]
+ , tyConName ptr == constPtrConName
= text "const"
| otherwise = empty
=====================================
docs/users_guide/exts/ffi.rst
=====================================
@@ -438,7 +438,7 @@ specified. The syntax looks like: ::
newtype {-# CTYPE "useconds_t" #-} T = ...
In case foreign declarations contain ``const``-qualified pointer return
-type, `ConstPtr` from :base-ref:`Foreign.C.Types` may be used to
+type, ``ConstPtr`` from :base-ref:`Foreign.C.ConstPtr` may be used to
encode this, e.g. ::
foreign import capi "header.h f" f :: CInt -> ConstPtr CInt
=====================================
libraries/base/Data/Data.hs
=====================================
@@ -140,6 +140,7 @@ import Data.Word -- So we can give Data instance for Word8, ...
import GHC.Real -- So we can give Data instance for Ratio
--import GHC.IOBase -- So we can give Data instance for IO, Handle
import GHC.Ptr -- So we can give Data instance for Ptr
+import Foreign.C.ConstPtr -- So we can give Data instance for ConstPtr
import GHC.ForeignPtr -- So we can give Data instance for ForeignPtr
import Foreign.Ptr (IntPtr(..), WordPtr(..))
-- So we can give Data instance for IntPtr and WordPtr
@@ -1227,6 +1228,9 @@ instance Data a => Data (Ptr a) where
dataTypeOf _ = mkNoRepType "GHC.Ptr.Ptr"
dataCast1 x = gcast1 x
+-- | @since 4.18.0.0
+deriving instance Data a => Data (ConstPtr a)
+
------------------------------------------------------------------------------
-- | @since 4.8.0.0
=====================================
libraries/base/Foreign/C/ConstPtr.hs
=====================================
@@ -0,0 +1,45 @@
+{-# LANGUAGE NoImplicitPrelude #-}
+{-# LANGUAGE RoleAnnotations #-}
+{-# LANGUAGE StandaloneKindSignatures #-}
+{-# LANGUAGE Trustworthy #-}
+
+-----------------------------------------------------------------------------
+-- |
+-- Module : Foreign.C.ConstPtr
+-- Copyright : (c) GHC Developers
+-- License : BSD-style (see the file libraries/base/LICENSE)
+--
+-- Maintainer : ffi at haskell.org
+-- Stability : provisional
+-- Portability : portable
+--
+-- This module provides typed @const@ pointers to foreign data. It is part
+-- of the Foreign Function Interface (FFI).
+--
+-----------------------------------------------------------------------------
+
+module Foreign.C.ConstPtr (
+ ConstPtr(..)
+) where
+
+import GHC.Base
+import GHC.Ptr
+import GHC.Show
+
+-- | A pointer with the C @const@ qualifier. For instance, an argument of type
+-- @ConstPtr CInt@ would be marshalled as @const int*@.
+--
+-- While @const at -ness generally does not matter for @ccall@ imports (since
+-- @const@ and non- at const@ pointers typically have equivalent calling
+-- conventions), it does matter for @capi@ imports. See GHC #22043.
+--
+-- @since 4.18.0.0
+--
+type ConstPtr :: Type -> Type
+type role ConstPtr phantom
+newtype ConstPtr a = ConstPtr { unConstPtr :: Ptr a }
+ deriving (Eq, Ord)
+
+-- doesn't use record syntax
+instance Show (ConstPtr a) where
+ showsPrec d (ConstPtr p) = showParen (d > 10) $ showString "ConstPtr " . showsPrec 11 p
=====================================
libraries/base/Foreign/C/Types.hs
=====================================
@@ -86,11 +86,8 @@ module Foreign.C.Types
-- Instances of: Eq and Storable
, CFile, CFpos, CJmpBuf
-
- , ConstPtr(..)
) where
-import Foreign.Ptr ( Ptr )
import Foreign.Storable
import Data.Bits ( Bits(..), FiniteBits(..) )
import Data.Int ( Int8, Int16, Int32, Int64 )
@@ -226,9 +223,6 @@ INTEGRAL_TYPE(CUIntPtr,"uintptr_t",HTYPE_UINTPTR_T)
INTEGRAL_TYPE(CIntMax,"intmax_t",HTYPE_INTMAX_T)
INTEGRAL_TYPE(CUIntMax,"uintmax_t",HTYPE_UINTMAX_T)
--- | Used to produce 'const' qualifier in C code generator
-newtype ConstPtr a = ConstPtr { unConstPtr :: Ptr a } deriving newtype (Show, Eq, Storable)
-
-- C99 types which are still missing include:
-- wint_t, wctrans_t, wctype_t
=====================================
libraries/base/Foreign/Storable.hs
=====================================
@@ -1,5 +1,8 @@
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE CPP, NoImplicitPrelude, ScopedTypeVariables, BangPatterns #-}
+{-# LANGUAGE DerivingStrategies #-}
+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
+{-# LANGUAGE StandaloneDeriving #-}
-----------------------------------------------------------------------------
-- |
@@ -42,6 +45,7 @@ import GHC.Word
import GHC.Ptr
import GHC.Base
import GHC.Fingerprint.Type
+import Foreign.C.ConstPtr
import Data.Bits
import GHC.Real
@@ -280,3 +284,5 @@ pokeFingerprint p0 (Fingerprint high low) = do
pokeW64 (castPtr p0) 8 high
pokeW64 (castPtr p0 `plusPtr` 8) 8 low
+
+deriving newtype instance Storable (ConstPtr a)
=====================================
libraries/base/base.cabal
=====================================
@@ -169,6 +169,7 @@ Library
Debug.Trace
Foreign
Foreign.C
+ Foreign.C.ConstPtr
Foreign.C.Error
Foreign.C.String
Foreign.C.Types
=====================================
libraries/base/changelog.md
=====================================
@@ -1,9 +1,11 @@
# Changelog for [`base` package](http://hackage.haskell.org/package/base)
## 4.18.0.0 *TBA*
+ * `Foreign.C.ConstPtr.ConstrPtr` was added to encode `const`-qualified
+ pointer types in foreign declarations when using `CApiFFI` extension. ([CLC proposal #117](https://github.com/haskell/core-libraries-committee/issues/117))
* Add `forall a. Functor (p a)` superclass for `Bifunctor p` ([CLC proposal #91](https://github.com/haskell/core-libraries-committee/issues/91))
- * Add `Functor` instances for `(,,,,) a b c d`, `(,,,,,) a b c d e` and
- `(,,,,,) a b c d e f`
+ * Add Functor instances for `(,,,,) a b c d`, `(,,,,,) a b c d e` and
+ `(,,,,,) a b c d e f`.
* Exceptions thrown by weak pointer finalizers are now reported via a global
exception handler.
* Add `GHC.Weak.Finalize.{get,set}FinalizerExceptionHandler` which allows the
@@ -42,7 +44,7 @@
([CLC proposal #90](https://github.com/haskell/core-libraries-committee/issues/90))
* Add `Eq` and `Ord` instances for `Generically1`.
* Relax instances for Functor combinators; put superclass on Class1 and Class2
- to make non-breaking ([CLC proposal #10](https://github.com/haskell/core-libraries-committee/issues/10),
+ to make non-breaking ([CLC proposal #10](https://github.com/haskell/core-libraries-committee/issues/10),
[migration guide](https://github.com/haskell/core-libraries-committee/blob/main/guides/functor-combinator-instances-and-class1s.md))
* Add `gcdetails_block_fragmentation_bytes` to `GHC.Stats.GCDetails` to track heap fragmentation.
* `GHC.TypeLits` and `GHC.TypeNats` now export the `natSing`, `symbolSing`,
=====================================
testsuite/tests/ffi/should_compile/T22034.h → testsuite/tests/ffi/should_compile/T22043.h
=====================================
=====================================
testsuite/tests/ffi/should_compile/T22034.hs → testsuite/tests/ffi/should_compile/T22043.hs
=====================================
@@ -1,10 +1,11 @@
{-# LANGUAGE CApiFFI #-}
-module T22034 where
+module T22043 where
import Foreign.C.Types
+import Foreign.C.ConstPtr
-foreign import capi "T22034.h foo"
+foreign import capi "T22043.h foo"
c_foo :: IO (ConstPtr CInt)
-foreign import capi "T22034.h value bar"
+foreign import capi "T22043.h value bar"
c_bar :: ConstPtr CDouble
=====================================
testsuite/tests/ffi/should_compile/T22034_c.c → testsuite/tests/ffi/should_compile/T22043_c.c
=====================================
=====================================
testsuite/tests/ffi/should_compile/all.T
=====================================
@@ -43,4 +43,4 @@ test(
],
)
test('T15531', normal, compile, ['-Wall'])
-test('T22034', [omit_ways(['ghci'])], compile, [''])
+test('T22043', [omit_ways(['ghci'])], compile, [''])
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/77fdbd3f7798ae7095a6a22c3674c08c86a91c6c...b3a3534b6f75b34dc4db76e904e071485da6d5cc
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/77fdbd3f7798ae7095a6a22c3674c08c86a91c6c...b3a3534b6f75b34dc4db76e904e071485da6d5cc
You're receiving this email because of your account on gitlab.haskell.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20230128/135172a4/attachment-0001.html>
More information about the ghc-commits
mailing list