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: Missing "else" branch for if / elif expression #1157

Closed
Tracked by #1103
isaacabraham opened this issue May 6, 2016 · 6 comments
Closed
Tracked by #1103

Comments

@isaacabraham
Copy link
Contributor

What

Related to #1104, but not the same. The following code leads to an especially cryptic error message from the compiler:

let x = 10
let y =
   if x < 10 then "test"
   elif x < 20 then "foo"

As if is an expression in F#, you need to provide an else branch as well. The error message shown below is related to the elif branch above (but not the if branch): -

error FS0001: This expression was expected to have type
    string    
but here has type
    unit

Why

What's especially confusing about this error (in addition to the points put forward in #1104) is that the error suggests that the type being returned from this branch is a unit when in fact it is a string. Even worse, if you comment out the elif branch so that you just have the if branch, the types in the error message become inverted!

How

As per #1104.

@forki
Copy link
Contributor

forki commented May 6, 2016

I think I already know why this is reported in wrong order. I think I
fixed that by changing order of the equations in that related pr. But
@dsyme said I should be change that back. So we should add more test cases
and then fix the test cases and think really hard if the order is right.

But first things first. I will add tests for this to my other pr. Hopefully
on Monday.
On May 6, 2016 17:56, "Isaac Abraham" notifications@github.com wrote:

What

Related to #1104 #1104,
but not the same. The following code leads to an especially cryptic error
message from the compiler:

let x = 10let y =
if x < 10 then "test"
elif x < 20 then "foo"

As if is an expression in F#, you need to provide an else branch as well.
The error message shown below is related to the elif branch above (but
not the if branch): -

error FS0001: This expression was expected to have type
string
but here has type
unit

Why

What's especially confusing about this error (in addition to the points
put forward in #1104
#1104) is that the
error suggests that the type being returned from this branch is a unit when
in fact it is a string. Even worse, if you comment out the elif branch so
that you just have the if branch, the types in the error message become
inverted!
How

As per #1104 #1104.


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#1157

@KevinRansom
Copy link
Member

@isaacabraham, @forki

With Steffen's PR It now yields this error: I think the advice portion is accurate and relevant.

What do you think?

> let x = 10
- let y =
-    if x < 10 then "test"
-    elif x < 20 then "foo";;

     elif x < 20 then "foo";;
  ---^^^^^^^^^^^^^^^^^^^^^^
stdin(4,4): error FS0001: The 'if' expression is missing an 'else' branch. The 'then' branch has 
type 'string'. Because 'if' is an expression, and not a statement, add an 'else' branch which 
returns a value of the same type.
>

@isaacabraham
Copy link
Contributor Author

It's definitely a big improvement :-) My only question (and this is across all the if / else issues) is what is a "then" branch? Surely it should be the "if" branch as there are several occurrence of "then" within an if / else expression.

@isaacabraham
Copy link
Contributor Author

@forki Just noticed that this only works for simple values. If there's a function in between, it fails e.g.

let foo x = x + 1
let x = if true then (foo 5)

gives the error:

error FS0001: This expression was expected to have type 'unit' but here has type 'int'

If I remove the foo call e.g.

let x = if true then 5

Then it works properly:

error FS0001: The 'if' expression is missing an 'else' branch. The 'then' branch has type 'int'. Because 'if' is an expression, and not a statement, add an 'else' branch which returns a value of the same type.

Both errors have the same error code which suggests that the compiler understands it's the same error - it's just the it's getting the text from a different place or something?

@forki
Copy link
Contributor

forki commented Oct 16, 2017

image

lol it's the ()

@forki
Copy link
Contributor

forki commented Oct 16, 2017

image

function application is not needed

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

No branches or pull requests

3 participants