Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC Commits: 309b7f0e by sheaf at 2026-11-30T04:12:13-05:00 Make FunDepEqns a non-empty list This avoids creating a 'FunDepEqns' with no equations. This means that consumers dealing with [FunDepEqns] can reliably use "null fun_dep_eqns" to decide whether there are any equations. - - - - - 9bb95671 by sheaf at 2026-11-30T04:12:14-05:00 tryFamEqFunDeps: (CF1) only for closed tyfams This commit makes (CF1) in Note [Exploiting closed type families] only apply to *closed* type families, not *open* type families. This allows the program reported in #28082 to continue to be accepted, fixing a regression. Fixes #28082 - - - - - 5 changed files: - + changelog.d/T28082 - compiler/GHC/Tc/Instance/FunDeps.hs - compiler/GHC/Tc/Solver/FunDeps.hs - + testsuite/tests/indexed-types/should_compile/T28082.hs - testsuite/tests/indexed-types/should_compile/all.T Changes: ===================================== changelog.d/T28082 ===================================== @@ -0,0 +1,9 @@ +section: compiler +issues: #28082 +mrs: !16930 +synopsis: + Fix a regression in type inference for injective open type families +description: + Some programs involving an injective open type family with a Given equality + involving that same type family are no longer incorrectly rejected by the + typechecker. ===================================== compiler/GHC/Tc/Instance/FunDeps.hs ===================================== @@ -48,6 +48,8 @@ import GHC.Utils.Panic import GHC.Data.Pair ( Pair(..) ) import Data.List ( nubBy ) +import Data.List.NonEmpty ( NonEmpty, nonEmpty ) +import qualified Data.List.NonEmpty as NE import Data.Maybe ( isJust, isNothing ) {- @@ -123,7 +125,7 @@ Wrinkles: FDEqns { fd_qtvs = [x2], fd_eqs = [ Maybe x2 ~ ty ] } with two FDEqns, generating two separate unification variables. - This is why fd_eqs is a [TypeEqn] not just a TypeEqn + This is why fd_eqs is a (NonEmpty TypeEqn) not just a TypeEqn (IMP3) improveFromInstEnv doesn't return any equations that already hold. Reason: just an optimisation; the caller does the same thing, but @@ -136,7 +138,7 @@ data FunDepEqns -- Plural becuase fd_eqs has multiple equations -- to fresh unification vars, -- See (IMP2) in Note [Improving against instances] - , fd_eqs :: [TypeEqn] -- Make these pairs of types equal + , fd_eqs :: NonEmpty TypeEqn -- Make these pairs of types equal -- Invariant: In each (Pair ty1 ty2), the fd_qtvs may be -- free in ty1 but not in ty2. See Wrinkle (1) of -- Note [Improving against instances] @@ -149,7 +151,7 @@ pprEquation :: FunDepEqns -> SDoc pprEquation (FDEqns { fd_qtvs = qtvs, fd_eqs = pairs }) = vcat [text "forall" <+> braces (pprWithCommas ppr qtvs), nest 2 (vcat [ ppr t1 <+> text "~" <+> ppr t2 - | Pair t1 t2 <- pairs])] + | Pair t1 t2 <- NE.toList pairs])] {- Given a bunch of predicates that must hold, such as @@ -216,8 +218,7 @@ improveFromAnother pred1 pred2 , let (ltys1, rs1) = instFD fd cls_tvs tys1 (ltys2, rs2) = instFD fd cls_tvs tys2 , eqTypes ltys1 ltys2 -- The LHSs match - , let eqs = zipAndComputeFDEqs eqType rs1 rs2 - , not (null eqs) ] + , Just eqs <- [nonEmpty (zipAndComputeFDEqs eqType rs1 rs2)] ] improveFromAnother _ _ = [] @@ -232,7 +233,7 @@ improveFromInstEnv :: InstEnvs -- See Note [Improving against instances] -- Post: Equations oriented from the template (matching instance) to the workitem! improveFromInstEnv inst_env cls tys - = [ FDEqns { fd_qtvs = meta_tvs, fd_eqs = eqs } + = [ fd_eqn | fd <- cls_fds -- Iterate through the fundeps first, -- because there often are none! , let trimmed_tcs = trimRoughMatchTcs cls_tvs fd rough_tcs @@ -241,8 +242,8 @@ improveFromInstEnv inst_env cls tys -- symmetrically, so it's ok to trim the rough_tcs, -- rather than trimming each inst_tcs in turn , ispec <- instances - , (meta_tvs, eqs) <- improveClsFD cls_tvs fd ispec - tys trimmed_tcs -- NB: orientation + , fd_eqn <- improveClsFD cls_tvs fd ispec + tys trimmed_tcs -- NB: orientation ] where (cls_tvs, cls_fds) = classTvsFds cls @@ -252,7 +253,7 @@ improveFromInstEnv inst_env cls tys improveClsFD :: [TyVar] -> FunDep TyVar -- One functional dependency from the class -> ClsInst -- An instance template -> [Type] -> [RoughMatchTc] -- Arguments of this (C tys) predicate - -> [([TyCoVar], [TypeEqn])] -- Empty or singleton + -> [FunDepEqns] -- Empty or singleton -- See Note [Improving against instances] improveClsFD clas_tvs fd @@ -284,10 +285,7 @@ improveClsFD clas_tvs fd -- so we would produce no FDs, which is clearly wrong. -> [] - | null fdeqs - -> [] - - | otherwise + | Just fdeqs_ne <- nonEmpty fdeqs -> -- pprTrace "iproveClsFD" (vcat -- [ text "is_tvs =" <+> ppr qtvs -- , text "tys_inst =" <+> ppr tys_inst @@ -295,12 +293,15 @@ improveClsFD clas_tvs fd -- , text "ltys1 =" <+> ppr ltys1 -- , text "ltys2 =" <+> ppr ltys2 -- , text "subst =" <+> ppr subst ]) $ - [(meta_tvs, fdeqs)] + [FDEqns { fd_qtvs = meta_tvs, fd_eqs = fdeqs_ne }] -- We could avoid this substTy stuff by producing the eqn -- (qtvs, ls1++rs1, ls2++rs2) -- which will re-do the ls1/ls2 unification when the equation is -- executed. What we're doing instead is recording the partial -- work of the ls1/ls2 unification leaving a smaller unification problem + + | otherwise + -> [] where rtys1' = map (substTy subst) rtys1 ===================================== compiler/GHC/Tc/Solver/FunDeps.hs ===================================== @@ -40,9 +40,10 @@ import GHC.Types.Var.Env( mkInScopeSet ) import GHC.Utils.Outputable import GHC.Utils.Panic -import Control.Monad( unless ) import GHC.Data.Pair -import Data.Maybe( isJust, mapMaybe ) +import Data.List.NonEmpty( NonEmpty(..), nonEmpty ) +import qualified Data.List.NonEmpty as NE +import Data.Maybe( isJust, mapMaybe, maybeToList ) {- Note [Overview of functional dependencies in type inference] @@ -584,10 +585,8 @@ tryFamEqFunDeps mode eqs_for_me fam_tc work_args eqns <- mkLocalBuiltinFamEqFDs eqs_for_me fam_tc ops work_args work_rhs ; tryFDEqns False fam_tc work_args work_item eqns - ; unless (hasRelevantGiven eqs_for_me work_args work_item) $ - -- hasRelevantGiven: See (CF1) in Note [Exploiting closed type families] - do { eqns <- mkTopBuiltinFamEqFDs fam_tc ops work_args work_rhs - ; tryFDEqns True fam_tc work_args work_item eqns } } + ; eqns <- mkTopBuiltinFamEqFDs eqs_for_me fam_tc ops work_args work_item + ; tryFDEqns True fam_tc work_args work_item eqns } | isGiven ev -- See (INJFAM:Given) , not (tcsmResumable mode) -- In the pattern-match checker, continue even for @@ -598,20 +597,16 @@ tryFamEqFunDeps mode eqs_for_me fam_tc work_args | otherwise -- Wanted, user-defined type families = do { -- Note [Do local fundeps before top-level instances] - case tyConInjectivityInfo fam_tc of - NotInjective -> nopStage () - Injective inj -> do { eqns <- mkLocalFamEqFDs eqs_for_me fam_tc inj work_args work_rhs - ; tryFDEqns False fam_tc work_args work_item eqns } + eqns <- mkLocalFamEqFDs eqs_for_me fam_tc work_args work_rhs + ; tryFDEqns False fam_tc work_args work_item eqns -- NB: tryFDEqns bales out on an insoluble fundep -- See (CF4) in Note [Exploiting closed type families] - ; unless (hasRelevantGiven eqs_for_me work_args work_item) $ - -- hasRelevantGiven: See (CF1) in Note [Exploiting closed type families] - do { eqns <- mkTopFamEqFDs fam_tc work_args work_item - ; tryFDEqns True fam_tc work_args work_item eqns } } + ; eqns <- mkTopFamEqFDs eqs_for_me fam_tc work_args work_item + ; tryFDEqns True fam_tc work_args work_item eqns } -mkTopFamEqFDs :: TyCon -> [TcType] -> EqCt -> SolverStage [FunDepEqns] -mkTopFamEqFDs fam_tc work_args work_item +mkTopFamEqFDs :: [EqCt] -> TyCon -> [TcType] -> EqCt -> SolverStage [FunDepEqns] +mkTopFamEqFDs eqs_for_me fam_tc work_args work_item -- NB: work_args exactly saturate fam_tc | isOpenTypeFamilyTyCon fam_tc , Injective inj_flags <- tyConInjectivityInfo fam_tc @@ -621,7 +616,11 @@ mkTopFamEqFDs fam_tc work_args work_item -- Non built-in closed type families -- (Built-in closed type families were dealt with in 'tryFamEqFunDeps'.) | Just ax <- closedFamilyTyConCoAxiom_maybe fam_tc - = mkTopClosedFamEqFDs ax work_args work_item + = if hasRelevantGiven eqs_for_me work_args work_item + -- hasRelevantGiven: See (CF1) in Note [Exploiting closed type families] + -- NB: (CF1) applies only to /closed/ type families (#28082) + then return [] + else mkTopClosedFamEqFDs ax work_args work_item | otherwise = -- Anything else: data families, open type families that are not injective, @@ -630,6 +629,8 @@ mkTopFamEqFDs fam_tc work_args work_item tryFDEqns :: Bool -> TyCon -> [TcType] -> EqCt -> [FunDepEqns] -> SolverStage () tryFDEqns is_top fam_tc work_args work_item@(EqCt { eq_ev = ev, eq_rhs= rhs }) fd_eqns + | null fd_eqns = return () -- nothing to do + | otherwise = Stage $ do { traceTcS "tryFDEqns" (vcat [ text "lhs:" <+> ppr fam_tc <+> ppr work_args , text "rhs:" <+> ppr rhs @@ -699,7 +700,7 @@ getRelevantBranches :: CoAxiom Branched -> [TcType] -> Xi -> [FunDepEqns] getRelevantBranches ax work_args work_rhs = go (fromBranches (coAxiomBranches ax)) where - work_tys = work_rhs : work_args + work_tys = work_rhs :| work_args go [] = [] go (branch:branches) @@ -710,7 +711,7 @@ getRelevantBranches ax work_args work_rhs is_relevant branch@(CoAxBranch { cab_tvs = qtvs , cab_lhs = lhs_tys , cab_rhs = rhs_ty }) - | Just subst <- tcUnifyTysForInjectivity Unifying (rhs_ty:lhs_tys) work_tys + | Just subst <- tcUnifyTysForInjectivity Unifying (rhs_ty:lhs_tys) (NE.toList work_tys) -- See (RW1)-(RW3) in Note [Relevance] , let lhs_tys' = map (deepSubstTy subst) lhs_tys -- deepSubstTy: see (CF8) in Note [Exploiting closed type families] @@ -721,15 +722,15 @@ getRelevantBranches ax work_args work_rhs then -- `subst` eliminates all qtvs; and no qtvs in range of subst -- See (CF7)(A) in Note [Exploiting closed type families] FDEqns { fd_qtvs = [] - , fd_eqs = zipWith Pair (substTy subst rhs_ty : lhs_tys') - work_tys } + , fd_eqs = NE.zipWith Pair (substTy subst rhs_ty :| lhs_tys') + work_tys } -- Do not apply `subst` to `work_tys`; -- See (CF9) in Note [Exploiting closed type families] else -- Abandon `subst`; re-create it from (rhs_ty ~ work_rhs) -- See (CF7)(B) in Note [Exploiting closed type families] FDEqns { fd_qtvs = qtvs - , fd_eqs = zipWith Pair (rhs_ty:lhs_tys) work_tys } + , fd_eqs = NE.zipWith Pair (rhs_ty :| lhs_tys) work_tys } | otherwise = Nothing @@ -757,32 +758,36 @@ mkTopOpenFamEqFDs fam_tc inj_flags work_args (EqCt { eq_rhs = work_rhs }) branch_lhs_tys' = substTys subst' branch_lhs_tys -- We must apply the substitution here see -- the Basic Plan in Note [Exploiting open type families] - = Just (mkInjectivityFDEqn qtvs' inj_flags branch_lhs_tys' work_args) + = mkInjectivityFDEqn qtvs' inj_flags branch_lhs_tys' work_args | otherwise = Nothing -mkLocalFamEqFDs :: [EqCt] -> TyCon -> [Bool] -> [TcType] -> Xi -> SolverStage [FunDepEqns] +mkLocalFamEqFDs :: [EqCt] -> TyCon -> [TcType] -> Xi -> SolverStage [FunDepEqns] -- Wanted constraints only -- Both open and closed type families, but only ones with declared injectivity -- See Note [Exploiting local type-family injectivity] -mkLocalFamEqFDs eqs_for_me fam_tc inj_flags work_args work_rhs - = do { let -- eqns_from_inerts: see (INJFAM:Wanted/other) - eqns_from_inerts = mapMaybe do_one eqs_for_me - -- eqns_from_self: see (INJFAM:Wanted/Self) - eqns_from_self = case saturatedFamApp_maybe work_rhs of - Just (tc, rhs_tys) | fam_tc == tc -> [mk_eqn rhs_tys] - _ -> [] +mkLocalFamEqFDs eqs_for_me fam_tc work_args work_rhs + | Injective inj_flags <- tyConInjectivityInfo fam_tc + = let -- eqns_from_inerts: see (INJFAM:Wanted/other) + eqns_from_inerts = mapMaybe do_one eqs_for_me + -- eqns_from_self: see (INJFAM:Wanted/Self) + eqns_from_self = case saturatedFamApp_maybe work_rhs of + Just (tc, rhs_tys) | fam_tc == tc -> maybeToList (mk_eqn rhs_tys) + _ -> [] - ; return (eqns_from_inerts ++ eqns_from_self) } - where - do_one :: EqCt -> Maybe FunDepEqns - do_one (EqCt { eq_lhs = TyFamLHS _ inert_args, eq_rhs = inert_rhs }) - | work_rhs `tcEqType` inert_rhs = Just (mk_eqn inert_args) - | otherwise = Nothing - do_one _ = pprPanic "interactFunEq 2" (ppr fam_tc) -- TyVarLHS + do_one :: EqCt -> Maybe FunDepEqns + do_one (EqCt { eq_lhs = TyFamLHS _ inert_args, eq_rhs = inert_rhs }) + | work_rhs `tcEqType` inert_rhs = mk_eqn inert_args + | otherwise = Nothing + do_one _ = pprPanic "interactFunEq 2" (ppr fam_tc) -- TyVarLHS - mk_eqn iargs = mkInjectivityFDEqn [] inj_flags work_args iargs + mk_eqn iargs = mkInjectivityFDEqn [] inj_flags work_args iargs + + in return (eqns_from_inerts ++ eqns_from_self) + + | otherwise -- Not injective + = return [] trimQtvs :: Subst -> [TyVar] -> (Subst, [TyVar]) @@ -871,28 +876,34 @@ tryGivenBuiltinFamEqFDs eqs_for_me fam_tc ops work_args do_one _ = return () -mkTopBuiltinFamEqFDs :: TyCon -> BuiltinClosedTyFam -> [TcType] -> Xi -> SolverStage [FunDepEqns] -mkTopBuiltinFamEqFDs fam_tc ops work_args work_rhs - = return [FDEqns { fd_qtvs = [] - , fd_eqs = map snd $ tryInteractTopFam ops fam_tc work_args work_rhs }] +mkTopBuiltinFamEqFDs :: [EqCt] -> TyCon -> BuiltinClosedTyFam -> [TcType] -> EqCt + -> SolverStage [FunDepEqns] +mkTopBuiltinFamEqFDs eqs_for_me fam_tc ops work_args work_item@(EqCt { eq_rhs = work_rhs }) + | hasRelevantGiven eqs_for_me work_args work_item + -- hasRelevantGiven: See (CF1) in Note [Exploiting closed type families] + -- A built-in family is very like a closed type family + = return [] + | otherwise + = return [ FDEqns { fd_qtvs = [], fd_eqs = eqs } + | Just eqs <- [nonEmpty $ map snd $ tryInteractTopFam ops fam_tc work_args work_rhs] ] mkLocalBuiltinFamEqFDs :: [EqCt] -> TyCon -> BuiltinClosedTyFam -> [TcType] -> Xi -> SolverStage [FunDepEqns] mkLocalBuiltinFamEqFDs eqs_for_me fam_tc ops work_args work_rhs = do { let do_one :: EqCt -> [FunDepEqns] do_one (EqCt { eq_lhs = TyFamLHS _ inert_args, eq_rhs = inert_rhs }) - | inert_rhs `tcEqType` work_rhs = [mk_eqn inert_args] + | inert_rhs `tcEqType` work_rhs = maybeToList (mk_eqn inert_args) | otherwise = [] do_one _ = pprPanic "interactFunEq 1" (ppr fam_tc) -- TyVarLHS - mk_eqn :: [TcType] -> FunDepEqns - mk_eqn iargs = FDEqns { fd_qtvs = [] - , fd_eqs = map snd $ tryInteractInertFam ops fam_tc - work_args iargs } + mk_eqn :: [TcType] -> Maybe FunDepEqns + mk_eqn iargs = do { eqs <- nonEmpty $ map snd $ + tryInteractInertFam ops fam_tc work_args iargs + ; return (FDEqns { fd_qtvs = [], fd_eqs = eqs }) } ; let eqns_from_inerts = concatMap do_one eqs_for_me eqns_from_self = case tcSplitTyConApp_maybe work_rhs of - Just (tc,rhs_tys) | tc==fam_tc -> [mk_eqn rhs_tys] + Just (tc,rhs_tys) | tc==fam_tc -> maybeToList (mk_eqn rhs_tys) _ -> [] ; return (eqns_from_inerts ++ eqns_from_self) } @@ -900,7 +911,7 @@ mkLocalBuiltinFamEqFDs eqs_for_me fam_tc ops work_args work_rhs mkInjectivityFDEqn :: [TcTyVar] -- Quantify these -> [Bool] -- Injectivity flags -> [TcType] -> [TcType] -- Make these equal - -> FunDepEqns + -> Maybe FunDepEqns -- When F s1 s2 s3 ~ F t1 t2 t3, and F has injectivity info [True,False,True] -- return the FDEqns { fd_eqs = [Pair s1 t1, Pair s3 t3] } -- The injectivity flags [Bool] will not all be False, @@ -912,10 +923,11 @@ mkInjectivityFDEqn qtvs inj_args lhs_args rhs_args , text "lhs_args:" <+> ppr lhs_args , text "rhs_args:" <+> ppr rhs_args ]) $ -- Need inj_args lhs_args and rhs_args to be same length for zip3 - FDEqns { fd_qtvs = qtvs, fd_eqs = eqs } + do { eqs <- nonEmpty eqs_list + ; return (FDEqns { fd_qtvs = qtvs, fd_eqs = eqs }) } where - eqs = [ Pair lhs_arg rhs_arg - | (True, lhs_arg, rhs_arg) <- zip3 inj_args lhs_args rhs_args ] + eqs_list = [ Pair lhs_arg rhs_arg + | (True, lhs_arg, rhs_arg) <- zip3 inj_args lhs_args rhs_args ] getInertFamEqsFor :: TyCon -> [TcType] -> Xi -> TcS [EqCt] -- Look in the InertSet, and return all inert equalities @@ -1603,8 +1615,8 @@ instantiateFunDepEqns (FDEqns { fd_qtvs = qtvs, fd_eqs = eqs }) ; (_, subst) <- instFlexiXTcM empty_subst qtvs -- Takes account of kind substitution ; return (map (subst_pair subst) rev_eqs) } where - rev_eqs = reverse eqs - -- (reverse eqs): See Note [Reverse order of fundep equations] + rev_eqs = reverse $ NE.toList eqs + -- reverse: See Note [Reverse order of fundep equations] -- The kinds of `qtvs` can have free variables; -- in the FDEqns produced by GHC.Tc.Instance.FunDeps.improveFromInstEnv ===================================== testsuite/tests/indexed-types/should_compile/T28082.hs ===================================== @@ -0,0 +1,16 @@ + +{-# LANGUAGE TypeFamilyDependencies #-} + +module T28082 where + +type family F g = r | r -> g + +data P a = P a a + +type instance F ( P a ) = P a + +idF :: F g -> F g +idF x = x + +f :: F a ~ a => P a -> P a +f p = idF p ===================================== testsuite/tests/indexed-types/should_compile/all.T ===================================== @@ -319,4 +319,5 @@ test('T25657', normal, compile, ['']) test('T27828', normal, compile, ['']) test('T27828a', normal, compile, ['']) test('T27828b', normal, compile, ['']) +test('T28082', normal, compile, ['']) test('ZapDeps', normal, compile, ['']) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/e274a92b39a2762fe54f9b2eb245d4f... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/e274a92b39a2762fe54f9b2eb245d4f... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help