@@ -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 }
@@ -3374,7 +3380,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
33743380 else if (ctx.mode.is(Mode .Pattern ))
33753381 typedUnApply(cpy.Apply (tree)(op, l :: r :: Nil ), pt)
33763382 else {
3377- val app = typedApply(desugar.binop(l, op, r), pt)
3383+ val app = typedApply(desugar.binop(l, op, r).withAttachmentsFrom(tree) , pt)
33783384 if op.name.isRightAssocOperatorName && ! ctx.mode.is(Mode .QuotedExprPattern ) then
33793385 val defs = new mutable.ListBuffer [Tree ]
33803386 def lift (app : Tree ): Tree = (app : @ unchecked) match
@@ -4578,9 +4584,14 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
45784584 // so will take the code path that decides on inlining
45794585 val tree1 = adapt(tree, WildcardType , locked)
45804586 checkStatementPurity(tree1)(tree, ctx.owner, isUnitExpr = true )
4581- if (! ctx.isAfterTyper && ! tree.isInstanceOf [Inlined ] && ctx.settings.Whas .valueDiscard && ! isThisTypeResult(tree)) {
4587+
4588+ if ctx.settings.Whas .valueDiscard
4589+ && ! ctx.isAfterTyper
4590+ && ! tree.isInstanceOf [Inlined ]
4591+ && ! isThisTypeResult(tree)
4592+ && ! tree.hasAttachment(AscribedToUnit ) then
45824593 report.warning(ValueDiscarding (tree.tpe), tree.srcPos)
4583- }
4594+
45844595 return tpd.Block (tree1 :: Nil , unitLiteral)
45854596 }
45864597
@@ -4858,6 +4869,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
48584869 // sometimes we do not have the original anymore and use the transformed tree instead.
48594870 // But taken together, the two criteria are quite accurate.
48604871 missingArgs(tree, tree.tpe.widen)
4872+ case _ if tree.hasAttachment(AscribedToUnit ) =>
4873+ // The tree was ascribed to `Unit` explicitly to silence the warning.
4874+ ()
48614875 case _ if isUnitExpr =>
48624876 report.warning(PureUnitExpression (original, tree.tpe), original.srcPos)
48634877 case _ =>
0 commit comments