Instantiation of annotations for JVM IR with the corresponding feature flag

Seperate checker for platforms that do not support this language feature yet

Synthetic implementations of annotations are generated on-demand with proper 
equals, hashCode, and annotationType methods

#KT-47699 Fixed
This commit is contained in:
Leonid Startsev
2021-07-21 10:23:51 +00:00
committed by Space
parent 4bc521249b
commit ce0a3a57df
59 changed files with 1589 additions and 64 deletions
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.resolve.calls
@@ -238,7 +227,8 @@ class CallExpressionResolver(
if (functionDescriptor is ConstructorDescriptor) {
val constructedClass = functionDescriptor.constructedClass
if (DescriptorUtils.isAnnotationClass(constructedClass) && !canInstantiateAnnotationClass(callExpression, context.trace)) {
context.trace.report(ANNOTATION_CLASS_CONSTRUCTOR_CALL.on(callExpression))
val supported = context.languageVersionSettings.supportsFeature(LanguageFeature.InstantiationOfAnnotationClasses) && constructedClass.declaredTypeParameters.isEmpty()
if (!supported) context.trace.report(ANNOTATION_CLASS_CONSTRUCTOR_CALL.on(callExpression))
}
if (DescriptorUtils.isEnumClass(constructedClass)) {
context.trace.report(ENUM_CLASS_CONSTRUCTOR_CALL.on(callExpression))
@@ -515,7 +505,7 @@ class CallExpressionResolver(
companion object {
private fun canInstantiateAnnotationClass(expression: KtCallExpression, trace: BindingTrace): Boolean {
fun canInstantiateAnnotationClass(expression: KtCallExpression, trace: BindingTrace): Boolean {
//noinspection unchecked
var parent: PsiElement? = PsiTreeUtil.getParentOfType(expression, KtValueArgument::class.java, KtParameter::class.java)
if (parent is KtValueArgument) {
@@ -0,0 +1,40 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.resolve.calls.checkers
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.calls.CallExpressionResolver
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
/**
* Additional checker that prohibits usage of LanguageFeature.InstantiationOfAnnotationClasses on backends
* that do not support this feature yet
*/
object InstantiationOfAnnotationClassesCallChecker : CallChecker {
override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) {
if (!context.languageVersionSettings.supportsFeature(LanguageFeature.InstantiationOfAnnotationClasses)) return
val calledDescriptor = resolvedCall.resultingDescriptor as? ConstructorDescriptor ?: return
val constructedClass = calledDescriptor.constructedClass
val expression = resolvedCall.call.callElement as? KtCallExpression ?: return
if (DescriptorUtils.isAnnotationClass(constructedClass) && !CallExpressionResolver.canInstantiateAnnotationClass(
expression,
context.trace
)
) {
val supported = constructedClass.declaredTypeParameters.isEmpty()
if (supported) {
context.trace.report(Errors.ANNOTATION_CLASS_CONSTRUCTOR_CALL.on(expression))
} else {
// already reported in CallExpressionResolver.getCallExpressionTypeInfoWithoutFinalTypeCheck
}
}
}
}