-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Possible bug in tasty reflection regarding matching of trees #12253
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
Comments
The waarning and error are correct. -- Warning: Foo.scala:11:10 ----------------------------------------------------
11 | case extractors.InlinedLambda(_, Select(_, name)) => Expr(name)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|the type test for extractors.quotes.reflect.Term cannot be checked at runtime Here we see that the extractor is returning a To fix the code it just implement the extractor directly in an object: def extractNameFromSelectorImpl[To: Type, T: Type](code: Expr[To => T])(using Quotes): Expr[String] =
import quotes.reflect.*
code.asTerm match
case InlinedLambda(_, Select(_, name)) => Expr(name)
case t => report.throwError(s"Illegal argument to extractor: ${code.show}, in tasty: $t")
object InlinedLambda:
def unapply(using Quotes)(arg: quotes.reflect.Term): Option[(List[quotes.reflect.ValDef], quotes.reflect.Term)] =
import quotes.reflect.*
arg match
case Inlined(_, _, Lambda(vals, term)) => Some((vals, term))
case Inlined(_, _, nested) => InlinedLambda.unapply(nested)
case t => None
end InlinedLambda |
Thank you, that solves my issue, hopefully someone else will learn from my mistakes. |
Also, this can be simplified - import scala.quoted.{given, *}
+ import scala.quoted.* |
I have seen this pattern several times and plan to include examples the macro tutorial. |
This will help with the siuation found in scala#12253
@wookievx. These additions to the macro tutorial may be helpful o avoid this kind of errors in the future. scala/docs.scala-lang#1999 |
I hope that this will be as helpful for others as it was for me. Great addition. |
Uh oh!
There was an error while loading. Please reload this page.
Compiler version
3.0.0-RC3
Minimized example
I am attempting to extract name of the selected field from lambda expression (to create lens-like functionality). I am attempting to remove layers of inlined because of the passing of the lambda as an inline parameter multiple times
Full example can be found here:
https://github.com/wookievx/chimney/blob/chimney3/chimney3/src/test/scala/io/scalaland/chimney/TransformerDslSpec.scala#L69
Output
When attempting to compile
Usage
I get the following compilation exception:When macro is invoked with correct lambda body (for example:
_.y
), the following error is reported in the macro instead (but name extraction works fine):I am suspecting that I messed-up imports and I am using wrong extractors (via importing quotes.reflect multiple times or something like that)
Expectation
I am trying to provide nice error message in case passed lambda expression is incorrect (which should be matched correctly given my understanding of quotes.reflect extractors) and also avoid this warning, which hints that for some reason some unsound pattern matching is taking place in macro implementation.
The text was updated successfully, but these errors were encountered: