Ticket #1133 (closed bug: fixed)

Opened 6 years ago

Last modified 4 months ago

auto-derivation of Enum is disallowed with recursive imports

Reported by: heatsink Owned by: simonpj
Priority: lowest Milestone: _|_
Component: Compiler Version: 6.6
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Difficulty: Unknown
Test Case: T1133, T1133A Blocked By:
Blocking: Related Tickets:

Description

A data type with one single-parameter constructor can normally be declared deriving(Enum) when compiling with -fglasgow-exts. However, when the data type is also declared in a hs-boot file, this is not accepted. The following test case demonstrates the problem. The same thing happens if the 'deriving' clause is also present in the hs-boot file.

{- file X.hs-boot -}
module X where
newtype X = X Int

{- file X.hs -}
module X where
import {-# SOURCE #-} X
newtype X = X Int deriving(Enum)

ghc -c -fglasgow-exts X.hs-boot X.hs

The error occurs in X.hs: Can't make a derived instance of `Enum X' (`X' has non-nullary constructors)

Attachments

log.txt Download (489 bytes) - added by heatsink 6 years ago.
command line log of compile error

Change History

Changed 6 years ago by simonpj

Are you sure you are using -fglasgow-exts. I have just compiled your example with GHC 6.6, thus

bash-3.1$ ghc -c -fglasgow-exts X.hs-boot X.hs
bash-3.1$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 6.6
bash-3.1$ 

And all is happy. Without -fglasgow-exts it fails, because in Haskell 98 you can't derive Enum for a newtype.

Can you double check your test case and send and exact log of what happened? Remove all .o and .hi and .hi-boot files first.

Simon

Changed 6 years ago by heatsink

command line log of compile error

Changed 6 years ago by heatsink

The log file was generated with the OS X Intel binary distribution of GHC 6.6.

Changed 6 years ago by heatsink

Correction. The log file was generated with the OS X PPC binary distribution of GHC 6.6.

Changed 6 years ago by igloo

  • owner set to simonpj
  • milestone set to 6.6.1

I can reproduce this with 6.6, HEAD and 6.6-branch on amd64/Linux.

Changed 6 years ago by simonpj

  • priority changed from normal to lowest
  • milestone changed from 6.6.1 to _|_

The error message is certainly misleading. It now says:

    Can't make a derived instance of `Enum X'
      (even with cunning newtype deriving:
       the newtype may be recursive)
    In the newtype declaration for `X'

The problem is this. Generally, the "newtype deriving" mechanism is cautious about recursive newtypes. Consider

newtype A = MkA [A]

We do not want to generate the instance

instance Eq [A] => Eq A

In this case the newtype patently isn't recursive; but in that case why is it being declared in a hs-boot file? Generally GHC treats all hs-boot file types as recursive, because generally it can't see their definitions.

The whole deal about recursive types is an unsatisfactory aspect of GHC. I'm going to treat this one as fixed for 6.6.1 (via the error message improvement). But I'll leave it open at low priority for later versions.

Simon

Changed 5 years ago by simonmar

  • architecture changed from Unknown to Unknown/Multiple

Changed 5 years ago by simonmar

  • os changed from Unknown to Unknown/Multiple

Changed 4 months ago by morabbin

  • failure set to None/Unknown

This is now fine when compiled with flag -XGeneralizedNewtypeDeriving; close as fixed?

Changed 4 months ago by igloo

  • status changed from new to closed
  • testcase set to T1133, T1133A
  • resolution set to fixed

Yes, looks good, thanks. I added a couple of tests.

Note: See TracTickets for help on using tickets.