From f9fe8cd341aa973c899628c25c487ee9e5e2951d Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Tue, 9 Jun 2015 16:14:00 +0300 Subject: [PATCH] Fixed annotation resolve for loop parameter or catched exceptions type --- .../kotlin/resolve/DescriptorResolver.java | 21 ++++++++++++------- .../ExpressionTypingVisitorForStatements.java | 1 - .../tests/annotations/AnnotatedLoop.kt | 7 +++++++ .../tests/annotations/AnnotatedLoop.txt | 10 +++++++++ .../tests/annotations/AnnotatedTryCatch.kt | 9 ++++++++ .../tests/annotations/AnnotatedTryCatch.txt | 10 +++++++++ .../checkers/JetDiagnosticsTestGenerated.java | 12 +++++++++++ 7 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/annotations/AnnotatedLoop.kt create mode 100644 compiler/testData/diagnostics/tests/annotations/AnnotatedLoop.txt create mode 100644 compiler/testData/diagnostics/tests/annotations/AnnotatedTryCatch.kt create mode 100644 compiler/testData/diagnostics/tests/annotations/AnnotatedTryCatch.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java index 7015d54f9b7..6b2b9465f28 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java @@ -612,6 +612,8 @@ public class DescriptorResolver { toSourceElement(parameter) ); trace.record(BindingContext.VALUE_PARAMETER, parameter, variableDescriptor); + // Type annotations also should be resolved + AnnotationResolver.resolveAnnotationsArguments(type.getAnnotations()); return variableDescriptor; } @@ -623,6 +625,8 @@ public class DescriptorResolver { BindingTrace trace ) { DeclarationDescriptor containingDeclaration = scope.getContainingDeclaration(); + VariableDescriptor result; + JetType type; // SCRIPT: Create property descriptors if (JetPsiUtil.isScriptDeclaration(variable)) { PropertyDescriptorImpl propertyDescriptor = PropertyDescriptorImpl.create( @@ -635,25 +639,26 @@ public class DescriptorResolver { CallableMemberDescriptor.Kind.DECLARATION, toSourceElement(variable) ); - - JetType type = - getVariableType(propertyDescriptor, scope, variable, dataFlowInfo, false, trace); // For a local variable the type must not be deferred + // For a local variable the type must not be deferred + type = getVariableType(propertyDescriptor, scope, variable, dataFlowInfo, false, trace); ReceiverParameterDescriptor receiverParameter = ((ScriptDescriptor) containingDeclaration).getThisAsReceiverParameter(); propertyDescriptor.setType(type, Collections.emptyList(), receiverParameter, (JetType) null); initializeWithDefaultGetterSetter(propertyDescriptor); trace.record(BindingContext.VARIABLE, variable, propertyDescriptor); - return propertyDescriptor; + result = propertyDescriptor; } else { VariableDescriptorImpl variableDescriptor = resolveLocalVariableDescriptorWithType(scope, variable, null, trace); - - JetType type = - getVariableType(variableDescriptor, scope, variable, dataFlowInfo, false, trace); // For a local variable the type must not be deferred + // For a local variable the type must not be deferred + type = getVariableType(variableDescriptor, scope, variable, dataFlowInfo, false, trace); variableDescriptor.setOutType(type); - return variableDescriptor; + result = variableDescriptor; } + // Type annotations also should be resolved + AnnotationResolver.resolveAnnotationsArguments(type.getAnnotations()); + return result; } private static void initializeWithDefaultGetterSetter(PropertyDescriptorImpl propertyDescriptor) { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingVisitorForStatements.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingVisitorForStatements.java index 5dc79eae135..6dece3bb9b1 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingVisitorForStatements.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingVisitorForStatements.java @@ -136,7 +136,6 @@ public class ExpressionTypingVisitorForStatements extends ExpressionTypingVisito VariableDescriptor propertyDescriptor = components.descriptorResolver. resolveLocalVariableDescriptor(scope, property, context.dataFlowInfo, context.trace); - AnnotationResolver.resolveAnnotationsArguments(propertyDescriptor.getType().getAnnotations()); JetExpression initializer = property.getInitializer(); JetTypeInfo typeInfo; if (initializer != null) { diff --git a/compiler/testData/diagnostics/tests/annotations/AnnotatedLoop.kt b/compiler/testData/diagnostics/tests/annotations/AnnotatedLoop.kt new file mode 100644 index 00000000000..c7526291777 --- /dev/null +++ b/compiler/testData/diagnostics/tests/annotations/AnnotatedLoop.kt @@ -0,0 +1,7 @@ +annotation class My + +fun foo() { + for (i: @My Int in 0..41) { + if (i == 13) return + } +} diff --git a/compiler/testData/diagnostics/tests/annotations/AnnotatedLoop.txt b/compiler/testData/diagnostics/tests/annotations/AnnotatedLoop.txt new file mode 100644 index 00000000000..21dbc11cf3e --- /dev/null +++ b/compiler/testData/diagnostics/tests/annotations/AnnotatedLoop.txt @@ -0,0 +1,10 @@ +package + +internal fun foo(): kotlin.Unit + +internal final annotation class My : kotlin.Annotation { + public constructor My() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/testData/diagnostics/tests/annotations/AnnotatedTryCatch.kt b/compiler/testData/diagnostics/tests/annotations/AnnotatedTryCatch.kt new file mode 100644 index 00000000000..e98b5122708 --- /dev/null +++ b/compiler/testData/diagnostics/tests/annotations/AnnotatedTryCatch.kt @@ -0,0 +1,9 @@ +annotation class My + +fun foo(arg: Int): Int { + try { + return 1 / (arg - arg) + } catch (e: @My Exception) { + return -1 + } +} diff --git a/compiler/testData/diagnostics/tests/annotations/AnnotatedTryCatch.txt b/compiler/testData/diagnostics/tests/annotations/AnnotatedTryCatch.txt new file mode 100644 index 00000000000..8df46703222 --- /dev/null +++ b/compiler/testData/diagnostics/tests/annotations/AnnotatedTryCatch.txt @@ -0,0 +1,10 @@ +package + +internal fun foo(/*0*/ arg: kotlin.Int): kotlin.Int + +internal final annotation class My : kotlin.Annotation { + public constructor My() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java index 6c136a2ac13..b581a80a1d2 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java @@ -627,12 +627,24 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("AnnotatedLoop.kt") + public void testAnnotatedLoop() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/annotations/AnnotatedLoop.kt"); + doTest(fileName); + } + @TestMetadata("AnnotatedResultType.kt") public void testAnnotatedResultType() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/annotations/AnnotatedResultType.kt"); doTest(fileName); } + @TestMetadata("AnnotatedTryCatch.kt") + public void testAnnotatedTryCatch() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/annotations/AnnotatedTryCatch.kt"); + doTest(fileName); + } + @TestMetadata("AnnotationForClassTypeParameter.kt") public void testAnnotationForClassTypeParameter() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/annotations/AnnotationForClassTypeParameter.kt");