haskell-suite / haskell-src-exts

Manipulating Haskell source: abstract syntax, lexer, parser, and pretty-printer
Other
193 stars 94 forks source link

Associated empty data decl in instance causes error in `exactPrint` #461

Open Melvar opened 2 years ago

Melvar commented 2 years ago
{-# LANGUAGE EmptyDataDecls, TypeFamilies #-}

instance Foo Bar where
  data Baz Bar

This source file can be parsed just fine, but attempting to exactPrint the resulting module throws an exception:

"{-# LANGUAGE EmptyDataDecls, TypeFamilies #-}\n\ninstance Foo Bar where\n  data Baz Bar*** Exception: ExactP: InstDecl: InsGData is given too few srcInfoPoints
CallStack (from HasCallStack):
  error, called at src/Language/Haskell/Exts/ExactPrint.hs:63:10 in haskell-src-exts-1.23.1-FvLIaTIGxmRVhU8jEdAAL:Language.Haskell.Exts.ExactPrint

It’s a bit suspicious that the parser produced an InsGData (instance GADT declaration) instead of an InsData, even without GADTs enabled. It appears from a cursory inspection of the source that the exact printing implementation for InsGData assumes that a where always needs to be printed, which is violated by the parser in this case but would make sense if an ambiguous empty data declaration is supposed to produce an InsData.

Melvar commented 2 years ago

As a bonus, I cannot work around this error by including a where (and GADTs which I am using anyway) because that runs into #358.