Skip to content

Commit 46cd256

Browse files
mbovelnmcbrochala
committed
Allow discarding "Discarded non-Unit" warnings with : Unit
Co-Authored-By: nmc.borst <1698211+nmcb@users.noreply.github.com> Co-Authored-By: Jędrzej Rochala <48657087+rochala@users.noreply.github.com>
1 parent 1947828 commit 46cd256

File tree

15 files changed

+90
-51
lines changed

15 files changed

+90
-51
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,7 +2469,7 @@ class PureExpressionInStatementPosition(stat: untpd.Tree, val exprOwner: Symbol)
24692469
class PureUnitExpression(stat: untpd.Tree, tpe: Type)(using Context)
24702470
extends Message(PureUnitExpressionID) {
24712471
def kind = MessageKind.PotentialIssue
2472-
def msg(using Context) = i"Discarded non-Unit value of type ${tpe.widen}. You may want to use `()`."
2472+
def msg(using Context) = i"Discarded non-Unit value of type ${tpe.widen}. Add `: Unit` to discard silently."
24732473
def explain(using Context) =
24742474
i"""As this expression is not of type Unit, it is desugared into `{ $stat; () }`.
24752475
|Here the `$stat` expression is a pure statement that can be discarded.
@@ -3173,7 +3173,7 @@ class InlinedAnonClassWarning()(using Context)
31733173
class ValueDiscarding(tp: Type)(using Context)
31743174
extends Message(ValueDiscardingID):
31753175
def kind = MessageKind.PotentialIssue
3176-
def msg(using Context) = i"discarded non-Unit value of type $tp"
3176+
def msg(using Context) = i"discarded non-Unit value of type ${tp.widen}. Add `: Unit` to discard silently."
31773177
def explain(using Context) = ""
31783178

31793179
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
@@ -81,6 +81,9 @@ object Typer {
8181
/** Indicates that a definition was copied over from the parent refinements */
8282
val RefinementFromParent = new Property.StickyKey[Unit]
8383

84+
/** Indicates that an expression is explicitly ascribed to [[Unit]] type. */
85+
val AscribedToUnit = new Property.StickyKey[Unit]
86+
8487
/** An attachment on a Select node with an `apply` field indicating that the `apply`
8588
* was inserted by the Typer.
8689
*/
@@ -1193,7 +1196,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
11931196
else tpt
11941197
val expr1 =
11951198
if isWildcard then tree.expr.withType(underlyingTreeTpe.tpe)
1196-
else typed(tree.expr, underlyingTreeTpe.tpe.widenSkolem)
1199+
else
1200+
if underlyingTreeTpe.tpe.isRef(defn.UnitClass) then
1201+
untpd.unsplice(tree.expr).putAttachment(AscribedToUnit, ())
1202+
typed(tree.expr, underlyingTreeTpe.tpe.widenSkolem)
11971203
assignType(cpy.Typed(tree)(expr1, tpt), underlyingTreeTpe)
11981204
.withNotNullInfo(expr1.notNullInfo)
11991205
}
@@ -3377,7 +3383,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
33773383
else if (ctx.mode.is(Mode.Pattern))
33783384
typedUnApply(cpy.Apply(tree)(op, l :: r :: Nil), pt)
33793385
else {
3380-
val app = typedApply(desugar.binop(l, op, r), pt)
3386+
val app = typedApply(desugar.binop(l, op, r).withAttachmentsFrom(tree), pt)
33813387
if op.name.isRightAssocOperatorName && !ctx.mode.is(Mode.QuotedExprPattern) then
33823388
val defs = new mutable.ListBuffer[Tree]
33833389
def lift(app: Tree): Tree = (app: @unchecked) match
@@ -4581,9 +4587,14 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
45814587
// so will take the code path that decides on inlining
45824588
val tree1 = adapt(tree, WildcardType, locked)
45834589
checkStatementPurity(tree1)(tree, ctx.owner, isUnitExpr = true)
4584-
if (!ctx.isAfterTyper && !tree.isInstanceOf[Inlined] && ctx.settings.Whas.valueDiscard && !isThisTypeResult(tree)) {
4590+
4591+
if ctx.settings.Whas.valueDiscard
4592+
&& !ctx.isAfterTyper
4593+
&& !tree.isInstanceOf[Inlined]
4594+
&& !isThisTypeResult(tree)
4595+
&& !tree.hasAttachment(AscribedToUnit) then
45854596
report.warning(ValueDiscarding(tree.tpe), tree.srcPos)
4586-
}
4597+
45874598
return tpd.Block(tree1 :: Nil, unitLiteral)
45884599
}
45894600

@@ -4839,6 +4850,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
48394850
// sometimes we do not have the original anymore and use the transformed tree instead.
48404851
// But taken together, the two criteria are quite accurate.
48414852
missingArgs(tree, tree.tpe.widen)
4853+
case _ if tree.hasAttachment(AscribedToUnit) =>
4854+
// The tree was ascribed to `Unit` explicitly to silence the warning.
4855+
()
48424856
case _ if isUnitExpr =>
48434857
report.warning(PureUnitExpression(original, tree.tpe), original.srcPos)
48444858
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:38:4 ----------------------------------
22
38 | 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:14:2 -----------------------------------------------------------

tests/neg/i13091.check

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
-- [E190] Potential Issue Warning: tests/neg/i13091.scala:7:17 ---------------------------------------------------------
2-
7 |def test: Unit = new Foo // error: class Foo is marked @experimental ...
3-
| ^^^^^^^
4-
| Discarded non-Unit value of type Foo. You may want to use `()`.
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
55
|
6-
| longer explanation available when compiling with `-explain`
7-
-- Error: tests/neg/i13091.scala:7:21 ----------------------------------------------------------------------------------
8-
7 |def test: Unit = new Foo // error: class Foo is marked @experimental ...
9-
| ^^^
10-
| class Foo is marked @experimental
11-
|
12-
| Experimental definition may only be used under experimental mode:
13-
| 1. in a definition marked as @experimental, or
14-
| 2. an experimental feature is imported at the package level, or
15-
| 3. compiling with the -experimental compiler flag.
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)