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

Functions defined locally within a macro implementation cannot be used in a quote #16532

Closed
hmf opened this issue Dec 15, 2022 · 3 comments · Fixed by #16572
Closed

Functions defined locally within a macro implementation cannot be used in a quote #16532

hmf opened this issue Dec 15, 2022 · 3 comments · Fixed by #16572
Labels
area:metaprogramming:quotes Issues related to quotes and splices area:reporting Error reporting including formatting, implicit suggestions, etc
Milestone

Comments

@hmf
Copy link

hmf commented Dec 15, 2022

Compiler version

Version 3.2.1

Minimized code

I have a set of "standard" functions defined locally within a macro implementation. I cannot seem to be able to place these in a quote. The global version of the methods work correctly. May not be a bug, but is not intuitive. Here is the code (also in scastie):

import scala.quoted.*


inline def recurseIIGlobal(a:Int, n:Int): Int =
  inline if n == 0
  then
    1
  else
    a * recurseIIGlobal(a, n-1)

transparent inline def power0(a: Int, b:Int): Any = ${ power0Impl('a,'b) }

def power0Impl(a: Expr[Int], b: Expr[Int])(using Quotes): Expr[Int] =
  import quotes.reflect.*
  // Functions defined inside implementation cannot be called directly
  inline def recurseII(a:Int, n:Int): Int =
    inline if n == 0
    then
      1
    else
      a * recurseII(a, n-1)

  inline def recurseI(a:Int, n:Int): Int =
    if n == 0
    then
      1
    else
      a * recurseI(a, n-1)

  def recurse(a:Int, n:Int): Int =
    if n == 0
    then
      1
    else
      a * recurse(a, n-1)

	// Ok
  val x = recurseII(1,2)
  '{    
    // Ok
    val x1 = recurseIIGlobal($a, $b)
    val x1a = ${ Expr(recurseIIGlobal(1, 2)) }
    val x2a = ${ Expr[Int](recurseII(1, 2))  }
    // Below all fail
    // recurse($a, $b)
    // recurseI($a, $b)
    val x2 = recurseII($a, $b)
    x2
   }

Output

    access to method recurseII from wrong staging level:
     - the definition is at level 0,
     - but the access is at level 1.

Expectation

I expect the functions defined within the macro implementation to work as any other.

@hmf hmf added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 15, 2022
@anatoliykmetyuk anatoliykmetyuk added area:metaprogramming:quotes Issues related to quotes and splices and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 19, 2022
@nicolasstucki
Copy link
Contributor

import quotes.reflect.* is not needed in this example.

@nicolasstucki
Copy link
Contributor

Minimized

import scala.quoted.*

def power0Impl(a: Expr[Int], b: Expr[Int])(using Quotes): Expr[Int] =
  inline def recurseII(a:Int, n:Int): Int = ???

  '{
    val x2 = recurseII($a, $b)
    // error: access to method recurseII from wrong staging level:
    //   - the definition is at level 0,
    //   - but the access is at level 1.
    x2
   }

@nicolasstucki
Copy link
Contributor

recurseII is a local definition and cannot be used in staged code as it will not be available in the next stage where the quote is inlined. Note that inline definitions are not expanded within the quote. They are only inlined when they are at staging level 0 (i.e. during macro expansion).

The error is correct. We could give a hint with an explanation.

@nicolasstucki nicolasstucki added area:reporting Error reporting including formatting, implicit suggestions, etc and removed itype:bug labels Dec 22, 2022
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Dec 22, 2022
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Dec 22, 2022
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Dec 23, 2022
little-inferno pushed a commit to little-inferno/dotty that referenced this issue Jan 25, 2023
@Kordyjan Kordyjan added this to the 3.3.0 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:metaprogramming:quotes Issues related to quotes and splices area:reporting Error reporting including formatting, implicit suggestions, etc
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants