diff options
Diffstat (limited to 'compiler/GHC/Hs')
| -rw-r--r-- | compiler/GHC/Hs/Expr.hs | 8 | ||||
| -rw-r--r-- | compiler/GHC/Hs/Pat.hs | 10 | ||||
| -rw-r--r-- | compiler/GHC/Hs/Types.hs | 15 |
3 files changed, 30 insertions, 3 deletions
diff --git a/compiler/GHC/Hs/Expr.hs b/compiler/GHC/Hs/Expr.hs index 7a9caa8c6e..847ecd1743 100644 --- a/compiler/GHC/Hs/Expr.hs +++ b/compiler/GHC/Hs/Expr.hs @@ -13,6 +13,7 @@ {-# LANGUAGE ExistentialQuantification #-} {-# LANGUAGE DeriveFunctor #-} {-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE ViewPatterns #-} -- | Abstract Haskell syntax for expressions. module GHC.Hs.Expr where @@ -43,6 +44,7 @@ import Util import Outputable import FastString import Type +import TysWiredIn (mkTupleStr) import TcType (TcType) import {-# SOURCE #-} TcRnTypes (TcLclEnv) @@ -908,6 +910,12 @@ ppr_expr (SectionR _ op expr) pp_infixly v = sep [v, pp_expr] ppr_expr (ExplicitTuple _ exprs boxity) + -- Special-case unary boxed tuples so that they are pretty-printed as + -- `Unit x`, not `(x)` + | [dL -> L _ (Present _ expr)] <- exprs + , Boxed <- boxity + = hsep [text (mkTupleStr Boxed 1), ppr expr] + | otherwise = tupleParens (boxityTupleSort boxity) (fcat (ppr_tup_args $ map unLoc exprs)) where ppr_tup_args [] = [] diff --git a/compiler/GHC/Hs/Pat.hs b/compiler/GHC/Hs/Pat.hs index 0fa6dca7b8..cae7144a8c 100644 --- a/compiler/GHC/Hs/Pat.hs +++ b/compiler/GHC/Hs/Pat.hs @@ -529,8 +529,14 @@ pprPat (CoPat _ co pat _) = pprHsWrapper co $ \parens else pprPat pat pprPat (SigPat _ pat ty) = ppr pat <+> dcolon <+> ppr ty pprPat (ListPat _ pats) = brackets (interpp'SP pats) -pprPat (TuplePat _ pats bx) = tupleParens (boxityTupleSort bx) - (pprWithCommas ppr pats) +pprPat (TuplePat _ pats bx) + -- Special-case unary boxed tuples so that they are pretty-printed as + -- `Unit x`, not `(x)` + | [pat] <- pats + , Boxed <- bx + = hcat [text (mkTupleStr Boxed 1), pprParendLPat appPrec pat] + | otherwise + = tupleParens (boxityTupleSort bx) (pprWithCommas ppr pats) pprPat (SumPat _ pat alt arity) = sumParens (pprAlternative ppr pat alt arity) pprPat (ConPatIn con details) = pprUserCon (unLoc con) details pprPat (ConPatOut { pat_con = con diff --git a/compiler/GHC/Hs/Types.hs b/compiler/GHC/Hs/Types.hs index cd5e59745b..fcf22584cb 100644 --- a/compiler/GHC/Hs/Types.hs +++ b/compiler/GHC/Hs/Types.hs @@ -85,6 +85,7 @@ import RdrName ( RdrName ) import DataCon( HsSrcBang(..), HsImplBang(..), SrcStrictness(..), SrcUnpackedness(..) ) import TysPrim( funTyConName ) +import TysWiredIn( mkTupleStr ) import Type import GHC.Hs.Doc import BasicTypes @@ -1600,7 +1601,14 @@ ppr_mono_ty (HsTyVar _ prom (L _ name)) | isPromoted prom = quote (pprPrefixOcc name) | otherwise = pprPrefixOcc name ppr_mono_ty (HsFunTy _ ty1 ty2) = ppr_fun_ty ty1 ty2 -ppr_mono_ty (HsTupleTy _ con tys) = tupleParens std_con (pprWithCommas ppr tys) +ppr_mono_ty (HsTupleTy _ con tys) + -- Special-case unary boxed tuples so that they are pretty-printed as + -- `Unit x`, not `(x)` + | [ty] <- tys + , BoxedTuple <- std_con + = sep [text (mkTupleStr Boxed 1), ppr_mono_lty ty] + | otherwise + = tupleParens std_con (pprWithCommas ppr tys) where std_con = case con of HsUnboxedTuple -> UnboxedTuple _ -> BoxedTuple @@ -1615,6 +1623,11 @@ ppr_mono_ty (HsExplicitListTy _ prom tys) | isPromoted prom = quote $ brackets (maybeAddSpace tys $ interpp'SP tys) | otherwise = brackets (interpp'SP tys) ppr_mono_ty (HsExplicitTupleTy _ tys) + -- Special-case unary boxed tuples so that they are pretty-printed as + -- `'Unit x`, not `'(x)` + | [ty] <- tys + = quote $ sep [text (mkTupleStr Boxed 1), ppr_mono_lty ty] + | otherwise = quote $ parens (maybeAddSpace tys $ interpp'SP tys) ppr_mono_ty (HsTyLit _ t) = ppr_tylit t ppr_mono_ty (HsWildCardTy {}) = char '_' |
