summaryrefslogtreecommitdiff
path: root/testsuite/tests/generics
diff options
context:
space:
mode:
authorSimon Peyton Jones <simonpj@microsoft.com>2016-06-23 14:51:22 +0100
committerSimon Peyton Jones <simonpj@microsoft.com>2016-06-24 11:04:21 +0100
commitd2958bd08a049b61941f078e51809c7e63bc3354 (patch)
treece836b88f13dae393870999e23ad21f0507ff8ab /testsuite/tests/generics
parentdf9611ee5c056066fe88fe3ef2f64382cec8b741 (diff)
downloadhaskell-d2958bd08a049b61941f078e51809c7e63bc3354.tar.gz
Improve typechecking of instance defaults
In an instance declaration when you don't specify the code for a method, GHC fills in from the default binding in the class. The type of the default method can legitmiately be ambiguous --- see Note [Default methods in instances] in TcInstDcls --- so typechecking it can be tricky. Trac #12220 showed that although we were dealing with that ambiguity for /vanilla/ default methods, we were not doing so for /generic/ default methods. Moreover we were dealing with it clumsily, by generating post-typechecked code. This patch fixes the bug AND deletes code! We now use the same code path for both vanilla and generic default methods; and generate /pre-typechecked/ code in both cases. The key trick is that we can use Visible Type Application to deal with the ambiguity, which wasn't possible before. Hooray. There is a small hit to performance in compiler/perf/T1969 which consists of nothing BUT instance declarations with several default methods to fill, which we now have to typecheck. The actual hit is from 724 -> 756 or 4% in that extreme example. Real world programs have vastly fewer instance decls.
Diffstat (limited to 'testsuite/tests/generics')
-rw-r--r--testsuite/tests/generics/GShow/GShow.hs3
-rw-r--r--testsuite/tests/generics/GenDerivOutput.stderr6
-rw-r--r--testsuite/tests/generics/T10604/T10604_deriving.stderr6
-rwxr-xr-xtestsuite/tests/generics/T12220.hs37
-rw-r--r--testsuite/tests/generics/all.T1
5 files changed, 52 insertions, 1 deletions
diff --git a/testsuite/tests/generics/GShow/GShow.hs b/testsuite/tests/generics/GShow/GShow.hs
index cfe0230411..24c1959ec3 100644
--- a/testsuite/tests/generics/GShow/GShow.hs
+++ b/testsuite/tests/generics/GShow/GShow.hs
@@ -119,7 +119,8 @@ instance GShow Char where gshowsPrec = showsPrec
instance GShow Double where gshowsPrec = showsPrec
instance GShow Int where gshowsPrec = showsPrec
instance GShow Float where gshowsPrec = showsPrec
-instance GShow String where gshowsPrec = showsPrec
+instance {-# OVERLAPPING #-}
+ GShow String where gshowsPrec = showsPrec
instance GShow Word where gshowsPrec = showsPrec
instance GShow Bool where gshowsPrec = showsPrec
diff --git a/testsuite/tests/generics/GenDerivOutput.stderr b/testsuite/tests/generics/GenDerivOutput.stderr
index 2226783029..1b573f26bb 100644
--- a/testsuite/tests/generics/GenDerivOutput.stderr
+++ b/testsuite/tests/generics/GenDerivOutput.stderr
@@ -224,3 +224,9 @@ GHC.Generics representation types:
GenDerivOutput.Rose)))
+
+==================== Filling in method body ====================
+GHC.Base.Functor [GenDerivOutput.List]
+ GHC.Base.<$ = GHC.Base.$dm<$ @GenDerivOutput.List
+
+
diff --git a/testsuite/tests/generics/T10604/T10604_deriving.stderr b/testsuite/tests/generics/T10604/T10604_deriving.stderr
index cbaba8dbbe..04c87ff33d 100644
--- a/testsuite/tests/generics/T10604/T10604_deriving.stderr
+++ b/testsuite/tests/generics/T10604/T10604_deriving.stderr
@@ -541,3 +541,9 @@ GHC.Generics representation types:
* GHC.Types.Int))))
+
+==================== Filling in method body ====================
+GHC.Base.Functor [T10604_deriving.Proxy *]
+ GHC.Base.<$ = GHC.Base.$dm<$ @T10604_deriving.Proxy *
+
+
diff --git a/testsuite/tests/generics/T12220.hs b/testsuite/tests/generics/T12220.hs
new file mode 100755
index 0000000000..70f8b58f84
--- /dev/null
+++ b/testsuite/tests/generics/T12220.hs
@@ -0,0 +1,37 @@
+{-#LANGUAGE TypeApplications#-}
+{-#LANGUAGE MultiParamTypeClasses #-}
+{-#LANGUAGE AllowAmbiguousTypes #-}
+{-#LANGUAGE FlexibleInstances #-}
+{-#LANGUAGE ScopedTypeVariables #-}
+{-#LANGUAGE DefaultSignatures #-}
+module T12220 where
+
+-- | Type a is only used for
+-- type application.
+class ToUse a where
+ toUse :: Int -> Int
+
+-- | The type used for
+-- type application
+data Default
+
+
+-- | The instance using Default as type application.
+-- To call use:
+-- > toUse @Default
+instance ToUse Default where
+ toUse a = 3*a
+
+-- | Typeclass whose methods work
+-- only with type application.
+class Uses a b where
+ uses :: b -> [b]
+ -- | Default Signature, which generates the problem.
+ -- It is the same as the normal one
+ -- Comment it to 'fix' the bug.
+ default uses :: b -> [b]
+ uses v = [v]
+
+-- | But this one doesn't.
+-- Unless you comment the default signature.
+instance (Uses t a, Uses t b, Uses t c) => Uses t (a,b,c)
diff --git a/testsuite/tests/generics/all.T b/testsuite/tests/generics/all.T
index cae975c89e..6bf949fc3b 100644
--- a/testsuite/tests/generics/all.T
+++ b/testsuite/tests/generics/all.T
@@ -45,3 +45,4 @@ test('T10030', normal, compile_and_run, [''])
test('T10361a', normal, compile, [''])
test('T10361b', normal, compile, [''])
test('T11358', normal, compile_and_run, [''])
+test('T12220', normal, compile, [''])