+-- Check that a type index does not contain any type family applications
+--
+-- * Earlier phases have already checked that there are no foralls in the
+-- type; we also cannot have PredTys and NoteTys are being skipped by using
+-- the core view.
+--
+checkTyFamFreeness :: Type -> TcM ()
+checkTyFamFreeness ty | Just (tycon, tys) <- splitTyConApp_maybe ty
+ = if isSynTyCon tycon
+ then addErr $ tyFamAppInIndexErr ty
+ else mappM_ checkTyFamFreeness tys
+ -- splitTyConApp_maybe uses the core view; hence,
+ -- any synonym tycon must be a family tycon
+
+ | Just (ty1, ty2) <- splitAppTy_maybe ty
+ = checkTyFamFreeness ty1 >> checkTyFamFreeness ty2
+
+ | otherwise -- only vars remaining
+ = return ()
+
+