@@ -74,6 +74,9 @@ object Typer {
74
74
/** An attachment for GADT constraints that were inferred for a pattern. */
75
75
val InferredGadtConstraints = new Property .StickyKey [core.GadtConstraint ]
76
76
77
+ /** Indicates that an expression is explicitly ascribed to [[Unit ]] type. */
78
+ val AscribedToUnit = new Property .StickyKey [Unit ]
79
+
77
80
/** An attachment on a Select node with an `apply` field indicating that the `apply`
78
81
* was inserted by the Typer.
79
82
*/
@@ -992,7 +995,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
992
995
else tpt
993
996
val expr1 =
994
997
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)
996
1002
assignType(cpy.Typed (tree)(expr1, tpt), underlyingTreeTpe)
997
1003
.withNotNullInfo(expr1.notNullInfo)
998
1004
}
@@ -3009,7 +3015,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
3009
3015
else if (ctx.mode.is(Mode .Pattern ))
3010
3016
typedUnApply(cpy.Apply (tree)(op, l :: r :: Nil ), pt)
3011
3017
else {
3012
- val app = typedApply(desugar.binop(l, op, r), pt)
3018
+ val app = typedApply(desugar.binop(l, op, r).withAttachmentsFrom(tree) , pt)
3013
3019
if op.name.isRightAssocOperatorName && ! ctx.mode.is(Mode .QuotedPattern ) then
3014
3020
val defs = new mutable.ListBuffer [Tree ]
3015
3021
def lift (app : Tree ): Tree = (app : @ unchecked) match
@@ -4221,9 +4227,14 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
4221
4227
// so will take the code path that decides on inlining
4222
4228
val tree1 = adapt(tree, WildcardType , locked)
4223
4229
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
4225
4236
report.warning(ValueDiscarding (tree.tpe), tree.srcPos)
4226
- }
4237
+
4227
4238
return tpd.Block (tree1 :: Nil , unitLiteral)
4228
4239
}
4229
4240
@@ -4578,6 +4589,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
4578
4589
// sometimes we do not have the original anymore and use the transformed tree instead.
4579
4590
// But taken together, the two criteria are quite accurate.
4580
4591
missingArgs(tree, tree.tpe.widen)
4592
+ case _ if tree.hasAttachment(AscribedToUnit ) =>
4593
+ // The tree was ascribed to `Unit` explicitly to silence the warning.
4594
+ ()
4581
4595
case _ if isUnitExpr =>
4582
4596
report.warning(PureUnitExpression (original, tree.tpe), original.srcPos)
4583
4597
case _ =>
0 commit comments