summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKrzysztof Gogolewski <krzysztof.gogolewski@tweag.io>2020-08-20 17:30:51 +0200
committerBen Gamari <ben@smart-cactus.org>2020-09-17 19:46:29 -0400
commit264afed3da46a6a2995236b152fd568b1b5f47d9 (patch)
treeb7b94462019faf7c4afb86181b7c52b19c9e9b67
parent7e315b1585085157778807c70745f5a8cf4138d0 (diff)
downloadhaskell-264afed3da46a6a2995236b152fd568b1b5f47d9.tar.gz
Do not print synonyms in :i (->), :i Type (#18594)
This adds a new printing flag `sdocPrintTypeAbbreviations` that is used specifically to avoid ghci printing 'type (->) = (->)' and 'type Type = Type'. (cherry picked from commit 8b86509270227dbc61f0700c7d9261a4c7672361)
-rw-r--r--compiler/GHC/Driver/Session.hs1
-rw-r--r--compiler/GHC/Iface/Syntax.hs10
-rw-r--r--compiler/GHC/Iface/Type.hs26
-rw-r--r--compiler/GHC/Utils/Outputable.hs1
-rw-r--r--testsuite/tests/ghci/T18060/T18060.stdout2
-rw-r--r--testsuite/tests/ghci/scripts/T8535.stdout2
-rw-r--r--testsuite/tests/ghci/scripts/ghci020.stdout2
-rw-r--r--testsuite/tests/ghci/should_run/T10145.stdout2
-rw-r--r--testsuite/tests/ghci/should_run/T18594.script6
-rw-r--r--testsuite/tests/ghci/should_run/T18594.stdout15
-rw-r--r--testsuite/tests/ghci/should_run/all.T1
11 files changed, 60 insertions, 8 deletions
diff --git a/compiler/GHC/Driver/Session.hs b/compiler/GHC/Driver/Session.hs
index 3f34bd87ae..9bc9b24329 100644
--- a/compiler/GHC/Driver/Session.hs
+++ b/compiler/GHC/Driver/Session.hs
@@ -5218,6 +5218,7 @@ initSDocContext dflags style = SDC
, sdocStarIsType = xopt LangExt.StarIsType dflags
, sdocImpredicativeTypes = xopt LangExt.ImpredicativeTypes dflags
, sdocLinearTypes = xopt LangExt.LinearTypes dflags
+ , sdocPrintTypeAbbreviations = True
, sdocDynFlags = dflags
}
diff --git a/compiler/GHC/Iface/Syntax.hs b/compiler/GHC/Iface/Syntax.hs
index 5bc777e465..14549ae2b0 100644
--- a/compiler/GHC/Iface/Syntax.hs
+++ b/compiler/GHC/Iface/Syntax.hs
@@ -45,6 +45,8 @@ module GHC.Iface.Syntax (
import GHC.Prelude
+import GHC.Builtin.Names ( unrestrictedFunTyConKey, liftedTypeKindTyConKey )
+import GHC.Types.Unique ( hasKey )
import GHC.Iface.Type
import GHC.Iface.Recomp.Binary
import GHC.Core( IsOrphan, isOrphan )
@@ -945,13 +947,19 @@ pprIfaceDecl ss (IfaceSynonym { ifName = tc
, ifResKind = res_kind})
= vcat [ pprStandaloneKindSig name_doc (mkIfaceTyConKind binders res_kind)
, hang (text "type" <+> pprIfaceDeclHead suppress_bndr_sig [] ss tc binders <+> equals)
- 2 (sep [ pprIfaceForAll tvs, pprIfaceContextArr theta, ppr tau
+ 2 (sep [ pprIfaceForAll tvs, pprIfaceContextArr theta, ppr_tau
, ppUnless (isIfaceLiftedTypeKind res_kind) (dcolon <+> ppr res_kind) ])
]
where
(tvs, theta, tau) = splitIfaceSigmaTy mono_ty
name_doc = pprPrefixIfDeclBndr (ss_how_much ss) (occName tc)
+ -- See Note [Printing type abbreviations] in GHC.Iface.Type
+ ppr_tau | tc `hasKey` liftedTypeKindTyConKey ||
+ tc `hasKey` unrestrictedFunTyConKey
+ = updSDocContext (\ctx -> ctx { sdocPrintTypeAbbreviations = False }) $ ppr tau
+ | otherwise = ppr tau
+
-- See Note [Suppressing binder signatures] in GHC.Iface.Type
suppress_bndr_sig = SuppressBndrSig True
diff --git a/compiler/GHC/Iface/Type.hs b/compiler/GHC/Iface/Type.hs
index 28a628344f..76f21ee05f 100644
--- a/compiler/GHC/Iface/Type.hs
+++ b/compiler/GHC/Iface/Type.hs
@@ -775,6 +775,22 @@ Here we'd like to omit the kind annotation:
type F :: Symbol -> Type
type F s = blah
+
+Note [Printing type abbreviations]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+Normally, we pretty-print `TYPE 'LiftedRep` as `Type` (or `*`) and
+`FUN 'Many` as `(->)`.
+This way, error messages don't refer to levity polymorphism or linearity
+if it is not necessary.
+
+However, when printing the definition of Type or (->) with :info,
+this would give confusing output: `type (->) = (->)` (#18594).
+Solution: detect when we are in :info and disable displaying the synonym
+with the SDoc option sdocPrintTypeAbbreviations.
+
+If there will be a need, in the future we could expose it as a flag
+-fprint-type-abbreviations or even two separate flags controlling
+TYPE 'LiftedRep and FUN 'Many.
-}
-- | Do we want to suppress kind annotations on binders?
@@ -1341,12 +1357,14 @@ pprIfaceTypeApp prec tc args = pprTyTcApp prec tc args
pprTyTcApp :: PprPrec -> IfaceTyCon -> IfaceAppArgs -> SDoc
pprTyTcApp ctxt_prec tc tys =
sdocOption sdocPrintExplicitKinds $ \print_kinds ->
+ sdocOption sdocPrintTypeAbbreviations $ \print_type_abbreviations ->
getPprDebug $ \debug ->
- pprTyTcApp' ctxt_prec tc tys (PrintExplicitKinds print_kinds) debug
+ pprTyTcApp' ctxt_prec tc tys (PrintExplicitKinds print_kinds)
+ print_type_abbreviations debug
pprTyTcApp' :: PprPrec -> IfaceTyCon -> IfaceAppArgs
- -> PrintExplicitKinds -> Bool -> SDoc
-pprTyTcApp' ctxt_prec tc tys printExplicitKinds debug
+ -> PrintExplicitKinds -> Bool -> Bool -> SDoc
+pprTyTcApp' ctxt_prec tc tys printExplicitKinds printTypeAbbreviations debug
| ifaceTyConName tc `hasKey` ipClassKey
, IA_Arg (IfaceLitTy (IfaceStrTyLit n))
Required (IA_Arg ty Required IA_Nil) <- tys
@@ -1370,11 +1388,13 @@ pprTyTcApp' ctxt_prec tc tys printExplicitKinds debug
| tc `ifaceTyConHasKey` tYPETyConKey
, IA_Arg (IfaceTyConApp rep IA_Nil) Required IA_Nil <- tys
, rep `ifaceTyConHasKey` liftedRepDataConKey
+ , printTypeAbbreviations -- See Note [Printing type abbreviations]
= ppr_kind_type ctxt_prec
| tc `ifaceTyConHasKey` funTyConKey
, IA_Arg (IfaceTyConApp rep IA_Nil) Required args <- tys
, rep `ifaceTyConHasKey` manyDataConKey
+ , printTypeAbbreviations -- See Note [Printing type abbreviations]
= pprIfacePrefixApp ctxt_prec (parens arrow) (map (ppr_ty appPrec) (appArgsIfaceTypes $ stripInvisArgs printExplicitKinds args))
| otherwise
diff --git a/compiler/GHC/Utils/Outputable.hs b/compiler/GHC/Utils/Outputable.hs
index b3d1772076..2c3db29399 100644
--- a/compiler/GHC/Utils/Outputable.hs
+++ b/compiler/GHC/Utils/Outputable.hs
@@ -359,6 +359,7 @@ data SDocContext = SDC
, sdocStarIsType :: !Bool
, sdocLinearTypes :: !Bool
, sdocImpredicativeTypes :: !Bool
+ , sdocPrintTypeAbbreviations :: !Bool
, sdocDynFlags :: DynFlags -- TODO: remove
}
diff --git a/testsuite/tests/ghci/T18060/T18060.stdout b/testsuite/tests/ghci/T18060/T18060.stdout
index 8c3a2794fe..9e4683a1c4 100644
--- a/testsuite/tests/ghci/T18060/T18060.stdout
+++ b/testsuite/tests/ghci/T18060/T18060.stdout
@@ -1,5 +1,5 @@
type (->) :: * -> * -> *
-type (->) = (->) :: * -> * -> *
+type (->) = FUN 'Many :: * -> * -> *
-- Defined in ‘GHC.Types’
infixr -1 ->
instance Applicative ((->) r) -- Defined in ‘GHC.Base’
diff --git a/testsuite/tests/ghci/scripts/T8535.stdout b/testsuite/tests/ghci/scripts/T8535.stdout
index f7e40fd0f4..5786372e9d 100644
--- a/testsuite/tests/ghci/scripts/T8535.stdout
+++ b/testsuite/tests/ghci/scripts/T8535.stdout
@@ -1,5 +1,5 @@
type (->) :: * -> * -> *
-type (->) = (->) :: * -> * -> *
+type (->) = FUN 'Many :: * -> * -> *
-- Defined in ‘GHC.Types’
infixr -1 ->
instance Applicative ((->) r) -- Defined in ‘GHC.Base’
diff --git a/testsuite/tests/ghci/scripts/ghci020.stdout b/testsuite/tests/ghci/scripts/ghci020.stdout
index f7e40fd0f4..5786372e9d 100644
--- a/testsuite/tests/ghci/scripts/ghci020.stdout
+++ b/testsuite/tests/ghci/scripts/ghci020.stdout
@@ -1,5 +1,5 @@
type (->) :: * -> * -> *
-type (->) = (->) :: * -> * -> *
+type (->) = FUN 'Many :: * -> * -> *
-- Defined in ‘GHC.Types’
infixr -1 ->
instance Applicative ((->) r) -- Defined in ‘GHC.Base’
diff --git a/testsuite/tests/ghci/should_run/T10145.stdout b/testsuite/tests/ghci/should_run/T10145.stdout
index f7e40fd0f4..5786372e9d 100644
--- a/testsuite/tests/ghci/should_run/T10145.stdout
+++ b/testsuite/tests/ghci/should_run/T10145.stdout
@@ -1,5 +1,5 @@
type (->) :: * -> * -> *
-type (->) = (->) :: * -> * -> *
+type (->) = FUN 'Many :: * -> * -> *
-- Defined in ‘GHC.Types’
infixr -1 ->
instance Applicative ((->) r) -- Defined in ‘GHC.Base’
diff --git a/testsuite/tests/ghci/should_run/T18594.script b/testsuite/tests/ghci/should_run/T18594.script
new file mode 100644
index 0000000000..815275914c
--- /dev/null
+++ b/testsuite/tests/ghci/should_run/T18594.script
@@ -0,0 +1,6 @@
+:m GHC.Types
+:i (->)
+:set -XStarIsType
+:i Type
+:set -XNoStarIsType
+:i Type
diff --git a/testsuite/tests/ghci/should_run/T18594.stdout b/testsuite/tests/ghci/should_run/T18594.stdout
new file mode 100644
index 0000000000..9e2e79cd8b
--- /dev/null
+++ b/testsuite/tests/ghci/should_run/T18594.stdout
@@ -0,0 +1,15 @@
+type (->) :: * -> * -> *
+type (->) = FUN 'Many :: * -> * -> *
+ -- Defined in ‘GHC.Types’
+infixr -1 ->
+instance Applicative ((->) r) -- Defined in ‘GHC.Base’
+instance Functor ((->) r) -- Defined in ‘GHC.Base’
+instance Monad ((->) r) -- Defined in ‘GHC.Base’
+instance Monoid b => Monoid (a -> b) -- Defined in ‘GHC.Base’
+instance Semigroup b => Semigroup (a -> b) -- Defined in ‘GHC.Base’
+type Type :: *
+type Type = TYPE 'LiftedRep
+ -- Defined in ‘GHC.Types’
+type Type :: Type
+type Type = TYPE 'LiftedRep
+ -- Defined in ‘GHC.Types’
diff --git a/testsuite/tests/ghci/should_run/all.T b/testsuite/tests/ghci/should_run/all.T
index fa4c78cdce..36be4585d7 100644
--- a/testsuite/tests/ghci/should_run/all.T
+++ b/testsuite/tests/ghci/should_run/all.T
@@ -66,3 +66,4 @@ test('T16012', just_ghci, ghci_script, ['T16012.script'])
test('T16096', just_ghci, ghci_script, ['T16096.script'])
test('T507', just_ghci, ghci_script, ['T507.script'])
test('T18027', just_ghci, ghci_script, ['T18027.script'])
+test('T18594', just_ghci, ghci_script, ['T18594.script'])