#14578 closed bug (fixed)

Incorrect parenthesization of types in -ddump-deriv

Reported by: RyanGlScott Owned by:
Priority: normal Milestone: 8.4.1
Component: Compiler Version: 8.2.2
Keywords: deriving Cc: alanz
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Debugging information is incorrect Test Case: deriving/should_compile/T14578
Blocked By: Blocking:
Related Tickets: Differential Rev(s): Phab:D4266
Wiki Page:

Description (last modified by RyanGlScott)

Compiling this program on GHC 8.2 or later:

{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# OPTIONS_GHC -ddump-deriv #-}
module Bug where

import Control.Applicative
import Data.Functor.Compose
import Data.Semigroup

newtype App f a = MkApp (f a)
  deriving (Functor, Applicative)

instance (Applicative f, Semigroup a) => Semigroup (App f a) where
  (<>) = liftA2 (<>)

newtype Wat f g a = MkWat (App (Compose f g) a)
  deriving Semigroup

Will result in some incorrectly pretty-printed types in the -ddump-deriv output:

$ /opt/ghc/8.2.2/bin/ghci Bug.hs -dsuppress-uniques
GHCi, version 8.2.2: http://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /home/rgscott/.ghci
[1 of 1] Compiling Bug              ( Bug.hs, interpreted )

==================== Derived instances ====================
Derived class instances:

  <elided for brevity>

  instance (Data.Semigroup.Semigroup a, GHC.Base.Applicative g,
            GHC.Base.Applicative f) =>
           Data.Semigroup.Semigroup (Bug.Wat f g a) where
    (Data.Semigroup.<>)
      = GHC.Prim.coerce
          @(Bug.App Data.Functor.Compose.Compose f g a
            -> Bug.App Data.Functor.Compose.Compose f g a
               -> Bug.App Data.Functor.Compose.Compose f g a)
          @(Bug.Wat f g a -> Bug.Wat f g a -> Bug.Wat f g a)
          (Data.Semigroup.<>)
    Data.Semigroup.sconcat
      = GHC.Prim.coerce
          @(Data.List.NonEmpty.NonEmpty Bug.App Data.Functor.Compose.Compose f g a
            -> Bug.App Data.Functor.Compose.Compose f g a)
          @(Data.List.NonEmpty.NonEmpty Bug.Wat f g a -> Bug.Wat f g a)
          Data.Semigroup.sconcat
    Data.Semigroup.stimes
      = GHC.Prim.coerce
          @(forall (b :: TYPE GHC.Types.LiftedRep).
            GHC.Real.Integral b =>
            b
            -> Bug.App Data.Functor.Compose.Compose f g a
               -> Bug.App Data.Functor.Compose.Compose f g a)
          @(forall (b :: TYPE GHC.Types.LiftedRep).
            GHC.Real.Integral b => b -> Bug.Wat f g a -> Bug.Wat f g a)
          Data.Semigroup.stimes

Notice that it shows Bug.App Data.Functor.Compose.Compose f g a within the visible type applications, which is wrong. It should be Bug.App (Data.Functor.Compose.Compose f g) a.

Change History (8)

comment:1 Changed 22 months ago by RyanGlScott

Description: modified (diff)

comment:2 Changed 22 months ago by alanz

Cc: alanz added

comment:3 Changed 22 months ago by RyanGlScott

And here is why: the type App (Compose f g) a is produced by the function typeToLHsType. Here is the line of code that's responsible:

    go (TyConApp tc args)   = nlHsTyConApp (getRdrName tc) (map go args')
       where
         args' = filterOutInvisibleTypes tc args

Notice that this does not attempt to put the appropriate HsParTys in place. I think an appropriate way to fix this would be to adopt the same trick that alanz has used elsewhere: apply the mk_apps function to tc and args'.

comment:4 Changed 22 months ago by RyanGlScott

Differential Rev(s): Phab:D4266
Status: newpatch

comment:5 Changed 21 months ago by Ryan Scott <ryan.gl.scott@…>

In 1bd91a7/ghc:

Fix #14578 by checking isCompoundHsType in more places

Summary:
The `HsType` pretty-printer does not automatically insert
parentheses where necessary for type applications, so a function
`isCompoundHsType` was created in D4056 towards this purpose.
However, it was not used in as many places as it ought to be,
resulting in #14578.

Test Plan: make test TEST=T14578

Reviewers: alanz, bgamari, simonpj

Reviewed By: alanz, simonpj

Subscribers: simonpj, rwbarton, thomie, carter

GHC Trac Issues: #14578

Differential Revision: https://phabricator.haskell.org/D4266

comment:6 Changed 21 months ago by RyanGlScott

Resolution: fixed
Status: patchclosed
Test Case: deriving/should_compile/T14578

comment:7 Changed 21 months ago by bgamari

Milestone: 8.4.1
Status: closedmerge

Merged in e32f582783.

comment:8 Changed 21 months ago by bgamari

Status: mergeclosed
Note: See TracTickets for help on using tickets.