tweag / ormolu

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

Constraints in data and newtype declarations are dropped. #1012

Closed fisx closed 1 year ago

fisx commented 1 year ago

Describe the bug OrmoluCounterexample.hs:

import Prelude
data IsString s => T s = T

then:

$ ormolu OrmoluCounterexample.hs
OrmoluCounterexample.hs
@@ -1,3 +1,4 @@
  import Prelude
- data IsString s => T s = T

+ data T s = T
+

  AST of input and AST of formatted code differ.
    at OrmoluCounterexample.hs:2:1-26
  Please, consider reporting the bug.
  To format anyway, use --unsafe.

$ ormolu --version
ormolu 0.6.0.1
using ghc-lib-parser 9.6.1.20230312
amesgen commented 1 year ago

This is semi-intentional as DatatypeContexts are deprecated:

This is widely considered a misfeature, and is going to be removed from the language.

It came up most recently in https://github.com/tweag/ormolu/pull/885#discussion_r952307846 and in #725. It would be very easy to support though if that would help in your usecase.

fisx commented 1 year ago

I wasn't aware it's deprecated. Is there an easy way to add that information to the error message? That would be ideal, otherwise maybe it's worth changing it so people won't trip over this again in the future.

I will just figure out how to not use DatatypeContext any more now, so you've already helped me, thanks!

amesgen commented 1 year ago

Hmm, warning about DatatypeContexts being deprecated sounds more of a job for GHC than for a formatter (it is already no longer enabled by default when using GHC2021); so I guess the easiest way to improve the current suboptimal experience when formatting DatatypeContexts is to actually support them as that is sufficiently simple, see #1014.