diff options
author | Ryan Scott <ryan.gl.scott@gmail.com> | 2020-08-26 09:23:54 -0400 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2020-08-27 14:20:01 -0400 |
commit | bacccb73c9b080c3c01a5e55ecb0a00cd8a77e55 (patch) | |
tree | b3807157c7c0b59abccdf1c732e5c9ce99c6e9c0 /compiler/GHC/Hs | |
parent | 01ff8c89727a91cbc1571ae54f73f5919d6aaa71 (diff) | |
download | haskell-bacccb73c9b080c3c01a5e55ecb0a00cd8a77e55.tar.gz |
Make {hsExpr,hsType,pat}NeedsParens aware of boxed 1-tuples
`hsExprNeedsParens`, `hsTypeNeedsParens`, and `patNeedsParens`
previously assumed that all uses of explicit tuples in the source
syntax never need to be parenthesized. This is true save for one
exception: boxed one-tuples, which use the `Solo` data type from
`GHC.Tuple` instead of special tuple syntax. This patch adds the
necessary logic to the three `*NeedsParens` functions to handle
`Solo` correctly.
Fixes #18612.
Diffstat (limited to 'compiler/GHC/Hs')
-rw-r--r-- | compiler/GHC/Hs/Expr.hs | 5 | ||||
-rw-r--r-- | compiler/GHC/Hs/Pat.hs | 7 | ||||
-rw-r--r-- | compiler/GHC/Hs/Type.hs | 14 |
3 files changed, 25 insertions, 1 deletions
diff --git a/compiler/GHC/Hs/Expr.hs b/compiler/GHC/Hs/Expr.hs index e7b904736d..829a789d36 100644 --- a/compiler/GHC/Hs/Expr.hs +++ b/compiler/GHC/Hs/Expr.hs @@ -1320,6 +1320,11 @@ hsExprNeedsParens p = go go (NegApp{}) = p > topPrec go (SectionL{}) = True go (SectionR{}) = True + -- Special-case unary boxed tuple applications so that they are + -- parenthesized as `Identity (Solo x)`, not `Identity Solo x` (#18612) + -- See Note [One-tuples] in GHC.Builtin.Types + go (ExplicitTuple _ [L _ Present{}] Boxed) + = p >= appPrec go (ExplicitTuple{}) = False go (ExplicitSum{}) = False go (HsLam{}) = p > topPrec diff --git a/compiler/GHC/Hs/Pat.hs b/compiler/GHC/Hs/Pat.hs index 62de0ab182..b1507f0adc 100644 --- a/compiler/GHC/Hs/Pat.hs +++ b/compiler/GHC/Hs/Pat.hs @@ -857,7 +857,12 @@ patNeedsParens p = go go (BangPat {}) = False go (ParPat {}) = False go (AsPat {}) = False - go (TuplePat {}) = False + -- Special-case unary boxed tuple applications so that they are + -- parenthesized as `Identity (Solo x)`, not `Identity Solo x` (#18612) + -- See Note [One-tuples] in GHC.Builtin.Types + go (TuplePat _ [_] Boxed) + = p >= appPrec + go (TuplePat{}) = False go (SumPat {}) = False go (ListPat {}) = False go (LitPat _ l) = hsLitNeedsParens p l diff --git a/compiler/GHC/Hs/Type.hs b/compiler/GHC/Hs/Type.hs index c6960c9c77..343cc1d1ba 100644 --- a/compiler/GHC/Hs/Type.hs +++ b/compiler/GHC/Hs/Type.hs @@ -1979,6 +1979,15 @@ hsTypeNeedsParens p = go_hs_ty go_hs_ty (HsRecTy{}) = False go_hs_ty (HsTyVar{}) = False go_hs_ty (HsFunTy{}) = p >= funPrec + -- Special-case unary boxed tuple applications so that they are + -- parenthesized as `Identity (Solo x)`, not `Identity Solo x` (#18612) + -- See Note [One-tuples] in GHC.Builtin.Types + go_hs_ty (HsTupleTy _ con [L _ ty]) + = case con of + HsBoxedTuple -> p >= appPrec + HsBoxedOrConstraintTuple -> p >= appPrec + HsConstraintTuple -> go_hs_ty ty + HsUnboxedTuple -> False go_hs_ty (HsTupleTy{}) = False go_hs_ty (HsSumTy{}) = False go_hs_ty (HsKindSig{}) = p >= sigPrec @@ -1986,6 +1995,11 @@ hsTypeNeedsParens p = go_hs_ty go_hs_ty (HsIParamTy{}) = p > topPrec go_hs_ty (HsSpliceTy{}) = False go_hs_ty (HsExplicitListTy{}) = False + -- Special-case unary boxed tuple applications so that they are + -- parenthesized as `Proxy ('Solo x)`, not `Proxy 'Solo x` (#18612) + -- See Note [One-tuples] in GHC.Builtin.Types + go_hs_ty (HsExplicitTupleTy _ [_]) + = p >= appPrec go_hs_ty (HsExplicitTupleTy{}) = False go_hs_ty (HsTyLit{}) = False go_hs_ty (HsWildCardTy{}) = False |