summaryrefslogtreecommitdiff
path: root/compiler/GHC/Hs
diff options
context:
space:
mode:
authorKrzysztof Gogolewski <krzysztof.gogolewski@tweag.io>2022-12-07 05:38:40 +0100
committerMarge Bot <ben+marge-bot@smart-cactus.org>2022-12-08 22:50:21 -0500
commitb0cc2fcfc485da772c5ffef1b625af9e7ae73129 (patch)
tree95ac4a78e5d33afd049775c58e1fe0053b2fb360 /compiler/GHC/Hs
parent3609340743c1b25fdfd0e18b1670dac54c8d8623 (diff)
downloadhaskell-b0cc2fcfc485da772c5ffef1b625af9e7ae73129.tar.gz
Fixes around primitive literals
* The SourceText of primitive characters 'a'# did not include the #, unlike for other primitive literals 1#, 1##, 1.0#, 1.0##, "a"#. We can now remove the function pp_st_suffix, which was a hack to add the # back. * Negative primitive literals shouldn't use parentheses, as described in Note [Printing of literals in Core]. Added a testcase to T14681.
Diffstat (limited to 'compiler/GHC/Hs')
-rw-r--r--compiler/GHC/Hs/Lit.hs21
1 files changed, 10 insertions, 11 deletions
diff --git a/compiler/GHC/Hs/Lit.hs b/compiler/GHC/Hs/Lit.hs
index 838e3348dd..7d2df811ee 100644
--- a/compiler/GHC/Hs/Lit.hs
+++ b/compiler/GHC/Hs/Lit.hs
@@ -117,6 +117,9 @@ hsOverLitNeedsParens _ (XOverLit { }) = False
-- | @'hsLitNeedsParens' p l@ returns 'True' if a literal @l@ needs
-- to be parenthesized under precedence @p@.
+--
+-- See Note [Printing of literals in Core] in GHC.Types.Literal
+-- for the reasoning.
hsLitNeedsParens :: PprPrec -> HsLit x -> Bool
hsLitNeedsParens p = go
where
@@ -125,14 +128,14 @@ hsLitNeedsParens p = go
go (HsString {}) = False
go (HsStringPrim {}) = False
go (HsInt _ x) = p > topPrec && il_neg x
- go (HsIntPrim _ x) = p > topPrec && x < 0
+ go (HsIntPrim {}) = False
go (HsWordPrim {}) = False
- go (HsInt64Prim _ x) = p > topPrec && x < 0
+ go (HsInt64Prim {}) = False
go (HsWord64Prim {}) = False
go (HsInteger _ x _) = p > topPrec && x < 0
go (HsRat _ x _) = p > topPrec && fl_neg x
- go (HsFloatPrim _ x) = p > topPrec && fl_neg x
- go (HsDoublePrim _ x) = p > topPrec && fl_neg x
+ go (HsFloatPrim {}) = False
+ go (HsDoublePrim {}) = False
go (XLit _) = False
-- | Convert a literal from one index type to another
@@ -169,7 +172,7 @@ Equivalently it's True if
-- Instance specific to GhcPs, need the SourceText
instance Outputable (HsLit (GhcPass p)) where
ppr (HsChar st c) = pprWithSourceText st (pprHsChar c)
- ppr (HsCharPrim st c) = pp_st_suffix st primCharSuffix (pprPrimChar c)
+ ppr (HsCharPrim st c) = pprWithSourceText st (pprPrimChar c)
ppr (HsString st s) = pprWithSourceText st (pprHsString s)
ppr (HsStringPrim st s) = pprWithSourceText st (pprHsBytes s)
ppr (HsInt _ i)
@@ -180,12 +183,8 @@ instance Outputable (HsLit (GhcPass p)) where
ppr (HsDoublePrim _ d) = ppr d <> primDoubleSuffix
ppr (HsIntPrim st i) = pprWithSourceText st (pprPrimInt i)
ppr (HsWordPrim st w) = pprWithSourceText st (pprPrimWord w)
- ppr (HsInt64Prim st i) = pp_st_suffix st primInt64Suffix (pprPrimInt64 i)
- ppr (HsWord64Prim st w) = pp_st_suffix st primWord64Suffix (pprPrimWord64 w)
-
-pp_st_suffix :: SourceText -> SDoc -> SDoc -> SDoc
-pp_st_suffix NoSourceText _ doc = doc
-pp_st_suffix (SourceText st) suffix _ = text st <> suffix
+ ppr (HsInt64Prim st i) = pprWithSourceText st (pprPrimInt64 i)
+ ppr (HsWord64Prim st w) = pprWithSourceText st (pprPrimWord64 w)
-- in debug mode, print the expression that it's resolved to, too
instance OutputableBndrId p