Skip to content

Commit fcf96de

Browse files
committed
Refactor Reflect implicit search
Move implicit search method into implicit search module. This makes the Relfect API more design more homogeneous.
1 parent 01fc4a1 commit fcf96de

File tree

7 files changed

+41
-35
lines changed

7 files changed

+41
-35
lines changed

compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,14 +2118,16 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
21182118
end extension
21192119
end IdMethodsImpl
21202120

2121-
type ImplicitSearchResult = Tree
2121+
type ImplicitSearch = Tree
21222122

2123-
def searchImplicit(tpe: Type): ImplicitSearchResult =
2124-
ctx.typer.inferImplicitArg(tpe, rootPosition.span)
2123+
object ImplicitSearch extends ImplicitSearchModule:
2124+
def apply(tpe: Type): ImplicitSearch =
2125+
ctx.typer.inferImplicitArg(tpe, rootPosition.span)
2126+
end ImplicitSearch
21252127

21262128
type ImplicitSearchSuccess = Tree
21272129

2128-
object ImplicitSearchSuccessTypeTest extends TypeTest[ImplicitSearchResult, ImplicitSearchSuccess]:
2130+
object ImplicitSearchSuccessTypeTest extends TypeTest[ImplicitSearch, ImplicitSearchSuccess]:
21292131
def runtimeClass: Class[?] = classOf[ImplicitSearchSuccess]
21302132
override def unapply(x: Any): Option[ImplicitSearchSuccess] = x match
21312133
case x: Tree @unchecked =>
@@ -2143,7 +2145,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
21432145

21442146
type ImplicitSearchFailure = Tree
21452147

2146-
object ImplicitSearchFailureTypeTest extends TypeTest[ImplicitSearchResult, ImplicitSearchFailure]:
2148+
object ImplicitSearchFailureTypeTest extends TypeTest[ImplicitSearch, ImplicitSearchFailure]:
21472149
def runtimeClass: Class[?] = classOf[ImplicitSearchFailure]
21482150
override def unapply(x: Any): Option[ImplicitSearchFailure] = x match
21492151
case x: Tree @unchecked =>
@@ -2162,7 +2164,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
21622164

21632165
type DivergingImplicit = Tree
21642166

2165-
object DivergingImplicitTypeTest extends TypeTest[ImplicitSearchResult, DivergingImplicit]:
2167+
object DivergingImplicitTypeTest extends TypeTest[ImplicitSearch, DivergingImplicit]:
21662168
def runtimeClass: Class[?] = classOf[DivergingImplicit]
21672169
override def unapply(x: Any): Option[DivergingImplicit] = x match
21682170
case x: Tree @unchecked =>
@@ -2174,7 +2176,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
21742176

21752177
type NoMatchingImplicits = Tree
21762178

2177-
object NoMatchingImplicitsTypeTest extends TypeTest[ImplicitSearchResult, NoMatchingImplicits]:
2179+
object NoMatchingImplicitsTypeTest extends TypeTest[ImplicitSearch, NoMatchingImplicits]:
21782180
def runtimeClass: Class[?] = classOf[NoMatchingImplicits]
21792181
override def unapply(x: Any): Option[NoMatchingImplicits] = x match
21802182
case x: Tree @unchecked =>
@@ -2186,7 +2188,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
21862188

21872189
type AmbiguousImplicits = Tree
21882190

2189-
object AmbiguousImplicitsTypeTest extends TypeTest[ImplicitSearchResult, AmbiguousImplicits]:
2191+
object AmbiguousImplicitsTypeTest extends TypeTest[ImplicitSearch, AmbiguousImplicits]:
21902192
def runtimeClass: Class[?] = classOf[AmbiguousImplicits]
21912193
override def unapply(x: Any): Option[AmbiguousImplicits] = x match
21922194
case x: Tree @unchecked =>

library/src-bootstrapped/scala/quoted/Expr.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ object Expr {
180180
ofTupleFromSeq(elems).asExprOf[Tuple.InverseMap[T, Expr]]
181181
}
182182

183-
/** Find an implicit of type `T` in the current scope given by `qctx`.
183+
/** Find a given instance of type `T` in the current scope.
184184
* Return `Some` containing the expression of the implicit or
185185
* `None` if implicit resolution failed.
186186
*
@@ -190,7 +190,7 @@ object Expr {
190190
*/
191191
def summon[T](using tpe: Type[T])(using qctx: QuoteContext): Option[Expr[T]] = {
192192
import qctx.tasty._
193-
searchImplicit(tpe.unseal.tpe) match {
193+
ImplicitSearch(tpe.unseal.tpe) match {
194194
case iss: ImplicitSearchSuccess => Some(iss.tree.seal.asInstanceOf[Expr[T]])
195195
case isf: ImplicitSearchFailure => None
196196
}

library/src/scala/tasty/Reflection.scala

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2409,21 +2409,25 @@ trait Reflection { reflection =>
24092409
// IMPLICIT SEARCH //
24102410
/////////////////////
24112411

2412-
// TODO: this should not be top level
2413-
/** Find an implicit of type `T` in the current scope given by `ctx`.
2414-
* Return an `ImplicitSearchResult`.
2415-
*
2416-
* @param tpe type of the implicit parameter
2417-
* @param ctx current context
2418-
*/
2419-
def searchImplicit(tpe: Type): ImplicitSearchResult
2412+
/** Result of a given instance search */
2413+
type ImplicitSearch <: AnyRef
2414+
2415+
given TypeTest[ImplicitSearch, ImplicitSearchSuccess] = ImplicitSearchSuccessTypeTest
2416+
protected val ImplicitSearchSuccessTypeTest: TypeTest[ImplicitSearch, ImplicitSearchSuccess]
24202417

2421-
type ImplicitSearchResult <: AnyRef
2418+
val ImplicitSearch: ImplicitSearchModule
24222419

2423-
given TypeTest[ImplicitSearchResult, ImplicitSearchSuccess] = ImplicitSearchSuccessTypeTest
2424-
protected val ImplicitSearchSuccessTypeTest: TypeTest[ImplicitSearchResult, ImplicitSearchSuccess]
2420+
trait ImplicitSearchModule { self: ImplicitSearch.type =>
2421+
/** Find a given instance of type `T` in the current scope.
2422+
* Return an `ImplicitSearch`.
2423+
*
2424+
* @param tpe type of the implicit parameter
2425+
* @param ctx current context
2426+
*/
2427+
def apply(tpe: Type): ImplicitSearch
2428+
}
24252429

2426-
type ImplicitSearchSuccess <: ImplicitSearchResult
2430+
type ImplicitSearchSuccess <: ImplicitSearch
24272431

24282432
given ImplicitSearchSuccessMethods as ImplicitSearchSuccessMethods = ImplicitSearchSuccessMethodsImpl
24292433
protected val ImplicitSearchSuccessMethodsImpl: ImplicitSearchSuccessMethods
@@ -2434,10 +2438,10 @@ trait Reflection { reflection =>
24342438
end extension
24352439
end ImplicitSearchSuccessMethods
24362440

2437-
type ImplicitSearchFailure <: ImplicitSearchResult
2441+
type ImplicitSearchFailure <: ImplicitSearch
24382442

2439-
given TypeTest[ImplicitSearchResult, ImplicitSearchFailure] = ImplicitSearchFailureTypeTest
2440-
protected val ImplicitSearchFailureTypeTest: TypeTest[ImplicitSearchResult, ImplicitSearchFailure]
2443+
given TypeTest[ImplicitSearch, ImplicitSearchFailure] = ImplicitSearchFailureTypeTest
2444+
protected val ImplicitSearchFailureTypeTest: TypeTest[ImplicitSearch, ImplicitSearchFailure]
24412445

24422446
given ImplicitSearchFailureMethods as ImplicitSearchFailureMethods = ImplicitSearchFailureMethodsImpl
24432447
protected val ImplicitSearchFailureMethodsImpl: ImplicitSearchFailureMethods
@@ -2450,18 +2454,18 @@ trait Reflection { reflection =>
24502454

24512455
type DivergingImplicit <: ImplicitSearchFailure
24522456

2453-
given TypeTest[ImplicitSearchResult, DivergingImplicit] = DivergingImplicitTypeTest
2454-
protected val DivergingImplicitTypeTest: TypeTest[ImplicitSearchResult, DivergingImplicit]
2457+
given TypeTest[ImplicitSearch, DivergingImplicit] = DivergingImplicitTypeTest
2458+
protected val DivergingImplicitTypeTest: TypeTest[ImplicitSearch, DivergingImplicit]
24552459

24562460
type NoMatchingImplicits <: ImplicitSearchFailure
24572461

2458-
given TypeTest[ImplicitSearchResult, NoMatchingImplicits] = NoMatchingImplicitsTypeTest
2459-
protected val NoMatchingImplicitsTypeTest: TypeTest[ImplicitSearchResult, NoMatchingImplicits]
2462+
given TypeTest[ImplicitSearch, NoMatchingImplicits] = NoMatchingImplicitsTypeTest
2463+
protected val NoMatchingImplicitsTypeTest: TypeTest[ImplicitSearch, NoMatchingImplicits]
24602464

24612465
type AmbiguousImplicits <: ImplicitSearchFailure
24622466

2463-
given TypeTest[ImplicitSearchResult, AmbiguousImplicits] = AmbiguousImplicitsTypeTest
2464-
protected val AmbiguousImplicitsTypeTest: TypeTest[ImplicitSearchResult, AmbiguousImplicits]
2467+
given TypeTest[ImplicitSearch, AmbiguousImplicits] = AmbiguousImplicitsTypeTest
2468+
protected val AmbiguousImplicitsTypeTest: TypeTest[ImplicitSearch, AmbiguousImplicits]
24652469

24662470
/////////////
24672471
// SYMBOLS //

tests/neg-macros/delegate-match-1/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ inline def f: Any = ${ fImpl }
55

66
private def fImpl(using qctx: QuoteContext): Expr[Unit] = {
77
import qctx.tasty._
8-
searchImplicit(('[A]).unseal.tpe) match {
8+
ImplicitSearch(('[A]).unseal.tpe) match {
99
case x: ImplicitSearchSuccess =>
1010
'{}
1111
case x: DivergingImplicit => '{}

tests/neg-macros/delegate-match-2/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ inline def f: Any = ${ fImpl }
55

66
private def fImpl (using qctx: QuoteContext) : Expr[Unit] = {
77
import qctx.tasty._
8-
searchImplicit(('[A]).unseal.tpe) match {
8+
ImplicitSearch(('[A]).unseal.tpe) match {
99
case x: ImplicitSearchSuccess =>
1010
'{}
1111
case x: DivergingImplicit => '{}

tests/neg-macros/delegate-match-3/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ inline def f: Any = ${ fImpl }
55

66
private def fImpl(using qctx: QuoteContext) : Expr[Unit] = {
77
import qctx.tasty._
8-
searchImplicit(('[A]).unseal.tpe) match {
8+
ImplicitSearch(('[A]).unseal.tpe) match {
99
case x: ImplicitSearchSuccess =>
1010
'{}
1111
case x: DivergingImplicit => '{}

0 commit comments

Comments
 (0)