Skip to content

Commit 93b0256

Browse files
authored
Merge pull request #425 from haskellfoundation/ghc-93557-ghc-54540
Document GHC-54540 and GHC-93557
2 parents 7e2013c + cc67979 commit 93b0256

File tree

8 files changed

+93
-0
lines changed

8 files changed

+93
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{-# LANGUAGE StandaloneDeriving #-}
2+
module ConstructorsNotInScope where
3+
4+
import System.IO( Handle )
5+
6+
-- It is not possible to derive an instance for the type class in this module.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{-# LANGUAGE StandaloneDeriving #-}
2+
module ConstructorsNotInScope where
3+
4+
import System.IO( Handle )
5+
6+
deriving instance Eq Handle
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: Cannot derive an instance for an abstract type
3+
---
4+
5+
The `Handle` type is abstract and does not export its constructors. It is therefore not possible to derive an instance of a typeclass for the `Handle` type in another module. You can only derive an instance of a typeclass in the module where `Handle` is defined.
6+
7+
````
8+
messages/GHC-54540/constructorsNotInScope/before/ConstructorsNotInScope.hs:6:1: error: [GHC-54540]
9+
• Can't make a derived instance of ‘Eq Handle’:
10+
The data constructors of ‘Handle’ are not all in scope
11+
so you cannot derive an instance for it
12+
• In the stand-alone deriving instance for ‘Eq Handle’
13+
|
14+
6 | deriving instance Eq Handle
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: Cannot derive instance without constructors in scope
3+
summary: It is not possible to derive a typeclass instance if the constructors of the type are not in scope
4+
severity: error
5+
introduced: 9.6.1
6+
---
7+
8+
Deriving an instance of a typeclass for a type is only possible if the constructors of the type are in scope at the point where you want to derive the instance.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
module IllegalSynonymInstance where
3+
4+
data RGB = R | G | B
5+
6+
instance Eq RGB where
7+
R == R = True
8+
G == G = True
9+
B == B = True
10+
_ == _ = False
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
module IllegalSynonymInstance where
3+
4+
data RGB = R | G | B
5+
6+
type T = RGB
7+
8+
instance Eq T where
9+
R == R = True
10+
G == G = True
11+
B == B = True
12+
_ == _ = False
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Cannot implement a typeclass for type synonyms by default
3+
---
4+
5+
The programmer defined a data type `RGB`, and a type synonym `T`, and then tried to implement an instance of the type class `Eq` for `T`.
6+
In the language version Haskell 2010 this is not allowed, since all type class instances have to start with a type constructor.
7+
This problem can be fixed by implementing the typeclass instance directly for the type `RGB` instead of the type synonym `T`.
8+
9+
```
10+
messages/GHC-93557/illegalSynonymInstance/before/IllegalSynonymInstance.hs:8:10: error: [GHC-93557]
11+
• Illegal instance declaration for ‘Eq T’:
12+
All instance types must be of the form (T t1 ... tn)
13+
where T is not a synonym.
14+
• In the instance declaration for ‘Eq T’
15+
Suggested fix: Perhaps you intended to use TypeSynonymInstances
16+
|
17+
8 | instance Eq T where
18+
| ^^^^
19+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: Illegal typeclass instance
3+
summary: Illegal typeclass instance
4+
severity: error
5+
introduced: 9.8.1
6+
---
7+
8+
Using the language version Haskell 2010, only certain types are allowed as instances of a type class.
9+
Every type class instance in Haskell 2010 has the following form:
10+
```
11+
instance C t where
12+
````
13+
Here, `C` is the name of a type class, such as `Eq`, `Show`, `Read` or `Functor`, and `t` is a type.
14+
But only a subset of types `t` is allowed to appear in typeclass instances.
15+
These types must all have the form `T` or `T a ... b`, where `T` has to be the name of a type introduced by a data or newtype declaration, and the arguments `a` to `b` all have to be type variables.
16+
For example, `Maybe a` is allowed as a type in an instance, because the data type `Maybe` instantiates `T` and `a` is a type variable`, but `Maybe Int` is not allowed, since `Int` is not a type variable.

0 commit comments

Comments
 (0)