Skip to content

Buggy validity checks for built-in classes

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.

{-# LANGUAGE ExplicitForAll #-}

module M1 where

import GHC.Generics

instance Generic (forall a . a)

Here's another example that should not be accepted:

{-# 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 fd0f0334, 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.)

Edited by Adam Gundry
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information