Opened 5 years ago

Last modified 2 years ago

#9450 new bug

GHC instantiates Data instances before checking hs-boot files

Reported by: alanz Owned by:
Priority: normal Milestone:
Component: Compiler (Type checker) Version: 7.8.3
Keywords: deriving, hs-boot Cc:
Operating System: Unknown/Multiple Architecture: x86_64 (amd64)
Type of failure: Compile-time crash Test Case:
Blocked By: Blocking:
Related Tickets: #13591 Differential Rev(s):
Wiki Page:

Description

When compiling a file with deriving instances in it and making use of boot files, it appears that the derivation is attempted before the boot files are checked for consistency.

This can lead to GHC panics, as when compiling GHC after this commit https://github.com/alanz/ghc/commit/c73182ca7345f0debba47d0b17a907bcac27c41f

In the attached files, if you try to load HsLit.lhs into ghci it will complain about the missing Data instance, not about the boot file inconsistency. If you remove the Data from the deriving clause of HsOverLit, it complains about the boot file inconsistency.

I have not managed to reproduce the original panic in this stripped down environment, but if it is needed to more fully understand the problem I can try again to do that.

Attachments (4)

HsLit.hs (852 bytes) - added by alanz 5 years ago.
HsLit.hs
HsLit.hs-boot (142 bytes) - added by alanz 5 years ago.
HsLit.hs-boot
HsExpr.hs (48 bytes) - added by alanz 5 years ago.
HsExpr.hs
HsExpr.hs-boot (437 bytes) - added by alanz 5 years ago.
HsExpr.hs-boot

Download all attachments as: .zip

Change History (13)

Changed 5 years ago by alanz

Attachment: HsLit.hs added

HsLit.hs

Changed 5 years ago by alanz

Attachment: HsLit.hs-boot added

HsLit.hs-boot

Changed 5 years ago by alanz

Attachment: HsExpr.hs added

HsExpr.hs

Changed 5 years ago by alanz

Attachment: HsExpr.hs-boot added

HsExpr.hs-boot

comment:1 Changed 5 years ago by simonpj

This is awkward. GHC currently typechecks the source file, and then compares with the hs-boot file for consistency. And indeed, we must do that, because until we have done at least some typechecking we don't have any type from the source file to compare with those in the hs-boot file.

I suppose that the difficulty is that the very act of typechecking the source file leads to an unexpected situation and a crash. I have not worked out the details.

I'm not sure how to solve this. One route (which seems like the right one) might be to make the check whenever extending the type environment; that is, as soon as we have got to the point of adding a data type T, or function f, to the environment, at that moment check compatibility.

Probably not too hard, but a bit fiddly, and I'm definitely not going to have time to do it soon. And yet it's bad that GHC actually crashes. It should never do that.

Simon

comment:2 Changed 4 years ago by thomie

Component: CompilerCompiler (Type checker)

comment:3 Changed 3 years ago by simonpj

Keywords: hs-boot added; boot removed

comment:4 Changed 3 years ago by simonpj

Keywords: hs-boot,derivinghs-boot, deriving

comment:5 Changed 3 years ago by simonpj

Keywords: hs-boot, derivinghs-boot deriving

comment:6 Changed 3 years ago by simonpj

Keywords: deriving removed

comment:7 Changed 3 years ago by simonpj

Keywords: deriving added

comment:8 Changed 3 years ago by simonpj

Keywords: deriving hs-bootderiving, hs-boot

comment:9 Changed 2 years ago by RyanGlScott

Interestingly, if you compile this with GHC 8.0.1 or later, you get an error described in #13591:

$ /opt/ghc/8.0.1/bin/ghci HsLit.hs
GHCi, version 8.0.1: http://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /home/rgscott/.ghci
[1 of 4] Compiling HsLit[boot]      ( HsLit.hs-boot, interpreted )
[2 of 4] Compiling HsExpr[boot]     ( HsExpr.hs-boot, interpreted )
[3 of 4] Compiling HsExpr           ( HsExpr.hs, interpreted )

HsExpr.hs:1:1: error:
    instance (Data.Data.Data id, Data.Data.Data (HsLit.TypeAnnot id),
              Data.Data.Data (HsLit.NameAnnot id)) =>
             Data.Data.Data (HsExpr id)
      -- Defined at HsExpr.hs-boot:19:10
      is defined in the hs-boot file, but not in the module itself

HsExpr.hs-boot:17:1: error:
    Type constructor ‘HsExpr’ has conflicting definitions in the module
    and its hs-boot file
    Main module: data HsExpr i = E i | E2 i
    Boot file:   type role HsExpr nominal
                 abstract HsExpr i
    The roles do not match.
    Roles on abstract types default to ‘representational’ in boot files.
*** Exception: expectJust showModule
CallStack (from HasCallStack):
  error, called at compiler/utils/Maybes.hs:48:27 in ghc:Maybes
Note: See TracTickets for help on using tickets.