[commit: ghc] wip/merge-queue: Fix precedence handling for Data.Fixed's Show instance (#16031) (01b60b0)

git at git.haskell.org git at git.haskell.org
Sun Jan 6 12:33:27 UTC 2019


Repository : ssh://git@git.haskell.org/ghc

On branch  : wip/merge-queue
Link       : http://ghc.haskell.org/trac/ghc/changeset/01b60b0e1ee57f882c5b729854343916c2295c51/ghc

>---------------------------------------------------------------

commit 01b60b0e1ee57f882c5b729854343916c2295c51
Author: Sven Tennie <sven.tennie at gmail.com>
Date:   Wed Dec 26 12:04:36 2018 +0100

    Fix precedence handling for Data.Fixed's Show instance (#16031)
    
    Use `showsPrec` instead of `show` to respect the precedence of the surrounding
    context.


>---------------------------------------------------------------

01b60b0e1ee57f882c5b729854343916c2295c51
 libraries/base/Data/Fixed.hs                     | 2 +-
 libraries/base/tests/data-fixed-show-read.hs     | 4 ++++
 libraries/base/tests/data-fixed-show-read.stdout | 4 ++++
 3 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/libraries/base/Data/Fixed.hs b/libraries/base/Data/Fixed.hs
index 98acb76..482ec0a 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 7e947f4..8766f0a 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 4abb2d9..4f24242 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



More information about the ghc-commits mailing list