-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Allow to search for implicits in macros #6819
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package scala.quoted | ||
|
||
package object matching { | ||
|
||
/** Find an implicit of type `T` in the current scope given by `qctx`. | ||
* Return `Some` containing the expression of the implicit or | ||
* `None` if implicit resolution failed. | ||
* | ||
* @tparam T type of the implicit parameter | ||
* @param tpe quoted type of the implicit parameter | ||
* @param qctx current context | ||
*/ | ||
def searchImplicitExpr[T] given (tpe: Type[T], qctx: QuoteContext): Option[Expr[T]] = { | ||
import qctx.tasty._ | ||
searchImplicit(tpe.unseal.tpe) match { | ||
case IsImplicitSearchSuccess(iss) => Some(iss.tree.seal.asInstanceOf[Expr[T]]) | ||
case IsImplicitSearchFailure(isf) => None | ||
} | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package scala.tasty.reflect | ||
|
||
trait ImplicitsOps extends Core { | ||
|
||
def searchImplicit(tpe: Type)(implicit ctx: Context): ImplicitSearchResult = | ||
kernel.searchImplicit(tpe) | ||
|
||
object IsImplicitSearchSuccess { | ||
def unapply(isr: ImplicitSearchResult)(implicit ctx: Context): Option[ImplicitSearchSuccess] = | ||
kernel.matchImplicitSearchSuccess(isr) | ||
} | ||
|
||
implicit class IsImplicitSearchSuccessAPI(self: ImplicitSearchSuccess) { | ||
def tree(implicit ctx: Context): Term = kernel.ImplicitSearchSuccess_tree(self) | ||
} | ||
|
||
object IsImplicitSearchFailure { | ||
def unapply(isr: ImplicitSearchResult)(implicit ctx: Context): Option[ImplicitSearchFailure] = | ||
kernel.matchImplicitSearchFailure(isr) | ||
} | ||
|
||
implicit class ImplicitSearchFailureAPI(self: ImplicitSearchFailure) { | ||
def explanation(implicit ctx: Context): String = kernel.ImplicitSearchFailure_explanation(self) | ||
} | ||
|
||
object IsDivergingImplicit { | ||
def unapply(isr: ImplicitSearchResult)(implicit ctx: Context): Option[DivergingImplicit] = | ||
kernel.matchDivergingImplicit(isr) | ||
} | ||
|
||
object IsNoMatchingImplicits { | ||
def unapply(isr: ImplicitSearchResult)(implicit ctx: Context): Option[NoMatchingImplicits] = | ||
kernel.matchNoMatchingImplicits(isr) | ||
} | ||
|
||
object IsAmbiguousImplicits { | ||
def unapply(isr: ImplicitSearchResult)(implicit ctx: Context): Option[AmbiguousImplicits] = | ||
kernel.matchAmbiguousImplicits(isr) | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
-- Error: tests/neg-macros/delegate-match-1/Test_2.scala:6:2 ----------------------------------------------------------- | ||
6 | f // error | ||
| ^ | ||
| AmbiguousImplicits | ||
| both value a1 in class Test1 and value a2 in class Test1 match type A | ||
| This location is in code that was inlined at Test_2.scala:6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import scala.quoted._ | ||
import scala.quoted.matching._ | ||
|
||
inline def f: Any = ${ fImpl } | ||
|
||
private def fImpl given (qctx: QuoteContext): Expr[Unit] = { | ||
import qctx.tasty._ | ||
searchImplicit(('[A]).unseal.tpe) match { | ||
case IsImplicitSearchSuccess(x) => | ||
'{} | ||
case IsDivergingImplicit(x) => '{} | ||
error("DivergingImplicit\n" + x.explanation, rootPosition) | ||
'{} | ||
case IsNoMatchingImplicits(x) => | ||
error("NoMatchingImplicits\n" + x.explanation, rootPosition) | ||
'{} | ||
case IsAmbiguousImplicits(x) => | ||
error("AmbiguousImplicits\n" + x.explanation, rootPosition) | ||
'{} | ||
} | ||
} | ||
|
||
class A |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
class Test1 extends App { | ||
|
||
implicit val a1: A = new A | ||
implicit val a2: A = new A | ||
f // error | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
-- Error: tests/neg-macros/delegate-match-2/Test_2.scala:5:2 ----------------------------------------------------------- | ||
5 | f // error | ||
| ^ | ||
| DivergingImplicit | ||
| method a1 in class Test produces a diverging implicit search when trying to match type A | ||
| This location is in code that was inlined at Test_2.scala:5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import scala.quoted._ | ||
import scala.quoted.matching._ | ||
|
||
inline def f: Any = ${ fImpl } | ||
|
||
private def fImpl given (qctx: QuoteContext): Expr[Unit] = { | ||
import qctx.tasty._ | ||
searchImplicit(('[A]).unseal.tpe) match { | ||
case IsImplicitSearchSuccess(x) => | ||
'{} | ||
case IsDivergingImplicit(x) => '{} | ||
error("DivergingImplicit\n" + x.explanation, rootPosition) | ||
'{} | ||
case IsNoMatchingImplicits(x) => | ||
error("NoMatchingImplicits\n" + x.explanation, rootPosition) | ||
'{} | ||
case IsAmbiguousImplicits(x) => | ||
error("AmbiguousImplicits\n" + x.explanation, rootPosition) | ||
'{} | ||
} | ||
} | ||
|
||
class A |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
class Test extends App { | ||
|
||
implicit def a1(implicit a: A): A = new A | ||
f // error | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
-- Error: tests/neg-macros/delegate-match-3/Test_2.scala:3:2 ----------------------------------------------------------- | ||
3 | f // error | ||
| ^ | ||
| NoMatchingImplicits | ||
| no implicit values were found that match type A | ||
| This location is in code that was inlined at Test_2.scala:3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import scala.quoted._ | ||
import scala.quoted.matching._ | ||
|
||
inline def f: Any = ${ fImpl } | ||
|
||
private def fImpl given (qctx: QuoteContext): Expr[Unit] = { | ||
import qctx.tasty._ | ||
searchImplicit(('[A]).unseal.tpe) match { | ||
case IsImplicitSearchSuccess(x) => | ||
'{} | ||
case IsDivergingImplicit(x) => '{} | ||
error("DivergingImplicit\n" + x.explanation, rootPosition) | ||
'{} | ||
case IsNoMatchingImplicits(x) => | ||
error("NoMatchingImplicits\n" + x.explanation, rootPosition) | ||
'{} | ||
case IsAmbiguousImplicits(x) => | ||
error("AmbiguousImplicits\n" + x.explanation, rootPosition) | ||
'{} | ||
} | ||
} | ||
|
||
class A |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
class Test extends App { | ||
f // error | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class scala.collection.immutable.TreeSet | ||
class scala.collection.immutable.HashSet | ||
B |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import collection.immutable.TreeSet | ||
import collection.immutable.HashSet | ||
import scala.quoted._ | ||
import scala.quoted.matching._ | ||
|
||
inline def f1[T]() = ${ f1Impl[T] } | ||
|
||
def f1Impl[T: Type] given QuoteContext = { | ||
searchImplicitExpr[Ordering[T]] match { | ||
case Some(ord) => '{ new TreeSet[T]()($ord) } | ||
case _ => '{ new HashSet[T] } | ||
} | ||
} | ||
|
||
class A | ||
class B | ||
|
||
inline def g = ${ gImpl } | ||
|
||
def gImpl given QuoteContext = { | ||
if (searchImplicitExpr[A].isDefined) '{ println("A") } | ||
else if (searchImplicitExpr[B].isDefined) '{ println("B") } | ||
else throw new MatchError("") | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
object Test extends App { | ||
|
||
implicitly[Ordering[String]] | ||
|
||
println(f1[String]().getClass) | ||
println(f1[AnyRef]().getClass) | ||
|
||
implicit val b: B = new B | ||
implicitly[B] | ||
g | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.