Skip to content

Commit 983476f

Browse files
committed
Make sure deriving examples actually compile
Doctests do awful job checking it. Constructor may be in scope for doctest and not in scope for client code
1 parent 5aade90 commit 983476f

File tree

4 files changed

+148
-1
lines changed

4 files changed

+148
-1
lines changed

vector/src/Data/Vector/Unboxed.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
-- @
6969
module Data.Vector.Unboxed (
7070
-- * Unboxed vectors
71-
Vector(V_UnboxAs, V_UnboxViaPrim), MVector(..), Unbox,
71+
Vector(V_UnboxAs, V_UnboxViaPrim, V_UnboxViaStorable), MVector(..), Unbox,
7272

7373
-- * Accessors
7474

vector/tests/Main.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import qualified Tests.Vector.Strict
88
import qualified Tests.Vector.Unboxed
99
import qualified Tests.Bundle
1010
import qualified Tests.Move
11+
import qualified Tests.Deriving ()
1112

1213
import Test.Tasty (defaultMain,testGroup)
1314

vector/tests/Tests/Deriving.hs

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
{-# LANGUAGE DerivingVia #-}
2+
{-# LANGUAGE FlexibleInstances #-}
3+
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4+
{-# LANGUAGE MagicHash #-}
5+
{-# LANGUAGE MultiParamTypeClasses #-}
6+
{-# LANGUAGE StandaloneDeriving #-}
7+
{-# LANGUAGE TypeApplications #-}
8+
{-# LANGUAGE TypeFamilies #-}
9+
{-# LANGUAGE TypeOperators #-}
10+
{-# LANGUAGE UnboxedTuples #-}
11+
-- |
12+
-- These tests make sure that derived Unbox instances actually works.
13+
-- It's distressingly easy to forget to export some constructor and
14+
-- make seemingly fine code noncompilable.
15+
--
16+
-- We're only interested in checking whether examples compiling.
17+
-- Doctests aren't reliable in ensuring that!
18+
module Tests.Deriving () where
19+
20+
import Control.DeepSeq
21+
import qualified Data.Vector.Generic as VG
22+
import qualified Data.Vector.Generic.Mutable as VGM
23+
import qualified Data.Vector.Storable as VS
24+
import qualified Data.Vector.Primitive as VP
25+
import qualified Data.Vector.Unboxed as VU
26+
27+
28+
----------------------------------------------------------------
29+
-- Primitive
30+
31+
newtype FooP1 = FooP1 Int deriving VP.Prim
32+
33+
newtype instance VU.MVector s FooP1 = MV_FooP1 (VP.MVector s FooP1)
34+
newtype instance VU.Vector FooP1 = V_FooP1 (VP.Vector FooP1)
35+
deriving via (VU.UnboxViaPrim FooP1) instance VGM.MVector VU.MVector FooP1
36+
deriving via (VU.UnboxViaPrim FooP1) instance VG.Vector VU.Vector FooP1
37+
instance VU.Unbox FooP1
38+
39+
40+
41+
newtype FooP2 = FooP2 Int
42+
43+
newtype instance VU.MVector s FooP2 = MV_FooP2 (VP.MVector s Int)
44+
newtype instance VU.Vector FooP2 = V_FooP2 (VP.Vector Int)
45+
deriving via (VU.UnboxViaPrim Int) instance VGM.MVector VU.MVector FooP2
46+
deriving via (VU.UnboxViaPrim Int) instance VG.Vector VU.Vector FooP2
47+
instance VU.Unbox FooP2
48+
49+
50+
----------------------------------------------------------------
51+
-- Storable
52+
53+
newtype FooS1 = FooS1 Int deriving VS.Storable
54+
55+
newtype instance VU.MVector s FooS1 = MV_FooS1 (VS.MVector s FooS1)
56+
newtype instance VU.Vector FooS1 = V_FooS1 (VS.Vector FooS1)
57+
deriving via (VU.UnboxViaStorable FooS1) instance VGM.MVector VU.MVector FooS1
58+
deriving via (VU.UnboxViaStorable FooS1) instance VG.Vector VU.Vector FooS1
59+
instance VU.Unbox FooS1
60+
61+
62+
newtype FooS2 = FooS2 Int
63+
64+
newtype instance VU.MVector s FooS2 = MV_FooS2 (VS.MVector s Int)
65+
newtype instance VU.Vector FooS2 = V_FooS2 (VS.Vector Int)
66+
deriving via (VU.UnboxViaStorable Int) instance VGM.MVector VU.MVector FooS2
67+
deriving via (VU.UnboxViaStorable Int) instance VG.Vector VU.Vector FooS2
68+
instance VU.Unbox FooS2
69+
70+
71+
----------------------------------------------------------------
72+
-- Boxed variants
73+
74+
75+
data FooLazy a = FooLazy Int a
76+
deriving (Eq, Ord, Show)
77+
78+
instance VU.IsoUnbox (FooLazy a) (Int, VU.DoNotUnboxLazy a) where
79+
toURepr (FooLazy i a) = (i, VU.DoNotUnboxLazy a)
80+
fromURepr (i, VU.DoNotUnboxLazy a) = FooLazy i a
81+
{-# INLINE toURepr #-}
82+
{-# INLINE fromURepr #-}
83+
84+
newtype instance VU.MVector s (FooLazy a) = MV_FooLazy (VU.MVector s (Int, VU.DoNotUnboxLazy a))
85+
newtype instance VU.Vector (FooLazy a) = V_FooLazy (VU.Vector (Int, VU.DoNotUnboxLazy a))
86+
deriving via (FooLazy a `VU.As` (Int, VU.DoNotUnboxLazy a)) instance VGM.MVector VU.MVector (FooLazy a)
87+
deriving via (FooLazy a `VU.As` (Int, VU.DoNotUnboxLazy a)) instance VG.Vector VU.Vector (FooLazy a)
88+
instance VU.Unbox (FooLazy a)
89+
90+
91+
92+
data FooStrict a = FooStrict Int a
93+
deriving (Eq, Ord, Show)
94+
95+
instance VU.IsoUnbox (FooStrict a) (Int, VU.DoNotUnboxStrict a) where
96+
toURepr (FooStrict i a) = (i, VU.DoNotUnboxStrict a)
97+
fromURepr (i, VU.DoNotUnboxStrict a) = FooStrict i a
98+
{-# INLINE toURepr #-}
99+
{-# INLINE fromURepr #-}
100+
101+
newtype instance VU.MVector s (FooStrict a) = MV_FooStrict (VU.MVector s (Int, VU.DoNotUnboxStrict a))
102+
newtype instance VU.Vector (FooStrict a) = V_FooStrict (VU.Vector (Int, VU.DoNotUnboxStrict a))
103+
deriving via (FooStrict a `VU.As` (Int, VU.DoNotUnboxStrict a)) instance VGM.MVector VU.MVector (FooStrict a)
104+
deriving via (FooStrict a `VU.As` (Int, VU.DoNotUnboxStrict a)) instance VG.Vector VU.Vector (FooStrict a)
105+
instance VU.Unbox (FooStrict a)
106+
107+
108+
data FooNormalForm a = FooNormalForm Int a
109+
deriving (Eq, Ord, Show)
110+
111+
instance VU.IsoUnbox (FooNormalForm a) (Int, VU.DoNotUnboxNormalForm a) where
112+
toURepr (FooNormalForm i a) = (i, VU.DoNotUnboxNormalForm a)
113+
fromURepr (i, VU.DoNotUnboxNormalForm a) = FooNormalForm i a
114+
{-# INLINE toURepr #-}
115+
{-# INLINE fromURepr #-}
116+
117+
newtype instance VU.MVector s (FooNormalForm a) = MV_FooNormalForm (VU.MVector s (Int, VU.DoNotUnboxNormalForm a))
118+
newtype instance VU.Vector (FooNormalForm a) = V_FooNormalForm (VU.Vector (Int, VU.DoNotUnboxNormalForm a))
119+
deriving via (FooNormalForm a `VU.As` (Int, VU.DoNotUnboxNormalForm a))
120+
instance NFData a => VGM.MVector VU.MVector (FooNormalForm a)
121+
deriving via (FooNormalForm a `VU.As` (Int, VU.DoNotUnboxNormalForm a))
122+
instance NFData a => VG.Vector VU.Vector (FooNormalForm a)
123+
instance NFData a => VU.Unbox (FooNormalForm a)
124+
125+
126+
127+
----------------------------------------------------------------
128+
-- Unboxed
129+
130+
131+
data FooAs a = FooAs Int a
132+
deriving Show
133+
134+
instance VU.IsoUnbox (FooAs a) (Int,a) where
135+
toURepr (FooAs i a) = (i,a)
136+
fromURepr (i,a) = FooAs i a
137+
{-# INLINE toURepr #-}
138+
{-# INLINE fromURepr #-}
139+
140+
newtype instance VU.MVector s (FooAs a) = MV_FooAs (VU.MVector s (Int, a))
141+
newtype instance VU.Vector (FooAs a) = V_FooAs (VU.Vector (Int, a))
142+
deriving via (FooAs a `VU.As` (Int, a)) instance VU.Unbox a => VGM.MVector VU.MVector (FooAs a)
143+
deriving via (FooAs a `VU.As` (Int, a)) instance VU.Unbox a => VG.Vector VU.Vector (FooAs a)
144+
instance VU.Unbox a => VU.Unbox (FooAs a)

vector/vector.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ common tests-common
188188
, template-haskell
189189
, base-orphans >= 0.6
190190
, vector
191+
, deepseq
191192
, primitive
192193
, random
193194
, QuickCheck >= 2.9 && < 2.16
@@ -206,6 +207,7 @@ common tests-common
206207
Tests.Vector.Primitive
207208
Tests.Vector.Unboxed
208209
Tests.Vector.UnitTests
210+
Tests.Deriving
209211
Utilities
210212

211213
default-extensions:

0 commit comments

Comments
 (0)