summaryrefslogtreecommitdiff
path: root/compiler/typecheck/TcGenDeriv.hs
diff options
context:
space:
mode:
authorRyan Scott <ryan.gl.scott@gmail.com>2018-05-13 18:36:23 -0400
committerBen Gamari <ben@smart-cactus.org>2018-05-13 22:22:43 -0400
commit21e1a00c0ccf3072ccc04cd1acfc541c141189d2 (patch)
tree6730896263197984b0466c22b84ab007401d775a /compiler/typecheck/TcGenDeriv.hs
parentbf6cad8b86ee34ed5aa5fa0e295304b51f2a2324 (diff)
downloadhaskell-21e1a00c0ccf3072ccc04cd1acfc541c141189d2.tar.gz
Fix #14875 by introducing PprPrec, and using it
Trying to determine when to insert parentheses during TH conversion is a bit of a mess. There is an assortment of functions that try to detect this, such as: * `hsExprNeedsParens` * `isCompoundHsType` * `hsPatNeedsParens` * `isCompoundPat` * etc. To make things worse, each of them have slightly different semantics. Plus, they don't work well in the presence of explicit type signatures, as #14875 demonstrates. All of these problems can be alleviated with the use of an explicit precedence argument (much like what `showsPrec` currently does). To accomplish this, I introduce a new `PprPrec` data type, and define standard predences for things like function application, infix operators, function arrows, and explicit type signatures (that last one is new). I then added `PprPrec` arguments to the various `-NeedsParens` functions, and use them to make smarter decisions about when things need to be parenthesized. A nice side effect is that functions like `isCompoundHsType` are now completely unneeded, since they're simply aliases for `hsTypeNeedsParens appPrec`. As a result, I did a bit of refactoring to remove these sorts of functions. I also did a pass over various utility functions in GHC for constructing AST forms and used more appropriate precedences where convenient. Along the way, I also ripped out the existing `TyPrec` data type (which was tailor-made for pretty-printing `Type`s) and replaced it with `PprPrec` for consistency. Test Plan: make test TEST=T14875 Reviewers: alanz, goldfire, bgamari Reviewed By: bgamari Subscribers: rwbarton, thomie, carter GHC Trac Issues: #14875 Differential Revision: https://phabricator.haskell.org/D4688
Diffstat (limited to 'compiler/typecheck/TcGenDeriv.hs')
-rw-r--r--compiler/typecheck/TcGenDeriv.hs7
1 files changed, 4 insertions, 3 deletions
diff --git a/compiler/typecheck/TcGenDeriv.hs b/compiler/typecheck/TcGenDeriv.hs
index 05c6276cb5..b94452059d 100644
--- a/compiler/typecheck/TcGenDeriv.hs
+++ b/compiler/typecheck/TcGenDeriv.hs
@@ -1709,7 +1709,8 @@ nlHsAppType e s = noLoc (HsAppType hs_ty e)
hs_ty = mkHsWildCardBndrs $ nlHsParTy (typeToLHsType s)
nlExprWithTySig :: LHsExpr GhcPs -> Type -> LHsExpr GhcPs
-nlExprWithTySig e s = noLoc (ExprWithTySig hs_ty e)
+nlExprWithTySig e s = noLoc $ ExprWithTySig hs_ty
+ $ parenthesizeHsExpr sigPrec e
where
hs_ty = mkLHsSigWcType (typeToLHsType s)
@@ -1855,7 +1856,7 @@ mkFunBindSE arity loc fun pats_and_exprs
= mkRdrFunBindSE arity (L loc fun) matches
where
matches = [mkMatch (mkPrefixFunRhs (L loc fun))
- (map parenthesizeCompoundPat p) e
+ (map (parenthesizePat appPrec) p) e
(noLoc emptyLocalBinds)
| (p,e) <-pats_and_exprs]
@@ -1876,7 +1877,7 @@ mkFunBindEC arity loc fun catch_all pats_and_exprs
= mkRdrFunBindEC arity catch_all (L loc fun) matches
where
matches = [ mkMatch (mkPrefixFunRhs (L loc fun))
- (map parenthesizeCompoundPat p) e
+ (map (parenthesizePat appPrec) p) e
(noLoc emptyLocalBinds)
| (p,e) <- pats_and_exprs ]