@@ -81,6 +81,9 @@ object Typer {
81
81
/** Indicates that a definition was copied over from the parent refinements */
82
82
val RefinementFromParent = new Property .StickyKey [Unit ]
83
83
84
+ /** Indicates that an expression is explicitly ascribed to [[Unit ]] type. */
85
+ val AscribedToUnit = new Property .StickyKey [Unit ]
86
+
84
87
/** An attachment on a Select node with an `apply` field indicating that the `apply`
85
88
* was inserted by the Typer.
86
89
*/
@@ -1193,7 +1196,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
1193
1196
else tpt
1194
1197
val expr1 =
1195
1198
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)
1197
1203
assignType(cpy.Typed (tree)(expr1, tpt), underlyingTreeTpe)
1198
1204
.withNotNullInfo(expr1.notNullInfo)
1199
1205
}
@@ -3377,7 +3383,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
3377
3383
else if (ctx.mode.is(Mode .Pattern ))
3378
3384
typedUnApply(cpy.Apply (tree)(op, l :: r :: Nil ), pt)
3379
3385
else {
3380
- val app = typedApply(desugar.binop(l, op, r), pt)
3386
+ val app = typedApply(desugar.binop(l, op, r).withAttachmentsFrom(tree) , pt)
3381
3387
if op.name.isRightAssocOperatorName && ! ctx.mode.is(Mode .QuotedExprPattern ) then
3382
3388
val defs = new mutable.ListBuffer [Tree ]
3383
3389
def lift (app : Tree ): Tree = (app : @ unchecked) match
@@ -4581,9 +4587,14 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
4581
4587
// so will take the code path that decides on inlining
4582
4588
val tree1 = adapt(tree, WildcardType , locked)
4583
4589
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
4585
4596
report.warning(ValueDiscarding (tree.tpe), tree.srcPos)
4586
- }
4597
+
4587
4598
return tpd.Block (tree1 :: Nil , unitLiteral)
4588
4599
}
4589
4600
@@ -4839,6 +4850,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
4839
4850
// sometimes we do not have the original anymore and use the transformed tree instead.
4840
4851
// But taken together, the two criteria are quite accurate.
4841
4852
missingArgs(tree, tree.tpe.widen)
4853
+ case _ if tree.hasAttachment(AscribedToUnit ) =>
4854
+ // The tree was ascribed to `Unit` explicitly to silence the warning.
4855
+ ()
4842
4856
case _ if isUnitExpr =>
4843
4857
report.warning(PureUnitExpression (original, tree.tpe), original.srcPos)
4844
4858
case _ =>
0 commit comments