Skip to content

Add reflect TypeRepr.substituteTypes #13166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1747,6 +1747,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
dotc.core.Types.decorateTypeApplications(self).appliedTo(targ)
def appliedTo(targs: List[TypeRepr]): TypeRepr =
dotc.core.Types.decorateTypeApplications(self).appliedTo(targs)
def substituteTypes(from: List[Symbol], to: List[TypeRepr]): TypeRepr =
self.subst(from, to)
end extension
end TypeReprMethods

Expand Down
2 changes: 1 addition & 1 deletion compiler/test/dotty/tools/repl/TabcompleteTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class TabcompleteTests extends ReplTest {
}

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

Expand Down
6 changes: 6 additions & 0 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2565,6 +2565,12 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
/** The current type applied to given type arguments: `this[targ0, ..., targN]` */
def appliedTo(targs: List[TypeRepr]): TypeRepr

/** Substitute all types that refer in their symbol attribute to
* one of the symbols in `from` by the corresponding types in `to`.
*/
@experimental
def substituteTypes(from: List[Symbol], to: List[TypeRepr]): TypeRepr

end extension
}

Expand Down
5 changes: 3 additions & 2 deletions project/MiMaFilters.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@

import com.typesafe.tools.mima.core._
import com.typesafe.tools.mima.core.ProblemFilters._

object MiMaFilters {
val Library: Seq[ProblemFilter] = Seq(
// Experimental APIs that can be added in 3.2.0
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.runtime.Tuples.init"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.runtime.Tuples.last"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.runtime.Tuples.append")
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.runtime.Tuples.append"),
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeReprMethods.substituteTypes"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeReprMethods.substituteTypes"),
)
}
1 change: 1 addition & 0 deletions tests/run-macros/i12392.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scala.Option[scala.Predef.String] to scala.Option[scala.Int]
24 changes: 24 additions & 0 deletions tests/run-macros/i12392/Macros_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import scala.quoted.*

inline def testSubst: Unit = ${ testSubstImpl }

def testSubstImpl(using Quotes): Expr[Unit] = {
import quotes.reflect.*

val intTpe = TypeRepr.of[Int]
val strOptTpe = TypeRepr.of[Option[String]]

val tpeArgs: List[TypeRepr] = strOptTpe match {
case AppliedType(_, args) => args
case _ => List.empty[TypeRepr]
}

val intOptTpe = strOptTpe.substituteTypes(
tpeArgs.map(_.typeSymbol), List(intTpe))

val repr = s"${strOptTpe.show} to ${intOptTpe.show}"

'{
println(${Expr(repr)})
}
}
1 change: 1 addition & 0 deletions tests/run-macros/i12392/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@main def Test = testSubst