Commit c5609577 authored by sean's avatar sean Committed by Matthew Pickering

Disallow users to write instances of KnownNat and KnownSym

As noted in #12837, these classes are special and the user should
not be able to define their own instances.

Test Plan: Validate

Reviewers: adamgundry, goldfire, mpickering, austin, bgamari

Reviewed By: goldfire, mpickering

Subscribers: goldfire, mpickering, thomie

Differential Revision: https://phabricator.haskell.org/D2898

GHC Trac Issues: #12837
parent 9ff07382
......@@ -46,7 +46,8 @@ import Class
import Var
import VarEnv
import VarSet
import PrelNames ( typeableClassName, genericClassNames )
import PrelNames ( typeableClassName, genericClassNames
, knownNatClassName, knownSymbolClassName )
import Bag
import BasicTypes
import DynFlags
......@@ -518,9 +519,10 @@ doClsInstErrorChecks inst_info
-- In hs-boot files there should be no bindings
; failIfTc (is_boot && not no_binds) badBootDeclErr
-- Handwritten instances of the poly-kinded Typeable
-- class are always forbidden
; failIfTc (clas_nm == typeableClassName) typeable_err
-- Handwritten instances of any rejected
-- class is always forbidden
-- #12837
; failIfTc (clas_nm `elem` rejectedClassNames) clas_err
-- Check for hand-written Generic instances (disallowed in Safe Haskell)
; when (clas_nm `elem` genericClassNames) $
......@@ -538,11 +540,14 @@ doClsInstErrorChecks inst_info
text "Replace the following instance:")
2 (pprInstanceHdr ispec)
-- Report an error or a warning for a Typeable instances.
-- Report an error or a warning for certain class instances.
-- If we are working on an .hs-boot file, we just report a warning,
-- and ignore the instance. We do this, to give users a chance to fix
-- their code.
typeable_err = text "Class" <+> quotes (ppr clas_nm)
rejectedClassNames = [ typeableClassName
, knownNatClassName
, knownSymbolClassName ]
clas_err = text "Class" <+> quotes (ppr clas_nm)
<+> text "does not support user-specified instances"
{-
......
{-# LANGUAGE FlexibleInstances #-}
module T12837 where
import GHC.TypeLits
import Data.Typeable
data K = K
instance Typeable K
instance KnownNat n
instance KnownSymbol n
T12837.hs:10:1: error:
• Class ‘Typeable’ does not support user-specified instances
• In the instance declaration for ‘Typeable K’
T12837.hs:11:1: error:
• Class ‘KnownNat’ does not support user-specified instances
• In the instance declaration for ‘KnownNat n’
T12837.hs:12:1: error:
• Class ‘KnownSymbol’ does not support user-specified instances
• In the instance declaration for ‘KnownSymbol n’
......@@ -433,3 +433,4 @@ test('T12729', normal, compile_fail, [''])
test('T12803', normal, compile_fail, [''])
test('T12042', extra_clean(['T12042a.hi', 'T12042a.o', 'T12042.hi-boot', 'T12042.o-boot']), multimod_compile_fail, ['T12042', ''])
test('T12966', normal, compile_fail, [''])
test('T12837', normal, compile_fail, [''])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment