Skip to content

Commit 004eb67

Browse files
Backport "Use enclosing enclosingInlineds for empty call" to 3.7.4 (#24286)
Backports #24281 to the 3.7.4. PR submitted by the release tooling.
2 parents 15655ec + 6a1e1a5 commit 004eb67

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

compiler/src/dotty/tools/dotc/transform/CheckUnused.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
5555
if tree.symbol.exists then
5656
// if in an inline expansion, resolve at summonInline (synthetic pos) or in an enclosing call site
5757
val resolving =
58-
tree.srcPos.isUserCode
58+
tree.srcPos.isUserCode(using if tree.hasAttachment(InlinedParameter) then ctx.outer else ctx)
5959
|| tree.srcPos.isZeroExtentSynthetic // take as summonInline
6060
if !ignoreTree(tree) then
6161
def loopOverPrefixes(prefix: Type, depth: Int): Unit =
@@ -140,6 +140,10 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
140140
case _ =>
141141
tree
142142

143+
override def prepareForInlined(tree: Inlined)(using Context): Context =
144+
if tree.inlinedFromOuterScope then
145+
tree.expansion.putAttachment(InlinedParameter, ())
146+
ctx
143147
override def transformInlined(tree: Inlined)(using Context): tree.type =
144148
if !tree.call.isEmpty then
145149
if !refInfos.calls.containsKey(tree.call) then
@@ -422,6 +426,9 @@ object CheckUnused:
422426
/** Tree is LHS of Assign. */
423427
val AssignmentTarget = Property.StickyKey[Unit]
424428

429+
/** Tree is an inlined parameter. */
430+
val InlinedParameter = Property.StickyKey[Unit]
431+
425432
class PostTyper extends CheckUnused(PhaseMode.Aggregate, "PostTyper")
426433

427434
class PostInlining extends CheckUnused(PhaseMode.Report, "PostInlining")
@@ -1010,7 +1017,7 @@ object CheckUnused:
10101017
def isUserCode(using Context): Boolean =
10111018
val inlineds = enclosingInlineds // per current context
10121019
inlineds.isEmpty
1013-
|| inlineds.last.srcPos.sourcePos.contains(pos.sourcePos)
1020+
|| inlineds.exists(_.srcPos.sourcePos.contains(pos.sourcePos)) // include intermediate inlinings or quotes
10141021

10151022
extension [A <: AnyRef](arr: Array[A])
10161023
// returns `until` if not satisfied

tests/warn/i24248/lib.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
import scala.quoted.*
3+
4+
trait Thing
5+
object Stuff:
6+
given Thing()
7+
8+
object lib:
9+
inline def m: Thing = ${ mImpl[Thing] }
10+
11+
def mImpl[T](using Quotes, Type[T]): Expr[T] =
12+
import quotes.reflect.*
13+
val thing = Implicits.search(TypeRepr.of[T]) match
14+
case iss: ImplicitSearchSuccess => iss.tree.asExprOf[T]
15+
'{
16+
val res = $thing
17+
res
18+
}

tests/warn/i24248/test.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//> using options -Werror -Wunused:all
2+
3+
import Stuff.given
4+
5+
@main def test = println:
6+
lib.m

0 commit comments

Comments
 (0)