From 8b47f56eb961d477b0b608639f5892fa01582428 Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Tue, 4 Aug 2015 15:21:53 +0300 Subject: [PATCH] Allow property annotations on primary constructor parameter --- .../resolve/AnnotationUseSiteTargetChecker.kt | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationUseSiteTargetChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationUseSiteTargetChecker.kt index 30522661c33..ec83d157d9b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationUseSiteTargetChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationUseSiteTargetChecker.kt @@ -79,34 +79,39 @@ public object AnnotationUseSiteTargetChecker { modifierListOwner: JetAnnotated, descriptor: DeclarationDescriptor, annotationWithTarget: AnnotationWithTarget) { - if (checkIfPropertyDescriptor(descriptor, annotationWithTarget)) return + val propertyDescriptor = checkIfPropertyDescriptor(descriptor, annotationWithTarget) ?: return - descriptor as PropertyDescriptor val hasDelegate = modifierListOwner is JetProperty && modifierListOwner.hasDelegate() - if (!hasDelegate && !(bindingContext.get(BindingContext.BACKING_FIELD_REQUIRED, descriptor) ?: false)) { + if (!hasDelegate && !(bindingContext.get(BindingContext.BACKING_FIELD_REQUIRED, propertyDescriptor) ?: false)) { report(annotationWithTarget.annotation, INAPPLICABLE_FIELD_TARGET_NO_BACKING_FIELD) } } private fun BindingTrace.checkMutableProperty(descriptor: DeclarationDescriptor, annotationWIthTarget: AnnotationWithTarget) { - checkIfPropertyDescriptor(descriptor, annotationWIthTarget) + val propertyDescriptor = checkIfPropertyDescriptor(descriptor, annotationWIthTarget) ?: return - if (descriptor is PropertyDescriptor) { - if (!descriptor.isVar) { - report(annotationWIthTarget.annotation, INAPPLICABLE_TARGET_PROPERTY_IMMUTABLE) - } + if (!propertyDescriptor.isVar) { + report(annotationWIthTarget.annotation, INAPPLICABLE_TARGET_PROPERTY_IMMUTABLE) } } private fun BindingTrace.checkIfPropertyDescriptor( descriptor: DeclarationDescriptor, - annotationWithTarget: AnnotationWithTarget): Boolean { - if (descriptor !is PropertyDescriptor) { - report(annotationWithTarget, INAPPLICABLE_TARGET_ON_PROPERTY) - return true + annotationWithTarget: AnnotationWithTarget): PropertyDescriptor? { + if (descriptor is PropertyDescriptor) { + return descriptor } - return false + else if (descriptor is ValueParameterDescriptor) { + val jetParameter = DescriptorToSourceUtils.descriptorToDeclaration(descriptor) as? JetParameter + if (jetParameter != null && jetParameter.hasValOrVar()) { + val propertyDescriptor = bindingContext[BindingContext.VALUE_PARAMETER_AS_PROPERTY, descriptor] + if (propertyDescriptor != null) return propertyDescriptor + } + } + + report(annotationWithTarget, INAPPLICABLE_TARGET_ON_PROPERTY) + return null } private fun BindingTrace.report(annotation: AnnotationDescriptor, diagnosticFactory: DiagnosticFactory0) {