-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #5750: Type then part of If with no else part as Unit #5751
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, that was quick! Doing it in Typer seems like a good idea.
Before merging, please delete the ()
and associated comment in https://github.com/dotty-staging/dotty/blob/2ee9994efaf6b3b8b97377002c4a237fe9b5ce00/compiler/src/dotty/tools/dotc/ast/Positioned.scala#L56
Actually I am not sure this is the correct fix. Consider: class Test {
def foo: String = ""
def test(cond: Boolean): Int = {
if (cond) foo else foo
1
}
} This compiles to: public class Test {
public String foo() {
return "";
}
public int test(boolean cond) {
String string = cond ? this.foo() : this.foo();
return 1;
}
} which is not optimal. I believe the correct fix it to typecheck |
Careful though, we don't want |
Why not? This is already the case: def foo: String = if (...) ... else ... // typed with `String` as expected type
val x = if (...) ... else ... // typed with no expected type So it seems reasonable to me to implicitly type an And we already have expressions that are implicitly typed with an expected type when in a certain position: throw expr // typed as `Throwable`
if (cond) // cond typed as `Boolean` |
hmm, ok, maybe it's not a big deal. |
23243b3
to
0ab02f0
Compare
I think we tried checking statements with |
0ab02f0
to
b6dc6bc
Compare
Comments addressed. Maybe we should keep the issue open since it doesn't solve the problem for |
No description provided.