Skip to content

Commit 83e93a8

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 4b8a1de commit 83e93a8

File tree

7 files changed

+46
-42
lines changed

7 files changed

+46
-42
lines changed

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import dotty.tools.dotc.core.NameKinds
1111
import dotty.tools.dotc.core.StdNames._
1212
import dotty.tools.dotc.quoted.reflect._
1313
import dotty.tools.dotc.core.Decorators._
14-
import dotty.tools.dotc.typer.Implicits
1514

1615
import scala.quoted.QuoteContext
1716
import scala.quoted.show.SyntaxHighlight
@@ -2173,19 +2172,21 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
21732172
end extension
21742173
end ConstantMethodsImpl
21752174

2176-
type ImplicitSearchResult = Tree
2175+
object Implicits extends ImplicitsModule:
2176+
def search(tpe: Type): ImplicitSearch =
2177+
ctx.typer.inferImplicitArg(tpe, rootPosition.span)
2178+
end Implicits
21772179

2178-
def searchImplicit(tpe: Type): ImplicitSearchResult =
2179-
ctx.typer.inferImplicitArg(tpe, rootPosition.span)
2180+
type ImplicitSearch = Tree
21802181

21812182
type ImplicitSearchSuccess = Tree
21822183

2183-
object ImplicitSearchSuccessTypeTest extends TypeTest[ImplicitSearchResult, ImplicitSearchSuccess]:
2184+
object ImplicitSearchSuccessTypeTest extends TypeTest[ImplicitSearch, ImplicitSearchSuccess]:
21842185
def runtimeClass: Class[?] = classOf[ImplicitSearchSuccess]
21852186
override def unapply(x: Any): Option[ImplicitSearchSuccess] = x match
21862187
case x: Tree @unchecked =>
21872188
x.tpe match
2188-
case _: Implicits.SearchFailureType => None
2189+
case _: dotc.typer.Implicits.SearchFailureType => None
21892190
case _ => Some(x)
21902191
case _ => None
21912192
end ImplicitSearchSuccessTypeTest
@@ -2198,55 +2199,55 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
21982199

21992200
type ImplicitSearchFailure = Tree
22002201

2201-
object ImplicitSearchFailureTypeTest extends TypeTest[ImplicitSearchResult, ImplicitSearchFailure]:
2202+
object ImplicitSearchFailureTypeTest extends TypeTest[ImplicitSearch, ImplicitSearchFailure]:
22022203
def runtimeClass: Class[?] = classOf[ImplicitSearchFailure]
22032204
override def unapply(x: Any): Option[ImplicitSearchFailure] = x match
22042205
case x: Tree @unchecked =>
22052206
x.tpe match
2206-
case _: Implicits.SearchFailureType => Some(x)
2207+
case _: dotc.typer.Implicits.SearchFailureType => Some(x)
22072208
case _ => None
22082209
case _ => None
22092210
end ImplicitSearchFailureTypeTest
22102211

22112212
object ImplicitSearchFailureMethodsImpl extends ImplicitSearchFailureMethods:
22122213
extension (self: ImplicitSearchFailure):
22132214
def explanation: String =
2214-
self.tpe.asInstanceOf[Implicits.SearchFailureType].explanation
2215+
self.tpe.asInstanceOf[dotc.typer.Implicits.SearchFailureType].explanation
22152216
end extension
22162217
end ImplicitSearchFailureMethodsImpl
22172218

22182219
type DivergingImplicit = Tree
22192220

2220-
object DivergingImplicitTypeTest extends TypeTest[ImplicitSearchResult, DivergingImplicit]:
2221+
object DivergingImplicitTypeTest extends TypeTest[ImplicitSearch, DivergingImplicit]:
22212222
def runtimeClass: Class[?] = classOf[DivergingImplicit]
22222223
override def unapply(x: Any): Option[DivergingImplicit] = x match
22232224
case x: Tree @unchecked =>
22242225
x.tpe match
2225-
case _: Implicits.DivergingImplicit => Some(x)
2226+
case _: dotc.typer.Implicits.DivergingImplicit => Some(x)
22262227
case _ => None
22272228
case _ => None
22282229
end DivergingImplicitTypeTest
22292230

22302231
type NoMatchingImplicits = Tree
22312232

2232-
object NoMatchingImplicitsTypeTest extends TypeTest[ImplicitSearchResult, NoMatchingImplicits]:
2233+
object NoMatchingImplicitsTypeTest extends TypeTest[ImplicitSearch, NoMatchingImplicits]:
22332234
def runtimeClass: Class[?] = classOf[NoMatchingImplicits]
22342235
override def unapply(x: Any): Option[NoMatchingImplicits] = x match
22352236
case x: Tree @unchecked =>
22362237
x.tpe match
2237-
case _: Implicits.NoMatchingImplicits => Some(x)
2238+
case _: dotc.typer.Implicits.NoMatchingImplicits => Some(x)
22382239
case _ => None
22392240
case _ => None
22402241
end NoMatchingImplicitsTypeTest
22412242

22422243
type AmbiguousImplicits = Tree
22432244

2244-
object AmbiguousImplicitsTypeTest extends TypeTest[ImplicitSearchResult, AmbiguousImplicits]:
2245+
object AmbiguousImplicitsTypeTest extends TypeTest[ImplicitSearch, AmbiguousImplicits]:
22452246
def runtimeClass: Class[?] = classOf[AmbiguousImplicits]
22462247
override def unapply(x: Any): Option[AmbiguousImplicits] = x match
22472248
case x: Tree @unchecked =>
22482249
x.tpe match
2249-
case _: Implicits.AmbiguousImplicits => Some(x)
2250+
case _: dotc.typer.Implicits.AmbiguousImplicits => Some(x)
22502251
case _ => None
22512252
case _ => None
22522253
end AmbiguousImplicitsTypeTest

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

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

185-
/** Find an implicit of type `T` in the current scope given by `qctx`.
185+
/** Find a given instance of type `T` in the current scope.
186186
* Return `Some` containing the expression of the implicit or
187187
* `None` if implicit resolution failed.
188188
*
@@ -192,7 +192,7 @@ object Expr {
192192
*/
193193
def summon[T](using tpe: Type[T])(using qctx: QuoteContext): Option[Expr[T]] = {
194194
import qctx.tasty._
195-
searchImplicit(tpe.unseal.tpe) match {
195+
Implicits.search(tpe.unseal.tpe) match {
196196
case iss: ImplicitSearchSuccess => Some(iss.tree.seal.asInstanceOf[Expr[T]])
197197
case isf: ImplicitSearchFailure => None
198198
}

library/src/scala/tasty/Reflection.scala

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,21 +2493,24 @@ trait Reflection { reflection =>
24932493
// IMPLICIT SEARCH //
24942494
/////////////////////
24952495

2496-
// TODO: this should not be top level
2497-
/** Find an implicit of type `T` in the current scope given by `ctx`.
2498-
* Return an `ImplicitSearchResult`.
2499-
*
2500-
* @param tpe type of the implicit parameter
2501-
* @param ctx current context
2502-
*/
2503-
def searchImplicit(tpe: Type): ImplicitSearchResult
2496+
val Implicits: ImplicitsModule
2497+
2498+
trait ImplicitsModule { self: Implicits.type =>
2499+
/** Find a given instance of type `T` in the current scope.
2500+
* Return an `ImplicitSearch`.
2501+
*
2502+
* @param tpe type of the implicit parameter
2503+
*/
2504+
def search(tpe: Type): ImplicitSearch
2505+
}
25042506

2505-
type ImplicitSearchResult <: AnyRef
2507+
/** Result of a given instance search */
2508+
type ImplicitSearch <: AnyRef
25062509

2507-
given TypeTest[ImplicitSearchResult, ImplicitSearchSuccess] = ImplicitSearchSuccessTypeTest
2508-
protected val ImplicitSearchSuccessTypeTest: TypeTest[ImplicitSearchResult, ImplicitSearchSuccess]
2510+
given TypeTest[ImplicitSearch, ImplicitSearchSuccess] = ImplicitSearchSuccessTypeTest
2511+
protected val ImplicitSearchSuccessTypeTest: TypeTest[ImplicitSearch, ImplicitSearchSuccess]
25092512

2510-
type ImplicitSearchSuccess <: ImplicitSearchResult
2513+
type ImplicitSearchSuccess <: ImplicitSearch
25112514

25122515
given ImplicitSearchSuccessMethods as ImplicitSearchSuccessMethods = ImplicitSearchSuccessMethodsImpl
25132516
protected val ImplicitSearchSuccessMethodsImpl: ImplicitSearchSuccessMethods
@@ -2518,10 +2521,10 @@ trait Reflection { reflection =>
25182521
end extension
25192522
end ImplicitSearchSuccessMethods
25202523

2521-
type ImplicitSearchFailure <: ImplicitSearchResult
2524+
type ImplicitSearchFailure <: ImplicitSearch
25222525

2523-
given TypeTest[ImplicitSearchResult, ImplicitSearchFailure] = ImplicitSearchFailureTypeTest
2524-
protected val ImplicitSearchFailureTypeTest: TypeTest[ImplicitSearchResult, ImplicitSearchFailure]
2526+
given TypeTest[ImplicitSearch, ImplicitSearchFailure] = ImplicitSearchFailureTypeTest
2527+
protected val ImplicitSearchFailureTypeTest: TypeTest[ImplicitSearch, ImplicitSearchFailure]
25252528

25262529
given ImplicitSearchFailureMethods as ImplicitSearchFailureMethods = ImplicitSearchFailureMethodsImpl
25272530
protected val ImplicitSearchFailureMethodsImpl: ImplicitSearchFailureMethods
@@ -2534,18 +2537,18 @@ trait Reflection { reflection =>
25342537

25352538
type DivergingImplicit <: ImplicitSearchFailure
25362539

2537-
given TypeTest[ImplicitSearchResult, DivergingImplicit] = DivergingImplicitTypeTest
2538-
protected val DivergingImplicitTypeTest: TypeTest[ImplicitSearchResult, DivergingImplicit]
2540+
given TypeTest[ImplicitSearch, DivergingImplicit] = DivergingImplicitTypeTest
2541+
protected val DivergingImplicitTypeTest: TypeTest[ImplicitSearch, DivergingImplicit]
25392542

25402543
type NoMatchingImplicits <: ImplicitSearchFailure
25412544

2542-
given TypeTest[ImplicitSearchResult, NoMatchingImplicits] = NoMatchingImplicitsTypeTest
2543-
protected val NoMatchingImplicitsTypeTest: TypeTest[ImplicitSearchResult, NoMatchingImplicits]
2545+
given TypeTest[ImplicitSearch, NoMatchingImplicits] = NoMatchingImplicitsTypeTest
2546+
protected val NoMatchingImplicitsTypeTest: TypeTest[ImplicitSearch, NoMatchingImplicits]
25442547

25452548
type AmbiguousImplicits <: ImplicitSearchFailure
25462549

2547-
given TypeTest[ImplicitSearchResult, AmbiguousImplicits] = AmbiguousImplicitsTypeTest
2548-
protected val AmbiguousImplicitsTypeTest: TypeTest[ImplicitSearchResult, AmbiguousImplicits]
2550+
given TypeTest[ImplicitSearch, AmbiguousImplicits] = AmbiguousImplicitsTypeTest
2551+
protected val AmbiguousImplicitsTypeTest: TypeTest[ImplicitSearch, AmbiguousImplicits]
25492552

25502553
/////////////
25512554
// 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+
Implicits.search(('[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+
Implicits.search(('[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+
Implicits.search(('[A]).unseal.tpe) match {
99
case x: ImplicitSearchSuccess =>
1010
'{}
1111
case x: DivergingImplicit => '{}

0 commit comments

Comments
 (0)