Solved: Scala 3 macros: How can I use a FromExpr in a macro?

I need to have some implicit values provided by the compiler in a macro. The basic setup is this:

import scala.quoted.*

object From: 

  inline def testFromExpr0[K,V](inline l: Tuple2[K,V]): Any = 
    ${ testFromExprImpl0('l) }

  def testFromExprImpl0[K, V](x: Expr[Tuple2[K,V]])(using Type[K], FromExpr[K], Type[V], FromExpr[V], Quotes): Expr[Int] =
    val v = x.valueOrAbort

end From

The code above can be found here. In the case above I get the following error:

[error] -- Error: /home/hmf/VSCodeProjects/sploty/meta/src/icollection/FromExpr0.scala:90:28 
[error] 90 |    ${ testFromExprImpl0('l) }
[error]    |                            ^
[error]    |No given instance of type quoted.FromExpr[K] was found for parameter x$3 of method testFromExprImpl0 in object From.
[error]    |I found:
[error]    |
[error]    |    quoted.FromExpr.ByteFromExpr[T]
[error]    |
[error]    |But given instance ByteFromExpr in object FromExpr does not match type quoted.FromExpr[K].
[error] one error found

I have made many attempts to solve this. In this example I get the error:

Malformed macro parameter: x$2

Parameters may only be:
 * Quoted parameters or fields
 * Literal values of primitive types
 * References to `inline val`s

I found this issue, which seems to indicate that the error message above is correct.

So how can I use the required implicits? I need these to use the correct FromExpr classes from within a macro (see this question).



Can anyone take a stab at this? Here is may latest attempt:

  def testFromExprImpl0[K:Type, V:Type](x: Expr[Tuple2[K,V]])(using Quotes): Expr[Int] =
    val tt = Expr.summon[FromExpr[Tuple2[K,V]]]
    val istt = tt.get.isExprOf[FromExpr[Tuple2[K,V]]]
    val from = tt.get.asExprOf[FromExpr[Tuple2[K,V]]]
    val r: Expr[K] = Expr.summon[FromExpr[Tuple2[K,V]]] match
      case Some(t) => '{$x._1}
      case _ =>  report.errorAndAbort("Expr.summon")
    val key = r.valueOrAbort

The r.valueOrAbort still generates this error:

value valueOrAbort is not a member of quoted.Expr[K].
An extension method was tried, but could not be fully constructed:



Apparently it is not possible to do what I tried above. @nicolasstucki explains this in #16738 . One has to provide the type in order to use FromExpr.