Skip to content

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

Closed
@hmf

Description

@hmf

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:metaprogramming:quotesIssues related to quotes and splicesarea:reportingError reporting including formatting, implicit suggestions, etc

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions