diff --git a/compiler/GHC/Tc/Errors/Types.hs b/compiler/GHC/Tc/Errors/Types.hs index cbd737cc7a7139ce0efb519cf39abd23de1f04d1..fc41719d830497e47d1d53d27e6605eaf19de0aa 100644 --- a/compiler/GHC/Tc/Errors/Types.hs +++ b/compiler/GHC/Tc/Errors/Types.hs @@ -1201,11 +1201,15 @@ data TcRnMessage where {-| TcRnIllegalClassInst is an error that occurs whenever a class instance is specified for a non-class. + This also includes derived instances. See the T23522 test case. + Examples(s): type C1 a = (Show (a -> Bool)) instance C1 Int where - Test cases: polykinds/T13267 + Test cases: + polykinds/T13267 + deriving/should_fail/T23522 -} TcRnIllegalClassInst :: !(TyConFlavour TyCon) -> TcRnMessage diff --git a/compiler/GHC/Tc/Gen/HsType.hs b/compiler/GHC/Tc/Gen/HsType.hs index bbad885ac0cee91ae61844da5f0d120852fb7999..71d1c5952387928018a6107be297507414cef6d8 100644 --- a/compiler/GHC/Tc/Gen/HsType.hs +++ b/compiler/GHC/Tc/Gen/HsType.hs @@ -99,7 +99,6 @@ import GHC.Tc.Utils.Instantiate ( tcInstInvisibleTyBinders, tcInstInvisibleTyBin import GHC.Tc.Zonk.TcType import GHC.Core.Type -import GHC.Core.Predicate import GHC.Core.TyCo.Rep import GHC.Core.TyCo.Ppr @@ -639,9 +638,15 @@ tcHsDeriv hs_ty = do { ty <- tcTopLHsType DerivClauseCtxt hs_ty ; let (tvs, pred) = splitForAllTyCoVars ty (kind_args, _) = splitFunTys (typeKind pred) - ; case getClassPredTys_maybe pred of - Just (cls, tys) -> return (tvs, cls, tys, map scaledThing kind_args) - Nothing -> failWithTc $ TcRnIllegalDerivingItem hs_ty } + -- Checking that `pred` a is type class application + ; case splitTyConApp_maybe pred of + Just (tyCon, tyConArgs) -> + case tyConClass_maybe tyCon of + Just clas -> + return (tvs, clas, tyConArgs, map scaledThing kind_args) + Nothing -> failWithTc $ TcRnIllegalClassInst (tyConFlavour tyCon) + Nothing -> failWithTc $ TcRnIllegalDerivingItem hs_ty + } -- | Typecheck a deriving strategy. For most deriving strategies, this is a -- no-op, but for the @via@ strategy, this requires typechecking the @via@ type. diff --git a/testsuite/tests/deriving/should_fail/T23522.hs b/testsuite/tests/deriving/should_fail/T23522.hs new file mode 100644 index 0000000000000000000000000000000000000000..1a5c4a29042bcca410b22e2537a6594d0cc43230 --- /dev/null +++ b/testsuite/tests/deriving/should_fail/T23522.hs @@ -0,0 +1,5 @@ +module T23522 where + +type F x = Show x + +data Proposition = Proposition deriving (F) diff --git a/testsuite/tests/deriving/should_fail/T23522.stderr b/testsuite/tests/deriving/should_fail/T23522.stderr new file mode 100644 index 0000000000000000000000000000000000000000..f5a9c9b7bee03ddf9d5951fbcc79069cbec93e09 --- /dev/null +++ b/testsuite/tests/deriving/should_fail/T23522.stderr @@ -0,0 +1,5 @@ + +T23522.hs:5:42: [GHC-53946] + Illegal instance for a type synonym + A class instance must be for a class + In the data declaration for ‘Proposition’ diff --git a/testsuite/tests/deriving/should_fail/all.T b/testsuite/tests/deriving/should_fail/all.T index d1c897e059cc45c2b203020870a57ceab4cfb7d7..e0ef6d61749f1d523e2de74fdea7a0d06e9ae4f1 100644 --- a/testsuite/tests/deriving/should_fail/all.T +++ b/testsuite/tests/deriving/should_fail/all.T @@ -87,3 +87,4 @@ test('T21087b', [extra_files(['T21087b_aux.hs','T21087b_aux.hs-boot'])], multimo test('T21302', normal, compile_fail, ['']) test('T21871', normal, compile_fail, ['']) test('T22696b', normal, compile_fail, ['']) +test('T23522', normal, compile_fail, [''])