Incomplete inferred types showed for type holes since GHC 9.8
Summary
Since ghc 9.8 the inferred types of wildcards (sometimes?) don't contains type class constraints.
Steps to reproduce
Consider this code:
$ cat A.hs
f :: _
f x y = x + y
When compiled with GHC 9.6, I get this error:
A.hs:1:6: error: [GHC-88464]
• Found type wildcard ‘_’
standing for ‘Integer -> Integer -> Integer’
To use the inferred type, enable PartialTypeSignatures
• In the type signature: f :: _
|
1 | f :: _
| ^
But when compiled with GHC 9.8.2 I get this (notice the inferred type doesn't contain Num a constraint):
A.hs:1:6: error: [GHC-88464]
• Found type wildcard ‘_’ standing for ‘a -> a -> a’
Where: ‘a’ is a rigid type variable bound by
the inferred type of f :: a -> a -> a
at A.hs:2:1-13
To use the inferred type, enable PartialTypeSignatures
• In the type signature: f :: _
|
1 | f :: _
| ^
A.hs:2:11: error: [GHC-39999]
• No instance for ‘Num a’ arising from a use of ‘+’
Possible fix:
add (Num a) to the context of the inferred type of f :: a -> a -> a
• In the expression: x + y
In an equation for ‘f’: f x y = x + y
|
2 | f x y = x + y
| ^
Expected behavior
I would expect that if I use the inferred type, I will get code that type checks (e.g. Num a => a -> a -> a
or Integer -> Integer -> Integer
).
This is breaking haskell-language-server functionality, which uses the type displayed in the warning (a -> a -> a
) in that it leads it to creation of code actions (small popups you can use to locally "fix" your code) that break user's code as in this example:
See this "broken" test case for illustration: https://github.com/haskell/haskell-language-server/blob/c50a0e118b8570b2bd405de6ff5be6a54926aa5d/plugins/hls-refactor-plugin/test/Main.hs#L665-L673
Environment
- GHC version used: 9.8.2
Optional:
- Operating System: Fedora Linux 39
- System Architecture: x86_64