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

Differing AST on empty {- ^ -} Haddock markup #1065

Closed
tomjaguarpaw opened this issue Sep 1, 2023 · 5 comments · Fixed by #1068
Closed

Differing AST on empty {- ^ -} Haddock markup #1065

tomjaguarpaw opened this issue Sep 1, 2023 · 5 comments · Fixed by #1068
Labels
bug Something isn't working comments Issues related to comment placement

Comments

@tomjaguarpaw
Copy link
Contributor

Describe the bug

If I have an empty {- ^ -} Haddock annotation then ormolu fails with an AST of input and AST of formatted code differ error.

To Reproduce

$ cat /tmp/example.hs
data T = T {- ^ -}
$ ormolu /tmp/example.hs
/tmp/example.hs
@@ -1,1 +1,3 @@
- data T = T {- ^ -}
+ data T
+   = -- |
+     T

  AST of input and AST of formatted code differ.
    at /tmp/example.hs:1:10
  Please, consider reporting the bug.
  To format anyway, use --unsafe.

Expected behavior

ormolu to produce the same reformatted code as above but to not complain that the ASTs differ (I don't think they do!). {- ^ ... -} is not mentioned in the Haddock documentation as valid Haddock syntax, but it does accept it. Note that if there is text in the markup then everything is fine:

$ cat /tmp/example2.hs
data T = T {- ^ text -}
$ ormolu /tmp/example2.hs
data T
  = -- | text
    T

Environment

  • OS name + version: Debian GNU/Linux 11.7
  • Version of the code:
$ ormolu --version
ormolu 0.7.1.0
using ghc-lib-parser 9.6.2.20230523

Additional context

ormolu is great, thanks!

Perhaps related: #822

@mrkkrp mrkkrp added bug Something isn't working comments Issues related to comment placement labels Sep 4, 2023
@amesgen
Copy link
Member

amesgen commented Sep 5, 2023

Thanks for the report! The problem is that in the input, the AST contains a Haddock comment (HsDoc construct) with text " ", but the output does not contain a Haddock comment (you can eg see this on Ormolu Live with the "Show internal parse result" option), so the AST diffing reports an error. This somewhat surprising behavior of the GHC parser already came up in #726, and was fixed in #818 by removing all Haddock comments that are completely blank. #818 was missing that operation for Haddock comments in data declarations, which I added in #1068, which fixes this issue.

Hence, with #1068, your example

data T = T {- ^ -}

is formatted to

data T = T

Hope this makes sense!

@tomjaguarpaw
Copy link
Contributor Author

Thanks for your response @amesgen.

Hope this makes sense!

It makes sense but I think it's the wrong thing to do. See #1068 (comment)

@tomjaguarpaw
Copy link
Contributor Author

Personally I would prefer the approach that AST diffing considers comments consisting of only of any amount of whitespace to be equivalent.

@tomjaguarpaw
Copy link
Contributor Author

Or, if you want to keep AST diffing as it is, then I think that translating empty -- ^ to empty -- |, but deleting empty {- ^ -} would be satisfactory, because it would provide the escape hatch of using -- |/-- ^ as an alternative to {- ^ -}.

@amesgen
Copy link
Member

amesgen commented Sep 5, 2023

(Just FTR: There is additional discussion in #1068)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working comments Issues related to comment placement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants