tweag / ormolu

A formatter for Haskell source code
https://ormolu-live.tweag.io
Other
958 stars 83 forks source link

Unsupported extensions present as GHC bug #380

Closed b-mehta closed 5 years ago

b-mehta commented 5 years ago

Using unfamiliar extensions in source files gives an error which presents as a bug in GHC.

Example:

{-# LANGUAGE WeirdExtension #-}

main :: IO ()
main = return ()

when run with ormolu gives output

ormolu: panic! (the 'impossible' happened)
  (GHC version 8.6.5 for x86_64-apple-darwin):
        Unsupported extension: WeirdExtension

Please report this as a GHC bug:  http://www.haskell.org/ghc/reportabug

Expected output: Some sort of ormolu-specific error that the extension was not recognised.

[Motivation: Someone suggested to run ormolu on linear-base.]

mrkkrp commented 5 years ago

I imagine language extensions can affect how parser works, so it's reasonable in a sense for GHC to complain at parsing stage if it encounters an unknown extension.

Someone suggested to run ormolu on linear-base.

I think linear base uses syntax such as (.->) which is not supported by GHC 8.6.5 (we use this version so far) and so there is no chance it's going to work.

mrkkrp commented 5 years ago

@utdemir When you have time, it would be good to look into this a bit and figure out why we get the GHC panic and whether or not this behavior should be changed.

b-mehta commented 5 years ago

I imagine language extensions can affect how parser works, so it's reasonable in a sense for GHC to complain at parsing stage if it encounters an unknown extension.

Sure this is reasonable, but I'd expected a complaint rather than a GHC panic.

I think linear base uses syntax such as (.->) which is not supported by GHC 8.6.5 (we use this version so far) and so there is no chance it's going to work.

Yeah, LinearTypes uses ->. syntax, but my hope was that ormolu might accept this as a standard type operator and continue as normal (possibly with some minor patches in a fork I could make).

utdemir commented 5 years ago

@utdemir When you have time, it would be good to look into this a bit and figure out why we get the GHC panic and whether or not this behavior should be changed.

We use DynFlags.parseDynamicFilePragma from GHC to parse the language extensions, and it throws the exception on unknown extensions. Since it runs on GhcMonad and we use GHC.defaultErrorHandler the exception is wrapped as a GHC panic and reported as one. I am going to send a PR removing the use of defaultErrorHandler so we don't report this as a GHC bug but we will still throw the exception.

Handling unknown extensions unfortunately requires us to parse the language extensions ourselves and I do not think it's worth the effort. Chances are in the presence of an unknown language extension we won't be able to parse/format the syntax correctly either.

Yeah, LinearTypes uses ->. syntax, but my hope was that ormolu might accept this as a standard type operator and continue as normal ...

I just tried this and that's exactly what happens. I was able to format linear-base after removing the language pragmas(sed '/LANGUAGE LinearTypes/d' -i **/*.hs). I found one bug (#383), otherwise it works fine.