Skip to content

Commit

Permalink
Backport "Map over refs of ImportTypes in TypeMap" to 3.5.1 (#20957)
Browse files Browse the repository at this point in the history
Backports #20837 to 3.5.1-RC1
  • Loading branch information
WojciechMazur authored Jul 3, 2024
2 parents 3f57b29 + a16ed38 commit 2eb77ff
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
7 changes: 6 additions & 1 deletion compiler/src/dotty/tools/dotc/ast/TreeTypeMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ class TreeTypeMap(
}

def mapType(tp: Type): Type =
mapOwnerThis(typeMap(tp).substSym(substFrom, substTo))
val substMap = new TypeMap():
def apply(tp: Type): Type = tp match
case tp: TermRef if tp.symbol.isImport => mapOver(tp)
case tp => tp.substSym(substFrom, substTo)
mapOwnerThis(substMap(typeMap(tp)))
end mapType

private def updateDecls(prevStats: List[Tree], newStats: List[Tree]): Unit =
if (prevStats.isEmpty) assert(newStats.isEmpty)
Expand Down
6 changes: 6 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6295,6 +6295,12 @@ object Types extends TypeUtils {
val ctx = this.mapCtx // optimization for performance
given Context = ctx
tp match {
case tp: TermRef if tp.symbol.isImport =>
// see tests/pos/i19493.scala for examples requiring mapping over imports
val ImportType(e) = tp.info: @unchecked
val e1 = singleton(apply(e.tpe))
newImportSymbol(tp.symbol.owner, e1).termRef

case tp: NamedType =>
if stopBecauseStaticOrLocal(tp) then tp
else
Expand Down
18 changes: 18 additions & 0 deletions tests/pos-macros/i19436/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

import scala.quoted.*
import scala.compiletime.summonInline

trait SomeImplicits:
given int: Int

object Macro:

transparent inline def testSummon: SomeImplicits => Int = ${ testSummonImpl }

private def testSummonImpl(using Quotes): Expr[SomeImplicits => Int] =
import quotes.reflect.*
'{
(x: SomeImplicits) =>
import x.given
summonInline[Int]
}
2 changes: 2 additions & 0 deletions tests/pos-macros/i19436/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

def fn: Unit = Macro.testSummon
49 changes: 49 additions & 0 deletions tests/pos/i19493.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import scala.compiletime.{summonAll, summonInline}
import deriving.Mirror

type Sc[X] = X
case class Row[T[_]](name: T[String])

class DialectTypeMappers:
given String = ???

inline def metadata(dialect: DialectTypeMappers)(using m: Mirror.Of[Row[Sc]]): m.MirroredElemTypes =
import dialect.given
summonAll[m.MirroredElemTypes]

def f = metadata(???)


object Minimization:

class GivesString:
given aString: String = ???

inline def foo(x: GivesString): Unit =
import x.aString
summon[String] // ok
summonInline[String] // was error

foo(???)


trait A:
val x: GivesString

inline def bar: Unit =
import this.x.aString
summon[String] // ok
summonInline[String] // was error

val a: A = ???
a.bar


inline def baz() = (x: GivesString) =>
import x.aString
summon[String] // ok
summonInline[String] // was error

baz()

end Minimization

0 comments on commit 2eb77ff

Please sign in to comment.