Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support formatting over TypeOperators #271

Closed
plcplc opened this issue Dec 8, 2019 · 13 comments
Closed

Support formatting over TypeOperators #271

plcplc opened this issue Dec 8, 2019 · 13 comments

Comments

@plcplc
Copy link

plcplc commented Dec 8, 2019

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
Copy link
Collaborator

eborden commented Dec 9, 2019

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
Copy link
Collaborator

eborden commented Dec 9, 2019

@eborden
Copy link
Collaborator

eborden commented Dec 9, 2019

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
Copy link
Collaborator

eborden commented Dec 9, 2019

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
Copy link
Collaborator

eborden commented Dec 9, 2019

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
Copy link
Collaborator

eborden commented Dec 9, 2019

{-# LANGUAGE TypeOperators #-}
module HsOpTy where

import GHC.TypeLits

-- brittany-disable-next-binding
type Foo = 
  Int :
  '[]

@expipiplus1
Copy link
Contributor

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
Copy link
Contributor

This may be another good fit for #28 (passthrough for unhandled constructs)

@lspitzner
Copy link
Owner

@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
Copy link
Contributor

Great!

@andys8
Copy link
Contributor

andys8 commented Jul 20, 2020

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
Copy link

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
Copy link
Collaborator

tfausak commented Nov 29, 2021

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

@tfausak tfausak closed this as completed Nov 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants