-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Type If
and Match
in statement position as Unit
#5768
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
aa1fd9e
to
e8e6a0a
Compare
This is an alternative fix for scala#5750
e8e6a0a
to
9242e95
Compare
Is it more performant than the other fix? |
I am not sure what you mean by that. But it covers more cases than the previous fix. See the added bytecode tests. As a side effect, it also emits more (legitimate) "A pure expression does nothing in statement position" warnings (see the updated tests) |
This is fairly conservative as it only typechecks |
I think the original fix for #5750 is preferable to this version. It seems unsystematic to single out if and match to be typed with Unit expected type, but not other statements. So this means that (say) a function call The other possible design point would be to type all statements with Unit as expected type and to postulate an implicit conversion from T to Unit. It would get rid of a special case in adapt, so it might be interesting to check out. |
@allanrenucci If you want to work on the proposed other possible design point, we can leave this open. Otherwise I propose to close. |
This is a generalisation of the previous fix. In the previous fix, a function call
I am not sure what you are proposing. What would the implicit conversion synthesise? |
Something like:
but it could be implemented with compiler magic instead of having this conversion around. |
The issue with typing all statements with def foo(): String = ""
def bar: Unit = {
foo()
foo()
foo()
} After typer, would become: def foo(): String = ""
def bar = {
{ foo(); () }
{ foo(); () }
{ foo(); () }
} In that respect, special casing Replacing a special case in adapt with an implicit conversion seems orthogonal. |
If the implicit conversion is compiler-magic, we could avoid the creation of Unit trees. Just wrap the statements in a
and eliminate that later in a Miniphase. But it might well be that we are overthinking this. #5750 is fine, and we should stick with it if a general Unit dropping conversion is too complicated. But singling out If and Match for special treatment is worse than either. |
This is an alternative fix for #5750