Skip to content

Commit c085855

Browse files
kosmikusBinderDavid
authored andcommitted
Add explanation for GHC-87139.
1 parent f7c49e5 commit c085855

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{-# LANGUAGE DerivingStrategies #-}
2+
module IllegalDerivingStrategy where
3+
4+
newtype Year = MkYear Int
5+
deriving newtype Show
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module IllegalDerivingStrategy where
2+
3+
newtype Year = MkYear Int
4+
deriving newtype Show
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: Illegal deriving strategy on newtype
3+
---
4+
5+
In this example, there is a deriving clause instructing GHC
6+
to derive a `Show` instance for the newtype `Year`.
7+
8+
There are (at least) two different ways to derive such a `Show`
9+
instance. The `stock` strategy is the standard generic way of
10+
deriving `Show` instances for datatypes. The `newtype` strategy
11+
is available for `newtype`s and simply uses the existing instance
12+
of the underlying type, in this case `Int`.
13+
14+
The difference in the current example is that the `stock`-derived
15+
`show` includes the constructor name, the `newtype`-derived does
16+
not.
17+
18+
If multiple strategies are available, GHC chooses a default. In
19+
the case of `deriving` for classes for which `stock`-deriving is
20+
available, such as `Show`, `stock` is the default. If you want to
21+
be explicit about this or explicitly deviate from the default,
22+
you can specify a strategy explicitly.
23+
24+
This, however, requires the `DerivingStrategies` language extension
25+
to be enabled, and if it is not, you will get an error. The fix in
26+
this case is to enable the language extension, for example via a
27+
compiler pragma.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: Illegal deriving strategy
3+
summary: Use of a deriving strategy without enabling the corresponding language extension
4+
severity: error
5+
introduced: 9.8.1
6+
---
7+
8+
This error occurs if the syntax for specifying a deriving strategy
9+
is being used without having the corresponding language extension enabled.
10+
11+
The fix is usually to either enable the language extension, or to remove
12+
the deriving strategy (if it happens to match the default).

0 commit comments

Comments
 (0)