Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Error Reporting: help with ambiguity between subtraction and negation #13014

Open
Tracked by #1103
jasiozet opened this issue Apr 19, 2022 · 4 comments
Open
Tracked by #1103
Labels
Area-Diagnostics mistakes and possible improvements to diagnostics Feature Request
Milestone

Comments

@jasiozet
Copy link

Problem / Potential Improvement

F# treats:
2-1(no spaces)
and
2 - 1(spaces surround minus sign from both sides)
the same, but
2 -1 (space before minus sign, but not after).
is treated differently.
This can lead to a very easy to make, but hard to get right error messages for newcomers to the language.
(Source: I was/still am this newcomer)

Code examples

Example 1:

let minusOne x =  x -1
let twoPlusThreeMinusOne = 2 + minusOne 3

Would lead to:
error FS0001: This expression was expected to have type 'int -> 'a' but here has type 'int'

Example 2:

let getListWithoutFirstAndLastElement list =
    let l = List.length list
    list[1 .. l -2]

Would lead to:
error FS0003: This value is not a function and cannot be applied.

Why


For newcomers to the language, the difference between -1 and - 1 is very odd.
I know, because just recently I was that newcomer and this is the error that was the weirdest for me to debug.

How to improve

I assume treating "-1" as "- 1" in those instances is out of the question.
So the best thing I can think of right now for Example 2 with error like this is explanation that -1 is treated as a negation not as subtraction.

error FS0003: This value is not a function and cannot be applied.
warning: list[1 .. List.length list -1]
-1 is a negation not subtract operation. To get subtraction use „ - „

I don’t have a good idea how to fix example 1.

Related: #1103

@baronfel
Copy link
Member

This one is pretty difficult - so much so that in Ionide we added a code-fix to fix this automatically. It would be good to be able to have a more useful error message to make that codefix less necessary.

@isaacabraham
Copy link
Contributor

I think @cartermp spent some time looking at this as well.

@cartermp
Copy link
Contributor

Yeah, there's also a code fixer in VS that handles this. I haven't looked into improving the actual compiler error message, though.

@jasiozet
Copy link
Author

I think it would be better to this on top level instead of in the tooling (VS and VSCode have some support for this).

@dsyme dsyme added the Area-Diagnostics mistakes and possible improvements to diagnostics label Apr 20, 2022
@vzarytovskii vzarytovskii moved this to Not Planned in F# Compiler and Tooling Jun 17, 2022
@vzarytovskii vzarytovskii added this to the Backlog milestone Oct 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Diagnostics mistakes and possible improvements to diagnostics Feature Request
Projects
Status: New
Development

No branches or pull requests

6 participants