From 2fdc20b2901d1b640ff51f5b1171e601f79e2120 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Thu, 24 Sep 2020 10:05:53 +0200 Subject: [PATCH] Refactor Reflect implicit search Move implicit search method into implicit search module. This makes the Relfect API more design more homogeneous. --- community-build/community-projects/shapeless | 2 +- .../tools/dotc/quoted/QuoteContextImpl.scala | 21 ++++++++++--------- .../src-bootstrapped/scala/quoted/Expr.scala | 4 ++-- library/src/scala/tasty/Reflection.scala | 19 ++++++++++------- .../neg-macros/delegate-match-1/Macro_1.scala | 2 +- .../neg-macros/delegate-match-2/Macro_1.scala | 2 +- .../neg-macros/delegate-match-3/Macro_1.scala | 2 +- 7 files changed, 28 insertions(+), 24 deletions(-) diff --git a/community-build/community-projects/shapeless b/community-build/community-projects/shapeless index 9f6cb180bdab..b4c2d61ac785 160000 --- a/community-build/community-projects/shapeless +++ b/community-build/community-projects/shapeless @@ -1 +1 @@ -Subproject commit 9f6cb180bdabc09886c2665ed17ed8bc1ea77afd +Subproject commit b4c2d61ac785720c8ea33eda51104af15fb007d8 diff --git a/compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala b/compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala index 99378f0ea37c..54db94783d8a 100644 --- a/compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala +++ b/compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala @@ -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 @@ -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 @@ -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 @@ -2203,7 +2204,7 @@ 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 @@ -2211,7 +2212,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext: 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 @@ -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 @@ -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 @@ -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 diff --git a/library/src-bootstrapped/scala/quoted/Expr.scala b/library/src-bootstrapped/scala/quoted/Expr.scala index 8218d2115f97..7baa7048900f 100644 --- a/library/src-bootstrapped/scala/quoted/Expr.scala +++ b/library/src-bootstrapped/scala/quoted/Expr.scala @@ -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. * @@ -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 } diff --git a/library/src/scala/tasty/Reflection.scala b/library/src/scala/tasty/Reflection.scala index 9466b81751ee..2cd36d1db6df 100644 --- a/library/src/scala/tasty/Reflection.scala +++ b/library/src/scala/tasty/Reflection.scala @@ -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 diff --git a/tests/neg-macros/delegate-match-1/Macro_1.scala b/tests/neg-macros/delegate-match-1/Macro_1.scala index b08ab3f342fc..f6a006deb485 100644 --- a/tests/neg-macros/delegate-match-1/Macro_1.scala +++ b/tests/neg-macros/delegate-match-1/Macro_1.scala @@ -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 => '{} diff --git a/tests/neg-macros/delegate-match-2/Macro_1.scala b/tests/neg-macros/delegate-match-2/Macro_1.scala index 6ac29dea0b32..f4226053a08e 100644 --- a/tests/neg-macros/delegate-match-2/Macro_1.scala +++ b/tests/neg-macros/delegate-match-2/Macro_1.scala @@ -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 => '{} diff --git a/tests/neg-macros/delegate-match-3/Macro_1.scala b/tests/neg-macros/delegate-match-3/Macro_1.scala index e97d1d4329fb..93a8d32a501c 100644 --- a/tests/neg-macros/delegate-match-3/Macro_1.scala +++ b/tests/neg-macros/delegate-match-3/Macro_1.scala @@ -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 => '{}