KT-782 Allow backing field usage for accessors of variables on namespace level

This commit is contained in:
svtk
2011-12-08 21:57:50 +04:00
parent 157875111d
commit 4d303b0225
3 changed files with 37 additions and 18 deletions
@@ -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);
@@ -49,27 +49,25 @@ public class ControlFlowAnalyzer {
if (!context.completeAnalysisNeeded(constructor)) continue;
checkFunction(constructor, JetStandardClasses.getUnitType());
}
for (Map.Entry<JetProperty, PropertyDescriptor> 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<JetDeclaration> 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());
}
}
@@ -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() = <!INACCESSIBLE_BACKING_FIELD!>$z<!>
fun foo() {
<!INACCESSIBLE_BACKING_FIELD!>$y<!> = 34
}