@@ -35,7 +35,7 @@ import util.common.*
35
35
import util .{Property , SimpleIdentityMap , SrcPos }
36
36
import Applications .{tupleComponentTypes , wrapDefs , defaultArgument }
37
37
38
- import collection .mutable
38
+ import collection .mutable , mutable . ListBuffer
39
39
import Implicits .*
40
40
import util .Stats .record
41
41
import config .Printers .{gadts , typr }
@@ -207,7 +207,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
207
207
* a reference for `m` is searched. `null` in all other situations.
208
208
*/
209
209
def findRef (name : Name , pt : Type , required : FlagSet , excluded : FlagSet , pos : SrcPos ,
210
- altImports : mutable. ListBuffer [TermRef ] | Null = null )(using Context ): Type = {
210
+ altImports : ListBuffer [TermRef ] | Null = null )(using Context ): Type = {
211
211
val refctx = ctx
212
212
val noImports = ctx.mode.is(Mode .InPackageClauseName )
213
213
def suppressErrors = excluded.is(ConstructorProxy )
@@ -1124,7 +1124,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
1124
1124
record(" typedNumber" )
1125
1125
val digits = tree.digits
1126
1126
val target = pt.dealias
1127
- def lit (value : Any ) = Literal (Constant (value)).withSpan(tree.span)
1127
+ def lit (value : Any ) = Literal (Constant (value)).withSpan(tree.span).withAttachmentsFrom(tree)
1128
1128
try {
1129
1129
// Special case primitive numeric types
1130
1130
if (target.isRef(defn.IntClass ) ||
@@ -1174,7 +1174,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
1174
1174
}
1175
1175
var app : untpd.Tree = untpd.Apply (fromDigits, firstArg :: otherArgs)
1176
1176
if (ctx.mode.is(Mode .Pattern )) app = untpd.Block (Nil , app)
1177
- return typed(app, pt)
1177
+ return typed(app, pt).withAttachmentsFrom(tree)
1178
1178
case _ =>
1179
1179
}
1180
1180
// Otherwise convert to Int or Double according to digits format
@@ -3490,7 +3490,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
3490
3490
else {
3491
3491
val app = typedApply(desugar.binop(l, op, r).withAttachmentsFrom(tree), pt)
3492
3492
if op.name.isRightAssocOperatorName && ! ctx.mode.is(Mode .QuotedExprPattern ) then
3493
- val defs = new mutable. ListBuffer [Tree ]
3493
+ val defs = ListBuffer .empty [Tree ]
3494
3494
def lift (app : Tree ): Tree = (app : @ unchecked) match
3495
3495
case Apply (fn, args) =>
3496
3496
if (app.tpe.isError) app
@@ -3790,7 +3790,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
3790
3790
trees mapconserve (typed(_))
3791
3791
3792
3792
def typedStats (stats : List [untpd.Tree ], exprOwner : Symbol )(using Context ): (List [Tree ], Context ) = {
3793
- val buf = new mutable. ListBuffer [Tree ]
3793
+ val buf = ListBuffer .empty [Tree ]
3794
3794
var enumContexts : SimpleIdentityMap [Symbol , Context ] = SimpleIdentityMap .empty
3795
3795
val initialNotNullInfos = ctx.notNullInfos
3796
3796
// A map from `enum` symbols to the contexts enclosing their definitions
@@ -3834,7 +3834,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
3834
3834
traverse(xtree :: rest)
3835
3835
case stat :: rest =>
3836
3836
val stat1 = typed(stat)(using ctx.exprContext(stat, exprOwner))
3837
- if ! Linter .warnOnInterestingResultInStatement(stat1) then checkStatementPurity(stat1)(stat, exprOwner)
3837
+ if ! Linter .warnOnInterestingResultInStatement(stat1) then
3838
+ checkStatementPurity(stat1)(stat, exprOwner, isUnitExpr = false )
3838
3839
buf += stat1
3839
3840
traverse(rest)(using stat1.nullableContext)
3840
3841
case nil =>
@@ -4041,7 +4042,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
4041
4042
def selectionProto = SelectionProto (tree.name, mbrProto, compat, privateOK = inSelect, tree.nameSpan)
4042
4043
4043
4044
def tryExtension (using Context ): Tree =
4044
- val altImports = new mutable. ListBuffer [TermRef ]()
4045
+ val altImports = ListBuffer .empty [TermRef ]
4045
4046
findRef(tree.name, WildcardType , ExtensionMethod , EmptyFlags , qual.srcPos, altImports) match
4046
4047
case ref : TermRef =>
4047
4048
def tryExtMethod (ref : TermRef )(using Context ) =
@@ -4050,7 +4051,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
4050
4051
tryExtMethod(ref)
4051
4052
else
4052
4053
// Try all possible imports and collect successes and failures
4053
- val successes, failures = new mutable. ListBuffer [(Tree , TyperState )]
4054
+ val successes, failures = ListBuffer .empty [(Tree , TyperState )]
4054
4055
for alt <- ref :: altImports.toList do
4055
4056
val nestedCtx = ctx.fresh.setNewTyperState()
4056
4057
val app = tryExtMethod(alt)(using nestedCtx)
@@ -4708,22 +4709,13 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
4708
4709
return readapt(tree.cast(captured))
4709
4710
4710
4711
// drop type if prototype is Unit
4711
- if (pt isRef defn.UnitClass ) {
4712
+ if pt. isRef( defn.UnitClass ) then
4712
4713
// local adaptation makes sure every adapted tree conforms to its pt
4713
4714
// so will take the code path that decides on inlining
4714
4715
val tree1 = adapt(tree, WildcardType , locked)
4715
4716
checkStatementPurity(tree1)(tree, ctx.owner, isUnitExpr = true )
4716
-
4717
- if ctx.settings.Whas .valueDiscard
4718
- && ! ctx.isAfterTyper
4719
- && ! tree.isInstanceOf [Inlined ]
4720
- && ! isThisTypeResult(tree)
4721
- && ! isAscribedToUnit(tree)
4722
- then
4723
- report.warning(ValueDiscarding (tree.tpe), tree.srcPos)
4724
-
4717
+ checkValueDiscard(tree)
4725
4718
return tpd.Block (tree1 :: Nil , unitLiteral)
4726
- }
4727
4719
4728
4720
// convert function literal to SAM closure
4729
4721
tree match {
@@ -4981,10 +4973,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
4981
4973
typedExpr(cmp, defn.BooleanType )
4982
4974
case _ =>
4983
4975
4984
- private def checkStatementPurity (tree : tpd.Tree )(original : untpd.Tree , exprOwner : Symbol , isUnitExpr : Boolean = false )(using Context ): Unit =
4976
+ private def checkStatementPurity (tree : tpd.Tree )(original : untpd.Tree , exprOwner : Symbol , isUnitExpr : Boolean )(using Context ): Unit =
4985
4977
if ! tree.tpe.isErroneous
4986
4978
&& ! ctx.isAfterTyper
4987
- && ! tree.isInstanceOf [Inlined ]
4988
4979
&& isPureExpr(tree)
4989
4980
&& ! isSelfOrSuperConstrCall(tree)
4990
4981
then tree match
@@ -4999,13 +4990,26 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
4999
4990
// sometimes we do not have the original anymore and use the transformed tree instead.
5000
4991
// But taken together, the two criteria are quite accurate.
5001
4992
missingArgs(tree, tree.tpe.widen)
5002
- case _ if tree.hasAttachment(AscribedToUnit ) =>
5003
- // The tree was ascribed to `Unit` explicitly to silence the warning.
5004
- ()
5005
- case _ if isUnitExpr =>
5006
- report.warning(PureUnitExpression (original, tree.tpe), original.srcPos)
5007
- case _ =>
5008
- report.warning(PureExpressionInStatementPosition (original, exprOwner), original.srcPos)
4993
+ case tree =>
4994
+ val warnable = tree match
4995
+ case inlined : Inlined => inlined.expansion
4996
+ case tree => tree
4997
+ // Check if the tree was ascribed to `Unit` explicitly to silence the warning.
4998
+ if ! isThisTypeResult(warnable) && ! isAscribedToUnit(warnable) then
4999
+ val msg =
5000
+ if isUnitExpr then
5001
+ PureUnitExpression (original, warnable.tpe)
5002
+ else
5003
+ PureExpressionInStatementPosition (original, exprOwner)
5004
+ report.warning(msg, original.srcPos)
5005
+
5006
+ private def checkValueDiscard (tree : tpd.Tree )(using Context ): Unit =
5007
+ if ctx.settings.Whas .valueDiscard && ! ctx.isAfterTyper then
5008
+ val warnable = tree match
5009
+ case inlined : Inlined => inlined.expansion
5010
+ case tree => tree
5011
+ if ! isThisTypeResult(warnable) && ! isAscribedToUnit(warnable) then
5012
+ report.warning(ValueDiscarding (warnable.tpe), tree.srcPos)
5009
5013
5010
5014
/** Types the body Scala 2 macro declaration `def f = macro <body>` */
5011
5015
protected def typedScala2MacroBody (call : untpd.Tree )(using Context ): Tree =
0 commit comments