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

Missing import term #12965

Closed
mbore opened this issue Jun 28, 2021 · 4 comments
Closed

Missing import term #12965

mbore opened this issue Jun 28, 2021 · 4 comments
Assignees

Comments

@mbore
Copy link

mbore commented Jun 28, 2021

Compiler version

3.0.1-RC2

Minimized code

Main.scala

object Instances {
  val s: String = "STRING"
}

object Test {
  import Instances.*

  val sMacro = Macros.f()
}

Macros.scala

import scala.quoted.*

object Macros {
  inline def f(): String = ${ fImpl }

  def fImpl(using q: Quotes): Expr[String] = {
    import q.reflect.*

    val classSymbol = Symbol.spliceOwner.owner.owner
    println(classSymbol.tree.show)

    ???
  }
}

Output

@scala.annotation.internal.SourceFile("src/main/scala/Main.scala") object Test extends java.lang.Object { this: Test.type =>
        val sMacro: scala.Predef.String
      }

Expectation

classSymbol.tree does not contain any Import term, but as far as I understand it should.
Based on output of

object Macros {
  inline def f(): String = ${ fImpl }

  def fImpl(using q: Quotes): Expr[String] = {
    import q.reflect.*

    val expr = '{
      import Instances.*

      val sMacro = Macros.f()
    }
      
    println(expr.asTerm.show)

    ???
  }
}

which is

{
  import Instances.{_}
  val sMacro: scala.Predef.String = Macros.f()
  ()
}

I would expect output:

@scala.annotation.internal.SourceFile("src/main/scala/Main.scala") object Test extends java.lang.Object { this: Test.type =>
        import Instances.{_}
        val sMacro: scala.Predef.String
      }
@nicolasstucki
Copy link
Contributor

That tree for the class seems to be synthesized. This should be run with -Yretain-trees to load the tree from the tasty file. That one should contain the import.

@mbore
Copy link
Author

mbore commented Jul 5, 2021

Yes, you're right, it works this way, thank you.
But in my use case, I would like to access the Import term in a macro used in a library (expanded in the user code).
Does it mean that every user of the library needs to add the -Yretain-trees flag to make it work?

@nicolasstucki
Copy link
Contributor

Yes, for now you will have to use -Yretain-trees. In the future, we should be able to enable -Yretain-trees by default. We do not currently do it because it currently we retain too much and we cannot GC those trees. An issue that is solvable but requires some work.

@mbore
Copy link
Author

mbore commented Jul 28, 2021

Ok I see, hope it will be fixed soon. thanks for the answer!

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

No branches or pull requests

2 participants