Skip to content

Commit 7e06899

Browse files
Merge pull request #13166 from dotty-staging/fix-#12392
Add reflect TypeRepr.substituteTypes
2 parents 5eb54a7 + 7293af1 commit 7e06899

File tree

7 files changed

+38
-3
lines changed

7 files changed

+38
-3
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
17471747
dotc.core.Types.decorateTypeApplications(self).appliedTo(targ)
17481748
def appliedTo(targs: List[TypeRepr]): TypeRepr =
17491749
dotc.core.Types.decorateTypeApplications(self).appliedTo(targs)
1750+
def substituteTypes(from: List[Symbol], to: List[TypeRepr]): TypeRepr =
1751+
self.subst(from, to)
17501752
end extension
17511753
end TypeReprMethods
17521754

compiler/test/dotty/tools/repl/TabcompleteTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class TabcompleteTests extends ReplTest {
134134
}
135135

136136
@Test def i12600 = fromInitialState { implicit s =>
137-
assertEquals(List("select", "show", "simplified"),
137+
assertEquals(List("select", "show", "simplified", "substituteTypes"),
138138
tabComplete("import quoted.* ; def fooImpl(using Quotes): Expr[Int] = { import quotes.reflect.* ; TypeRepr.of[Int].s"))
139139
}
140140

library/src/scala/quoted/Quotes.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2565,6 +2565,12 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
25652565
/** The current type applied to given type arguments: `this[targ0, ..., targN]` */
25662566
def appliedTo(targs: List[TypeRepr]): TypeRepr
25672567

2568+
/** Substitute all types that refer in their symbol attribute to
2569+
* one of the symbols in `from` by the corresponding types in `to`.
2570+
*/
2571+
@experimental
2572+
def substituteTypes(from: List[Symbol], to: List[TypeRepr]): TypeRepr
2573+
25682574
end extension
25692575
}
25702576

project/MiMaFilters.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11

22
import com.typesafe.tools.mima.core._
3-
import com.typesafe.tools.mima.core.ProblemFilters._
43

54
object MiMaFilters {
65
val Library: Seq[ProblemFilter] = Seq(
76
// Experimental APIs that can be added in 3.2.0
87
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.runtime.Tuples.init"),
98
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.runtime.Tuples.last"),
10-
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.runtime.Tuples.append")
9+
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.runtime.Tuples.append"),
10+
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeReprMethods.substituteTypes"),
11+
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeReprMethods.substituteTypes"),
1112
)
1213
}

tests/run-macros/i12392.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
scala.Option[scala.Predef.String] to scala.Option[scala.Int]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import scala.quoted.*
2+
3+
inline def testSubst: Unit = ${ testSubstImpl }
4+
5+
def testSubstImpl(using Quotes): Expr[Unit] = {
6+
import quotes.reflect.*
7+
8+
val intTpe = TypeRepr.of[Int]
9+
val strOptTpe = TypeRepr.of[Option[String]]
10+
11+
val tpeArgs: List[TypeRepr] = strOptTpe match {
12+
case AppliedType(_, args) => args
13+
case _ => List.empty[TypeRepr]
14+
}
15+
16+
val intOptTpe = strOptTpe.substituteTypes(
17+
tpeArgs.map(_.typeSymbol), List(intTpe))
18+
19+
val repr = s"${strOptTpe.show} to ${intOptTpe.show}"
20+
21+
'{
22+
println(${Expr(repr)})
23+
}
24+
}

tests/run-macros/i12392/Test_2.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@main def Test = testSubst

0 commit comments

Comments
 (0)