Opened 6 months ago

Last modified 6 months ago

#13765 new bug

GHC cannot parse valid Haskell98 whose first identifier is named signature

Reported by: ezyang Owned by:
Priority: low Milestone:
Component: Compiler (Parser) Version: 8.2.1-rc2
Keywords: backpack Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

This no longer parses in GHC 8.2:

ezyang@sabre:~$ cat A.hs
signature = 2
ezyang@sabre:~$ ghc-8.0 -c A.hs

A.hs:1:1: error:
    The IO action ‘main’ is not defined in module ‘Main’
ezyang@sabre:~$ ghc-head -c A.hs

A.hs:1:11: error:
    parse error on input ‘=’
    Perhaps you need a 'let' in a 'do' block?
    e.g. 'let x = 5' instead of 'x = 5'
  |
1 | signature = 

The problem is that signature is only a keyword in the module header and not in the rest of the code, which means that there is a shift/reduce conflict when you have a signature at the top of the module.

This is fairly benign bug as you can only trigger it when you have no imports; nevertheless, we should fix it. One possibility is to have a completely different parser entry when you are parsing an hsig file so that signature never occurs in a module header when you parse a normal Haskell file. ISTR this was modestly more irritating to implement but perhaps it is still not too bad. (Note that the shift/reduce conflict still exists, but since you'll never declare a signature without a module header it is essentially irrelevant.)

I discovered this when mpickering pointed out that adding 'signature' to the module header increased the number of shift reduce conflicts. (https://github.com/haskell-suite/haskell-src-exts/pull/355#issuecomment-304536290). This wasn't the case in GHC's parser but only accidentally: there is already a shift reduce conflict when you have a top-level Haddock doc block!

Change History (1)

comment:1 Changed 6 months ago by ezyang

Keywords: backpack added
Note: See TracTickets for help on using tickets.