Skip to content

Regression in code action "add xxx in context type signature" with ghc 9.10 when haddock comment appears in type signature #4648

Open
@guibou

Description

@guibou

The code action which adds a constraint in type signatures produces invalid code when the type signature contains comment.

However it seems that it depends on ghc version.

Your environment

Which OS do you use?

Linux Nixos

Which version of GHC do you use and how did you install it?

I observed first the problem using ghc 9.12.2 on work codebase with a recent dev version of HLS.

How is your project built (alternative: link to the project)?

Which LSP client (editor/plugin) do you use?

Neovim+neovim lsp

Which version of HLS do you use and how did you install it?

Details about GHC versions and HLS later in this description

Have you configured HLS in any way (especially: a hie.yaml file)?

Standalone file (but can be reproduced on projects with hie files)

Steps to reproduce

For example, the following piece of code:

foo :: 
  (Applicative m) =>
  -- | This is a comment
  x -> 
  m x
foo x = pure (x + 1)

Produced the following error:

 Could not deduce ‘Num x’ arising from a use of ‘+’
  from the context: Applicative m
    bound by the type signature for:
               foo :: forall (m :: * -> *) x. Applicative m => x -> m x
    at /home/guillaume/HLSAddConstraint.hs:(1,1)-(5,5)
  Possible fix:
    add (Num x) to the context of
      the type signature for:
        foo :: forall (m :: * -> *) x. Applicative m => x -> m x

Associated with code action "Add Num x to the context of the type signature for foo".

Using the same HLS version, 2.10.0.0, I observe different behavior with different GHC version:

Expected behaviour

  • With ghc 9.8.4 the code action returns the correct expected result:
foo :: 
  (Applicative m, Num x) =>
  -- | This is a comment
  x ->
  m x
foo x = pure (x + 1)

Actual behavior

  • With ghc 9.10, I obtain an invalid source file:
foo :: 
  (Applicative m,
  -- | This is a comment Num x) =>

  x ->
  m x
foo x = pure (x + 1)

See how Num x) => was moved at the end of the haddock comment

Debug information

I've checked with HLS 29b2ecb (current master at the time of authoring this issue) and GHC 9.12.2 and I can confirm that the code produced is invalid with this configuration.

To save a bit on build time, I had not tested master branch with more recent GHC version. HLS behaves differently with the same HLS (2.10.0.0) but different GHC (9.8 vs 9.10) and the issue is still there with the most recent GHC and HLS version availables. I may test on different GHC later to refine / understand if I find time to build.

I'll have a look to see if I understand the bug and if I can provide a quick fix. First I'll add a test.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: needs triagetype: bugSomething isn't right: doesn't work as intended, documentation is missing/outdated, etc..

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions