From 4d303b022540029f7829b6649ed3a60d1638a961 Mon Sep 17 00:00:00 2001 From: svtk Date: Thu, 8 Dec 2011 21:57:50 +0400 Subject: [PATCH] KT-782 Allow backing field usage for accessors of variables on namespace level --- .../lang/cfg/JetFlowInformationProvider.java | 5 ++-- .../jet/lang/resolve/ControlFlowAnalyzer.java | 30 +++++++++---------- .../backingField/kt782namespaceLevel.jet | 20 +++++++++++++ 3 files changed, 37 insertions(+), 18 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/backingField/kt782namespaceLevel.jet diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/cfg/JetFlowInformationProvider.java b/compiler/frontend/src/org/jetbrains/jet/lang/cfg/JetFlowInformationProvider.java index ac9e50bd495..d361c3297d0 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/cfg/JetFlowInformationProvider.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/cfg/JetFlowInformationProvider.java @@ -349,8 +349,8 @@ public class JetFlowInformationProvider { return true; } PsiElement property = trace.get(BindingContext.DESCRIPTOR_TO_DECLARATION, variableDescriptor); - if (!trace.get(BindingContext.BACKING_FIELD_REQUIRED, (PropertyDescriptor) variableDescriptor) && - !PsiTreeUtil.isAncestor(property, element, false)) { // not to generate error in accessors of abstract properties, there is one: declared accessor of abstract property + boolean insideSelfAccessors = PsiTreeUtil.isAncestor(property, element, false); + if (!trace.get(BindingContext.BACKING_FIELD_REQUIRED, (PropertyDescriptor) variableDescriptor) && !insideSelfAccessors) { // not to generate error in accessors of abstract properties, there is one: declared accessor of abstract property if (((PropertyDescriptor) variableDescriptor).getModality() == Modality.ABSTRACT) { trace.report(NO_BACKING_FIELD_ABSTRACT_PROPERTY.on(element)); } @@ -359,6 +359,7 @@ public class JetFlowInformationProvider { } return true; } + if (insideSelfAccessors) return false; JetNamedDeclaration parentDeclaration = PsiTreeUtil.getParentOfType(element, JetNamedDeclaration.class); DeclarationDescriptor declarationDescriptor = trace.get(BindingContext.DECLARATION_TO_DESCRIPTOR, parentDeclaration); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/ControlFlowAnalyzer.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/ControlFlowAnalyzer.java index 82e31375232..699122ea92a 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/ControlFlowAnalyzer.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/ControlFlowAnalyzer.java @@ -49,27 +49,25 @@ public class ControlFlowAnalyzer { if (!context.completeAnalysisNeeded(constructor)) continue; checkFunction(constructor, JetStandardClasses.getUnitType()); } + for (Map.Entry entry : context.getProperties().entrySet()) { + JetProperty property = entry.getKey(); + PropertyDescriptor propertyDescriptor = entry.getValue(); + checkProperty(property, propertyDescriptor); + } } private void checkClassOrObject(JetClassOrObject klass) { JetFlowInformationProvider flowInformationProvider = new JetFlowInformationProvider((JetDeclaration) klass, (JetExpression) klass, flowDataTraceFactory, context.getTrace()); flowInformationProvider.markUninitializedVariables((JetElement) klass, processLocalDeclaration); - - List declarations = klass.getDeclarations(); - for (JetDeclaration declaration : declarations) { - if (declaration instanceof JetProperty) { - JetProperty property = (JetProperty) declaration; - DeclarationDescriptor descriptor = context.getTrace().get(BindingContext.DECLARATION_TO_DESCRIPTOR, property); - assert descriptor instanceof PropertyDescriptor; - PropertyDescriptor propertyDescriptor = (PropertyDescriptor) descriptor; - for (JetPropertyAccessor accessor : property.getAccessors()) { - PropertyAccessorDescriptor accessorDescriptor = accessor.isGetter() - ? propertyDescriptor.getGetter() - : propertyDescriptor.getSetter(); - assert accessorDescriptor != null; - checkFunction(accessor, accessorDescriptor.getReturnType()); - } - } + } + + private void checkProperty(JetProperty property, PropertyDescriptor propertyDescriptor) { + for (JetPropertyAccessor accessor : property.getAccessors()) { + PropertyAccessorDescriptor accessorDescriptor = accessor.isGetter() + ? propertyDescriptor.getGetter() + : propertyDescriptor.getSetter(); + assert accessorDescriptor != null; + checkFunction(accessor, accessorDescriptor.getReturnType()); } } diff --git a/compiler/testData/diagnostics/tests/backingField/kt782namespaceLevel.jet b/compiler/testData/diagnostics/tests/backingField/kt782namespaceLevel.jet new file mode 100644 index 00000000000..70e44773cdf --- /dev/null +++ b/compiler/testData/diagnostics/tests/backingField/kt782namespaceLevel.jet @@ -0,0 +1,20 @@ +// KT-782 Allow backing field usage for accessors of variables on namespace level + +namespace kt782 + +val z : Int = 34 + +val y : Int = 11 +get() { + return $y +} + +val x : Int +get() = z + +val w : Int +get() = $z + +fun foo() { + $y = 34 +} \ No newline at end of file