Skip to content

Commit 663134e

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 ff7068f commit 663134e

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
@@ -2111,14 +2111,16 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
21112111
end extension
21122112
end ConstantMethodsImpl
21132113

2114-
type ImplicitSearchResult = Tree
2114+
type ImplicitSearch = Tree
21152115

2116-
def searchImplicit(tpe: Type): ImplicitSearchResult =
2117-
ctx.typer.inferImplicitArg(tpe, rootPosition.span)
2116+
object ImplicitSearch extends ImplicitSearchModule:
2117+
def apply(tpe: Type): ImplicitSearch =
2118+
ctx.typer.inferImplicitArg(tpe, rootPosition.span)
2119+
end ImplicitSearch
21182120

21192121
type ImplicitSearchSuccess = Tree
21202122

2121-
object ImplicitSearchSuccessTypeTest extends TypeTest[ImplicitSearchResult, ImplicitSearchSuccess]:
2123+
object ImplicitSearchSuccessTypeTest extends TypeTest[ImplicitSearch, ImplicitSearchSuccess]:
21222124
def runtimeClass: Class[?] = classOf[ImplicitSearchSuccess]
21232125
override def unapply(x: Any): Option[ImplicitSearchSuccess] = x match
21242126
case x: Tree @unchecked =>
@@ -2136,7 +2138,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
21362138

21372139
type ImplicitSearchFailure = Tree
21382140

2139-
object ImplicitSearchFailureTypeTest extends TypeTest[ImplicitSearchResult, ImplicitSearchFailure]:
2141+
object ImplicitSearchFailureTypeTest extends TypeTest[ImplicitSearch, ImplicitSearchFailure]:
21402142
def runtimeClass: Class[?] = classOf[ImplicitSearchFailure]
21412143
override def unapply(x: Any): Option[ImplicitSearchFailure] = x match
21422144
case x: Tree @unchecked =>
@@ -2155,7 +2157,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
21552157

21562158
type DivergingImplicit = Tree
21572159

2158-
object DivergingImplicitTypeTest extends TypeTest[ImplicitSearchResult, DivergingImplicit]:
2160+
object DivergingImplicitTypeTest extends TypeTest[ImplicitSearch, DivergingImplicit]:
21592161
def runtimeClass: Class[?] = classOf[DivergingImplicit]
21602162
override def unapply(x: Any): Option[DivergingImplicit] = x match
21612163
case x: Tree @unchecked =>
@@ -2167,7 +2169,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
21672169

21682170
type NoMatchingImplicits = Tree
21692171

2170-
object NoMatchingImplicitsTypeTest extends TypeTest[ImplicitSearchResult, NoMatchingImplicits]:
2172+
object NoMatchingImplicitsTypeTest extends TypeTest[ImplicitSearch, NoMatchingImplicits]:
21712173
def runtimeClass: Class[?] = classOf[NoMatchingImplicits]
21722174
override def unapply(x: Any): Option[NoMatchingImplicits] = x match
21732175
case x: Tree @unchecked =>
@@ -2179,7 +2181,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
21792181

21802182
type AmbiguousImplicits = Tree
21812183

2182-
object AmbiguousImplicitsTypeTest extends TypeTest[ImplicitSearchResult, AmbiguousImplicits]:
2184+
object AmbiguousImplicitsTypeTest extends TypeTest[ImplicitSearch, AmbiguousImplicits]:
21832185
def runtimeClass: Class[?] = classOf[AmbiguousImplicits]
21842186
override def unapply(x: Any): Option[AmbiguousImplicits] = x match
21852187
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
@@ -2385,21 +2385,25 @@ trait Reflection { reflection =>
23852385
// IMPLICIT SEARCH //
23862386
/////////////////////
23872387

2388-
// TODO: this should not be top level
2389-
/** Find an implicit of type `T` in the current scope given by `ctx`.
2390-
* Return an `ImplicitSearchResult`.
2391-
*
2392-
* @param tpe type of the implicit parameter
2393-
* @param ctx current context
2394-
*/
2395-
def searchImplicit(tpe: Type): ImplicitSearchResult
2388+
/** Result of a given instance search */
2389+
type ImplicitSearch <: AnyRef
2390+
2391+
given TypeTest[ImplicitSearch, ImplicitSearchSuccess] = ImplicitSearchSuccessTypeTest
2392+
protected val ImplicitSearchSuccessTypeTest: TypeTest[ImplicitSearch, ImplicitSearchSuccess]
23962393

2397-
type ImplicitSearchResult <: AnyRef
2394+
val ImplicitSearch: ImplicitSearchModule
23982395

2399-
given TypeTest[ImplicitSearchResult, ImplicitSearchSuccess] = ImplicitSearchSuccessTypeTest
2400-
protected val ImplicitSearchSuccessTypeTest: TypeTest[ImplicitSearchResult, ImplicitSearchSuccess]
2396+
trait ImplicitSearchModule { self: ImplicitSearch.type =>
2397+
/** Find a given instance of type `T` in the current scope.
2398+
* Return an `ImplicitSearch`.
2399+
*
2400+
* @param tpe type of the implicit parameter
2401+
* @param ctx current context
2402+
*/
2403+
def apply(tpe: Type): ImplicitSearch
2404+
}
24012405

2402-
type ImplicitSearchSuccess <: ImplicitSearchResult
2406+
type ImplicitSearchSuccess <: ImplicitSearch
24032407

24042408
given ImplicitSearchSuccessMethods as ImplicitSearchSuccessMethods = ImplicitSearchSuccessMethodsImpl
24052409
protected val ImplicitSearchSuccessMethodsImpl: ImplicitSearchSuccessMethods
@@ -2410,10 +2414,10 @@ trait Reflection { reflection =>
24102414
end extension
24112415
end ImplicitSearchSuccessMethods
24122416

2413-
type ImplicitSearchFailure <: ImplicitSearchResult
2417+
type ImplicitSearchFailure <: ImplicitSearch
24142418

2415-
given TypeTest[ImplicitSearchResult, ImplicitSearchFailure] = ImplicitSearchFailureTypeTest
2416-
protected val ImplicitSearchFailureTypeTest: TypeTest[ImplicitSearchResult, ImplicitSearchFailure]
2419+
given TypeTest[ImplicitSearch, ImplicitSearchFailure] = ImplicitSearchFailureTypeTest
2420+
protected val ImplicitSearchFailureTypeTest: TypeTest[ImplicitSearch, ImplicitSearchFailure]
24172421

24182422
given ImplicitSearchFailureMethods as ImplicitSearchFailureMethods = ImplicitSearchFailureMethodsImpl
24192423
protected val ImplicitSearchFailureMethodsImpl: ImplicitSearchFailureMethods
@@ -2426,18 +2430,18 @@ trait Reflection { reflection =>
24262430

24272431
type DivergingImplicit <: ImplicitSearchFailure
24282432

2429-
given TypeTest[ImplicitSearchResult, DivergingImplicit] = DivergingImplicitTypeTest
2430-
protected val DivergingImplicitTypeTest: TypeTest[ImplicitSearchResult, DivergingImplicit]
2433+
given TypeTest[ImplicitSearch, DivergingImplicit] = DivergingImplicitTypeTest
2434+
protected val DivergingImplicitTypeTest: TypeTest[ImplicitSearch, DivergingImplicit]
24312435

24322436
type NoMatchingImplicits <: ImplicitSearchFailure
24332437

2434-
given TypeTest[ImplicitSearchResult, NoMatchingImplicits] = NoMatchingImplicitsTypeTest
2435-
protected val NoMatchingImplicitsTypeTest: TypeTest[ImplicitSearchResult, NoMatchingImplicits]
2438+
given TypeTest[ImplicitSearch, NoMatchingImplicits] = NoMatchingImplicitsTypeTest
2439+
protected val NoMatchingImplicitsTypeTest: TypeTest[ImplicitSearch, NoMatchingImplicits]
24362440

24372441
type AmbiguousImplicits <: ImplicitSearchFailure
24382442

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

24422446
/////////////
24432447
// 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)