Skip to content

Commit 1b9b913

Browse files
EugeneFlesselleWojciechMazur
authored andcommitted
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 [Cherry-picked ff003fd]
1 parent 191b41a commit 1b9b913

File tree

6 files changed

+40
-6
lines changed

6 files changed

+40
-6
lines changed

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@ class TreeTypeMap(
6969
}
7070

7171
def mapType(tp: Type): Type =
72-
mapOwnerThis(typeMap(tp).substSym(substFrom, substTo))
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+
mapOwnerThis(substMap(typeMap(tp)))
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

+6
Original file line numberDiff line numberDiff line change
@@ -5797,6 +5797,12 @@ object Types extends TypeUtils {
57975797
val ctx = this.mapCtx // optimization for performance
57985798
given Context = ctx
57995799
tp match {
5800+
case tp: TermRef if tp.symbol.isImport =>
5801+
// see tests/pos/i19493.scala for examples requiring mapping over imports
5802+
val ImportType(e) = tp.info: @unchecked
5803+
val e1 = singleton(apply(e.tpe))
5804+
newImportSymbol(tp.symbol.owner, e1).termRef
5805+
58005806
case tp: NamedType =>
58015807
if stopBecauseStaticOrLocal(tp) then tp
58025808
else

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

-4
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

+18
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

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

tests/pos/i19493.scala

+8-1
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)