#14692 closed bug (fixed)

Deriving Show with -XEmptyDataDeriving cases on the wrong argument

Reported by: RyanGlScott Owned by:
Priority: high Milestone: 8.4.1
Component: Compiler Version: 8.4.1-alpha1
Keywords: deriving Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Incorrect result at runtime Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s): Phab:D4328
Wiki Page:

Description

Running this program in GHC 8.4.1-alpha:

{-# LANGUAGE EmptyDataDeriving #-}
{-# OPTIONS_GHC -ddump-deriv #-}
module Main (main) where

data Empty deriving Show

loop :: Empty
loop = let x = x in x

main :: IO ()
main = print loop

One would expect this to loop infinitely at runtime, but in practice, that is not the case:

$ /opt/ghc/8.4.1/bin/runghc Bug.hs

==================== Derived instances ====================
Derived class instances:
  instance GHC.Show.Show Main.Empty where
    GHC.Show.showsPrec z_a1Iu = case z_a1Iu of
  

Derived type family instances:



==================== Filling in method body ====================
GHC.Show.Show [Main.Empty]
  GHC.Show.show = GHC.Show.$dmshow @(Main.Empty)



==================== Filling in method body ====================
GHC.Show.Show [Main.Empty]
  GHC.Show.showList = GHC.Show.$dmshowList @(Main.Empty)


Bug.hs: Bug.hs:5:21-24: Non-exhaustive patterns in case

The -ddump-deriv output reveals why: the showsPrec implementation for Empty is casing on the precedence argument, not the actual value of type Empty! This results in the non-exhaustive patterns error.

This is my fault, so I'll prepare a fix :)

Change History (3)

comment:1 Changed 20 months ago by RyanGlScott

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

comment:2 Changed 20 months ago by Ben Gamari <ben@…>

In 0074a08e/ghc:

Fix #14692 by correcting an off-by-one error in TcGenDeriv

A silly mistake in `gen_Show_binds` was causing derived
`Show` instances for empty data types to case on the precedence
argument instead of the actual value being showed.

Test Plan: make test TEST=drv-empty-data

Reviewers: bgamari

Reviewed By: bgamari

Subscribers: rwbarton, thomie, carter

GHC Trac Issues: #14692

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

comment:3 Changed 20 months ago by bgamari

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