summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSven Tennie <sven.tennie@gmail.com>2018-12-26 12:04:36 +0100
committerBen Gamari <ben@smart-cactus.org>2019-01-06 07:27:09 -0500
commit01b60b0e1ee57f882c5b729854343916c2295c51 (patch)
treebe1bfafd1e9a4dafc8c1a412b7c83fba3993b25a
parentc121e33f9b039acf2ac6939af8bfafe593560039 (diff)
downloadhaskell-01b60b0e1ee57f882c5b729854343916c2295c51.tar.gz
Fix precedence handling for Data.Fixed's Show instance (#16031)
Use `showsPrec` instead of `show` to respect the precedence of the surrounding context.
-rw-r--r--libraries/base/Data/Fixed.hs2
-rw-r--r--libraries/base/tests/data-fixed-show-read.hs4
-rw-r--r--libraries/base/tests/data-fixed-show-read.stdout4
3 files changed, 9 insertions, 1 deletions
diff --git a/libraries/base/Data/Fixed.hs b/libraries/base/Data/Fixed.hs
index 98acb76967..482ec0a694 100644
--- a/libraries/base/Data/Fixed.hs
+++ b/libraries/base/Data/Fixed.hs
@@ -158,7 +158,7 @@ showFixed chopTrailingZeros fa@(MkFixed a) = (show i) ++ (withDot (showIntegerZe
-- | @since 2.01
instance (HasResolution a) => Show (Fixed a) where
- show = showFixed False
+ showsPrec p n = showParen (p > 6 && n < 0) $ showString $ showFixed False n
-- | @since 4.3.0.0
instance (HasResolution a) => Read (Fixed a) where
diff --git a/libraries/base/tests/data-fixed-show-read.hs b/libraries/base/tests/data-fixed-show-read.hs
index 7e947f466e..8766f0ae0c 100644
--- a/libraries/base/tests/data-fixed-show-read.hs
+++ b/libraries/base/tests/data-fixed-show-read.hs
@@ -21,6 +21,10 @@ main = do doit 38.001
print (read "-38" :: Centi)
print (read "0.008" :: Fixed B7)
print (read "-0.008" :: Fixed B7)
+ print $ show (Just (-1 :: Milli))
+ print $ show (Just (1 :: Milli))
+ print ((read $ show (Just (-1 :: Deci))) :: Maybe Deci)
+ print ((read $ show (Just (1 :: Deci))) :: Maybe Deci)
doit :: Centi -> IO ()
doit c = do let s = show c
diff --git a/libraries/base/tests/data-fixed-show-read.stdout b/libraries/base/tests/data-fixed-show-read.stdout
index 4abb2d9676..4f24242da9 100644
--- a/libraries/base/tests/data-fixed-show-read.stdout
+++ b/libraries/base/tests/data-fixed-show-read.stdout
@@ -18,3 +18,7 @@
-38.00
0.008
-0.008
+"Just (-1.000)"
+"Just 1.000"
+Just (-1.0)
+Just 1.0