Error when deriving instances in hs-boot files
authorRyan Scott <ryan.gl.scott@gmail.com>
Thu, 19 Oct 2017 14:21:17 +0000 (10:21 -0400)
committerRyan Scott <ryan.gl.scott@gmail.com>
Thu, 19 Oct 2017 14:21:17 +0000 (10:21 -0400)
Summary:
According to the GHC users' guide, one cannot derive
instances for data types in `.hs-boot` files.
However, GHC was not enforcing this in practice, which led to
#14365.

Fix this by actually throwing an error if a derived instance is
detected in an `.hs-boot` file (and recommend how to fix it in the
error message.)

Test Plan: make test TEST=T14365

Reviewers: ezyang, austin, bgamari, simonpj

Reviewed By: simonpj

Subscribers: simonpj, rwbarton, thomie

GHC Trac Issues: #14365

Differential Revision: https://phabricator.haskell.org/D4102

compiler/typecheck/TcDeriv.hs
testsuite/tests/deriving/should_fail/T14365.stderr [new file with mode: 0644]
testsuite/tests/deriving/should_fail/T14365A.hs [new file with mode: 0644]
testsuite/tests/deriving/should_fail/T14365B.hs [new file with mode: 0644]
testsuite/tests/deriving/should_fail/T14365B.hs-boot [new file with mode: 0644]
testsuite/tests/deriving/should_fail/all.T
testsuite/tests/deriving/should_fail/drvfail016.stderr

index 77a313b..33ce581 100644 (file)
@@ -956,6 +956,10 @@ mkEqnHelp overlap_mode tvs cls cls_tys tycon tc_args mtheta deriv_strat
               -- If it's still a data family, the lookup failed; i.e no instance exists
        ; when (isDataFamilyTyCon rep_tc)
               (bale_out (text "No family instance for" <+> quotes (pprTypeApp tycon tc_args)))
+       ; is_boot <- tcIsHsBootOrSig
+       ; when is_boot $
+              bale_out (text "Cannot derive instances in hs-boot files"
+                    $+$ text "Write an instance declaration instead")
 
        ; let deriv_env = DerivEnv
                          { denv_overlap_mode = overlap_mode
diff --git a/testsuite/tests/deriving/should_fail/T14365.stderr b/testsuite/tests/deriving/should_fail/T14365.stderr
new file mode 100644 (file)
index 0000000..f8f106f
--- /dev/null
@@ -0,0 +1,13 @@
+[1 of 3] Compiling T14365B[boot]    ( T14365B.hs-boot, T14365B.o-boot )
+
+T14365B.hs-boot:5:13: error:
+    • Can't make a derived instance of ‘Functor Foo’:
+        Cannot derive instances in hs-boot files
+        Write an instance declaration instead
+    • In the data declaration for ‘Foo’
+
+T14365B.hs-boot:7:1: error:
+    • Can't make a derived instance of ‘Foldable Foo’:
+        Cannot derive instances in hs-boot files
+        Write an instance declaration instead
+    • In the stand-alone deriving instance for ‘Foldable Foo’
diff --git a/testsuite/tests/deriving/should_fail/T14365A.hs b/testsuite/tests/deriving/should_fail/T14365A.hs
new file mode 100644 (file)
index 0000000..e80e7d1
--- /dev/null
@@ -0,0 +1,5 @@
+module T14365A where
+
+import {-# SOURCE #-} T14365B
+
+main = return ()
diff --git a/testsuite/tests/deriving/should_fail/T14365B.hs b/testsuite/tests/deriving/should_fail/T14365B.hs
new file mode 100644 (file)
index 0000000..596c275
--- /dev/null
@@ -0,0 +1,4 @@
+module T14365B where
+
+data Foo a = Foo a
+  deriving (Functor)
diff --git a/testsuite/tests/deriving/should_fail/T14365B.hs-boot b/testsuite/tests/deriving/should_fail/T14365B.hs-boot
new file mode 100644 (file)
index 0000000..1dcbc94
--- /dev/null
@@ -0,0 +1,7 @@
+{-# LANGUAGE StandaloneDeriving #-}
+module T14365B where
+
+data Foo a
+  deriving (Functor)
+
+deriving instance Foldable Foo
index 77ce145..1861e6d 100644 (file)
@@ -66,3 +66,5 @@ test('T11509_1', [when(doing_ghci(), extra_hc_opts('-fobject-code'))],
 test('T12163', normal, compile_fail, [''])
 test('T12512', omit_ways(['ghci']), compile_fail, [''])
 test('T12801', normal, compile_fail, [''])
+test('T14365', [extra_files(['T14365B.hs','T14365B.hs-boot'])],
+               multimod_compile_fail, ['T14365A',''])
index 182b748..05abbf5 100644 (file)
@@ -1,4 +1,6 @@
 
 drvfail016.hs-boot:7:14: error:
-    Deriving not permitted in hs-boot file
-      Use an instance declaration instead
+    • Can't make a derived instance of ‘Show D’:
+        Cannot derive instances in hs-boot files
+        Write an instance declaration instead
+    • In the data declaration for ‘D’