[commit: ghc] master: Error when deriving instances in hs-boot files (101a8c7)
git at git.haskell.org
git at git.haskell.org
Thu Oct 19 14:26:24 UTC 2017
Repository : ssh://git@git.haskell.org/ghc
On branch : master
Link : http://ghc.haskell.org/trac/ghc/changeset/101a8c770b9d3abd57ff289bffea3d838cf25c80/ghc
>---------------------------------------------------------------
commit 101a8c770b9d3abd57ff289bffea3d838cf25c80
Author: Ryan Scott <ryan.gl.scott at gmail.com>
Date: Thu Oct 19 10:21:17 2017 -0400
Error when deriving instances in hs-boot files
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
>---------------------------------------------------------------
101a8c770b9d3abd57ff289bffea3d838cf25c80
compiler/typecheck/TcDeriv.hs | 4 ++++
testsuite/tests/deriving/should_fail/T14365.stderr | 13 +++++++++++++
testsuite/tests/deriving/should_fail/T14365A.hs | 5 +++++
testsuite/tests/deriving/should_fail/T14365B.hs | 4 ++++
testsuite/tests/deriving/should_fail/T14365B.hs-boot | 7 +++++++
testsuite/tests/deriving/should_fail/all.T | 2 ++
testsuite/tests/deriving/should_fail/drvfail016.stderr | 6 ++++--
7 files changed, 39 insertions(+), 2 deletions(-)
diff --git a/compiler/typecheck/TcDeriv.hs b/compiler/typecheck/TcDeriv.hs
index 77a313b..33ce581 100644
--- a/compiler/typecheck/TcDeriv.hs
+++ b/compiler/typecheck/TcDeriv.hs
@@ -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
index 0000000..f8f106f
--- /dev/null
+++ b/testsuite/tests/deriving/should_fail/T14365.stderr
@@ -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
index 0000000..e80e7d1
--- /dev/null
+++ b/testsuite/tests/deriving/should_fail/T14365A.hs
@@ -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
index 0000000..596c275
--- /dev/null
+++ b/testsuite/tests/deriving/should_fail/T14365B.hs
@@ -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
index 0000000..1dcbc94
--- /dev/null
+++ b/testsuite/tests/deriving/should_fail/T14365B.hs-boot
@@ -0,0 +1,7 @@
+{-# LANGUAGE StandaloneDeriving #-}
+module T14365B where
+
+data Foo a
+ deriving (Functor)
+
+deriving instance Foldable Foo
diff --git a/testsuite/tests/deriving/should_fail/all.T b/testsuite/tests/deriving/should_fail/all.T
index 77ce145..1861e6d 100644
--- a/testsuite/tests/deriving/should_fail/all.T
+++ b/testsuite/tests/deriving/should_fail/all.T
@@ -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',''])
diff --git a/testsuite/tests/deriving/should_fail/drvfail016.stderr b/testsuite/tests/deriving/should_fail/drvfail016.stderr
index 182b748..05abbf5 100644
--- a/testsuite/tests/deriving/should_fail/drvfail016.stderr
+++ b/testsuite/tests/deriving/should_fail/drvfail016.stderr
@@ -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’
More information about the ghc-commits
mailing list