Skip to content

Commit 36249c7

Browse files
mbovelnmcbrochala
authored andcommitted
Allow discarding "Discarded non-Unit" warnings with : Unit
Co-Authored-By: nmc.borst <[email protected]> Co-Authored-By: Jędrzej Rochala <[email protected]>
1 parent bb136eb commit 36249c7

File tree

15 files changed

+91
-37
lines changed

15 files changed

+91
-37
lines changed

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2429,7 +2429,7 @@ class PureExpressionInStatementPosition(stat: untpd.Tree, val exprOwner: Symbol)
24292429
class PureUnitExpression(stat: untpd.Tree, tpe: Type)(using Context)
24302430
extends Message(PureUnitExpressionID) {
24312431
def kind = MessageKind.PotentialIssue
2432-
def msg(using Context) = i"Discarded non-Unit value of type ${tpe.widen}. You may want to use `()`."
2432+
def msg(using Context) = i"Discarded non-Unit value of type ${tpe.widen}. Add `: Unit` to discard silently."
24332433
def explain(using Context) =
24342434
i"""As this expression is not of type Unit, it is desugared into `{ $stat; () }`.
24352435
|Here the `$stat` expression is a pure statement that can be discarded.
@@ -3133,7 +3133,7 @@ class InlinedAnonClassWarning()(using Context)
31333133
class ValueDiscarding(tp: Type)(using Context)
31343134
extends Message(ValueDiscardingID):
31353135
def kind = MessageKind.PotentialIssue
3136-
def msg(using Context) = i"discarded non-Unit value of type $tp"
3136+
def msg(using Context) = i"discarded non-Unit value of type ${tp.widen}. Add `: Unit` to discard silently."
31373137
def explain(using Context) = ""
31383138

31393139
class UnusedNonUnitValue(tp: Type)(using Context)

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ object Typer {
7474
/** An attachment for GADT constraints that were inferred for a pattern. */
7575
val InferredGadtConstraints = new Property.StickyKey[core.GadtConstraint]
7676

77+
/** Indicates that an expression is explicitly ascribed to [[Unit]] type. */
78+
val AscribedToUnit = new Property.StickyKey[Unit]
79+
7780
/** An attachment on a Select node with an `apply` field indicating that the `apply`
7881
* was inserted by the Typer.
7982
*/
@@ -992,7 +995,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
992995
else tpt
993996
val expr1 =
994997
if isWildcard then tree.expr.withType(underlyingTreeTpe.tpe)
995-
else typed(tree.expr, underlyingTreeTpe.tpe.widenSkolem)
998+
else
999+
if underlyingTreeTpe.tpe.isRef(defn.UnitClass) then
1000+
untpd.unsplice(tree.expr).putAttachment(AscribedToUnit, ())
1001+
typed(tree.expr, underlyingTreeTpe.tpe.widenSkolem)
9961002
assignType(cpy.Typed(tree)(expr1, tpt), underlyingTreeTpe)
9971003
.withNotNullInfo(expr1.notNullInfo)
9981004
}
@@ -3009,7 +3015,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
30093015
else if (ctx.mode.is(Mode.Pattern))
30103016
typedUnApply(cpy.Apply(tree)(op, l :: r :: Nil), pt)
30113017
else {
3012-
val app = typedApply(desugar.binop(l, op, r), pt)
3018+
val app = typedApply(desugar.binop(l, op, r).withAttachmentsFrom(tree), pt)
30133019
if op.name.isRightAssocOperatorName && !ctx.mode.is(Mode.QuotedPattern) then
30143020
val defs = new mutable.ListBuffer[Tree]
30153021
def lift(app: Tree): Tree = (app: @unchecked) match
@@ -4221,9 +4227,14 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
42214227
// so will take the code path that decides on inlining
42224228
val tree1 = adapt(tree, WildcardType, locked)
42234229
checkStatementPurity(tree1)(tree, ctx.owner, isUnitExpr = true)
4224-
if (!ctx.isAfterTyper && !tree.isInstanceOf[Inlined] && ctx.settings.Whas.valueDiscard && !isThisTypeResult(tree)) {
4230+
4231+
if ctx.settings.Whas.valueDiscard
4232+
&& !ctx.isAfterTyper
4233+
&& !tree.isInstanceOf[Inlined]
4234+
&& !isThisTypeResult(tree)
4235+
&& !tree.hasAttachment(AscribedToUnit) then
42254236
report.warning(ValueDiscarding(tree.tpe), tree.srcPos)
4226-
}
4237+
42274238
return tpd.Block(tree1 :: Nil, unitLiteral)
42284239
}
42294240

@@ -4578,6 +4589,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
45784589
// sometimes we do not have the original anymore and use the transformed tree instead.
45794590
// But taken together, the two criteria are quite accurate.
45804591
missingArgs(tree, tree.tpe.widen)
4592+
case _ if tree.hasAttachment(AscribedToUnit) =>
4593+
// The tree was ascribed to `Unit` explicitly to silence the warning.
4594+
()
45814595
case _ if isUnitExpr =>
45824596
report.warning(PureUnitExpression(original, tree.tpe), original.srcPos)
45834597
case _ =>

tests/neg-custom-args/captures/real-try.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- [E190] Potential Issue Warning: tests/neg-custom-args/captures/real-try.scala:30:4 ----------------------------------
22
30 | b.x
33
| ^^^
4-
| Discarded non-Unit value of type () -> Unit. You may want to use `()`.
4+
| Discarded non-Unit value of type () -> Unit. Add `: Unit` to discard silently.
55
|
66
| longer explanation available when compiling with `-explain`
77
-- Error: tests/neg-custom-args/captures/real-try.scala:12:2 -----------------------------------------------------------

tests/neg/i13091.check

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Error: tests/neg/i13091.scala:7:16 ----------------------------------------------------------------------------------
2+
7 |def test = (new Foo): Unit // error: class Foo is marked @experimental ...
3+
| ^^^
4+
| class Foo is marked @experimental
5+
|
6+
| Experimental definition may only be used under experimental mode:
7+
| 1. in a definition marked as @experimental, or
8+
| 2. an experimental feature is imported at the package level, or
9+
| 3. compiling with the -experimental compiler flag.

tests/neg/i13091.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ import annotation.experimental
44

55
@experimental class Foo
66

7-
def test: Unit = new Foo // error: class Foo is marked @experimental ...
7+
def test = (new Foo): Unit // error: class Foo is marked @experimental ...

tests/neg/i18408a.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
-- [E190] Potential Issue Warning: tests/neg/i18408a.scala:3:15 --------------------------------------------------------
88
3 |def test1 = fa(42)
99
| ^^
10-
| Discarded non-Unit value of type Int. You may want to use `()`.
10+
| Discarded non-Unit value of type Int. Add `: Unit` to discard silently.
1111
|
1212
| longer explanation available when compiling with `-explain`
1313
-- [E129] Potential Issue Warning: tests/neg/i18408a.scala:4:16 --------------------------------------------------------

tests/neg/i18408b.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
-- [E190] Potential Issue Warning: tests/neg/i18408b.scala:3:15 --------------------------------------------------------
88
3 |def test1 = fa(42)
99
| ^^
10-
| Discarded non-Unit value of type Int. You may want to use `()`.
10+
| Discarded non-Unit value of type Int. Add `: Unit` to discard silently.
1111
|
1212
| longer explanation available when compiling with `-explain`
1313
-- [E129] Potential Issue Warning: tests/neg/i18408b.scala:4:16 --------------------------------------------------------

tests/neg/i18408c.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
-- [E190] Potential Issue Warning: tests/neg/i18408c.scala:3:15 --------------------------------------------------------
88
3 |def test1 = fa(42)
99
| ^^
10-
| Discarded non-Unit value of type Int. You may want to use `()`.
10+
| Discarded non-Unit value of type Int. Add `: Unit` to discard silently.
1111
|
1212
| longer explanation available when compiling with `-explain`
1313
-- [E129] Potential Issue Warning: tests/neg/i18408c.scala:4:16 --------------------------------------------------------

tests/neg/spaces-vs-tabs.check

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,3 @@
2828
| The start of this line does not match any of the previous indentation widths.
2929
| Indentation width of current line : 1 tab, 2 spaces
3030
| This falls between previous widths: 1 tab and 1 tab, 4 spaces
31-
-- [E190] Potential Issue Warning: tests/neg/spaces-vs-tabs.scala:13:7 -------------------------------------------------
32-
13 | 1
33-
| ^
34-
| Discarded non-Unit value of type Int. You may want to use `()`.
35-
|
36-
| longer explanation available when compiling with `-explain`

tests/neg/spaces-vs-tabs.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ object Test:
1010
object Test2:
1111

1212
if true then
13-
1
13+
()
1414
else 2 // error
1515

0 commit comments

Comments
 (0)