-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Use tree checker in macros #16570
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
Use tree checker in macros #16570
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e40dbf2
Fix unbound type variables after implicit search
nicolasstucki 83ded21
Move `Checker` from `class TreeChecker` to `object TreeChecker`
nicolasstucki 49df379
Use tree checker for macro expanded trees
nicolasstucki aaf86b3
Remove Mode.ImplicitsEnabled from TreeChecker
nicolasstucki 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
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,13 @@ | ||
import scala.compiletime.{erasedValue, summonFrom} | ||
|
||
import scala.quoted._ | ||
|
||
inline given summonAfterTypeMatch[T]: Any = | ||
${ summonAfterTypeMatchExpr[T] } | ||
|
||
private def summonAfterTypeMatchExpr[T: Type](using Quotes): Expr[Any] = | ||
Expr.summon[Foo[T]].get | ||
|
||
trait Foo[T] | ||
|
||
given IntFoo[T <: Int]: Foo[T] = ??? |
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 @@ | ||
def test: Unit = summonAfterTypeMatch[Int] |
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,30 @@ | ||
import scala.quoted._ | ||
import scala.deriving.Mirror | ||
|
||
trait Encoder[-A] | ||
|
||
trait PrimitiveEncoder[A] extends Encoder[A] | ||
|
||
given intOpt: PrimitiveEncoder[Option[Int]] with {} | ||
|
||
given primitiveNotNull[T](using e: Encoder[Option[T]]): PrimitiveEncoder[T] = | ||
new PrimitiveEncoder[T] {} | ||
|
||
transparent inline given fromMirror[A]: Any = ${ fromMirrorImpl[A] } | ||
|
||
def fromMirrorImpl[A : Type](using q: Quotes): Expr[Any] = | ||
Expr.summon[Mirror.Of[A]].get match | ||
case '{ ${mirror}: Mirror.ProductOf[A] { type MirroredElemTypes = elementTypes } } => | ||
val encoder = Type.of[elementTypes] match | ||
case '[tpe *: EmptyTuple] => | ||
Expr.summon[Encoder[tpe]].get | ||
|
||
encoder match | ||
case '{ ${encoder}: Encoder[tpe] } => // ok | ||
case _ => ??? | ||
|
||
encoder match | ||
case '{ ${encoder}: Encoder[tpe] } => // ok | ||
case _ => ??? | ||
|
||
encoder |
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 @@ | ||
case class JustInt(i: Int) | ||
|
||
val x = fromMirror[JustInt] |
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,30 @@ | ||
import scala.quoted.* | ||
|
||
trait ReproTransformer[A, B] { | ||
def transform(from: A): B | ||
} | ||
|
||
object ReproTransformer { | ||
final class Identity[A, B >: A] extends ReproTransformer[A, B] { | ||
def transform(from: A): B = from | ||
} | ||
|
||
given identity[A, B >: A]: Identity[A, B] = Identity[A, B] | ||
|
||
inline def getTransformer[A, B]: ReproTransformer[A, B] = ${ getTransformerMacro[A, B] } | ||
|
||
def getTransformerMacro[A, B](using quotes: Quotes, A: Type[A], B: Type[B]) = { | ||
import quotes.reflect.* | ||
|
||
val transformer = (A -> B) match { | ||
case '[a] -> '[b] => | ||
val summoned = Expr.summon[ReproTransformer[a, b]].get | ||
// ----------- INTERESTING STUFF STARTS HERE | ||
summoned match { | ||
case '{ $t: ReproTransformer[src, dest] } => t | ||
} | ||
// ----------- INTERESTING STUFF ENDS HERE | ||
} | ||
transformer.asExprOf[ReproTransformer[A, B]] | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After rebasing, the check caught this unintended bug in this neg test.