Skip to content

Commit c4e703f

Browse files
Also handle imports on parameters of lambdas returned from inline defs
Both i19493 and i19436 require mapping the type of the expr in an `ImportType` which is itself the info of a `TermRef`. In the first issue, for the substitution of an inline def parameter proxy. In the second issue, for the parameter of a lambda returned from an inline def. Both can be handled in `TypeMap` by mapping over references to `ImportType`s. The second case also requires modifying `TreeTypeMap#mapType` such that the logic mapping over imports is done within a `TypeMap` doing the symbol substitutions. Fixes #19436
1 parent 6ad4fd8 commit c4e703f

File tree

6 files changed

+41
-7
lines changed

6 files changed

+41
-7
lines changed

compiler/src/dotty/tools/dotc/ast/TreeTypeMap.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,13 @@ class TreeTypeMap(
6868
}
6969
}
7070

71-
def mapType(tp: Type): Type =
72-
mapOwnerThis(typeMap(tp).substSym(substFrom, substTo))
71+
val mapType: Type => Type =
72+
val substMap = new TypeMap():
73+
def apply(tp: Type): Type = tp match
74+
case tp: TermRef if tp.symbol.isImport => mapOver(tp)
75+
case tp => tp.substSym(substFrom, substTo)
76+
typeMap.andThen(substMap).andThen(mapOwnerThis)
77+
end mapType
7378

7479
private def updateDecls(prevStats: List[Tree], newStats: List[Tree]): Unit =
7580
if (prevStats.isEmpty) assert(newStats.isEmpty)

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6280,6 +6280,12 @@ object Types extends TypeUtils {
62806280
val ctx = this.mapCtx // optimization for performance
62816281
given Context = ctx
62826282
tp match {
6283+
case tp: TermRef if tp.symbol.isImport =>
6284+
// see tests/pos/i19493.scala for examples requiring mapping over imports
6285+
val ImportType(e) = tp.info: @unchecked
6286+
val e1 = singleton(apply(e.tpe))
6287+
newImportSymbol(tp.symbol.owner, e1).termRef
6288+
62836289
case tp: NamedType =>
62846290
if stopBecauseStaticOrLocal(tp) then tp
62856291
else

compiler/src/dotty/tools/dotc/inlines/Inliner.scala

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -565,10 +565,6 @@ class Inliner(val call: tpd.Tree)(using Context):
565565
def apply(t: Type) = t match {
566566
case t: ThisType => thisProxy.getOrElse(t.cls, t)
567567
case t: TypeRef => paramProxy.getOrElse(t, mapOver(t))
568-
case t: TermRef if t.symbol.isImport =>
569-
val ImportType(e) = t.widenTermRefExpr: @unchecked
570-
val e1 = singleton(apply(e.tpe))
571-
newImportSymbol(ctx.owner, e1).termRef
572568
case t: SingletonType =>
573569
if t.termSymbol.isAllOf(InlineParam) then apply(t.widenTermRefExpr)
574570
else paramProxy.getOrElse(t, mapOver(t))

tests/pos-macros/i19436/Macro_1.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+
import scala.compiletime.summonInline
4+
5+
trait SomeImplicits:
6+
given int: Int
7+
8+
object Macro:
9+
10+
transparent inline def testSummon: SomeImplicits => Int = ${ testSummonImpl }
11+
12+
private def testSummonImpl(using Quotes): Expr[SomeImplicits => Int] =
13+
import quotes.reflect.*
14+
'{
15+
(x: SomeImplicits) =>
16+
import x.given
17+
summonInline[Int]
18+
}

tests/pos-macros/i19436/Test_2.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
def fn: Unit = Macro.testSummon

tests/pos/i19493.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import scala.compiletime.{summonAll, summonInline}
32
import deriving.Mirror
43

@@ -39,4 +38,12 @@ object Minimization:
3938
val a: A = ???
4039
a.bar
4140

41+
42+
inline def baz() = (x: GivesString) =>
43+
import x.aString
44+
summon[String] // ok
45+
summonInline[String] // was error
46+
47+
baz()
48+
4249
end Minimization

0 commit comments

Comments
 (0)