diff options
author | Ryan Scott <ryan.gl.scott@gmail.com> | 2016-11-06 09:09:36 -0500 |
---|---|---|
committer | Ryan Scott <ryan.gl.scott@gmail.com> | 2016-11-06 09:09:36 -0500 |
commit | 630d88176e8dd3ccc269451bca8f55398ef5265c (patch) | |
tree | 71660e73c5e770ee83a1bbad4452a0d23e20f42a /testsuite | |
parent | 25c8e80eccc512d05c0ca8df401271db65b5987b (diff) | |
download | haskell-630d88176e8dd3ccc269451bca8f55398ef5265c.tar.gz |
Allow GeneralizedNewtypeDeriving for classes with associated type families
Summary:
This implements the ability to derive associated type family instances
for newtypes automatically using `GeneralizedNewtypeDeriving`. Refer to the
users' guide additions for how this works; I essentially follow the pattern
laid out in https://ghc.haskell.org/trac/ghc/ticket/8165#comment:18.
Fixes #2721 and #8165.
Test Plan: ./validate
Reviewers: simonpj, goldfire, austin, bgamari
Reviewed By: simonpj
Subscribers: mpickering, thomie
Differential Revision: https://phabricator.haskell.org/D2636
GHC Trac Issues: #2721, #8165
Diffstat (limited to 'testsuite')
-rw-r--r-- | testsuite/tests/deriving/should_compile/T2721.hs (renamed from testsuite/tests/deriving/should_fail/T2721.hs) | 2 | ||||
-rw-r--r-- | testsuite/tests/deriving/should_compile/T8165.hs | 52 | ||||
-rw-r--r-- | testsuite/tests/deriving/should_compile/all.T | 2 | ||||
-rw-r--r-- | testsuite/tests/deriving/should_fail/T2721.stderr | 6 | ||||
-rw-r--r-- | testsuite/tests/deriving/should_fail/T4083.hs | 14 | ||||
-rw-r--r-- | testsuite/tests/deriving/should_fail/T4083.stderr | 7 | ||||
-rw-r--r-- | testsuite/tests/deriving/should_fail/T8165_fail1.hs | 28 | ||||
-rw-r--r-- | testsuite/tests/deriving/should_fail/T8165_fail1.stderr | 17 | ||||
-rw-r--r-- | testsuite/tests/deriving/should_fail/T8165_fail2.hs | 9 | ||||
-rw-r--r-- | testsuite/tests/deriving/should_fail/T8165_fail2.stderr | 5 | ||||
-rw-r--r-- | testsuite/tests/deriving/should_fail/all.T | 4 | ||||
-rw-r--r-- | testsuite/tests/generics/GenDerivOutput.stderr | 4 | ||||
-rw-r--r-- | testsuite/tests/generics/GenDerivOutput1_0.stderr | 4 | ||||
-rw-r--r-- | testsuite/tests/generics/GenDerivOutput1_1.stderr | 4 | ||||
-rw-r--r-- | testsuite/tests/generics/T10604/T10604_deriving.stderr | 4 |
15 files changed, 146 insertions, 16 deletions
diff --git a/testsuite/tests/deriving/should_fail/T2721.hs b/testsuite/tests/deriving/should_compile/T2721.hs index f6485ce514..916916d250 100644 --- a/testsuite/tests/deriving/should_fail/T2721.hs +++ b/testsuite/tests/deriving/should_compile/T2721.hs @@ -1,5 +1,5 @@ {-# LANGUAGE TypeFamilies, GeneralizedNewtypeDeriving #-} - +{-# LANGUAGE UndecidableInstances #-} -- Trac #2721 module T2721 where diff --git a/testsuite/tests/deriving/should_compile/T8165.hs b/testsuite/tests/deriving/should_compile/T8165.hs new file mode 100644 index 0000000000..dd56002648 --- /dev/null +++ b/testsuite/tests/deriving/should_compile/T8165.hs @@ -0,0 +1,52 @@ +{-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE GeneralizedNewtypeDeriving #-} +{-# LANGUAGE MultiParamTypeClasses #-} +{-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE UndecidableInstances #-} +module T8165 where + +----------------------------------------------------------- + +class C a where + type T a + +instance C Int where + type T Int = Bool + +newtype NT = NT Int + deriving C + +----------------------------------------------------------- + +class D a where + type U a + +instance D Int where + type U Int = Int + +newtype E = MkE Int + deriving D + +----------------------------------------------------------- + +class C2 a b where + type F b c a :: * + type G b (d :: * -> *) :: * -> * + +instance C2 a y => C2 a (Either x y) where + type F (Either x y) c a = F y c a + type G (Either x y) d = G y d + +newtype N a = MkN (Either Int a) + deriving (C2 x) + +----------------------------------------------------------- + +class HasRing a where + type Ring a + +newtype L2Norm a = L2Norm a + deriving HasRing + +newtype L1Norm a = L1Norm a + deriving HasRing diff --git a/testsuite/tests/deriving/should_compile/all.T b/testsuite/tests/deriving/should_compile/all.T index bd1f07abe6..39a765a16f 100644 --- a/testsuite/tests/deriving/should_compile/all.T +++ b/testsuite/tests/deriving/should_compile/all.T @@ -18,6 +18,7 @@ test('drv022', normal, compile, ['']) test('deriving-1935', normal, compile, ['']) test('T1830_2', normal, compile, ['']) test('T2378', normal, compile, ['']) +test('T2721', normal, compile, ['']) test('T2856', normal, compile, ['']) test('T3057', extra_clean(['T3057A.o', 'T3057A.hi']), multimod_compile, ['T3057', '-v0']) test('T3012', normal, compile, ['']) @@ -44,6 +45,7 @@ test('T7710', normal, compile, ['']) test('AutoDeriveTypeable', normal, compile, ['']) test('T8138', reqlib('primitive'), compile, ['-O2']) +test('T8165', normal, compile, ['']) test('T8631', normal, compile, ['']) test('T8758', extra_clean(['T8758a.o', 'T8758a.hi']), multimod_compile, ['T8758a', '-v0']) test('T8678', normal, compile, ['']) diff --git a/testsuite/tests/deriving/should_fail/T2721.stderr b/testsuite/tests/deriving/should_fail/T2721.stderr deleted file mode 100644 index 693ccd2dbd..0000000000 --- a/testsuite/tests/deriving/should_fail/T2721.stderr +++ /dev/null @@ -1,6 +0,0 @@ - -T2721.hs:15:28: error: - Can't make a derived instance of ‘C N’ - (even with cunning GeneralizedNewtypeDeriving): - the class has associated types - In the newtype declaration for ‘N’ diff --git a/testsuite/tests/deriving/should_fail/T4083.hs b/testsuite/tests/deriving/should_fail/T4083.hs new file mode 100644 index 0000000000..a995ad83dd --- /dev/null +++ b/testsuite/tests/deriving/should_fail/T4083.hs @@ -0,0 +1,14 @@ +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE GeneralizedNewtypeDeriving #-} +{-# LANGUAGE StandaloneDeriving #-} +{-# LANGUAGE TypeFamilies #-} +module T4083 where + +data family F a +newtype instance F [a] = Maybe a + +class C a where + data D a + +deriving instance C (Maybe a) => C (F [a]) diff --git a/testsuite/tests/deriving/should_fail/T4083.stderr b/testsuite/tests/deriving/should_fail/T4083.stderr new file mode 100644 index 0000000000..299e8d83c2 --- /dev/null +++ b/testsuite/tests/deriving/should_fail/T4083.stderr @@ -0,0 +1,7 @@ + +T4083.hs:14:1: error: + • Can't make a derived instance of ‘C (F [a])’ + (even with cunning GeneralizedNewtypeDeriving): + the class has associated data types + • In the stand-alone deriving instance for + ‘C (Maybe a) => C (F [a])’ diff --git a/testsuite/tests/deriving/should_fail/T8165_fail1.hs b/testsuite/tests/deriving/should_fail/T8165_fail1.hs new file mode 100644 index 0000000000..9c2c5a6a0d --- /dev/null +++ b/testsuite/tests/deriving/should_fail/T8165_fail1.hs @@ -0,0 +1,28 @@ +{-# LANGUAGE GeneralizedNewtypeDeriving #-} +{-# LANGUAGE PolyKinds #-} +{-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE TypeFamilyDependencies #-} +{-# LANGUAGE UndecidableInstances #-} +module T8165_fail where + +import Data.Kind + +class C (a :: k) where + type T k :: Type + +instance C Int where + type T Type = Int + +newtype MyInt = MyInt Int + deriving C + +----------------------------------------------------------- + +class D a where + type S a = r | r -> a + +instance D Int where + type S Int = Char + +newtype WrappedInt = WrapInt Int + deriving D diff --git a/testsuite/tests/deriving/should_fail/T8165_fail1.stderr b/testsuite/tests/deriving/should_fail/T8165_fail1.stderr new file mode 100644 index 0000000000..43bca52aa5 --- /dev/null +++ b/testsuite/tests/deriving/should_fail/T8165_fail1.stderr @@ -0,0 +1,17 @@ + +T8165_fail1.hs:17:12: error: + • Can't make a derived instance of ‘C MyInt’ + (even with cunning GeneralizedNewtypeDeriving): + the associated type ‘T’ is not parameterized over the last type variable + of the class ‘C’ + • In the newtype declaration for ‘MyInt’ + +T8165_fail1.hs:25:8: error: + Type family equations violate injectivity annotation: + S Int = Char -- Defined at T8165_fail1.hs:25:8 + S WrappedInt = S Int -- Defined at T8165_fail1.hs:28:12 + +T8165_fail1.hs:28:12: error: + Type family equation violates injectivity annotation. + RHS of injective type family equation cannot be a type family: + S WrappedInt = S Int -- Defined at T8165_fail1.hs:28:12 diff --git a/testsuite/tests/deriving/should_fail/T8165_fail2.hs b/testsuite/tests/deriving/should_fail/T8165_fail2.hs new file mode 100644 index 0000000000..6398aa21a5 --- /dev/null +++ b/testsuite/tests/deriving/should_fail/T8165_fail2.hs @@ -0,0 +1,9 @@ +{-# LANGUAGE GeneralizedNewtypeDeriving #-} +{-# LANGUAGE TypeFamilies #-} +module T8165_fail2 where + +class C a where + type T a + +newtype Loop = MkLoop Loop + deriving C diff --git a/testsuite/tests/deriving/should_fail/T8165_fail2.stderr b/testsuite/tests/deriving/should_fail/T8165_fail2.stderr new file mode 100644 index 0000000000..4c925f52a3 --- /dev/null +++ b/testsuite/tests/deriving/should_fail/T8165_fail2.stderr @@ -0,0 +1,5 @@ + +T8165_fail2.hs:9:12: error: + The type family application ‘T Loop’ + is no smaller than the instance head + (Use UndecidableInstances to permit this) diff --git a/testsuite/tests/deriving/should_fail/all.T b/testsuite/tests/deriving/should_fail/all.T index 5fec71eff5..2e686b883a 100644 --- a/testsuite/tests/deriving/should_fail/all.T +++ b/testsuite/tests/deriving/should_fail/all.T @@ -21,7 +21,6 @@ test('T2394', normal, compile_fail, ['']) # T2604 was removed as it was out of date re: fixing #9858 test('T2701', normal, compile_fail, ['']) test('T2851', normal, compile_fail, ['']) -test('T2721', normal, compile_fail, ['']) test('T3101', normal, compile_fail, ['']) test('T3621', normal, compile_fail, ['']) test('drvfail-functor1', normal, compile_fail, ['']) @@ -30,6 +29,7 @@ test('drvfail-foldable-traversable1', normal, compile_fail, ['']) test('T3833', normal, compile_fail, ['']) test('T3834', normal, compile_fail, ['']) +test('T4083', normal, compile_fail, ['']) test('T4528', normal, compile_fail, ['']) test('T5287', normal, compile_fail, ['']) test('T5478', normal, compile_fail, ['']) @@ -49,6 +49,8 @@ test('T7148a', normal, compile_fail, ['']) # T7800 was removed as it was out of date re: fixing #9858 test('T5498', normal, compile_fail, ['']) test('T6147', normal, compile_fail, ['']) +test('T8165_fail1', normal, compile_fail, ['']) +test('T8165_fail2', normal, compile_fail, ['']) test('T8851', normal, compile_fail, ['']) test('T9071', normal, multimod_compile_fail, ['T9071','']) test('T9071_2', normal, compile_fail, ['']) diff --git a/testsuite/tests/generics/GenDerivOutput.stderr b/testsuite/tests/generics/GenDerivOutput.stderr index 1b573f26bb..65dcadba85 100644 --- a/testsuite/tests/generics/GenDerivOutput.stderr +++ b/testsuite/tests/generics/GenDerivOutput.stderr @@ -1,6 +1,6 @@ ==================== Derived instances ==================== -Derived instances: +Derived class instances: instance GHC.Generics.Generic (GenDerivOutput.List a) where GHC.Generics.from x = GHC.Generics.M1 @@ -93,7 +93,7 @@ Derived instances: (GHC.Base.fmap GHC.Generics.unRec1) GHC.Generics.unComp1 g2) } -GHC.Generics representation types: +Derived type family instances: type GHC.Generics.Rep (GenDerivOutput.List a) = GHC.Generics.D1 ('GHC.Generics.MetaData "List" diff --git a/testsuite/tests/generics/GenDerivOutput1_0.stderr b/testsuite/tests/generics/GenDerivOutput1_0.stderr index cc12b64a39..162fa0fa08 100644 --- a/testsuite/tests/generics/GenDerivOutput1_0.stderr +++ b/testsuite/tests/generics/GenDerivOutput1_0.stderr @@ -1,6 +1,6 @@ ==================== Derived instances ==================== -Derived instances: +Derived class instances: instance GHC.Generics.Generic1 GenDerivOutput1_0.List where GHC.Generics.from1 x = GHC.Generics.M1 @@ -23,7 +23,7 @@ Derived instances: (GHC.Generics.unPar1 g1) (GHC.Generics.unRec1 g2) } -GHC.Generics representation types: +Derived type family instances: type GHC.Generics.Rep1 GenDerivOutput1_0.List = GHC.Generics.D1 ('GHC.Generics.MetaData "List" diff --git a/testsuite/tests/generics/GenDerivOutput1_1.stderr b/testsuite/tests/generics/GenDerivOutput1_1.stderr index 53dbda1d62..31a9e4368a 100644 --- a/testsuite/tests/generics/GenDerivOutput1_1.stderr +++ b/testsuite/tests/generics/GenDerivOutput1_1.stderr @@ -1,6 +1,6 @@ ==================== Derived instances ==================== -Derived instances: +Derived class instances: instance GHC.Generics.Generic1 CanDoRep1_1.Dd where GHC.Generics.from1 x = GHC.Generics.M1 @@ -162,7 +162,7 @@ Derived instances: (GHC.Generics.unPar1 g1) (GHC.Generics.unRec1 g2) } -GHC.Generics representation types: +Derived type family instances: type GHC.Generics.Rep1 CanDoRep1_1.Dd = GHC.Generics.D1 ('GHC.Generics.MetaData "Dd" "CanDoRep1_1" "main" 'GHC.Types.False) diff --git a/testsuite/tests/generics/T10604/T10604_deriving.stderr b/testsuite/tests/generics/T10604/T10604_deriving.stderr index 04c87ff33d..9576346899 100644 --- a/testsuite/tests/generics/T10604/T10604_deriving.stderr +++ b/testsuite/tests/generics/T10604/T10604_deriving.stderr @@ -1,6 +1,6 @@ ==================== Derived instances ==================== -Derived instances: +Derived class instances: instance GHC.Generics.Generic (T10604_deriving.Empty a) where GHC.Generics.from x = GHC.Generics.M1 @@ -185,7 +185,7 @@ Derived instances: -> T10604_deriving.Starify2 (GHC.Generics.unK1 g1) } -GHC.Generics representation types: +Derived type family instances: type GHC.Generics.Rep (T10604_deriving.Empty a) = GHC.Generics.D1 * ('GHC.Generics.MetaData |