Skip to content

Refactor Reflect implicit search #9863

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
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
21 changes: 11 additions & 10 deletions compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import dotty.tools.dotc.core.NameKinds
import dotty.tools.dotc.core.StdNames._
import dotty.tools.dotc.quoted.reflect._
import dotty.tools.dotc.core.Decorators._
import dotty.tools.dotc.typer.Implicits

import scala.quoted.QuoteContext
import scala.quoted.show.SyntaxHighlight
Expand Down Expand Up @@ -2173,10 +2172,12 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
end extension
end ConstantMethodsImpl

type ImplicitSearchResult = Tree
object Implicits extends ImplicitsModule:
def search(tpe: Type): ImplicitSearchResult =
ctx.typer.inferImplicitArg(tpe, rootPosition.span)
end Implicits

def searchImplicit(tpe: Type): ImplicitSearchResult =
ctx.typer.inferImplicitArg(tpe, rootPosition.span)
type ImplicitSearchResult = Tree

type ImplicitSearchSuccess = Tree

Expand All @@ -2185,7 +2186,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
override def unapply(x: Any): Option[ImplicitSearchSuccess] = x match
case x: Tree @unchecked =>
x.tpe match
case _: Implicits.SearchFailureType => None
case _: dotc.typer.Implicits.SearchFailureType => None
case _ => Some(x)
case _ => None
end ImplicitSearchSuccessTypeTest
Expand All @@ -2203,15 +2204,15 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
override def unapply(x: Any): Option[ImplicitSearchFailure] = x match
case x: Tree @unchecked =>
x.tpe match
case _: Implicits.SearchFailureType => Some(x)
case _: dotc.typer.Implicits.SearchFailureType => Some(x)
case _ => None
case _ => None
end ImplicitSearchFailureTypeTest

object ImplicitSearchFailureMethodsImpl extends ImplicitSearchFailureMethods:
extension (self: ImplicitSearchFailure):
def explanation: String =
self.tpe.asInstanceOf[Implicits.SearchFailureType].explanation
self.tpe.asInstanceOf[dotc.typer.Implicits.SearchFailureType].explanation
end extension
end ImplicitSearchFailureMethodsImpl

Expand All @@ -2222,7 +2223,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
override def unapply(x: Any): Option[DivergingImplicit] = x match
case x: Tree @unchecked =>
x.tpe match
case _: Implicits.DivergingImplicit => Some(x)
case _: dotc.typer.Implicits.DivergingImplicit => Some(x)
case _ => None
case _ => None
end DivergingImplicitTypeTest
Expand All @@ -2234,7 +2235,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
override def unapply(x: Any): Option[NoMatchingImplicits] = x match
case x: Tree @unchecked =>
x.tpe match
case _: Implicits.NoMatchingImplicits => Some(x)
case _: dotc.typer.Implicits.NoMatchingImplicits => Some(x)
case _ => None
case _ => None
end NoMatchingImplicitsTypeTest
Expand All @@ -2246,7 +2247,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
override def unapply(x: Any): Option[AmbiguousImplicits] = x match
case x: Tree @unchecked =>
x.tpe match
case _: Implicits.AmbiguousImplicits => Some(x)
case _: dotc.typer.Implicits.AmbiguousImplicits => Some(x)
case _ => None
case _ => None
end AmbiguousImplicitsTypeTest
Expand Down
4 changes: 2 additions & 2 deletions library/src-bootstrapped/scala/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ object Expr {
ofTupleFromSeq(elems).asExprOf[Tuple.InverseMap[T, Expr]]
}

/** Find an implicit of type `T` in the current scope given by `qctx`.
/** Find a given instance of type `T` in the current scope.
* Return `Some` containing the expression of the implicit or
* `None` if implicit resolution failed.
*
Expand All @@ -192,7 +192,7 @@ object Expr {
*/
def summon[T](using tpe: Type[T])(using qctx: QuoteContext): Option[Expr[T]] = {
import qctx.tasty._
searchImplicit(tpe.unseal.tpe) match {
Implicits.search(tpe.unseal.tpe) match {
case iss: ImplicitSearchSuccess => Some(iss.tree.seal.asInstanceOf[Expr[T]])
case isf: ImplicitSearchFailure => None
}
Expand Down
19 changes: 11 additions & 8 deletions library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2493,15 +2493,18 @@ trait Reflection { reflection =>
// IMPLICIT SEARCH //
/////////////////////

// TODO: this should not be top level
/** Find an implicit of type `T` in the current scope given by `ctx`.
* Return an `ImplicitSearchResult`.
*
* @param tpe type of the implicit parameter
* @param ctx current context
*/
def searchImplicit(tpe: Type): ImplicitSearchResult
val Implicits: ImplicitsModule

trait ImplicitsModule { self: Implicits.type =>
/** Find a given instance of type `T` in the current scope provided by the current enclosing splice.
* Return an `ImplicitSearchResult`.
*
* @param tpe type of the implicit parameter
*/
def search(tpe: Type): ImplicitSearchResult
}

/** Result of a given instance search */
type ImplicitSearchResult <: AnyRef

given TypeTest[ImplicitSearchResult, ImplicitSearchSuccess] = ImplicitSearchSuccessTypeTest
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/delegate-match-1/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ inline def f: Any = ${ fImpl }

private def fImpl(using qctx: QuoteContext): Expr[Unit] = {
import qctx.tasty._
searchImplicit(('[A]).unseal.tpe) match {
Implicits.search(('[A]).unseal.tpe) match {
case x: ImplicitSearchSuccess =>
'{}
case x: DivergingImplicit => '{}
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/delegate-match-2/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ inline def f: Any = ${ fImpl }

private def fImpl (using qctx: QuoteContext) : Expr[Unit] = {
import qctx.tasty._
searchImplicit(('[A]).unseal.tpe) match {
Implicits.search(('[A]).unseal.tpe) match {
case x: ImplicitSearchSuccess =>
'{}
case x: DivergingImplicit => '{}
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/delegate-match-3/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ inline def f: Any = ${ fImpl }

private def fImpl(using qctx: QuoteContext) : Expr[Unit] = {
import qctx.tasty._
searchImplicit(('[A]).unseal.tpe) match {
Implicits.search(('[A]).unseal.tpe) match {
case x: ImplicitSearchSuccess =>
'{}
case x: DivergingImplicit => '{}
Expand Down