Prohibit incorrect annotations with use-site targets more precisely
#KT-21696 Fixed The problem is coming from the fact that `AnnotationTarget.VALUE_PARAMETER` is mapped to receiver parameter and to value parameter, but annotation with use-site target `receiver` can be used only on type reference of receiver parameter
This commit is contained in:
@@ -1,23 +1,11 @@
|
||||
/*
|
||||
* 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 2000-2018 JetBrains s.r.o. 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
|
||||
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.config.LanguageFeature
|
||||
import org.jetbrains.kotlin.config.LanguageVersionSettings
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
@@ -154,15 +142,11 @@ class AnnotationChecker(
|
||||
}
|
||||
}
|
||||
|
||||
fun checkWithUseSiteTargets(additionalTarget: KotlinTarget? = null, useDeprecatedTargets: Boolean = false): Boolean {
|
||||
fun checkWithUseSiteTargets(): Boolean {
|
||||
if (useSiteTarget == null) return false
|
||||
|
||||
val useSiteMapping = KotlinTarget.USE_SITE_MAPPING[useSiteTarget].let {
|
||||
if (useDeprecatedTargets && it == RECEIVER) KotlinTarget.VALUE_PARAMETER else it
|
||||
}
|
||||
|
||||
val allTargets = actualTargets.onlyWithUseSiteTarget + listOfNotNull(additionalTarget)
|
||||
return allTargets.any { it in applicableTargets && it == useSiteMapping }
|
||||
val useSiteMapping = KotlinTarget.USE_SITE_MAPPING[useSiteTarget]
|
||||
return actualTargets.onlyWithUseSiteTarget.any { it in applicableTargets && it == useSiteMapping }
|
||||
}
|
||||
|
||||
if (check(actualTargets.defaultTargets) || check(actualTargets.canBeSubstituted) || checkWithUseSiteTargets()) {
|
||||
@@ -170,14 +154,6 @@ class AnnotationChecker(
|
||||
return
|
||||
}
|
||||
|
||||
if (!languageVersionSettings.supportsFeature(LanguageFeature.RestrictionOfWrongAnnotationsWithUseSiteTargetsOnTypes)) {
|
||||
val isAnnotationOnType = TargetLists.T_TYPE_REFERENCE == actualTargets
|
||||
if (isAnnotationOnType && checkWithUseSiteTargets(KotlinTarget.VALUE_PARAMETER, true)) {
|
||||
trace.report(Errors.WRONG_ANNOTATION_TARGET_WITH_USE_SITE_TARGET_ON_TYPE.on(entry, useSiteTarget!!.renderName))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if (useSiteTarget != null) {
|
||||
trace.report(
|
||||
Errors.WRONG_ANNOTATION_TARGET_WITH_USE_SITE_TARGET.on(
|
||||
@@ -350,7 +326,7 @@ class AnnotationChecker(
|
||||
val T_OBJECT_LITERAL = targetList(OBJECT_LITERAL, CLASS, EXPRESSION)
|
||||
|
||||
val T_TYPE_REFERENCE = targetList(TYPE) {
|
||||
onlyWithUseSiteTarget(RECEIVER)
|
||||
onlyWithUseSiteTarget(VALUE_PARAMETER)
|
||||
}
|
||||
|
||||
val T_TYPE_PARAMETER = targetList(TYPE_PARAMETER)
|
||||
|
||||
+54
-16
@@ -1,21 +1,12 @@
|
||||
/*
|
||||
* 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 2000-2018 JetBrains s.r.o. 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
|
||||
|
||||
import org.jetbrains.kotlin.config.LanguageFeature
|
||||
import org.jetbrains.kotlin.config.LanguageVersionSettings
|
||||
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
@@ -26,14 +17,60 @@ import org.jetbrains.kotlin.psi.*
|
||||
|
||||
object AnnotationUseSiteTargetChecker {
|
||||
|
||||
fun check(annotated: KtAnnotated, descriptor: DeclarationDescriptor, trace: BindingTrace) {
|
||||
fun check(
|
||||
annotated: KtAnnotated,
|
||||
descriptor: DeclarationDescriptor,
|
||||
trace: BindingTrace,
|
||||
languageVersionSettings: LanguageVersionSettings
|
||||
) {
|
||||
trace.checkDeclaration(annotated, descriptor)
|
||||
|
||||
if (annotated is KtCallableDeclaration) {
|
||||
annotated.receiverTypeReference?.let { trace.checkTypeReference(it, languageVersionSettings, isReceiver = true) }
|
||||
annotated.typeReference?.let { trace.checkTypeReference(it, languageVersionSettings, isReceiver = false) }
|
||||
}
|
||||
|
||||
if (annotated is KtFunction) {
|
||||
for (parameter in annotated.valueParameters) {
|
||||
if (parameter.hasValOrVar()) continue
|
||||
val parameterDescriptor = trace.bindingContext[BindingContext.VALUE_PARAMETER, parameter] ?: continue
|
||||
|
||||
trace.checkDeclaration(parameter, parameterDescriptor)
|
||||
parameter.typeReference?.let { trace.checkTypeReference(it, languageVersionSettings, isReceiver = false) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun BindingTrace.checkTypeReference(
|
||||
topLevelTypeReference: KtTypeReference,
|
||||
languageVersionSettings: LanguageVersionSettings,
|
||||
isReceiver: Boolean
|
||||
) {
|
||||
checkAsTopLevelTypeReference(topLevelTypeReference, languageVersionSettings, isReceiver)
|
||||
|
||||
topLevelTypeReference.acceptChildren(
|
||||
typeReferenceRecursiveVisitor { typeReference ->
|
||||
checkAsTopLevelTypeReference(typeReference, languageVersionSettings, isReceiver = false)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private fun BindingTrace.checkAsTopLevelTypeReference(
|
||||
topLevelTypeReference: KtTypeReference,
|
||||
languageVersionSettings: LanguageVersionSettings,
|
||||
isReceiver: Boolean
|
||||
) {
|
||||
for (annotationEntry in topLevelTypeReference.annotationEntries) {
|
||||
val target = annotationEntry.useSiteTarget?.getAnnotationUseSiteTarget() ?: continue
|
||||
|
||||
if (target != AnnotationUseSiteTarget.RECEIVER || !isReceiver) {
|
||||
val diagnostic =
|
||||
if (languageVersionSettings.supportsFeature(LanguageFeature.RestrictionOfWrongAnnotationsWithUseSiteTargetsOnTypes))
|
||||
WRONG_ANNOTATION_TARGET_WITH_USE_SITE_TARGET.on(annotationEntry, "undefined target", target.renderName)
|
||||
else
|
||||
WRONG_ANNOTATION_TARGET_WITH_USE_SITE_TARGET_ON_TYPE.on(annotationEntry, target.renderName)
|
||||
|
||||
reportDiagnosticOnce(diagnostic)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -64,8 +101,9 @@ object AnnotationUseSiteTargetChecker {
|
||||
}
|
||||
AnnotationUseSiteTarget.SETTER_PARAMETER -> checkIfMutableProperty(annotated, annotation)
|
||||
AnnotationUseSiteTarget.FILE -> reportDiagnosticOnce(INAPPLICABLE_FILE_TARGET.on(useSiteTarget))
|
||||
AnnotationUseSiteTarget.RECEIVER -> {
|
||||
}
|
||||
AnnotationUseSiteTarget.RECEIVER ->
|
||||
// annotation with use-site target `receiver` can be only on type reference, but not on declaration
|
||||
reportDiagnosticOnce(WRONG_ANNOTATION_TARGET_WITH_USE_SITE_TARGET.on(annotation, "declaration", target.renderName))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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 2000-2018 JetBrains s.r.o. 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;
|
||||
@@ -222,7 +211,7 @@ public class ModifiersChecker {
|
||||
}
|
||||
|
||||
private void checkModifierListCommon(@NotNull KtDeclaration modifierListOwner, @NotNull DeclarationDescriptor descriptor) {
|
||||
AnnotationUseSiteTargetChecker.INSTANCE.check(modifierListOwner, descriptor, trace);
|
||||
AnnotationUseSiteTargetChecker.INSTANCE.check(modifierListOwner, descriptor, trace, languageVersionSettings);
|
||||
runDeclarationCheckers(modifierListOwner, descriptor);
|
||||
annotationChecker.check(modifierListOwner, trace, descriptor);
|
||||
ModifierCheckerCore.INSTANCE.check(modifierListOwner, trace, descriptor, languageVersionSettings);
|
||||
|
||||
Reference in New Issue
Block a user