Skip to content
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

[2.13.9] Whitebox macro sometimes not return right side macro expansion type with -Xsource:3 #12645

Closed
jilen opened this issue Sep 22, 2022 · 3 comments · Fixed by scala/scala#10160
Labels

Comments

@jilen
Copy link

jilen commented Sep 22, 2022

Reproduction steps

Scala version: 2.13.9

For quill's quote method (whitebox macro)

implicit def quote[T](body: T): Quoted[T] = macro QuotationMacro.quote[T]

With following example

//> using lib "io.getquill::quill-core:4.4.1"
//> using option "-Xsource:3"

import io.getquill._

case class TestEntity(name: String)
object Foo {
  val ctx = new MirrorContext(MirrorIdiom, Literal)
  import ctx._
  val q1 = quote(query[TestEntity])
}

Foo.q1 is inferred as io.getquill.Quoted[io.getquill.EntityQuery[TestEntity]]

Problem

Foo.q1 should be the anonymous class type

Note

//> using lib "org.scala-lang:scala-reflect:2.13.9"
//> using option "-Xsource:3"

import scala.language.experimental.macros
import scala.reflect.macros.whitebox.Context

trait Ast
case class Foo(id: Int) extends Ast

trait Ctx {
  trait Quoted[+A] {
    def ast: Ast
    def data: A
  }
  final def quote[A](a: A): Quoted[A] = macro QuoteMacro.quoteImpl[A]
}


class QuoteMacro(val c: Context) {

  import c.universe._

  def quoteImpl[A](a: c.Expr[A])(implicit ta: WeakTypeTag[A]) = {
    val m = TermName(c.freshName("fff"))
    val tree = c.untypecheck {
      q"""
      new ${c.prefix}.Quoted[$ta] {
         def $m() = ()
         def data = $a
         final override def ast: Foo = Foo(0)
      }
      """
    }
    println(tree)
    tree
  }
}

object Foo {
  val ctx = new Ctx {}
}

Tried this example, but Foo.ctx.quote works just fine.

Maybe related to scala/scala#9891

@som-snytt
Copy link

That is a good question.

@SethTisue SethTisue changed the title [2.13.9] Whitebox macro sometimes not return right side maro expansion type with -Xsource:3 [2.13.9] Whitebox macro sometimes not return right side macro expansion type with -Xsource:3 Sep 22, 2022
@som-snytt
Copy link

Just realized that the code is not a minimization, and "works just fine" is not a typo.

@som-snytt
Copy link

I verified that it's sufficient to check for macros in flight before using the Xsource:3 behavior.

I'll try to make a test that fails.

I could not figure out how to make scala-cli give me -Vprint output, so I made a small sbt project. It will be a while before I touch scala-cli again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants