Found what seems to be a bug in FIR2IR with repeat...
# compiler
z
Found what seems to be a bug in FIR2IR with repeatable annotations KT-83185 IR does not unpack repeatable annotation containers from external declarations
d
The problem here is not that the
Container
annotation is repeatable, but it's missing on the classpath, so fir2ir cannot find an
IrConstructorSymbol
to generate the
IrConstructorCall
. So I kinda can justify the existing behavior.
After reading your report once again I'm getting more confused. Is it expected that
ContributesBinding.Container
doesn't exist? If yes, that what does it mean?
if it's not accessible, check if it's a
Container
Consider some default implementation for any missing annotation named
Container
?
m
After completing KT-74200, the new node for representing annotations will no longer require a constructor symbol, only a
classId
. Translating such annotations to the IR will be pretty straightforward then. Until these changes are introduced, please provide all required annotations in dependencies.
z
I'm a little unsure what to say, there is no ContributesBinding.Container source or symbol to point it at, it's a compiler intrinsic for handling repeatable annotations on the JVM
So there's no way to "provide all required annotations" in this scenario
d
Hm, I didn't know about this jvm implementation detail. But how it happened that the
ContributesBinding.Container
appeared in the metadata (so it was deserialized this way by the frontend)?
z
I'm not sure how it's serialized, but that is how it works :). Kotlin reflect follows a similar pattern at runtime to get repeatable annotations
@udalov might have insight
d
How this annotation appears in the dependency module? Is it written in sources or generated by the plugin?
z
In source it doesn't have that container, nor does it end up in the compiled bytecode for that annotation. It's purely an intrinsic at repeatable callsites and done this way for backward compatibility
d
No, I mean the specific annotation instance on a class.
Copy code
@Ann(1)
@Ann(2)
class Some
These
@Ann(1)
and
@Ann(2)
come from sources?
z
It is possible to declare a custom container I think, but I've never done that
Oh, yes
d
Got it. Will investigate tomorrow.
z
They're on source but then packaged by the compiler into this
Copy code
@Ann.Container([
  Ann(1),
  Ann(2)
])
class Some
java's Repeatable supports specifying a non-intrinsic container that the compiler can use to stuff them, however the kotlin stdlib one does not and always creates the implicit
Container
nested class
and while there's no FIR diagnostic for it, the compiler does prohibit custom nested classes called
Container
for this reason
👍 1
u
But how it happened that the
ContributesBinding.Container
appeared in the metadata (so it was deserialized this way by the frontend)?
There's no
Container
in metadata, and in fact there are no annotations in metadata until KT-75736. When we load annotations from the JVM bytecode though, we detect
Container
class and unwrap repeated annotations here: https://github.com/JetBrains/kotlin/blob/88ed6bcf257eeb0aa2693626a237c00f102550f9/[…]rains/kotlin/load/kotlin/AbstractBinaryClassAnnotationLoader.kt Looks like if we cannot resolve the
Container
class, we will not be able to unwrap it and the unresolved annotation will remain as
@Container(...)
. So I'm not sure how we can help with issue except for KT-75736.
z
Couldn’t we infer it? That's what I do elsewhere in that plugin and what we do in the workaround in that PR
u
What do you mean by "infer"?
z
If we see an annotation right now that's called Container and nested in a repeatable annotation class ID, we read the repeated annotations from the value array
Basically hire the impl detail of the container class, which I believe is what the Java reflect APIs do too
u
Yeah but if we cannot access the class, we cannot assume that it's a repeatable annotation container just based on its name and the fact that it's nested
z
As in it would be too expensive to check if the enclosing class is an annotation and annotated with @Repeatable?
u
I mean, the issue is about annotations missing in dependencies, right? So we just cannot load this annotation class at all. Maybe I misunderstood something.
z
The enclosing annotation class does exist
And is visible
In the debugger I was able to load it fine, you just would need to know the shape of the container class (which adheres to a spec as far I know) and then create the annotation that way
thinking spinhead 1
On a treadmill but I can write up a sample change in a bit
“It” meaning the ContributesBinding annotation that encloses the ContributesBinding.Container we see in FIR
u
And does the class file for
ContributesBinding.Container
exist?
z
It does not, but you don't need it for the API call happening in this scenario. You just need to need to extract its value array as an array of the enclosing class
Consumers never expect to encounter the Container class directly in my experience
u
I'm a bit confused. The code that I've linked above will not work if
ContributesBinding.Container
does not exist. How does it happen that
ContributesBinding
exists, but
ContributesBinding.Container
does not? The compiler assumes that repeatable annotation and its container are compiled together
z
That I don't have the answer to, but it ContributesBinding does exist in the debugger
I assumed it was something to do with it being a compiler intrinsic or sorts
But the inferred approach I described above works for metro elsewhere in the plugin for this scenario
u
Container
is a synthetic class from the Kotlin metadata's point of view, so maybe something filters it out, and that's where the root issue is, I think.
z
Yeah, the function I mention in the issue skips it if it can’t load it, but if it knew it was a repeatable container it could load it fine. It would maybe need some extra work to allow for returning multiple annotations instead of just one, but I don't see why it wouldn't work
Off the treadmill now, will get a code snippet after I shower and eat lunch!
u
the inferred approach
If the compiler sees
@X.Container(X(), X())
and it doesn't have the class file for
X.Container
, how do you suggest it infer that in reality it's
@X @X
? There can be a user-defined annotation named
Container
in
X
, that is not the repeatable container for
X
. I mean, we can check the value of
@java.lang.annotation.Repeatable
on
X
and infer that
X.Container
is its repeatable container. This should work fine, but it's a bit strange because it only helps the case when
X
exists in dependencies and
X.Container
is not, which is not the Kotlin compiler's problem. And also I'm not sure that's what you're suggesting. :)
z
That's basically what I'm suggesting yeah.
Container
as a name does seem to be a magic constant for JVM (at least since kotlin doesn't support a custom one like Java’s)
It's a special case but everywhere I've seen that handles repeatable annotations is basically special case code heh
And in my experience, the enclosing X does exist. But in the event that it doesn't, I think it's fine to definitely skip it in that case
So the decision tree would be 1. Look up annotation class symbol 2. If null, check if simple name is Container and if it has an outer class ID 3. If true, try lookup class symbol of the outer class ID 4. If not null and annotated repeatable, we now know the shape and can safely unpack the inner value array as instances of the outer annotation class
u
(at least since kotlin doesn't support a custom one like Java’s)
Kotlin does support custom containers: https://github.com/Kotlin/KEEP/blob/master/proposals/repeatable-annotations.md#proposal
If null, check if simple name is Container and if it has an outer class ID
(Btw, don't get me started on trying to obtain outer class ID from the JVM internal name. 😄 Since we load annotations from the JVM bytecode, we only see
pkg/X$Container
and we cannot assume that it's a nested class for
pkg/X
, because both in Java and in Kotlin, you could declare a top level class named
X$Container
.)
Anyway, this will surely work, but I still feel it's a workaround for some issue which is not in the Kotlin compiler. So I doubt if it's the correct place to fix it.
z
oooh TIL about
JvmRepeatable
would it work to do this as a short-term solution?
d
> There's no Container in metadata, and in fact there are no annotations in metadata until KT-75736. When we load annotations from the JVM bytecode though, we detect Container class and unwrap repeated annotations here: @udalov the code is actually there, but the problem is the JvmBinaryAnnotationDeserializer used in FIR deserialization doesn't use the
AbstractBinaryClassAnnotationAndConstantLoader
as it's descriptor-speicifc. I suppose that back in 2021 we were not that focused on fir productization to fix KT-48131 there too and then just missed the bug in K2. So the conclusion that there is actually a bug in the frontend.
🙏 3