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
Showing
- compiler/basicTypes/BasicTypes.hs 21 additions, 36 deletionscompiler/basicTypes/BasicTypes.hs
- compiler/hsSyn/Convert.hs 21 additions, 21 deletionscompiler/hsSyn/Convert.hs
- compiler/hsSyn/HsDecls.hs 3 additions, 4 deletionscompiler/hsSyn/HsDecls.hs
- compiler/hsSyn/HsExpr.hs 84 additions, 44 deletionscompiler/hsSyn/HsExpr.hs
- compiler/hsSyn/HsLit.hs 30 additions, 27 deletionscompiler/hsSyn/HsLit.hs
- compiler/hsSyn/HsPat.hs 64 additions, 101 deletionscompiler/hsSyn/HsPat.hs
- compiler/hsSyn/HsTypes.hs 37 additions, 17 deletionscompiler/hsSyn/HsTypes.hs
- compiler/hsSyn/HsUtils.hs 15 additions, 13 deletionscompiler/hsSyn/HsUtils.hs
- compiler/iface/IfaceSyn.hs 1 addition, 1 deletioncompiler/iface/IfaceSyn.hs
- compiler/iface/IfaceType.hs 63 additions, 63 deletionscompiler/iface/IfaceType.hs
- compiler/typecheck/TcGenDeriv.hs 4 additions, 3 deletionscompiler/typecheck/TcGenDeriv.hs
- compiler/types/TyCoRep.hs 19 additions, 18 deletionscompiler/types/TyCoRep.hs
- compiler/types/Type.hs 1 addition, 1 deletioncompiler/types/Type.hs
- libraries/template-haskell/Language/Haskell/TH/Ppr.hs 13 additions, 6 deletionslibraries/template-haskell/Language/Haskell/TH/Ppr.hs
- testsuite/tests/deriving/should_compile/T14682.stderr 2 additions, 2 deletionstestsuite/tests/deriving/should_compile/T14682.stderr
- testsuite/tests/th/T14875.hs 14 additions, 0 deletionstestsuite/tests/th/T14875.hs
- testsuite/tests/th/T14875.stderr 24 additions, 0 deletionstestsuite/tests/th/T14875.stderr
- testsuite/tests/th/all.T 1 addition, 0 deletionstestsuite/tests/th/all.T
Loading
Please register or sign in to comment