@@ -1057,7 +1057,14 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
1057
1057
case (Apply (Select (receiver, _), _), SingleType (_, sym)) => sym == receiver.symbol
1058
1058
case _ => false
1059
1059
}
1060
- if (! isThisTypeResult) context.warning(tree.pos, " discarded non-Unit value" )
1060
+ def wasTypedExpectingUnit = {
1061
+ tree.attachments.contains[TypedExpectingUnitAttachment .type ] && {
1062
+ tree.attachments.remove[TypedExpectingUnitAttachment .type ] // cleanup
1063
+ true
1064
+ }
1065
+ }
1066
+ if (! isThisTypeResult && ! wasTypedExpectingUnit)
1067
+ context.warning(tree.pos, " discarded non-Unit value" )
1061
1068
}
1062
1069
@ inline def warnNumericWiden (): Unit =
1063
1070
if (! isPastTyper && settings.warnNumericWiden) context.warning(tree.pos, " implicit numeric widening" )
@@ -5442,7 +5449,10 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
5442
5449
}
5443
5450
case Typed (expr, tpt) =>
5444
5451
val tpt1 = typedType(tpt, mode) // type the ascribed type first
5445
- val expr1 = typed(expr, mode.onlySticky, tpt1.tpe.deconst) // then type the expression with tpt1 as the expected type
5452
+ val exprWithAttachment =
5453
+ if (definitions.isUnitType(tpt1.tpe)) expr.updateAttachment(TypedExpectingUnitAttachment )
5454
+ else expr
5455
+ val expr1 = typed(exprWithAttachment, mode.onlySticky, tpt1.tpe.deconst) // then type the expression with tpt1 as the expected type
5446
5456
treeCopy.Typed (tree, expr1, tpt1) setType tpt1.tpe
5447
5457
}
5448
5458
}
0 commit comments