-
Notifications
You must be signed in to change notification settings - Fork 1.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 discarding "Discarded non-Unit" warnings with : Unit
#21927
Conversation
else typed(tree.expr, underlyingTreeTpe.tpe.widenSkolem) | ||
else | ||
if underlyingTreeTpe.tpe.isRef(defn.UnitClass) then | ||
untpd.unsplice(tree.expr).putAttachment(AscribedToUnit, ()) |
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.
untpd.unsplice
is needed to ignore parentheses (so that (1 + 1): Unit
also silents the warning, not only 1 + 1: Unit
).
@@ -10,6 +10,6 @@ object Test: | |||
object Test2: | |||
|
|||
if true then | |||
1 | |||
() |
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.
Avoids the "non-Unit" warning, that is not directly related to this test.
@@ -4,4 +4,4 @@ import annotation.experimental | |||
|
|||
@experimental class Foo | |||
|
|||
def test: Unit = new Foo // error: class Foo is marked @experimental ... | |||
def test = (new Foo): Unit // error: class Foo is marked @experimental ... |
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.
Avoids the "non-Unit" warning, that is not directly related to this test.
@@ -3377,7 +3383,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer | |||
else if (ctx.mode.is(Mode.Pattern)) | |||
typedUnApply(cpy.Apply(tree)(op, l :: r :: Nil), pt) | |||
else { | |||
val app = typedApply(desugar.binop(l, op, r), pt) | |||
val app = typedApply(desugar.binop(l, op, r).withAttachmentsFrom(tree), pt) |
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.
Needed to add withAttachmentsFrom(tree)
here so that we don't throw away attachments when desugaring binary operations.
Co-Authored-By: nmc.borst <[email protected]> Co-Authored-By: Jędrzej Rochala <[email protected]>
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.
I think that I shouldn't review this as I was also working on this and I'm not sure if there is better way than attachment
@@ -3173,7 +3173,7 @@ class InlinedAnonClassWarning()(using Context) | |||
class ValueDiscarding(tp: Type)(using Context) | |||
extends Message(ValueDiscardingID): | |||
def kind = MessageKind.PotentialIssue | |||
def msg(using Context) = i"discarded non-Unit value of type $tp" | |||
def msg(using Context) = i"discarded non-Unit value of type ${tp.widen}. Add `: Unit` to discard silently." |
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.
Let's create actionable diagnostics in the next spree for both diagnostics
Mmh, yeah, I thought two pairs of core-contributor eyes were enough, but it might indeed be even better to get an independent review. @dwijnand woud you have time to have a look? |
Continuation of the work done by @nmcb and @rochala during the Oct 20th spree.
This PR adds support for discarding "non-Unit" expressions by explicitly casting them to
Unit
:Closes #21557.