Opened 3 years ago

Closed 3 years ago

#12688 closed bug (fixed)

derived Show instances aren't protected from RebindableSyntax+OverloadedStrings

Reported by: daviddarais Owned by: RyanGlScott
Priority: normal Milestone: 8.0.2
Component: Compiler Version: 8.0.1
Keywords: rebindablesyntax overloadedstrings deriving show string Cc: RyanGlScott
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: GHC rejects valid program Test Case: deriving/should_compile/T12688
Blocked By: Blocking:
Related Tickets: Differential Rev(s): Phab:D2591
Wiki Page:

Description

When using both RebindableSyntax and OverloadedStrings, deriving (Show) no longer works.

Example program:

{-# LANGUAGE RebindableSyntax, OverloadedStrings #-}

module Bug where

import Prelude (String,Show(..))

newtype Text = Text String

fromString :: String -> Text
fromString = Text

x :: Text
x = "x"

newtype Foo = Foo ()
  deriving (Show)

In this example, fromString is redefined to return a type different from [Char]. The example x typechecks due to to the RebindableSyntax and OverloadedStrings extensions. However, the generated code for the deriving (Show) instance for Foo does not typecheck, and ghc gives a code generation error:

error:
     Couldn't match type Text with [GHC.Types.Char]
      Expected type: String
        Actual type: Text
     In the first argument of GHC.Show.showString, namely "Foo "
      In the first argument of (GHC.Base..), namely
        (GHC.Show.showString "Foo ")
      In the second argument of GHC.Show.showParen, namely
        ((GHC.Base..) (GHC.Show.showString "Foo ") (showsPrec 11 b1))
      When typechecking the code for showsPrec
        in a derived instance for Show Foo:
        To see the code I am typechecking, use -ddump-deriv

A solution would be to protect the code generated by deriving (Show) from OverloadedStrings and RebindableSyntax extensions.

Change History (7)

comment:1 Changed 3 years ago by simonpj

Cc: RyanGlScott added
Owner: set to RyanGlScott

A solution would be to protect the code generated by deriving (Show) from OverloadedStrings and RebindableSyntax extensions.

Yes, I think that's probably the right solution. Ryan, do you agree? Might you do it? I think it's a matter of fiddling with ib_extensions.

comment:2 Changed 3 years ago by RyanGlScott

Well, we've certainly changed the way deriving works in the past to accommodate the presence of RebindableSyntax (see #11396 and #12080 for examples). But those were quite small, non-invasive changes. On the other hand, derived Show instances make heavy use of string literals, so I don't think changing the derived Show methods is really an option here.

Simon, are you suggesting just disabling RebindableSyntax entirely for derived code? I'd be fine with that.

comment:3 Changed 3 years ago by simonpj

Simon, are you suggesting just disabling RebindableSyntax entirely for derived code? I'd be fine with that.

Yes, exactly

Simon

comment:4 Changed 3 years ago by RyanGlScott

Differential Rev(s): Phab:D2591
Milestone: 8.0.2
Status: newpatch

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

In b501709/ghc:

Fix Show derivation in the presence of RebindableSyntax/OverloadedStrings

Summary:
To fix this issue, we simply disable `RebindableSyntax` whenever we rename
the code generated from a deriving clause.

Fixes #12688.

Test Plan: make test TEST=T12688

Reviewers: simonpj, austin, bgamari

Reviewed By: simonpj, bgamari

Subscribers: thomie

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

GHC Trac Issues: #12688

comment:6 Changed 3 years ago by RyanGlScott

Status: patchmerge
Test Case: deriving/should_compile/T12688

comment:7 Changed 3 years ago by bgamari

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