lspitzner / brittany

haskell source code formatter
GNU Affero General Public License v3.0
690 stars 72 forks source link

Support formatting over TypeOperators #271

Closed plcplc closed 2 years ago

plcplc commented 4 years ago

Currently, running brittany over the below file:

{-# LANGUAGE TypeOperators #-}
module HsOpTy where

import GHC.TypeLits

type Foo = 
  Int :
  '[]

... results in the below error:

$ brittany HsOpTy.hs 
ERROR: brittany pretty printer returned syntactically invalid result.
ERROR: encountered unknown syntactical constructs:
HsOpTy{} at HsOpTy.hs:(6,3)-(7,5)

Note that when the definition of type Foo is just a single line brittany doesn't seem to mind.

Concretely, this is an issue for me in Servant API type definitions, which can get lengthy.

eborden commented 4 years ago

With --output-on-errors you can see the source of the issue.

$ brittany --output-on-errors < foo.hs
ERROR: brittany pretty printer returned syntactically invalid result.
ERROR: encountered unknown syntactical constructs:
HsOpTy{} at stdin:(7,3)-(8,5)
{-# LANGUAGE TypeOperators #-}
module HsOpTy where

import GHC.TypeLits

type Foo = {- BRITTANY ERROR UNHANDLED SYNTACTICAL CONSTRUCT -}
eborden commented 4 years ago

Looks like there is some in progress work here. https://github.com/lspitzner/brittany/blob/master/src/Language/Haskell/Brittany/Internal/Layouters/Type.hs#L473-L532

eborden commented 4 years ago

FYI, that was reached with the --dump-ast-unknown flag.

brittany --dump-ast-unknown < foo.hs
---- ast ----
A Just (Ann (DP (1,2)) [] [] [] Nothing Nothing)
  HsOpTy
    NoExt
    A Just (Ann (DP (0,0)) [] [] [] Nothing Nothing)
      HsTyVar
        NoExt
        NotPromoted
        A Just (Ann (DP (0,0)) [] [] [((G AnnVal),DP (0,0))] Nothing Nothing)
          Unqual {OccName: Int}
    A Just (Ann (DP (0,1)) [] [] [((G AnnVal),DP (0,0))] Nothing Nothing)
      Exact {abstract:Name}
    A Just (Ann (DP (1,2)) [] [] [((G AnnSimpleQuote),DP (0,0)),((G AnnOpenS),DP (0,0)),((G AnnCloseS),DP (0,0))] Nothing Nothing)
      HsExplicitListTy NoExt Promoted []
ERROR: brittany pretty printer returned syntactically invalid result.
ERROR: encountered unknown syntactical constructs:
HsOpTy{} at stdin:(7,3)-(8,5)
  HsOpTy NoExt (HsTyVar NoExt NotPromoted (Unqual {OccName: Int})) (Exact {abstract:Name}) (HsExplicitListTy NoExt Promoted [])
eborden commented 4 years ago

The main problem seems to be the presence of newlines. briDocByExactInlineOnly is being used, which explodes if newlines exist in the AST. So your code above fails, but this works.

{-# LANGUAGE TypeOperators #-}
module HsOpTy where

import GHC.TypeLits

type Foo = Int : '[]
eborden commented 4 years ago

If you can't omit newlines as a work around, I'd recommend using -- brittany-disable-next-binding to disable formatting on that statement in the meantime.

eborden commented 4 years ago
{-# LANGUAGE TypeOperators #-}
module HsOpTy where

import GHC.TypeLits

-- brittany-disable-next-binding
type Foo = 
  Int :
  '[]
expipiplus1 commented 4 years ago

I just bumped into this and made a simple test case. Interestingly moving this comma to the next line is enough to stop this happening :/

-- Works
a :: (a
     ,b + c)

-- Fails with the error mentioned above
a :: (a,
      b + c)
expipiplus1 commented 4 years ago

This may be another good fit for https://github.com/lspitzner/brittany/issues/28 (passthrough for unhandled constructs)

lspitzner commented 4 years ago

@expipiplus1 Thanks for the hint! #28 was in fact implemented by now. So on current master, the effect of this not being fully supported yet is much more limited.

expipiplus1 commented 4 years ago

Great!

andys8 commented 4 years ago

I think I'm experiencing the same issue with servant here.

https://github.com/theam/aws-lambda-haskell-runtime/blob/master/examples/wai-app/src/Lib.hs#L60-L66

Pitometsu commented 3 years ago

Any news related to this issue? Is there any help required? This issue actually blocks from using brittany on production projects and unfortunately forces to switch to ormolu without such a problems, but with an alien formatting style instead.

tfausak commented 2 years ago

I'm closing this in favor of #241, which I think is the same thing.