Skip to content

Commit 33140f4

Browse files
philderbeastbgamari
authored andcommitted
Show explicit quantifiers in conflicting definitions error
This fixes #12441, where definitions in a Haskell module and its boot file which differed only in their quantifiers produced a confusing error message. Here we teach GHC to always show quantifiers for these errors. Reviewers: goldfire, simonmar, erikd, austin, hvr, bgamari Reviewed By: bgamari Subscribers: snowleopard, simonpj, mpickering, thomie Differential Revision: https://phabricator.haskell.org/D2734 GHC Trac Issues: #12441
1 parent d49b2bb commit 33140f4

File tree

19 files changed

+235
-168
lines changed

19 files changed

+235
-168
lines changed

compiler/ghci/Debugger.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import GHCi.RemoteTypes
2222
import GhcMonad
2323
import HscTypes
2424
import Id
25+
import IfaceSyn ( showToHeader )
2526
import IfaceEnv( newInteractiveBinder )
2627
import Name
2728
import Var hiding ( varName )
@@ -214,7 +215,7 @@ pprTypeAndContents :: GhcMonad m => Id -> m SDoc
214215
pprTypeAndContents id = do
215216
dflags <- GHC.getSessionDynFlags
216217
let pcontents = gopt Opt_PrintBindContents dflags
217-
pprdId = (PprTyThing.pprTyThing . AnId) id
218+
pprdId = (pprTyThing showToHeader . AnId) id
218219
if pcontents
219220
then do
220221
let depthBound = 100

compiler/iface/IfaceSyn.hs

Lines changed: 92 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module IfaceSyn (
3535
-- Pretty printing
3636
pprIfaceExpr,
3737
pprIfaceDecl,
38-
ShowSub(..), ShowHowMuch(..)
38+
AltPpr(..), ShowSub(..), ShowHowMuch(..), showToIface, showToHeader
3939
) where
4040

4141
#include "HsVersions.h"
@@ -572,7 +572,7 @@ instance HasOccName IfaceDecl where
572572
occName = getOccName
573573

574574
instance Outputable IfaceDecl where
575-
ppr = pprIfaceDecl showAll
575+
ppr = pprIfaceDecl showToIface
576576

577577
{-
578578
Note [Minimal complete definition] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -583,61 +583,72 @@ filtering of method signatures. Instead we just check if anything at all is
583583
filtered and hide it in that case.
584584
-}
585585

586-
-- TODO: Kill this and Note [Printing IfaceDecl binders]
587586
data ShowSub
588587
= ShowSub
589-
{ ss_ppr_bndr :: OccName -> SDoc -- Pretty-printer for binders in IfaceDecl
590-
-- See Note [Printing IfaceDecl binders]
591-
, ss_how_much :: ShowHowMuch }
588+
{ ss_how_much :: ShowHowMuch
589+
, ss_forall :: ShowForAllFlag }
590+
591+
-- See Note [Printing IfaceDecl binders]
592+
-- The alternative pretty printer referred to in the note.
593+
newtype AltPpr = AltPpr (Maybe (OccName -> SDoc))
592594

593595
data ShowHowMuch
594-
= ShowHeader -- Header information only, not rhs
595-
| ShowSome [OccName] -- [] <=> Print all sub-components
596-
-- (n:ns) <=> print sub-component 'n' with ShowSub=ns
597-
-- elide other sub-components to "..."
598-
-- May 14: the list is max 1 element long at the moment
599-
| ShowIface -- Everything including GHC-internal information (used in --show-iface)
596+
= ShowHeader AltPpr -- ^Header information only, not rhs
597+
| ShowSome [OccName] AltPpr
598+
-- ^ Show only some sub-components. Specifically,
599+
--
600+
-- [@[]@] Print all sub-components.
601+
-- [@(n:ns)@] Print sub-component @n@ with @ShowSub = ns@;
602+
-- elide other sub-components to @...@
603+
-- May 14: the list is max 1 element long at the moment
604+
| ShowIface
605+
-- ^Everything including GHC-internal information (used in --show-iface)
606+
607+
{-
608+
Note [Printing IfaceDecl binders]
609+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
610+
The binders in an IfaceDecl are just OccNames, so we don't know what module they
611+
come from. But when we pretty-print a TyThing by converting to an IfaceDecl
612+
(see PprTyThing), the TyThing may come from some other module so we really need
613+
the module qualifier. We solve this by passing in a pretty-printer for the
614+
binders.
615+
616+
When printing an interface file (--show-iface), we want to print
617+
everything unqualified, so we can just print the OccName directly.
618+
-}
600619

601620
instance Outputable ShowHowMuch where
602-
ppr ShowHeader = text "ShowHeader"
603-
ppr ShowIface = text "ShowIface"
604-
ppr (ShowSome occs) = text "ShowSome" <+> ppr occs
621+
ppr (ShowHeader _) = text "ShowHeader"
622+
ppr ShowIface = text "ShowIface"
623+
ppr (ShowSome occs _) = text "ShowSome" <+> ppr occs
624+
625+
showToHeader :: ShowSub
626+
showToHeader = ShowSub { ss_how_much = ShowHeader $ AltPpr Nothing
627+
, ss_forall = ShowForAllWhen }
605628

606-
showAll :: ShowSub
607-
showAll = ShowSub { ss_how_much = ShowIface, ss_ppr_bndr = ppr }
629+
showToIface :: ShowSub
630+
showToIface = ShowSub { ss_how_much = ShowIface
631+
, ss_forall = ShowForAllWhen }
608632

609633
ppShowIface :: ShowSub -> SDoc -> SDoc
610634
ppShowIface (ShowSub { ss_how_much = ShowIface }) doc = doc
611635
ppShowIface _ _ = Outputable.empty
612636

613637
-- show if all sub-components or the complete interface is shown
614638
ppShowAllSubs :: ShowSub -> SDoc -> SDoc -- Note [Minimal complete definition]
615-
ppShowAllSubs (ShowSub { ss_how_much = ShowSome [] }) doc = doc
616-
ppShowAllSubs (ShowSub { ss_how_much = ShowIface }) doc = doc
617-
ppShowAllSubs _ _ = Outputable.empty
639+
ppShowAllSubs (ShowSub { ss_how_much = ShowSome [] _ }) doc = doc
640+
ppShowAllSubs (ShowSub { ss_how_much = ShowIface }) doc = doc
641+
ppShowAllSubs _ _ = Outputable.empty
618642

619643
ppShowRhs :: ShowSub -> SDoc -> SDoc
620-
ppShowRhs (ShowSub { ss_how_much = ShowHeader }) _ = Outputable.empty
621-
ppShowRhs _ doc = doc
644+
ppShowRhs (ShowSub { ss_how_much = ShowHeader _ }) _ = Outputable.empty
645+
ppShowRhs _ doc = doc
622646

623647
showSub :: HasOccName n => ShowSub -> n -> Bool
624-
showSub (ShowSub { ss_how_much = ShowHeader }) _ = False
625-
showSub (ShowSub { ss_how_much = ShowSome (n:_) }) thing = n == occName thing
648+
showSub (ShowSub { ss_how_much = ShowHeader _ }) _ = False
649+
showSub (ShowSub { ss_how_much = ShowSome (n:_) _ }) thing = n == occName thing
626650
showSub (ShowSub { ss_how_much = _ }) _ = True
627651

628-
{-
629-
Note [Printing IfaceDecl binders]
630-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
631-
The binders in an IfaceDecl are just OccNames, so we don't know what module they
632-
come from. But when we pretty-print a TyThing by converting to an IfaceDecl
633-
(see PprTyThing), the TyThing may come from some other module so we really need
634-
the module qualifier. We solve this by passing in a pretty-printer for the
635-
binders.
636-
637-
When printing an interface file (--show-iface), we want to print
638-
everything unqualified, so we can just print the OccName directly.
639-
-}
640-
641652
ppr_trim :: [Maybe SDoc] -> [SDoc]
642653
-- Collapse a group of Nothings to a single "..."
643654
ppr_trim xs
@@ -683,7 +694,9 @@ pprIfaceDecl ss (IfaceData { ifName = tycon, ifCType = ctype,
683694
pp_roles
684695
| is_data_instance = empty
685696
| otherwise = pprRoles (== Representational)
686-
(pprPrefixIfDeclBndr ss (occName tycon))
697+
(pprPrefixIfDeclBndr
698+
(ss_how_much ss)
699+
(occName tycon))
687700
binders roles
688701
-- Don't display roles for data family instances (yet)
689702
-- See discussion on Trac #8672.
@@ -714,7 +727,11 @@ pprIfaceDecl ss (IfaceClass { ifATs = ats, ifSigs = sigs
714727
, ifRoles = roles
715728
, ifFDs = fds, ifMinDef = minDef
716729
, ifBinders = binders })
717-
= vcat [ pprRoles (== Nominal) (pprPrefixIfDeclBndr ss (occName clas)) binders roles
730+
= vcat [ pprRoles
731+
(== Nominal)
732+
(pprPrefixIfDeclBndr (ss_how_much ss) (occName clas))
733+
binders
734+
roles
718735
, text "class" <+> pprIfaceDeclHead context ss clas binders Nothing
719736
<+> pprFundeps fds <+> pp_where
720737
, nest 2 (vcat [ vcat asocs, vcat dsigs
@@ -788,7 +805,11 @@ pprIfaceDecl ss (IfaceFamily { ifName = tycon
788805

789806
pp_branches (IfaceClosedSynFamilyTyCon (Just (ax, brs)))
790807
= hang (text "where")
791-
2 (vcat (map (pprAxBranch (pprPrefixIfDeclBndr ss (occName tycon))) brs)
808+
2 (vcat (map (pprAxBranch
809+
(pprPrefixIfDeclBndr
810+
(ss_how_much ss)
811+
(occName tycon))
812+
) brs)
792813
$$ ppShowIface ss (text "axiom" <+> ppr ax))
793814
pp_branches _ = Outputable.empty
794815

@@ -814,8 +835,8 @@ pprIfaceDecl _ (IfacePatSyn { ifName = name,
814835

815836
pprIfaceDecl ss (IfaceId { ifName = var, ifType = ty,
816837
ifIdDetails = details, ifIdInfo = info })
817-
= vcat [ hang (pprPrefixIfDeclBndr ss (occName var) <+> dcolon)
818-
2 (pprIfaceSigmaType ty)
838+
= vcat [ hang (pprPrefixIfDeclBndr (ss_how_much ss) (occName var) <+> dcolon)
839+
2 (pprIfaceSigmaType (ss_forall ss) ty)
819840
, ppShowIface ss (ppr details)
820841
, ppShowIface ss (ppr info) ]
821842

@@ -839,14 +860,22 @@ pprRoles suppress_if tyCon bndrs roles
839860
in ppUnless (all suppress_if roles || null froles) $
840861
text "type role" <+> tyCon <+> hsep (map ppr froles)
841862

842-
pprInfixIfDeclBndr, pprPrefixIfDeclBndr :: ShowSub -> OccName -> SDoc
843-
pprInfixIfDeclBndr (ShowSub { ss_ppr_bndr = ppr_bndr }) name
863+
pprInfixIfDeclBndr :: ShowHowMuch -> OccName -> SDoc
864+
pprInfixIfDeclBndr (ShowSome _ (AltPpr (Just ppr_bndr))) name
844865
= pprInfixVar (isSymOcc name) (ppr_bndr name)
845-
pprPrefixIfDeclBndr (ShowSub { ss_ppr_bndr = ppr_bndr }) name
866+
pprInfixIfDeclBndr _ name
867+
= pprInfixVar (isSymOcc name) (ppr name)
868+
869+
pprPrefixIfDeclBndr :: ShowHowMuch -> OccName -> SDoc
870+
pprPrefixIfDeclBndr (ShowHeader (AltPpr (Just ppr_bndr))) name
846871
= parenSymOcc name (ppr_bndr name)
872+
pprPrefixIfDeclBndr (ShowSome _ (AltPpr (Just ppr_bndr))) name
873+
= parenSymOcc name (ppr_bndr name)
874+
pprPrefixIfDeclBndr _ name
875+
= parenSymOcc name (ppr name)
847876

848877
instance Outputable IfaceClassOp where
849-
ppr = pprIfaceClassOp showAll
878+
ppr = pprIfaceClassOp showToIface
850879

851880
pprIfaceClassOp :: ShowSub -> IfaceClassOp -> SDoc
852881
pprIfaceClassOp ss (IfaceClassOp n ty dm)
@@ -856,10 +885,13 @@ pprIfaceClassOp ss (IfaceClassOp n ty dm)
856885
= text "default" <+> pp_sig n dm_ty
857886
| otherwise
858887
= empty
859-
pp_sig n ty = pprPrefixIfDeclBndr ss (occName n) <+> dcolon <+> pprIfaceSigmaType ty
888+
pp_sig n ty
889+
= pprPrefixIfDeclBndr (ss_how_much ss) (occName n)
890+
<+> dcolon
891+
<+> pprIfaceSigmaType ShowForAllWhen ty
860892

861893
instance Outputable IfaceAT where
862-
ppr = pprIfaceAT showAll
894+
ppr = pprIfaceAT showToIface
863895

864896
pprIfaceAT :: ShowSub -> IfaceAT -> SDoc
865897
pprIfaceAT ss (IfaceAT d mb_def)
@@ -887,7 +919,7 @@ pprIfaceDeclHead :: IfaceContext -> ShowSub -> Name
887919
pprIfaceDeclHead context ss tc_occ bndrs m_res_kind
888920
= sdocWithDynFlags $ \ dflags ->
889921
sep [ pprIfaceContextArr context
890-
, pprPrefixIfDeclBndr ss (occName tc_occ)
922+
, pprPrefixIfDeclBndr (ss_how_much ss) (occName tc_occ)
891923
<+> pprIfaceTyConBinders (suppressIfaceInvisibles dflags bndrs bndrs)
892924
, maybe empty (\res_kind -> dcolon <+> pprIfaceType res_kind) m_res_kind ]
893925

@@ -911,12 +943,16 @@ pprIfaceConDecl ss gadt_style fls tycon tc_binders parent
911943
| gadt_style = pp_prefix_con <+> dcolon <+> ppr_ty
912944
| not (null fields) = pp_prefix_con <+> pp_field_args
913945
| is_infix
914-
, [ty1, ty2] <- pp_args = sep [ty1, pprInfixIfDeclBndr ss (occName name), ty2]
946+
, [ty1, ty2] <- pp_args = sep [ ty1
947+
, pprInfixIfDeclBndr how_much (occName name)
948+
, ty2]
949+
915950
| otherwise = pp_prefix_con <+> sep pp_args
916951
where
952+
how_much = ss_how_much ss
917953
tys_w_strs :: [(IfaceBang, IfaceType)]
918954
tys_w_strs = zip stricts arg_tys
919-
pp_prefix_con = pprPrefixIfDeclBndr ss (occName name)
955+
pp_prefix_con = pprPrefixIfDeclBndr how_much (occName name)
920956

921957
(univ_tvs, pp_res_ty) = mk_user_con_res_ty eq_spec
922958
ppr_ty = pprIfaceForAllPart (map tv_to_forall_bndr univ_tvs ++ ex_tvs)
@@ -949,8 +985,10 @@ pprIfaceConDecl ss gadt_style fls tycon tc_binders parent
949985

950986
maybe_show_label :: IfaceTopBndr -> (IfaceBang, IfaceType) -> Maybe SDoc
951987
maybe_show_label sel bty
952-
| showSub ss sel = Just (pprPrefixIfDeclBndr ss lbl <+> dcolon <+> pprBangTy bty)
953-
| otherwise = Nothing
988+
| showSub ss sel =
989+
Just (pprPrefixIfDeclBndr how_much lbl <+> dcolon <+> pprBangTy bty)
990+
| otherwise =
991+
Nothing
954992
where
955993
-- IfaceConDecl contains the name of the selector function, so
956994
-- we have to look up the field label (in case
@@ -971,7 +1009,7 @@ pprIfaceConDecl ss gadt_style fls tycon tc_binders parent
9711009
con_univ_tvs = filterOut done_univ_tv (map ifTyConBinderTyVar tc_binders)
9721010

9731011
ppr_tc_app gadt_subst dflags
974-
= pprPrefixIfDeclBndr ss (occName tycon)
1012+
= pprPrefixIfDeclBndr how_much (occName tycon)
9751013
<+> sep [ pprParendIfaceType (substIfaceTyVar gadt_subst tv)
9761014
| (tv,_kind)
9771015
<- map ifTyConBinderTyVar $

compiler/iface/IfaceType.hs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module IfaceType (
1818
IfaceTyLit(..), IfaceTcArgs(..),
1919
IfaceContext, IfaceBndr(..), IfaceOneShot(..), IfaceLamBndr,
2020
IfaceTvBndr, IfaceIdBndr, IfaceTyConBinder,
21-
IfaceForAllBndr, ArgFlag(..),
21+
IfaceForAllBndr, ArgFlag(..), ShowForAllFlag(..),
2222

2323
ifTyConBinderTyVar, ifTyConBinderName,
2424

@@ -719,7 +719,7 @@ ppr_ty ctxt_prec (IfaceCoercionTy co)
719719
(text "<>")
720720

721721
ppr_ty ctxt_prec ty
722-
= maybeParen ctxt_prec FunPrec (ppr_iface_sigma_type True ty)
722+
= maybeParen ctxt_prec FunPrec (pprIfaceSigmaType ShowForAllMust ty)
723723

724724
{-
725725
Note [Defaulting RuntimeRep variables]
@@ -826,27 +826,21 @@ ppr_tc_args ctx_prec args
826826
ITC_Vis t ts -> pprTys t ts
827827
ITC_Invis t ts -> pprTys t ts
828828

829-
-------------------
830-
ppr_iface_sigma_type :: Bool -> IfaceType -> SDoc
831-
ppr_iface_sigma_type show_foralls_unconditionally ty
832-
= ppr_iface_forall_part show_foralls_unconditionally tvs theta (ppr tau)
833-
where
834-
(tvs, theta, tau) = splitIfaceSigmaTy ty
835-
836829
-------------------
837830
pprIfaceForAllPart :: [IfaceForAllBndr] -> [IfacePredType] -> SDoc -> SDoc
838-
pprIfaceForAllPart tvs ctxt sdoc = ppr_iface_forall_part False tvs ctxt sdoc
831+
pprIfaceForAllPart tvs ctxt sdoc
832+
= ppr_iface_forall_part ShowForAllWhen tvs ctxt sdoc
839833

840834
pprIfaceForAllCoPart :: [(IfLclName, IfaceCoercion)] -> SDoc -> SDoc
841-
pprIfaceForAllCoPart tvs sdoc =
842-
sep [ pprIfaceForAllCo tvs, sdoc ]
835+
pprIfaceForAllCoPart tvs sdoc
836+
= sep [ pprIfaceForAllCo tvs, sdoc ]
843837

844-
ppr_iface_forall_part :: Bool
838+
ppr_iface_forall_part :: ShowForAllFlag
845839
-> [IfaceForAllBndr] -> [IfacePredType] -> SDoc -> SDoc
846-
ppr_iface_forall_part show_foralls_unconditionally tvs ctxt sdoc
847-
= sep [ if show_foralls_unconditionally
848-
then pprIfaceForAll tvs
849-
else pprUserIfaceForAll tvs
840+
ppr_iface_forall_part show_forall tvs ctxt sdoc
841+
= sep [ case show_forall of
842+
ShowForAllMust -> pprIfaceForAll tvs
843+
ShowForAllWhen -> pprUserIfaceForAll tvs
850844
, pprIfaceContextArr ctxt
851845
, sdoc]
852846

@@ -893,8 +887,18 @@ pprIfaceForAllCoBndr :: (IfLclName, IfaceCoercion) -> SDoc
893887
pprIfaceForAllCoBndr (tv, kind_co)
894888
= parens (ppr tv <+> dcolon <+> pprIfaceCoercion kind_co)
895889

896-
pprIfaceSigmaType :: IfaceType -> SDoc
897-
pprIfaceSigmaType ty = ppr_iface_sigma_type False ty
890+
-- | Show forall flag
891+
--
892+
-- Unconditionally show the forall quantifier with ('ShowForAllMust')
893+
-- or when ('ShowForAllWhen') the names used are free in the binder
894+
-- or when compiling with -fprint-explicit-foralls.
895+
data ShowForAllFlag = ShowForAllMust | ShowForAllWhen
896+
897+
pprIfaceSigmaType :: ShowForAllFlag -> IfaceType -> SDoc
898+
pprIfaceSigmaType show_forall ty
899+
= ppr_iface_forall_part show_forall tvs theta (ppr tau)
900+
where
901+
(tvs, theta, tau) = splitIfaceSigmaTy ty
898902

899903
pprUserIfaceForAll :: [IfaceForAllBndr] -> SDoc
900904
pprUserIfaceForAll tvs

compiler/iface/IfaceType.hs-boot

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type IfLclName = FastString
1111
type IfaceKind = IfaceType
1212
type IfacePredType = IfaceType
1313

14+
data ShowForAllFlag
1415
data IfaceType
1516
data IfaceTyCon
1617
data IfaceTyLit
@@ -23,7 +24,7 @@ type IfaceForAllBndr = TyVarBndr IfaceTvBndr ArgFlag
2324
instance Outputable IfaceType
2425

2526
pprIfaceType, pprParendIfaceType :: IfaceType -> SDoc
26-
pprIfaceSigmaType :: IfaceType -> SDoc
27+
pprIfaceSigmaType :: ShowForAllFlag -> IfaceType -> SDoc
2728
pprIfaceTyLit :: IfaceTyLit -> SDoc
2829
pprIfaceForAll :: [IfaceForAllBndr] -> SDoc
2930
pprIfaceTvBndr :: Bool -> IfaceTvBndr -> SDoc

compiler/main/HscTypes.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1890,7 +1890,7 @@ isImplicitTyThing (ATyCon tc) = isImplicitTyCon tc
18901890
isImplicitTyThing (ACoAxiom ax) = isImplicitCoAxiom ax
18911891

18921892
-- | tyThingParent_maybe x returns (Just p)
1893-
-- when pprTyThingInContext sould print a declaration for p
1893+
-- when pprTyThingInContext should print a declaration for p
18941894
-- (albeit with some "..." in it) when asked to show x
18951895
-- It returns the *immediate* parent. So a datacon returns its tycon
18961896
-- but the tycon could be the associated type of a class, so it in turn

0 commit comments

Comments
 (0)