Skip to content

Can't use Symbol.tree to access DefDef body with blackbox macro using Tasty reflect #7592

Closed
@fhackett

Description

@fhackett

(you may recognise this from the Gitter chat, bumping because it seems to have gone unaddressed) never mind

minimized code

The macro:

  def compileImpl[T](expr : Expr[T])(given qctx : QuoteContext) : Expr[T] = {
    import qctx.tasty.{_,given}

    def proc(term : Term)(given outerCtx : Context) : Term = {
      given ctx : Context = term.symbol(given outerCtx).localContext(given outerCtx)
      term match {
        case Inlined(call, bindings, body) => Inlined(call, bindings, proc(body))
        case l : Literal => l
        case Block(statements, expr) =>
          proc(expr) // FIXME: very wrong
        case s : (Select|Ident) => {
          if( s.symbol.isDefDef ) {
            s.symbol.tree match {
              case DefDef(name, typeParams, params, returnTp, Some(rhs)) => proc(rhs)
            }
          } else {
            ???
          }
        }
      }
    }

    proc(expr.unseal).seal.asInstanceOf[Expr[T]]
  }

  inline def compile[T](expr : =>T) : T =
    ${ compileImpl('{ expr }) }

The usage (in a different package):

  def return1 = 1

  def testReturn1 = {
    assert(1 == compile(return1))
  }

expectation

The compile macro should be able to inspect the body of return1, allowing it to inline the method body into the macro expansion.

Currently the DefDef body is matched as None, preventing this. I know some DefDefs don't have bodies, but this one does. I'm at a loss as to how to achieve the effect I want.

Is there some undocumented thing I should be doing or is this a bug?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions