Skip to content

Annotating case class parameter annotates constructor parameter but not field #15665

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

Closed
sabenuse opened this issue Jul 13, 2022 · 2 comments
Closed
Labels
area:desugar Desugaring happens after parsing but before typing, see desugar.scala itype:bug

Comments

@sabenuse
Copy link

sabenuse commented Jul 13, 2022

Scala3.1.2

Minimized code

case class StVo(id: Int, @(Length@field)(32) name: String)

Output

When I call getAnnotation on the name field of StVo:

Length len = namefield.getAnnotation(Length.class);

len is null.

Decompiled code:

public class StVo implements Product, Serializable {
   private final int id;
   // fixme: I need @Length(32) here 
   private final String name;

   // fixme: why is @Length(32) here?
   public StVo(final int id, @Length(32) final String name) {
      this.id = id;
      this.name = name;
   }

   // ...
}

Expectation

@sabenuse sabenuse added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jul 13, 2022
@SethTisue SethTisue changed the title I can't access case class annotation field,please view code Annotating case class parameter annotates constructor parameter but not field Jul 13, 2022
@mbovel mbovel added area:desugar Desugaring happens after parsing but before typing, see desugar.scala and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Jul 15, 2022
@noti0na1
Copy link
Member

The logic is at PostTyper.removeUnwantedAnnotations, and documents about the meta annotations:
scala.annotation.meta
.

I'm not sure about the definition of the Length annotation in the minimized code.

By default, annotations on (val-, var- or plain) constructor parameters end up on the parameter, not on any other entity.

If you want to keep the annotation at the field, you should add @field to the annotation definition.

It seems Scala 2 has the same behaviour.

@smarter
Copy link
Member

smarter commented Jul 23, 2022

Duplicate of #12492

@smarter smarter marked this as a duplicate of #12492 Jul 23, 2022
@smarter smarter closed this as completed Jul 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:desugar Desugaring happens after parsing but before typing, see desugar.scala itype:bug
Projects
None yet
Development

No branches or pull requests

4 participants