-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Allow suppressing value-discard warning via type ascription to Unit
#7563
Allow suppressing value-discard warning via type ascription to Unit
#7563
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.
Nice one. Thanks, Som.
/rebuild 38d0b82 |
Going to rebase to give my commit a fresh SHA (can't get the CLA validation to pass...) and to squash Nth's suggestion. |
Also use the Unit ascription trick for other warning, where it's not as useful but is uniform.
f9e195d
to
f18fec7
Compare
I'm going to stop checking the box that allows pushy pushes. It's |
Apologies. It was "no warn" on the line below so I assumed it wasn't intentional. |
@dwijnand I was just rationalizing. Happy New Year to |
All I wanted for Xmas was a happy jenkins. The socks are nice too, I guess. (See also scala/scala-dev#594) |
Review by @scala/team-core-scala? |
we're "@scala/lightbend" now, I see |
Let's use |
Thanks for the review and merge (and the pairing session!) |
Just saw this merge and i thinks it's not a good idea to use this syntax for explicit discarding.
|
Note that this PR only influences whether we emit an optional warning. Even before this PR, |
To answer 2.:
|
Hm i think What would happen when writing this:
And to you answer for 2. :
|
Agreeing with @maphi here. To my knowledge, value discarding has always been just a quirk to help those coming from the Java However, I vote for not cementing the decision of the value-discard feature. This special rule is not worth the gain at all. If possible, it should be removed. |
I think there must be a misunderstanding here somewhere. Again, this PR is about warning suppression. Warnings do not influence the outcome of type checking or codegen. Also, a type ascription is not an upcast. It's also not just a hint -- it tells the compiler which type the ascribed expression has to meet, and this may trigger conversions (e.g. value discarding, implicit conversion or numeric widening). |
Ok agreeing that value discard is not different from implicit conversion. For me it feels like this MR takes same safety apart to not accidently discard something by ascribing it with A good way might be what @valenterry said. Configuring if value discard should happen at all would solve this. How is this implemented right now. Is there an implicit conversion Any -> Unit in Predef? |
It's in the spec: https://www.scala-lang.org/files/archive/spec/2.12/06-expressions.html#value-discarding, implementation: scala/src/compiler/scala/tools/nsc/typechecker/Typers.scala Lines 1119 to 1121 in 38cd84d
|
I agree that there is some confusion about the mechanisms involved, but I understand that folks are confused by As an example of what's been implemented previously, it doesn't warn when discarding
The current change is eliminate these warnings:
The test example is ignoring the boolean returned by |
There's an old thread where I commented naively. There the problem was unwittingly There's a blog about unwittingly (Edit: internet search engine reminded me scala/bug#9822)
That's because of the implicit arg, but I haven't looked closely at what it's doing yet:
Probably the tree attachment is lost during a tree copy during adaptation. Anyway, strict mode is at #7716 |
Unit
Extend
(e: Unit)
to mean no-warn for both value discard and expression in statement position warnings.