Support formatting over TypeOperators #271

Closed
opened 2019-12-08 17:18:54 +01:00 by plcplc · 13 comments
plcplc commented 2019-12-08 17:18:54 +01:00 (Migrated from github.com)

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.

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 2019-12-09 20:15:43 +01:00 (Migrated from github.com)

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 -}
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 2019-12-09 20:20:00 +01:00 (Migrated from github.com)
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 2019-12-09 20:21:35 +01:00 (Migrated from github.com)

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 [])
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 2019-12-09 20:23:39 +01:00 (Migrated from github.com)

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 : '[]
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. ```hs {-# LANGUAGE TypeOperators #-} module HsOpTy where import GHC.TypeLits type Foo = Int : '[] ```
eborden commented 2019-12-09 20:26:12 +01:00 (Migrated from github.com)

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.

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 2019-12-09 20:26:51 +01:00 (Migrated from github.com)
{-# LANGUAGE TypeOperators #-}
module HsOpTy where

import GHC.TypeLits

-- brittany-disable-next-binding
type Foo = 
  Int :
  '[]
```hs {-# LANGUAGE TypeOperators #-} module HsOpTy where import GHC.TypeLits -- brittany-disable-next-binding type Foo = Int : '[] ```
expipiplus1 commented 2020-02-17 03:59:28 +01:00 (Migrated from github.com)

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)
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 :/ ```haskell -- Works a :: (a ,b + c) -- Fails with the error mentioned above a :: (a, b + c) ```
expipiplus1 commented 2020-03-16 08:57:13 +01:00 (Migrated from github.com)

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

This may be another good fit for https://github.com/lspitzner/brittany/issues/28 (passthrough for unhandled constructs)
lspitzner commented 2020-03-16 12:27:16 +01:00 (Migrated from github.com)

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

@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 2020-03-16 12:32:37 +01:00 (Migrated from github.com)

Great!

Great!
andys8 commented 2020-07-20 14:29:30 +02:00 (Migrated from github.com)
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 2020-11-20 10:54:00 +01:00 (Migrated from github.com)

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.

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 2021-11-29 03:13:57 +01:00 (Migrated from github.com)

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

I'm closing this in favor of #241, which I think is the same thing.
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: hexagoxel/brittany#271
There is no content yet.