Skip to content

Better error message for ifs that miss an else branch #8672

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

Merged
merged 3 commits into from
Apr 6, 2020

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Apr 5, 2020

ryanberckmans added a commit to ryanberckmans/scala-helpful-compiler-errors that referenced this pull request Apr 5, 2020
errorTree(tree, TypeMismatch(treeTp, pt, implicitFailure.whyNoConversion))
def missingElse = tree match
case If(_, _, elsep @ Literal(Constant(()))) if elsep.span.isSynthetic =>
"\nMaybe you are missing an else part for the conditional?"
Copy link
Member

Choose a reason for hiding this comment

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

If the Unit in the error message is the confusing part, maybe we could explain that:

Suggested change
"\nMaybe you are missing an else part for the conditional?"
"\nNote: an `if` without a corresponding `else` will always have type `Unit`"

Not sure if that's better.

Copy link
Contributor Author

@odersky odersky Apr 6, 2020

Choose a reason for hiding this comment

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

I think the single line is enough of a hint. An intelligent reader will connect the dots.

It's cleaner to handle the choice what addendum to print in the message
itself.
@odersky odersky merged commit 4b27dc1 into scala:master Apr 6, 2020
@odersky odersky deleted the fix-error-if branch April 6, 2020 17:29
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 this pull request may close these issues.

2 participants