[IR] Add diagnostics to forbid annotations for MFVC-typed elements
Signed-off-by: Evgeniy.Zhelenskiy <Evgeniy.Zhelenskiy@jetbrains.com> #KT-1179
This commit is contained in:
committed by
Space Team
parent
adee33d3e5
commit
fa4ceb4ef4
@@ -430,6 +430,9 @@ public interface Errors {
|
||||
DiagnosticFactory0<KtContextReceiverList> VALUE_CLASS_CANNOT_HAVE_CONTEXT_RECEIVERS = DiagnosticFactory0.create(ERROR);
|
||||
DiagnosticFactory1<KtNamedFunction, KotlinType> INEFFICIENT_EQUALS_OVERRIDING_IN_VALUE_CLASS =
|
||||
DiagnosticFactory1.create(WARNING, DECLARATION_NAME);
|
||||
|
||||
DiagnosticFactory1<KtAnnotationEntry, String> ANNOTATION_ON_ILLEGAL_MULTI_FIELD_VALUE_CLASS_TYPED_TARGET =
|
||||
DiagnosticFactory1.create(ERROR);
|
||||
|
||||
// Result class
|
||||
|
||||
|
||||
+1
@@ -806,6 +806,7 @@ public class DefaultErrorMessages {
|
||||
MAP.put(INEFFICIENT_EQUALS_OVERRIDING_IN_VALUE_CLASS,
|
||||
"Overriding ''equals'' from ''Any'' in value class without operator ''equals(other: {0}): Boolean'' leads to boxing on every equality comparison",
|
||||
RENDER_TYPE);
|
||||
MAP.put(ANNOTATION_ON_ILLEGAL_MULTI_FIELD_VALUE_CLASS_TYPED_TARGET, "Annotations on {0} of multi-field value class type are not supported", STRING);
|
||||
|
||||
MAP.put(RESULT_CLASS_IN_RETURN_TYPE, "'kotlin.Result' cannot be used as a return type");
|
||||
MAP.put(RESULT_CLASS_WITH_NULLABLE_OPERATOR, "Expression of type ''kotlin.Result'' cannot be used as a left operand of ''{0}''", STRING);
|
||||
|
||||
@@ -27,6 +27,7 @@ private val DEFAULT_DECLARATION_CHECKERS = listOf(
|
||||
KClassWithIncorrectTypeArgumentChecker,
|
||||
SuspendLimitationsChecker,
|
||||
ValueClassDeclarationChecker,
|
||||
MultiFieldValueClassAnnotationsChecker,
|
||||
PropertiesWithBackingFieldsInsideValueClass(),
|
||||
InnerClassInsideValueClass(),
|
||||
AnnotationClassTargetAndRetentionChecker(),
|
||||
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright 2010-2022 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.checkers
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PropertyAccessorDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
||||
import org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.psi.KtAnnotationEntry
|
||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||
import org.jetbrains.kotlin.psi.KtProperty
|
||||
import org.jetbrains.kotlin.resolve.calls.util.getType
|
||||
import org.jetbrains.kotlin.resolve.needsMfvcFlattening
|
||||
import org.jetbrains.kotlin.resolve.source.getPsi
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
|
||||
object MultiFieldValueClassAnnotationsChecker : DeclarationChecker {
|
||||
private fun report(context: DeclarationCheckerContext, name: String, type: KotlinType, annotationEntry: KtAnnotationEntry) {
|
||||
if (!type.needsMfvcFlattening()) return
|
||||
context.trace.report(Errors.ANNOTATION_ON_ILLEGAL_MULTI_FIELD_VALUE_CLASS_TYPED_TARGET.on(annotationEntry, name))
|
||||
}
|
||||
|
||||
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
|
||||
fun report(name: String, type: KotlinType, annotations: Annotations) {
|
||||
for (annotationDescriptor in annotations) {
|
||||
report(context, name, type, annotationDescriptor.source.getPsi() as KtAnnotationEntry)
|
||||
}
|
||||
}
|
||||
|
||||
when (descriptor) {
|
||||
is PropertyDescriptor -> {
|
||||
descriptor.backingField?.let { report("fields", descriptor.type, it.annotations) }
|
||||
val delegateType = (declaration as? KtProperty)?.delegateExpression?.getType(context.trace.bindingContext)
|
||||
descriptor.delegateField?.let {
|
||||
if (delegateType == null) return@let
|
||||
report("delegate fields", delegateType, it.annotations)
|
||||
}
|
||||
descriptor.getter?.let { getterDescriptor ->
|
||||
if (getterDescriptor.contextReceiverParameters.isNotEmpty() || getterDescriptor.extensionReceiverParameter != null) return@let
|
||||
val type = getterDescriptor.returnType ?: return@let
|
||||
report("getters", type, getterDescriptor.annotations)
|
||||
}
|
||||
descriptor.setter?.valueParameters?.single()?.let { report("parameters", it.type, it.annotations) }
|
||||
descriptor.extensionReceiverParameter?.let { report("receivers", it.type, it.annotations) }
|
||||
descriptor.contextReceiverParameters.forEach { report("receivers", it.type, it.annotations) }
|
||||
}
|
||||
is PropertyAccessorDescriptor -> Unit
|
||||
is LocalVariableDescriptor -> {
|
||||
report("variables", descriptor.type, descriptor.annotations)
|
||||
}
|
||||
is CallableDescriptor -> {
|
||||
descriptor.extensionReceiverParameter?.let { report("receivers", it.type, it.annotations) }
|
||||
descriptor.contextReceiverParameters.forEach { report("receivers", it.type, it.annotations) }
|
||||
descriptor.valueParameters.forEach { report("parameters", it.type, it.annotations) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user