diff options
author | Ryan Scott <ryan.gl.scott@gmail.com> | 2019-10-08 14:37:00 -0400 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2019-10-09 16:21:50 -0400 |
commit | d584e3f08cfee6e28b70bf53c573d86e44f326f8 (patch) | |
tree | 289b0d0128e1cfae35321ddf1c9489d85ede4c7e /testsuite/tests/deriving/should_compile | |
parent | 35cc5eff3d54bacf626ecf0b6e0a1d660cbd6ba9 (diff) | |
download | haskell-d584e3f08cfee6e28b70bf53c573d86e44f326f8.tar.gz |
Use addUsedDataCons more judiciously in TcDeriv (#17324)
If you derive an instance like this:
```hs
deriving <...> instance Foo C
```
And the data constructors for `C` aren't in scope, then
`doDerivInstErrorChecks1` throws an error. Moreover, it will
_only_ throw an error if `<...>` is either `stock` or `newtype`.
This is because the code that the `anyclass` or `via` strategies
would generate would not require the use of the data constructors
for `C`.
However, `doDerivInstErrorChecks1` has another purpose. If you
write this:
```hs
import M (C(MkC1, ..., MkCn))
deriving <...> instance Foo C
```
Then `doDerivInstErrorChecks1` will call `addUsedDataCons` on
`MkC1` through `MkCn` to ensure that `-Wunused-imports` does not
complain about them. However, `doDerivInstErrorChecks1` was doing
this for _every_ deriving strategy, which mean that if `<...>` were
`anyclass` or `via`, then the warning about `MkC1` through `MkCn`
being unused would be suppressed!
The fix is simple enough: only call `addUsedDataCons` when the
strategy is `stock` or `newtype`, just like the other code paths
in `doDerivInstErrorChecks1`.
Fixes #17324.
Diffstat (limited to 'testsuite/tests/deriving/should_compile')
-rw-r--r-- | testsuite/tests/deriving/should_compile/T17324.hs | 17 | ||||
-rw-r--r-- | testsuite/tests/deriving/should_compile/T17324.stderr | 4 | ||||
-rw-r--r-- | testsuite/tests/deriving/should_compile/all.T | 1 |
3 files changed, 22 insertions, 0 deletions
diff --git a/testsuite/tests/deriving/should_compile/T17324.hs b/testsuite/tests/deriving/should_compile/T17324.hs new file mode 100644 index 0000000000..7373af8936 --- /dev/null +++ b/testsuite/tests/deriving/should_compile/T17324.hs @@ -0,0 +1,17 @@ +{-# LANGUAGE DeriveAnyClass #-} +{-# LANGUAGE DerivingStrategies #-} +{-# LANGUAGE DerivingVia #-} +{-# LANGUAGE StandaloneDeriving #-} +{-# OPTIONS_GHC -Wunused-imports #-} +module T17324 where + +import Data.Monoid (Sum(Sum), Product(Product), Dual(Dual)) + +class C1 a +deriving anyclass instance C1 (Sum a) + +class C2 a +deriving anyclass instance C2 (Product a) + +class C3 a +deriving via Dual a instance C3 (Dual a) diff --git a/testsuite/tests/deriving/should_compile/T17324.stderr b/testsuite/tests/deriving/should_compile/T17324.stderr new file mode 100644 index 0000000000..54e6534462 --- /dev/null +++ b/testsuite/tests/deriving/should_compile/T17324.stderr @@ -0,0 +1,4 @@ + +T17324.hs:8:1: warning: [-Wunused-imports (in -Wextra)] + The import of ‘Dual, Product, Sum’ + from module ‘Data.Monoid’ is redundant diff --git a/testsuite/tests/deriving/should_compile/all.T b/testsuite/tests/deriving/should_compile/all.T index a12cf95c28..04fd02518f 100644 --- a/testsuite/tests/deriving/should_compile/all.T +++ b/testsuite/tests/deriving/should_compile/all.T @@ -118,3 +118,4 @@ test('T15637', normal, compile, ['']) test('T15831', normal, compile, ['']) test('T16179', normal, compile, ['']) test('T16518', normal, compile, ['']) +test('T17324', normal, compile, ['']) |