Skip to content

Commit 4a59f61

Browse files
som-snytttgodzik
authored andcommitted
Use enclosing enclosingInlineds for empty call (scala#24281)
Fixes scala#24248 Adjust enclosing inlineds for inlined parameter, and do look-up in imports if tree position is enclosed by any enclosing inlined position (any intermediate position, not just outermost). [Cherry-picked 2f320b8]
1 parent b75cafd commit 4a59f61

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
@@ -59,7 +59,7 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
5959
if tree.symbol.exists then
6060
// if in an inline expansion, resolve at summonInline (synthetic pos) or in an enclosing call site
6161
val resolving =
62-
tree.srcPos.isUserCode
62+
tree.srcPos.isUserCode(using if tree.hasAttachment(InlinedParameter) then ctx.outer else ctx)
6363
|| tree.srcPos.isZeroExtentSynthetic // take as summonInline
6464
if !ignoreTree(tree) then
6565
def loopOverPrefixes(prefix: Type, depth: Int): Unit =
@@ -160,6 +160,10 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
160160
case _ =>
161161
tree
162162

163+
override def prepareForInlined(tree: Inlined)(using Context): Context =
164+
if tree.inlinedFromOuterScope then
165+
tree.expansion.putAttachment(InlinedParameter, ())
166+
ctx
163167
override def transformInlined(tree: Inlined)(using Context): tree.type =
164168
transformAllDeep(tree.call)
165169
tree
@@ -467,6 +471,9 @@ object CheckUnused:
467471
/** Tree is LHS of Assign. */
468472
val AssignmentTarget = Property.StickyKey[Unit]
469473

474+
/** Tree is an inlined parameter. */
475+
val InlinedParameter = Property.StickyKey[Unit]
476+
470477
class PostTyper extends CheckUnused(PhaseMode.Aggregate, "PostTyper")
471478

472479
class PostInlining extends CheckUnused(PhaseMode.Report, "PostInlining")
@@ -1035,7 +1042,7 @@ object CheckUnused:
10351042
def isUserCode(using Context): Boolean =
10361043
val inlineds = enclosingInlineds // per current context
10371044
inlineds.isEmpty
1038-
|| inlineds.last.srcPos.sourcePos.contains(pos.sourcePos)
1045+
|| inlineds.exists(_.srcPos.sourcePos.contains(pos.sourcePos)) // include intermediate inlinings or quotes
10391046

10401047
extension [A <: AnyRef](arr: Array[A])
10411048
// 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)