Skip to content

Inconsistent error message for incompatible return type in nested if / else #3146

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

Closed
isaacabraham opened this issue May 28, 2017 · 9 comments · Fixed by #3150
Closed

Inconsistent error message for incompatible return type in nested if / else #3146

isaacabraham opened this issue May 28, 2017 · 9 comments · Fixed by #3150

Comments

@isaacabraham
Copy link
Contributor

This code correctly does not compile. However, the error messages across both branches in the inner if / else are not consistent: -

let x = 1
if x = 1 then true
else
    if x = 2 then "A" // The expression was expected to have type 'bool' but here has type 'string'
    else "B" // All branches of an 'if' expression must have the same return type. This expression was expected to have type 'bool' but here has type 'string'.

I would have expected both errors to be the same, using second version (the F#4.1 improved error message).

cc: @forki

@forki
Copy link
Contributor

forki commented May 29, 2017

This is very hard to do since you don't know how deep this goes.

@forki
Copy link
Contributor

forki commented May 29, 2017

fsi only reports the first one:

image

@forki
Copy link
Contributor

forki commented May 29, 2017

image

would this help?

@forki
Copy link
Contributor

forki commented May 29, 2017

WIP: #3150

@isaacabraham
Copy link
Contributor Author

Would it be possible to use the same error messages in both places? I'm not really sure what "context type requirements" would mean to a newbie either.

@forki
Copy link
Contributor

forki commented May 30, 2017 via email

@isaacabraham
Copy link
Contributor Author

What are the different reasons? It's an expression that needs to give consistent results; one half returns bool and the other half returns some non-bool. Yes, it trickles down but why is it any different?

@forki
Copy link
Contributor

forki commented May 30, 2017

Seems I found a way:

image

@forki
Copy link
Contributor

forki commented May 30, 2017

as you can see in e2b9efd this evaluates the current context and passes the Else info down if we are already in else branch.

forki added a commit to forki/visualfsharp that referenced this issue May 31, 2017
KevinRansom pushed a commit that referenced this issue May 31, 2017
…3150)

* Give better error message when IF Statement has context requirements

* simplify code

* Update TypeChecker.fs

* something has a type

* Adding a test

* Transitive IF context

* Another test - fixes #3146

* fix wording
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants