Commit c448d551 authored by Ryan Scott's avatar Ryan Scott Committed by Ben Gamari

Disallow standalone deriving declarations involving unboxed tuples or sums

There was an awful leak where GHC permitted standalone `deriving`
declarations to create instances for unboxed sum or tuple types. This
fortifies the checks that GHC performs to catch this scenario and give
an appropriate error message.

Fixes #11509.

Test Plan: ./validate

Reviewers: goldfire, austin, bgamari

Reviewed By: bgamari

Subscribers: thomie

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

GHC Trac Issues: #11509

(cherry picked from commit 23cf32da)
parent 906ea044
......@@ -583,12 +583,21 @@ deriveStandalone (L loc (DerivDecl deriv_ty overlap_mode))
, text "class types:" <+> ppr cls_tys
, text "type:" <+> ppr inst_ty ]
; let bale_out msg = failWithTc (derivingThingErr False cls cls_tys
inst_ty deriv_strat msg)
; case tcSplitTyConApp_maybe inst_ty of
Just (tc, tc_args)
| className cls == typeableClassName
-> do warnUselessTypeable
return []
| isUnboxedTupleTyCon tc
-> bale_out $ unboxedTyConErr "tuple"
| isUnboxedSumTyCon tc
-> bale_out $ unboxedTyConErr "sum"
| isAlgTyCon tc || isDataFamilyTyCon tc -- All other classes
-> do { spec <- mkEqnHelp (fmap unLoc overlap_mode)
tvs cls cls_tys tc tc_args
......@@ -596,7 +605,7 @@ deriveStandalone (L loc (DerivDecl deriv_ty overlap_mode))
; return [spec] }
_ -> -- Complain about functions, primitive types, etc,
failWithTc $ derivingThingErr False cls cls_tys inst_ty $
bale_out $
text "The last argument of the instance must be a data or newtype application"
}
......@@ -2438,3 +2447,7 @@ standaloneCtxt ty = hang (text "In the stand-alone deriving instance for")
derivInstCtxt :: PredType -> MsgDoc
derivInstCtxt pred
= text "When deriving the instance for" <+> parens (ppr pred)
unboxedTyConErr :: String -> MsgDoc
unboxedTyConErr thing =
text "The last argument of the instance cannot be an unboxed" <+> text thing
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UnboxedSums #-}
{-# LANGUAGE UnboxedTuples #-}
module T12512 where
import GHC.Exts
class Wat1 (a :: TYPE 'UnboxedTupleRep)
deriving instance Wat1 (# a, b #)
class Wat2 (a :: TYPE 'UnboxedSumRep)
deriving instance Wat2 (# a | b #)
T12512.hs:11:1: error:
• Can't make a derived instance of ‘Wat1 (# a, b #)’:
The last argument of the instance cannot be an unboxed tuple
• In the stand-alone deriving instance for ‘Wat1 (# a, b #)’
T12512.hs:14:1: error:
• Can't make a derived instance of ‘Wat2 (# a | b #)’:
The last argument of the instance cannot be an unboxed sum
• In the stand-alone deriving instance for ‘Wat2 (# a | b #)’
......@@ -58,4 +58,4 @@ test('T9687', normal, compile_fail, [''])
test('T8984', normal, compile_fail, [''])
test('T9968a', normal, compile_fail, [''])
test('T12512', omit_ways(['ghci']), 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