GHC issueshttps://gitlab.haskell.org/ghc/ghc/-/issues2020-02-25T01:05:57Zhttps://gitlab.haskell.org/ghc/ghc/-/issues/17355Buggy validity checks for built-in classes2020-02-25T01:05:57ZAdam GundryBuggy validity checks for built-in classesBuilt-in classes such as `Generic` and `HasField` do not appear to have validity checks applied properly. In particular, it is possible to give impredicative instances without using `ImpredicativeTypes`.
The following obvious nonsense is accepted (with warnings) by GHC 8.8.1. In GHC 8.6.5 and earlier it asks for `FlexibleInstances`, but then fails with `Illegal polymorphic type: forall a. a`, which is a reasonable (if terse) response. Ideally it should fail with such an error without even requesting `FlexibleInstances`.
```hs
{-# LANGUAGE ExplicitForAll #-}
module M1 where
import GHC.Generics
instance Generic (forall a . a)
```
Here's another example that should not be accepted:
```hs
{-# LANGUAGE RankNTypes, DataKinds #-}
module M2 where
import GHC.Records
data Foo = Foo { poly :: forall a. a -> a }
instance HasField "myPoly" Foo (forall a. a -> a) where
getField (Foo x) = x
```
From a brief look, I suspect this may be related to fd0f0334189c0c5c9b186bd1b009f706d3d86086, which refactored `checkValidInstHead` and AFAICS dropped the relevant checks.
(Thanks to @phadej for spotting this in https://github.com/ghc-proposals/ghc-proposals/pull/282#issuecomment-541518676, although it is broader than just `HasField`.)Built-in classes such as `Generic` and `HasField` do not appear to have validity checks applied properly. In particular, it is possible to give impredicative instances without using `ImpredicativeTypes`.
The following obvious nonsense is accepted (with warnings) by GHC 8.8.1. In GHC 8.6.5 and earlier it asks for `FlexibleInstances`, but then fails with `Illegal polymorphic type: forall a. a`, which is a reasonable (if terse) response. Ideally it should fail with such an error without even requesting `FlexibleInstances`.
```hs
{-# LANGUAGE ExplicitForAll #-}
module M1 where
import GHC.Generics
instance Generic (forall a . a)
```
Here's another example that should not be accepted:
```hs
{-# LANGUAGE RankNTypes, DataKinds #-}
module M2 where
import GHC.Records
data Foo = Foo { poly :: forall a. a -> a }
instance HasField "myPoly" Foo (forall a. a -> a) where
getField (Foo x) = x
```
From a brief look, I suspect this may be related to fd0f0334189c0c5c9b186bd1b009f706d3d86086, which refactored `checkValidInstHead` and AFAICS dropped the relevant checks.
(Thanks to @phadej for spotting this in https://github.com/ghc-proposals/ghc-proposals/pull/282#issuecomment-541518676, although it is broader than just `HasField`.)8.8.2