Error messages suffer when applying a visible type application to a typoed/out-of-scope function
Summary
When working with the -XTypeApplications
extension, making a typo in the function name to which the type is being applied can lead to cryptic error messages. My team has observed this in production; new Haskellers can find it very confusing, since it’s not obvious that the message originates from a typo rather than a more fundamental type error.
I’m not sure what can be done about this, but Stephanie encouraged me to file this bug when I saw her at ICFP, so I figured it deserves a place in the tracker.
Steps to reproduce
The following simple program compiles correctly:
{-# LANGUAGE TypeApplications #-}
foo :: Num a => a
foo = 5
main :: IO ()
main = print (id @Int foo)
If we introduce an out-of-scope function into the id @Int foo
invocation, such as idd @Int foo
, we get the following message, which doesn’t indicate that the idd
function is out of scope:
Main.hs:12:15: error:
• Cannot apply expression of type ‘t1’
to a visible type argument ‘Int’
• In the first argument of ‘print’, namely ‘(idd @Int foo)’
In the expression: print (idd @Int foo)
In an equation for ‘main’: main = print (idd @Int foo)
|
12 | main = print (idd @Int foo)
| ^^^^^^^^^^^^
By contrast, if we insert a typo into the @Int
invocation, we get a good error message:
Main.hs:12:19: error:
Not in scope: type constructor or class ‘In’
Perhaps you meant one of these:
‘IO’ (imported from Prelude), ‘Int’ (imported from Prelude)
If we make a typo in the foo
parameter, we also get a good message:
Main.hs:12:23: error:
• Variable not in scope: oo :: Int
• Perhaps you meant one of these:
‘foo’ (line 9), ‘or’ (imported from Prelude)
|
12 | main = print (id @Int oo)
| ^^
Expected behavior
Given an invocation like idd @Int foo
, I would expect to see a message that warned Variable not in scope: idd
, rather than the somewhat-cryptic Cannot apply expression of type ‘t1’
message. As I mentioned above, I’m sure that this involves some sort of phasing restriction, so I understand if this is a nontrivial fix.
Environment
- GHC version used: 8.6.5
Optional:
- Operating System:
- System Architecture: