Opened 7 years ago

Closed 15 months ago

#1133 closed bug (fixed)

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 (1)

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

Download all attachments as: .zip

Change History (10)

comment:1 Changed 7 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 7 years ago by heatsink

command line log of compile error

comment:2 Changed 7 years ago by heatsink

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

comment:3 Changed 7 years ago by heatsink

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

comment:4 Changed 7 years ago by igloo

  • Milestone set to 6.6.1
  • Owner set to simonpj

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

comment:5 Changed 7 years ago by simonpj

  • Milestone changed from 6.6.1 to _|_
  • Priority changed from normal to lowest

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

comment:6 Changed 6 years ago by simonmar

  • Architecture changed from Unknown to Unknown/Multiple

comment:7 Changed 6 years ago by simonmar

  • Operating System changed from Unknown to Unknown/Multiple

comment:8 Changed 15 months ago by morabbin

  • Type of failure set to None/Unknown

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

comment:9 Changed 15 months ago by igloo

  • Resolution set to fixed
  • Status changed from new to closed
  • Test Case set to T1133, T1133A

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

Note: See TracTickets for help on using tickets.