summaryrefslogtreecommitdiff
path: root/testsuite/tests/generics/T12220.hs
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/T12220.hs
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/T12220.hs')
-rwxr-xr-xtestsuite/tests/generics/T12220.hs37
1 files changed, 37 insertions, 0 deletions
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)