Skip to content

Cannot access protected members in anonymous class quote #24821

@j-mie6

Description

@j-mie6

Compiler version

3.3.7

Minimized code

Given an interface:

trait Foo {
  // implementers can use this, but shouldn't be public Foo API
  protected final def p: Int = 4
  def foo: Int
}

and a macro like:

inline def macro = $macroImpl
def macroImpl(using Quotes)(): Expr[Foo] = '{
  new Foo {
    def foo: Int = p
  }
}

Output

If you use

val foo = macro

Gives an error message

undefined: this.inline$p # -1: TermRef(ThisType(TypeRef(NoPrefix,class $anon)),inline$p) at typer

Expectation

It should work, as the regular Scala snippet that does this does work. Ideally, I would be able to mark def p as private [pkg], which wouldn't work either (but in this case, I can buy that this would be inlined outside of pkg and therefore could leak scope, not that I'd mind, but this is a separate issue/feature request (#24822 ).)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions