Skip to content

Improve arity error messages#15861

Merged
sandersn merged 10 commits into
masterfrom
improve-arity-error
May 18, 2017
Merged

Improve arity error messages#15861
sandersn merged 10 commits into
masterfrom
improve-arity-error

Conversation

@sandersn
Copy link
Copy Markdown
Member

@sandersn sandersn commented May 15, 2017

Add a specific errors for incorrect number of arguments or of type arguments in resolveCall. I'm not sure how we lived without this before.


Update from @DanielRosenwasser on August 3, 2017: fixes #6045.

sandersn added 4 commits May 16, 2017 09:48
The old "supplied parameters do not match any call signature" was
either inaccurate, redundant or vague. The previous commits fix the
vagueness problem. This commit fixes the inaccuracy and redundancy.

1. When there are NO candidates, the error should say so. (This only
happens once in our tests, when calling `super()` with a base class of
type `any` in a JS file.)
2. When the call is to a decorator, `resolveCall` already receives a
specific fallback error message from the decorator handling code. Adding
"supplied parameters do not match ..." is not helpful.

I also cleaned up the new code a bit after I noticed that all the error
creation functions take `string | number`, so I didn't need calls to
`toString` in my code.
@sandersn
Copy link
Copy Markdown
Member Author

@RyanCavanaugh @DanielRosenwasser mind taking a look? This PR is pain to keep up to date because it changes so many error numbers ...

Comment thread src/compiler/checker.ts Outdated
max = Math.max(max, sig.parameters.length);
}
const paramMessage = some(signatures, sig => sig.hasRestParameter) ? "at least " + min :
///////////
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just add a newline?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not newlines? 🦀 👽 🦑

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Removed in the current version)

Comment thread src/compiler/diagnosticMessages.json Outdated
"category": "Error",
"code": 2555
},
"Expected at least {0} arguments, but got a minimum of {1}.": {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this mean? I can't tell from the message nor the condition in the code. Also, every error message we report this on seems to have "a minimum of 0"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used "a minimum of" for spreads to indicate that a spread adds 0 or more arguments to those already present. After reading the example you might have a better idea for the wording.

Here's an example that would give "a minimum of 1":

declare function f(first, second, ...rest): any[]
declare const car: number
declare const cdr: number[]
f(car, ...cdr) // Expected at least 2 arguments, but got a minimum of 1.

Note that this is legal Javascript and could succeed at runtime, but we can't prove that it will because cdr might be empty.

Comment thread src/compiler/diagnosticMessages.json Outdated
"code": 2553
},
"Expected {0} type arguments, but got {1}.": {
"Expected at least {0} arguments, but got {1}.": {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Expected {0} type arguments, but got {1}." is closer to the old message. I'd argue that the error code didn't need to change. Consider switching this with the message assigned to code 2557

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the diff is wonky here. All these messages are new, so there aren't any codes to reuse.

Comment thread src/compiler/diagnosticMessages.json Outdated
"category": "Error",
"code": 2556
},
"Expected {0} type arguments, but got {1}.": {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In some cases, type arguments also need "at least" some number. Is that something that would be done in a different PR?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"at least" indicates a rest argument, which TS doesn't have yet. I use a range, generated in the code, for optional [type] arguments, so you'd see a message like "Expected 1-3 type arguments, but got 4".

@sandersn
Copy link
Copy Markdown
Member Author

@DanielRosenwasser I replied to your now-hidden replies, but my replies are now also hidden. (!)

@sandersn sandersn merged commit ada39c5 into master May 18, 2017
@sandersn sandersn deleted the improve-arity-error branch May 18, 2017 22:26
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issue a useful error when calling a function with too many arguments

4 participants