-
Notifications
You must be signed in to change notification settings - Fork 803
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: Smarter heuristics on incorrect return type of if / else branch #1106
Comments
Maybe the compiler should list the several types:
verbose output can also contain this:
|
Similarly to here, I'd like to suggest something like: |
I think we can do better than that; just saying they must all match is good, but telling the user what the current branches are and what one should be changed is better. |
Trying again: |
See my comment in the other issue - splitting up by "then" and "else" doesn't (IMHO) make much sense. Easier just to say "all branches (string, int)" etc. I think? |
@isaacabraham, I agree - but as I commented on #1105, I think it makes sense to mention the existence of the "then" and "else" branches without spliting the type listing. |
#1142 already gives: I think this is already pretty good. |
A little background: the type checker is adding type unification formulas basically expression by expression. The first formula that introduces a conflict will be reported. At this point we don't have enough TAST information to make assumptions about the other branches. I vote to close this specific request since the error message is already better than before and I don't see a way to improve that as described here without changing the design of the type checker. |
Yes, it's very hard to vote across the different branches. I will close this specific suggestion. |
I suspected that this would be the case - it's good that we've looked into this though and understand why it's not appropriate. Perhaps this can be revisited another time :-) |
What
If types are incompatible across all branches of an if / else expression, the return type of the first branch is used to determine the "correct" type. Given the following code sample, this behaviour, whilst consistent, is usually not what the user expects: -
error FS0001: This expression was expected to have type string but here has type int
In this case, all four branches that return an integer are flagged as having an error when it is much more likely that the first branch ("test") is in error.
Why
Changing the first type in a conditional causes all the other cases to break, when in fact it's normally the first one that's in error. A beginner will follow all the error messages raised by the compiler and not realise that they are actually all correct, but in fact the error is located in the one branch that the compiler has not flagged.
How
The compiler should ideally find the most "common" return type and flag that as the correct one and mark the other branch(es) as being in error.
The text was updated successfully, but these errors were encountered: