Skip to content

Commit b38dad6

Browse files
committed
Check inline expansion for exclusion
1 parent 05b102a commit b38dad6

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import util.common.*
3535
import util.{Property, SimpleIdentityMap, SrcPos}
3636
import Applications.{tupleComponentTypes, wrapDefs, defaultArgument}
3737

38-
import collection.mutable
38+
import collection.mutable, mutable.ListBuffer
3939
import Implicits.*
4040
import util.Stats.record
4141
import config.Printers.{gadts, typr}
@@ -207,7 +207,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
207207
* a reference for `m` is searched. `null` in all other situations.
208208
*/
209209
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 = {
211211
val refctx = ctx
212212
val noImports = ctx.mode.is(Mode.InPackageClauseName)
213213
def suppressErrors = excluded.is(ConstructorProxy)
@@ -3435,7 +3435,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
34353435
else {
34363436
val app = typedApply(desugar.binop(l, op, r).withAttachmentsFrom(tree), pt)
34373437
if op.name.isRightAssocOperatorName && !ctx.mode.is(Mode.QuotedExprPattern) then
3438-
val defs = new mutable.ListBuffer[Tree]
3438+
val defs = ListBuffer.empty[Tree]
34393439
def lift(app: Tree): Tree = (app: @unchecked) match
34403440
case Apply(fn, args) =>
34413441
if (app.tpe.isError) app
@@ -3735,7 +3735,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
37353735
trees mapconserve (typed(_))
37363736

37373737
def typedStats(stats: List[untpd.Tree], exprOwner: Symbol)(using Context): (List[Tree], Context) = {
3738-
val buf = new mutable.ListBuffer[Tree]
3738+
val buf = ListBuffer.empty[Tree]
37393739
var enumContexts: SimpleIdentityMap[Symbol, Context] = SimpleIdentityMap.empty
37403740
val initialNotNullInfos = ctx.notNullInfos
37413741
// A map from `enum` symbols to the contexts enclosing their definitions
@@ -3779,7 +3779,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
37793779
traverse(xtree :: rest)
37803780
case stat :: rest =>
37813781
val stat1 = typed(stat)(using ctx.exprContext(stat, exprOwner))
3782-
if !Linter.warnOnInterestingResultInStatement(stat1) then checkStatementPurity(stat1)(stat, exprOwner)
3782+
if !Linter.warnOnInterestingResultInStatement(stat1) then
3783+
checkStatementPurity(stat1)(stat, exprOwner, isUnitExpr = false)
37833784
buf += stat1
37843785
traverse(rest)(using stat1.nullableContext)
37853786
case nil =>
@@ -3986,7 +3987,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
39863987
def selectionProto = SelectionProto(tree.name, mbrProto, compat, privateOK = inSelect, tree.nameSpan)
39873988

39883989
def tryExtension(using Context): Tree =
3989-
val altImports = new mutable.ListBuffer[TermRef]()
3990+
val altImports = ListBuffer.empty[TermRef]
39903991
findRef(tree.name, WildcardType, ExtensionMethod, EmptyFlags, qual.srcPos, altImports) match
39913992
case ref: TermRef =>
39923993
def tryExtMethod(ref: TermRef)(using Context) =
@@ -3995,7 +3996,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
39953996
tryExtMethod(ref)
39963997
else
39973998
// Try all possible imports and collect successes and failures
3998-
val successes, failures = new mutable.ListBuffer[(Tree, TyperState)]
3999+
val successes, failures = ListBuffer.empty[(Tree, TyperState)]
39994000
for alt <- ref :: altImports.toList do
40004001
val nestedCtx = ctx.fresh.setNewTyperState()
40014002
val app = tryExtMethod(alt)(using nestedCtx)
@@ -4653,19 +4654,19 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
46534654
return readapt(tree.cast(captured))
46544655

46554656
// drop type if prototype is Unit
4656-
if (pt isRef defn.UnitClass) {
4657+
if (pt.isRef(defn.UnitClass)) {
46574658
// local adaptation makes sure every adapted tree conforms to its pt
46584659
// so will take the code path that decides on inlining
46594660
val tree1 = adapt(tree, WildcardType, locked)
46604661
checkStatementPurity(tree1)(tree, ctx.owner, isUnitExpr = true)
46614662

4662-
if ctx.settings.Whas.valueDiscard
4663-
&& !ctx.isAfterTyper
4664-
&& !tree.isInstanceOf[Inlined]
4665-
&& !isThisTypeResult(tree)
4666-
&& !tree.hasAttachment(AscribedToUnit) then
4667-
report.warning(ValueDiscarding(tree.tpe), tree.srcPos)
4668-
4663+
if ctx.settings.Whas.valueDiscard && !ctx.isAfterTyper then
4664+
val warnable = tree match
4665+
case inlined: Inlined => inlined.expansion
4666+
case tree => tree
4667+
if !isThisTypeResult(warnable) && !warnable.hasAttachment(AscribedToUnit)
4668+
then
4669+
report.warning(ValueDiscarding(warnable.tpe), tree.srcPos)
46694670
return tpd.Block(tree1 :: Nil, unitLiteral)
46704671
}
46714672

@@ -4925,7 +4926,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
49254926
typedExpr(cmp, defn.BooleanType)
49264927
case _ =>
49274928

4928-
private def checkStatementPurity(tree: tpd.Tree)(original: untpd.Tree, exprOwner: Symbol, isUnitExpr: Boolean = false)(using Context): Unit =
4929+
private def checkStatementPurity(tree: tpd.Tree)(original: untpd.Tree, exprOwner: Symbol, isUnitExpr: Boolean)(using Context): Unit =
49294930
if !tree.tpe.isErroneous
49304931
&& !ctx.isAfterTyper
49314932
&& !tree.isInstanceOf[Inlined]

tests/warn/i23018.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//> using options -Wvalue-discard
2+
3+
transparent inline def toto: Any = 1
4+
transparent inline def uhoh = 42: Unit // warn ??
5+
def tata: Unit = toto // warn
6+
def hmm: Unit = uhoh
7+
def literally: Unit = 42 // warn pure discard
8+
def funnily = 42: Unit // warn ??

0 commit comments

Comments
 (0)