diff options
author | Alan Zimmerman <alan.zimm@gmail.com> | 2017-10-01 19:36:03 +0200 |
---|---|---|
committer | Alan Zimmerman <alan.zimm@gmail.com> | 2017-10-11 16:00:08 +0200 |
commit | 6869864eac211885edcd4b14425fd368069e4aba (patch) | |
tree | d59ec9374f100df6f9eb41e3452713003eaa82c6 /compiler/hsSyn/Convert.hs | |
parent | 9c3f73168a6f7f6632b6a3ffd2cfcd774976a7f1 (diff) | |
download | haskell-6869864eac211885edcd4b14425fd368069e4aba.tar.gz |
Pretty-printing of derived multi-parameter classes omits parentheses
Summary:
Pretty printing a splice with an HsAppType in the deriving clause, such as
$([d| data Foo a = Foo a deriving (C a) |])
would omit the parens.
Test Plan: ./validate
Reviewers: RyanGlScott, austin, bgamari
Subscribers: rwbarton, thomie
GHC Trac Issues: #14289
Differential Revision: https://phabricator.haskell.org/D4056
Diffstat (limited to 'compiler/hsSyn/Convert.hs')
-rw-r--r-- | compiler/hsSyn/Convert.hs | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/compiler/hsSyn/Convert.hs b/compiler/hsSyn/Convert.hs index bffb2028c3..4336243e91 100644 --- a/compiler/hsSyn/Convert.hs +++ b/compiler/hsSyn/Convert.hs @@ -1330,14 +1330,38 @@ mk_apps head_ty (ty:tys) = ; p_ty <- add_parens ty ; mk_apps (HsAppTy head_ty' p_ty) tys } where - add_parens t@(L _ HsAppTy{}) = returnL (HsParTy t) - add_parens t@(L _ HsFunTy{}) = returnL (HsParTy t) - add_parens t = return t + -- See Note [Adding parens for splices] + add_parens t + | isCompoundHsType t = returnL (HsParTy t) + | otherwise = return t wrap_apps :: LHsType GhcPs -> CvtM (LHsType GhcPs) wrap_apps t@(L _ HsAppTy {}) = returnL (HsParTy t) wrap_apps t = return t +-- --------------------------------------------------------------------- +-- Note [Adding parens for splices] +{- +The hsSyn representation of parsed source explicitly contains all the original +parens, as written in the source. + +When a Template Haskell (TH) splice is evaluated, the original splice is first +renamed and type checked and then finally converted to core in DsMeta. This core +is then run in the TH engine, and the result comes back as a TH AST. + +In the process, all parens are stripped out, as they are not needed. + +This Convert module then converts the TH AST back to hsSyn AST. + +In order to pretty-print this hsSyn AST, parens need to be adde back at certain +points so that the code is readable with its original meaning. + +So scattered through Convert.hs are various points where parens are added. + +See (among other closed issued) https://ghc.haskell.org/trac/ghc/ticket/14289 +-} +-- --------------------------------------------------------------------- + -- | Constructs an arrow type with a specified return type mk_arr_apps :: [LHsType GhcPs] -> HsType GhcPs -> CvtM (LHsType GhcPs) mk_arr_apps tys return_ty = foldrM go return_ty tys >>= returnL |