Skip to content

Macro annotation seems to not change the parent #18677

Open
@eed3si9n

Description

@eed3si9n

Compiler version

3.4.0-RC1-bin-20231010-7dc9798-NIGHTLY

Minimized code

https://github.com/eed3si9n/ifdef

import scala.annotation.{ experimental, MacroAnnotation }
import scala.quoted.*

@experimental
class ifdef(key: String) extends MacroAnnotation:
  private final val macroSetting = "com.eed3si9n.ifdef.declare:"

  def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
    import quotes.reflect.*
    tree match
      case cls @ ClassDef(className, ctr, parents, self, body) =>
        val keys = (CompilationInfo.XmacroSettings.collect:
          case x if x.startsWith(macroSetting) => x.drop(macroSetting.size)
        ).toSet
        if keys(key) then List(tree)
        else
          val trees = List(ClassDef.copy(tree)(
            className,
            DefDef(cls.symbol.primaryConstructor, _ => None),
            parents = Nil,
            None,
            Nil))
          println(trees.map(_.show))
          trees
      case _ =>
        report.error("annotation only supports `class`")
        List(tree)

then:

package example

import scala.annotation.experimental

class A:
  def foo: Int = 42

@experimental
@ifdef("test")
class ATest extends munit.FunSuite:
  test("hello"):
    val actual = new A().foo
    val expected = 42
    assertEquals(actual, expected)

Output

$ javap -cp app/target/scala-3.4.0-RC1-bin-20231010-7dc9798-NIGHTLY/classes/ example.ATest
Compiled from "app.scala"
public class example.ATest extends munit.FunSuite {
  public example.ATest();
}

Expectation

Given parents = Nil, I expect that example.ATest to not extend munit.FunSuite.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions