question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expr.summon won't compile with dynamically created Type

See original GitHub issue

Compiler version

3.0.0-RC1

Minimized code

import scala.quoted.*

def x(using Quotes): Unit =
  import quotes.reflect._
  val t = TypeRepr.of[Int].asType
  Expr.summon(using t)

Output

[error] -- [E007] Type Mismatch Error: /home/golly/projects/public/univeq/univeq/shared/src/main/scala-3/japgolly/univeq/internal/bug.scala:6:20 
[error] 6 |  Expr.summon(using t)
[error]   |                    ^
[error]   |Found:    quoted.Type[?1.CAP]
[error]   |Required: quoted.Type[T]
[error]   |
[error]   |where:    ?1 is an unknown value of type scala.runtime.TypeBox[Nothing, AnyKind]
[error]   |          T  is a type variable with constraint

Expectation

It should compile.

Workaround is to append .asInstanceOf[Type[Any]] like this:

import scala.quoted.*

def x(using Quotes): Unit =
  import quotes.reflect._
  val t = TypeRepr.of[Int].asType.asInstanceOf[Type[Any]]
  Expr.summon(using t)

Manual testing confirms that the type parameter being Any doesn’t affect the result, (as one would expect) the result is purely driven by the Type instance.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
nicolasstuckicommented, Mar 18, 2021

If there you have a Type[_] use

val tpe: Type[_] = ...
tpe match
  case '[a] => Expr.summon[a]

If you have a TypeRepr use

val tpe: TypeRepr = ...
Implicits.search(tpe) match
  case result: ImplicitSearchSuccess => result.tree
  case _ =>
1reaction
nicolasstuckicommented, Mar 16, 2021

I believe it should work with

val t = TypeRepr.of[Int].asType
t match
  case '[a] => Expr.summon[a]
Read more comments on GitHub >

github_iconTop Results From Across the Web

Scala3 macro summon typeclass instance of a TypeTree (no ...
Implicits.search can be used to summon implicit instance if there is no type arg avaiable for Expr.summon val valDefTree = cf.tree.
Read more >
Scala 3 macros tips & tricks - SoftwareMill
Typechecking and summoning givens​​ Thus we often end up using givens within a macro. A given can be summoned on-demand using Expr. summon[ ......
Read more >
intro to Scala 3 macros - eed3si9n
So the general process is that we will capture either the term-level parameters or types, and return a typed abtract syntax tree called...
Read more >
FAQ | Macros in Scala 3
How do I summon an expression for statically unknown types? You can summon an expression from either a TypeRepr or a Type as...
Read more >
Macros - Scala 3
Splicing takes expressions of type Expr[T] to expressions of type T and it takes ... But it will neither create nor remove quotes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found