KT-44756 Ignore annotations on value parameters if metadata says so

There is a `gradle-api-impldep.jar` created by Gradle which contains
our nullability annotations, but relocated in the
`org.gradle.internal.impldep` package

In the deserialization process we use a `HAS_ANNOTATIONS` flag, and
if it is present on the declaration, we do not try to build annotations
for it, even when they are actually present and even when they are
relocated
(see usages of Flags.HAS_ANNOTATIONS in org.jetbrains.kotlin.serialization.deserialization.MemberDeserializer)

In the stubs builder we also use this HAS_ANNOTATIONS flag, but we did
not use it for the value parameters. This commit fixes that - now, if
the `org.jetbrains.annotations` packages are relocated, it should not
cause `Stub Mismatch Error` for the value parameters

Lets consider two possible cases:

1. If the value parameter has a user-defined annotation,
it will be marked as having annotations (HAS_ANNOTATIONS == true), and
both stubs and deserialized descriptors will have to
use all available annotations (even a relocated ones)

2. If, for example, the value parameter is of non-nullable type, it will
have a `@NotNull` annotation on it, but will be marked as having
no annotations at all (HAS_ANNOTATIONS == false), since `NotNull` is
considered as auxiliary by the compiler. Because of the flag, both stubs
and descriptors will ignore all present annotations (even if they were
relocated)

In the both cases, the stubs and the descriptors will completely match

^KT-44756 Fixed

See IDEA-262971 about fixing the Gradle jar and the details

N.B. This does not fixes the cases when `kotlin.Metadata` and similar
annotations are relocated (e.g. KT-25709)
This commit is contained in:
Roman Golyshev
2021-03-09 17:43:05 +03:00
committed by Space
parent f7099d34d4
commit 5d0760c685
2 changed files with 9 additions and 6 deletions
@@ -28,7 +28,7 @@ object KotlinStubVersions {
// Binary stub version should be increased if stub format (org.jetbrains.kotlin.psi.stubs.impl) is changed
// or changes are made to the core stub building code (org.jetbrains.kotlin.idea.decompiler.stubBuilder).
// Increasing this version will lead to reindexing of all binary files that are potentially kotlin binaries (including all class files).
private const val BINARY_STUB_VERSION = 75
private const val BINARY_STUB_VERSION = 76
// Classfile stub version should be increased if changes are made to classfile stub building subsystem (org.jetbrains.kotlin.idea.decompiler.classFile)
// Increasing this version will lead to reindexing of all classfiles.
@@ -252,11 +252,14 @@ class TypeClsStubBuilder(private val c: ClsStubBuilderContext) {
}
val modifierList = createModifierListStub(parameterStub, modifiers)
val parameterAnnotations = c.components.annotationLoader.loadValueParameterAnnotations(
container, callableProto, callableProto.annotatedCallableKind, index, valueParameterProto
)
if (parameterAnnotations.isNotEmpty()) {
createAnnotationStubs(parameterAnnotations, modifierList ?: createEmptyModifierListStub(parameterStub))
if (Flags.HAS_ANNOTATIONS.get(valueParameterProto.flags)) {
val parameterAnnotations = c.components.annotationLoader.loadValueParameterAnnotations(
container, callableProto, callableProto.annotatedCallableKind, index, valueParameterProto
)
if (parameterAnnotations.isNotEmpty()) {
createAnnotationStubs(parameterAnnotations, modifierList ?: createEmptyModifierListStub(parameterStub))
}
}
createTypeReferenceStub(parameterStub, typeProto)