Skip to content
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

Merged
merged 1 commit into from
Nov 22, 2024

Conversation

mbovel
Copy link
Member

@mbovel mbovel commented Nov 11, 2024

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:

def test: Unit =
  (1 + 1): Unit // no warning
  1 + 1 // warn

Closes #21557.

@mbovel mbovel requested a review from rochala November 11, 2024 21:36
else typed(tree.expr, underlyingTreeTpe.tpe.widenSkolem)
else
if underlyingTreeTpe.tpe.isRef(defn.UnitClass) then
untpd.unsplice(tree.expr).putAttachment(AscribedToUnit, ())
Copy link
Member Author

@mbovel mbovel Nov 11, 2024

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
()
Copy link
Member Author

@mbovel mbovel Nov 11, 2024

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 ...
Copy link
Member Author

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)
Copy link
Member Author

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.

@mbovel mbovel added the fasttrack Simple fix. Reviewer should merge or apply additional changes directly. label Nov 11, 2024
Copy link
Contributor

@rochala rochala left a 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."
Copy link
Contributor

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

@mbovel
Copy link
Member Author

mbovel commented Nov 13, 2024

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

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?

@mbovel mbovel requested a review from dwijnand November 13, 2024 16:18
@mbovel mbovel removed the fasttrack Simple fix. Reviewer should merge or apply additional changes directly. label Nov 13, 2024
@mbovel mbovel merged commit 8fb27f1 into scala:main Nov 22, 2024
29 checks passed
@WojciechMazur WojciechMazur added this to the 3.6.3 milestone Nov 25, 2024
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.

Unable to discard non-unit value using : Unit
4 participants