Opened 5 months ago

Closed 3 months ago

#13902 closed bug (fixed)

Misleading function arity mismatch error with TypeApplications

Reported by: RyanGlScott Owned by:
Priority: normal Milestone: 8.4.1
Component: Compiler (Type checker) Version: 8.0.1
Keywords: TypeApplications Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Poor/confusing error message Test Case: typecheck/should_fail/T13902
Blocked By: Blocking:
Related Tickets: Differential Rev(s): Phab:D3868
Wiki Page:

Description (last modified by RyanGlScott)

{-# LANGUAGE TypeApplications #-}

f :: a -> a
f x = x

g :: Int
g = f @Int 42 5
GHCi, version 8.3.20170614: http://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /home/rgscott/.ghci
[1 of 1] Compiling Main             ( Bug.hs, interpreted )

Bug.hs:7:5: error:
    • Couldn't match expected type ‘Integer -> Int’
                  with actual type ‘Int’
    • The function ‘f’ is applied to three arguments,
      but its type ‘Int -> Int’ has only one
      In the expression: f @Int 42 5
      In an equation for ‘g’: g = f @Int 42 5
  |
7 | g = f @Int 42 5
  |     ^^^^^^^^^^^

That error message is quite confusing to read, since it reports that:

  • f is applied to three arguments, which includes a visible type application
  • f only has one argument, which excludes the visible type application

We ought to be able to do better. My suggestion would be to report this instead:

    • The expression ‘f @Int’ is applied to two arguments,
      but its type ‘Int -> Int’ has only one
      In the expression: f @Int 42 5
      In an equation for ‘g’: g = f @Int 42 5

Although I'm sure there are other ways the same information could be conveyed (e.g., The function ‘f’ is applied to two value arguments).

Change History (6)

comment:1 Changed 5 months ago by Iceland_jack

I like treating the function and visible type application as a whole. The type of f @Int is Int -> Int and takes a single argument but that is not the type of f

comment:2 Changed 5 months ago by vanto

We ought to be able to do better.

Yes and it's good to share ideas.
I'd like to make a suggestion. It is not worth writing

In the expression: f @Int 42 5
In an equation for ‘g’: g = f @Int 42 5

Since the compiler already indicates the place that has the expression and equation.

 |
7 | g = f @Int 42 5
  |     ^^^^^^^^^^^

Both sentences make redundancy and does not serve better to explain. Too much explanation is detrimental to the explanation.

comment:3 Changed 4 months ago by RyanGlScott

Description: modified (diff)

comment:4 Changed 3 months ago by RyanGlScott

Differential Rev(s): Phab:D3868
Status: newpatch

comment:5 Changed 3 months ago by Ryan Scott <ryan.gl.scott@…>

In 84760976/ghc:

Revise function arity mismatch errors involving TypeApplications

Summary:
Currently, whenever you apply a function to too many arguments and
some of those arguments happen to be visible type applications, the error
message that GHC gives is rather confusing. Consider the message you receive
when typechecking `id @Int 1 2`:

```
The function `id` is applied to three arguments,
but its type `Int -> Int` has only one
```

This is baffling, since the two lines treat the visible type argument `@Int`
differently. The top line ("applied to three arguments") includes `@Int`,
whereas the bottom line ("has only one") excludes `@Int` from consideration.

There are multiple ways one could fix this, which I explain in an addendum to
`Note [Herald for matchExpectedFunTys]`. The approach adopted here is to change
the herald of this error message to include visible type arguments, and to
avoid counting them in the "applied to n arguments" part of the error. The end
result is that the new error message for `id @Int 1 2` is now:

```
The expression `id @Int` is applied to two arguments,
but its type `Int -> Int` has only one
```

Test Plan: make test TEST=T13902

Reviewers: goldfire, austin, bgamari

Reviewed By: goldfire

Subscribers: rwbarton, thomie

GHC Trac Issues: #13902

Differential Revision: https://phabricator.haskell.org/D3868

comment:6 Changed 3 months ago by RyanGlScott

Milestone: 8.4.1
Resolution: fixed
Status: patchclosed
Test Case: typecheck/should_fail/T13902
Note: See TracTickets for help on using tickets.