FIR2IR: Drop Java annotations that lose required arguments Drop Java or binary annotations when a required argument is missing or erroneous; optional values retain their defaults. Nested annotation drops propagate to the enclosing annotation. An IrGetClass argument with an error type also drops its annotation. With Java Direct, a required argument that the constant evaluator cannot handle (for example `?:`) now drops the whole annotation instead of only that argument. Kapt behavior is unchanged: Kotlin-source annotations keep the per-argument cleanup in stub mode. ^KT-91584 Fixed ^KT-90361 ^KT-90363 Co-authored-by: Junie <junie@jetbrains.com>
diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/CallAndReferenceGenerator.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/CallAndReferenceGenerator.kt index 29d6879..d328ca0 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/CallAndReferenceGenerator.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/CallAndReferenceGenerator.kt
@@ -27,7 +27,6 @@ import org.jetbrains.kotlin.fir.resolve.calls.getExpectedType import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.approximateDeclarationType -import org.jetbrains.kotlin.fir.scopes.getDeclaredConstructors import org.jetbrains.kotlin.fir.scopes.impl.toConeType import org.jetbrains.kotlin.fir.scopes.impl.typeAliasConstructorInfo import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol @@ -974,23 +973,27 @@ } } return visitor.withAnnotationMode { - val annotationCall = annotation.toAnnotationCall() + val annotationCall = annotation.toAnnotationCall(firConstructorSymbol) + val isJavaOrBinaryAnnotation = annotation !is FirAnnotationCall + val requiredArgumentIndices = + if (isJavaOrBinaryAnnotation) firConstructorSymbol?.requiredArgumentIndices().orEmpty() else emptySet() irAnnotation .applyReceiversAndArguments(annotationCall, declarationSiteSymbol = firConstructorSymbol, explicitReceiverExpression = null) .applyTypeArgumentsWithTypealiasConstructorRemapping(firConstructorSymbol?.fir, annotationCall?.typeArguments.orEmpty()) - .filterOutErrorArgsInAnnotation(isJavaOrBinaryAnnotation = annotation !is FirAnnotationCall) + .filterOutErrorArgsInAnnotation(isJavaOrBinaryAnnotation, requiredArgumentIndices) } } - private fun FirAnnotation.toAnnotationCall(): FirAnnotationCall? { + private fun FirAnnotation.toAnnotationCall( + firConstructorSymbol: FirConstructorSymbol?, + ): FirAnnotationCall? { if (this is FirAnnotationCall) return this + val constructorSymbol = firConstructorSymbol ?: return null return buildAnnotationCall { useSiteTarget = this@toAnnotationCall.useSiteTarget annotationTypeRef = this@toAnnotationCall.annotationTypeRef val symbol = annotationTypeRef.coneType.fullyExpandedType().toRegularClassSymbol() ?: return null - val constructorSymbol = symbol.unsubstitutedScope().getDeclaredConstructors().firstOrNull() ?: return null - val argumentToParameterToMapping = constructorSymbol.valueParameterSymbols.mapNotNull { val parameter = it.fir val argument = this@toAnnotationCall.argumentMapping.mapping[parameter.name] ?: return@mapNotNull null @@ -1010,13 +1013,27 @@ } } + private fun FirConstructorSymbol.requiredArgumentIndices(): Set<Int> { + // Annotation constructors have no context parameters or receivers; the offset only mirrors the IrAnnotation argument layout. + val argumentOffset = contextParameterSymbols.size + + (if (dispatchReceiverType != null) 1 else 0) + + (if (isExtension) 1 else 0) + return valueParameterSymbols.mapIndexedNotNull { index, parameter -> + if (parameter.hasDefaultValue) null else argumentOffset + index + }.toSet() + } + /** * Drops erroneous arguments from this annotation if it is an [IrAnnotation], so that no error nodes reach the backend. * - * The default value of the parameter applies, if any. Error elements of array arguments are removed from the array. Nested - * annotations are cleaned up recursively. + * For Java and binary annotations ([isJavaOrBinaryAnnotation]), a missing or erroneous argument at one of + * [requiredArgumentIndices] drops the whole annotation, in both normal and Kapt modes. Kotlin-source annotations are never + * dropped whole: in Kapt mode only their erroneous arguments are removed. For optional arguments, the default applies after + * an erroneous value is removed. Error elements of array arguments are removed from the array, and nested annotations are + * cleaned up recursively. * - * The cleanup is done only in two cases: + * The [isJavaOrBinaryAnnotation] flag is true when the FIR annotation is not a Kotlin-source + * [FirAnnotationCall]. Cleanup is done only in two cases: * - In Kapt stub generation mode ([Fir2IrConfiguration.skipBodies]), for all annotations: Kapt generates stubs for code that * does not have to resolve, so frontend errors do not stop the compilation, and error nodes must not reach the backend. * - For annotations that do not come from Kotlin sources ([isJavaOrBinaryAnnotation]): Java sources and binaries are not @@ -1025,18 +1042,23 @@ * * Errors in annotations from Kotlin sources are reported by the frontend and stop the compilation before fir2ir. */ - private fun IrExpression.filterOutErrorArgsInAnnotation(isJavaOrBinaryAnnotation: Boolean): IrExpression { - fun IrElement?.cleanUp(): Boolean { + private fun IrExpression.filterOutErrorArgsInAnnotation( + isJavaOrBinaryAnnotation: Boolean, + requiredArgumentIndices: Set<Int>, + ): IrExpression { + fun IrElement?.cleanUp(requiredArgumentIndices: Set<Int>? = null): Boolean { return when (this) { is IrErrorExpression -> true is IrGetClass -> this.argument.type is IrErrorType is IrAnnotation -> { + var lostRequiredArgument = requiredArgumentIndices.orEmpty().any { this.arguments.getOrNull(it) == null } for ([i, expression] in this.arguments.withIndex()) { if (expression.cleanUp()) { this.arguments[i] = null + if (i in requiredArgumentIndices.orEmpty()) lostRequiredArgument = true } } - false + lostRequiredArgument } is IrVararg -> { this.elements.removeAll { it.cleanUp() } @@ -1046,7 +1068,12 @@ } } - if (this is IrAnnotation && (configuration.skipBodies || isJavaOrBinaryAnnotation)) cleanUp() + if (this is IrAnnotation && (configuration.skipBodies || isJavaOrBinaryAnnotation)) { + val lostRequiredArgument = cleanUp(requiredArgumentIndices) + if (isJavaOrBinaryAnnotation && lostRequiredArgument) { + return IrErrorCallExpressionImpl(startOffset, endOffset, type, "Annotation has an invalid required argument") + } + } return this }
diff --git a/compiler/testData/codegen/bytecodeListing/annotations/invalidJavaEnumArgumentsOnDelegatedMembers.kt b/compiler/testData/codegen/bytecodeListing/annotations/invalidJavaEnumArgumentsOnDelegatedMembers.kt index a8c4260c..3329728 100644 --- a/compiler/testData/codegen/bytecodeListing/annotations/invalidJavaEnumArgumentsOnDelegatedMembers.kt +++ b/compiler/testData/codegen/bytecodeListing/annotations/invalidJavaEnumArgumentsOnDelegatedMembers.kt
@@ -68,7 +68,7 @@ @DefaultedJavaAnnotation String javaAnnotationWithDefault(); - // The pre-fix IR keeps this annotation even though its required value is missing. + // The required value is missing, so this annotation is dropped from the delegated member. @RequiredJavaAnnotation String javaAnnotationWithoutDefault(); }
diff --git a/compiler/testData/codegen/bytecodeListing/annotations/invalidJavaEnumArgumentsOnDelegatedMembers.txt b/compiler/testData/codegen/bytecodeListing/annotations/invalidJavaEnumArgumentsOnDelegatedMembers.txt index 438a15f..3f40ea7 100644 --- a/compiler/testData/codegen/bytecodeListing/annotations/invalidJavaEnumArgumentsOnDelegatedMembers.txt +++ b/compiler/testData/codegen/bytecodeListing/annotations/invalidJavaEnumArgumentsOnDelegatedMembers.txt
@@ -14,12 +14,12 @@ public method <init>(@org.jetbrains.annotations.NotNull p0: neg.Greeter): void public @neg.ArrayMarker(value=[A]) method arrayWithMissingEntry(): java.lang.String public @neg.DefaultedJavaAnnotation method javaAnnotationWithDefault(): java.lang.String - public @neg.RequiredJavaAnnotation method javaAnnotationWithoutDefault(): java.lang.String - public @neg.Marker method missingEntry(): java.lang.String - public @neg.Marker method missingPackage(): java.lang.String - public @neg.Marker method missingQualifier(): java.lang.String + public method javaAnnotationWithoutDefault(): java.lang.String + public method missingEntry(): java.lang.String + public method missingPackage(): java.lang.String + public method missingQualifier(): java.lang.String public @neg.Marker(value=1) method nonEnumNestedField(): java.lang.String - public @neg.Marker method objectProperty(): java.lang.String + public method objectProperty(): java.lang.String public final inner class neg/Marker$Scope }
diff --git a/compiler/testData/codegen/bytecodeListing/annotations/nonConstantJavaFieldArgumentsOnDelegatedMembers.txt b/compiler/testData/codegen/bytecodeListing/annotations/nonConstantJavaFieldArgumentsOnDelegatedMembers.txt index 511a296..4a5fcb9 100644 --- a/compiler/testData/codegen/bytecodeListing/annotations/nonConstantJavaFieldArgumentsOnDelegatedMembers.txt +++ b/compiler/testData/codegen/bytecodeListing/annotations/nonConstantJavaFieldArgumentsOnDelegatedMembers.txt
@@ -3,6 +3,6 @@ // source: 'main.kt' private synthetic final field $$delegate_0: p.Greeter public method <init>(@org.jetbrains.annotations.NotNull p0: p.Greeter): void - public @p.Ann method finalNonConstantField(): java.lang.String - public @p.Ann method nonFinalField(): java.lang.String + public method finalNonConstantField(): java.lang.String + public method nonFinalField(): java.lang.String }
diff --git a/compiler/testData/codegen/bytecodeListing/annotations/unresolvedEnumQualifierShadowedByPackage.txt b/compiler/testData/codegen/bytecodeListing/annotations/unresolvedEnumQualifierShadowedByPackage.txt index af4f22c..dd05649 100644 --- a/compiler/testData/codegen/bytecodeListing/annotations/unresolvedEnumQualifierShadowedByPackage.txt +++ b/compiler/testData/codegen/bytecodeListing/annotations/unresolvedEnumQualifierShadowedByPackage.txt
@@ -18,7 +18,7 @@ // source: 'main.kt' private synthetic final field $$delegate_0: p.Greeter public method <init>(@org.jetbrains.annotations.NotNull p0: p.Greeter): void - public @p.Marker method greet(): java.lang.String + public method greet(): java.lang.String } @kotlin.Metadata