-
Notifications
You must be signed in to change notification settings - Fork 14
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
Fix AnnotationInfo when using defaults #884
Comments
There was a comment on dotty that they might re-engineer defaults to be inline. Your bullet 3 is very appealing, or to coin a phrase, "it would be great if". |
A workaround, at least for the case when the annotation parameters have distinct types, is defining constructor overloads: class ann(x: Int, y: String) extends annotation.StaticAnnotation {
def this(x: Int) = this(x, null)
def this(s: String) = this(null, s)
} scala> @ann(s = "ka") class K
scala> typeOf[K].typeSymbol.annotations.head
val res0: $r.intp.global.AnnotationInfo = ann("ka") |
Another item to fix - though it will be less relevant after the other fixes. For an annotation with auxiliary constructors, the |
... sometimes the simple ideas are not that simple. https://github.com/scala/scala/compare/2.13.x...lrytz:scala:annot-overload?expand=1 adds the constructor symbol to AnnotationInfo, but
What the user really needs is to match up the
|
chatted with Lukas about this today. he clarified that he doesn't think we should continue down the path of attempting to pickle a method/constructor symbol and he pointed out that if we fix this, it would make it possible for users to e.g. have a custom subclass of |
One option would be to add this method to def constructorSymbol(typer: Tree => Tree): Symbol = {
typer(New(atp, args: _*)) match {
case Apply(constr @ Select(New(_), nme.CONSTRUCTOR), _) => constr.symbol
case _ => atp.typeSymbol.primaryConstructor
}
} The users have to provide a type checker, so basically call it as I tested it, it works well. That seems to be the most straightforward solution. |
Before typing, the annotation is represented as
new ann(y = Nil.size)
. To construct theAnnotationInfo
, that expression is type checked like ordinary code, resulting inThe
AnnotationInfo
only hasann(x$2, x$1)
, the block is thrown away it seems, there's now way get to the actual arguments.Ideas
AnnotationInfo
. But IMO this is not a good solution, it leaves processing annotations too difficult.new ann(y = Nil.size)
can be typed asnew ann(<init>$default$1, Nil.size)
AnnotationInfo
, i.e.,new ann(1, Nil.size)
.For the last one, the question is how to get to the default value AST. One option is to make the compiler attach it to to a symbol, so the
class ann
above would becomeWhen constructing the
AnnotationInfo
fromnew ann(<init>$default$1, Nil.size)
we can get the AST for the default from the annotation parameter symbol.Earlier tickets: scala/bug#7656, scala/bug#9612
I did some prototyping when discussing
@apiStatus
(scala/scala#8820 / scala/scala@2.13.x...lrytz:constAnnDefaults). The goal here was to allow subclasses of annotations to define certain defaults, which would be great for@unused
: scala/bug#12367.Fixing that at the same time would be good.
The text was updated successfully, but these errors were encountered: