Skip to content

Commit 57596db

Browse files
authored
Merge pull request #968 from haskell/issue-967
Use unsafeDupablePerformIO in unsafePackLenLiteral
2 parents d636be9 + 720b857 commit 57596db

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed

aeson.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ test-suite aeson-tests
175175
PropertyRTFunctors
176176
PropertyTH
177177
PropUtils
178+
Regression.Issue967
178179
SerializationFormatSpec
179180
Types
180181
UnitTests

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ For the latest version of this document, please see [https://github.com/haskell/
33
### 2.1.1.0
44

55
- Add `Data.Aeson.KeyMap.!?` (flipped) alias to `Data.Aeson.KeyMap.lookup`.
6+
- Use `unsafeDupablePerformIO` instead of incorrect `accursedUnutterablePerformIO` in creation of keys in TH serialisation.
7+
This fixes a bug in TH deriving, e.g. when `Strict` pragma was enabled.
68

79
### 2.1.0.0
810

src/Data/Aeson/Internal/ByteString.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import Data.Word (Word8)
1313
import Foreign.ForeignPtr (ForeignPtr)
1414
import Data.ByteString.Short (ShortByteString, fromShort)
1515
import GHC.Exts (Addr#, Ptr (Ptr))
16-
import Data.ByteString.Internal (accursedUnutterablePerformIO)
1716
import Data.ByteString.Short.Internal (createFromPtr)
17+
import System.IO.Unsafe (unsafeDupablePerformIO)
1818

1919
import qualified Data.ByteString as BS
2020
import qualified Language.Haskell.TH.Lib as TH
@@ -82,6 +82,7 @@ liftSBS sbs = withBS bs $ \_ len -> [| unsafePackLenLiteral |]
8282
bs = fromShort sbs
8383
#endif
8484

85+
-- this is copied verbatim from @bytestring@, but only in recent versions.
8586
unsafePackLenLiteral :: Int -> Addr# -> ShortByteString
8687
unsafePackLenLiteral len addr# =
87-
accursedUnutterablePerformIO $ createFromPtr (Ptr addr#) len
88+
unsafeDupablePerformIO $ createFromPtr (Ptr addr#) len

tests/Regression/Issue967.hs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{-# LANGUAGE Strict #-}
2+
{-# LANGUAGE TemplateHaskell #-}
3+
-- {-# OPTIONS_GHC -ddump-splices #-}
4+
-- {-# OPTIONS_GHC -ddump-simpl -ddump-to-file #-}
5+
module Regression.Issue967 (issue967) where
6+
7+
import Test.Tasty (TestTree)
8+
import Test.Tasty.HUnit (testCase, assertEqual)
9+
10+
import qualified Data.Text.Lazy as LT
11+
import qualified Data.Text.Lazy.Encoding as LTE
12+
13+
import Data.Aeson
14+
import Data.Aeson.TH
15+
16+
data DataA = DataA
17+
{ val1 :: Int,
18+
val2 :: Int
19+
}
20+
deriving (Eq, Show)
21+
22+
-------------------------------------------------------------------------------
23+
-- Instances
24+
-------------------------------------------------------------------------------
25+
26+
$(deriveJSON defaultOptions ''DataA)
27+
28+
-------------------------------------------------------------------------------
29+
-- Test
30+
-------------------------------------------------------------------------------
31+
32+
issue967 :: TestTree
33+
issue967 = testCase "issue967" $ do
34+
let ev = DataA 1 2
35+
encoding = encode ev
36+
parsedEv = decode encoding :: Maybe DataA
37+
38+
assertEqual (LT.unpack $ LTE.decodeUtf8 encoding) (Just ev) parsedEv

tests/UnitTests.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ import qualified Data.Vector as Vector
8181
import qualified ErrorMessages
8282
import qualified SerializationFormatSpec
8383
import qualified Data.Map as Map -- Lazy!
84+
import Regression.Issue967
8485

8586
roundTripCamel :: String -> Assertion
8687
roundTripCamel name = assertEqual "" name (camelFrom '_' $ camelTo '_' name)
@@ -894,4 +895,5 @@ tests = testGroup "unit" [
894895
assertEqual "" (object ["foo" .= True]) [aesonQQ| {"foo": true } |]
895896
]
896897
, monadFixTests
898+
, issue967
897899
]

0 commit comments

Comments
 (0)