KT-469 Allow val-reassignment when appropriate functions are defined

This commit is contained in:
svtk
2011-11-21 17:34:31 +04:00
parent a00ad38a1c
commit a7a5fa852e
2 changed files with 47 additions and 1 deletions
@@ -283,7 +283,26 @@ public class JetFlowInformationProvider {
PsiElement psiElement = trace.get(BindingContext.DESCRIPTOR_TO_DECLARATION, variableDescriptor);
JetProperty property = psiElement instanceof JetProperty ? (JetProperty) psiElement : null;
varWithValReassignErrorGenerated.add(variableDescriptor);
trace.report(Errors.VAL_REASSIGNMENT.on(expression, variableDescriptor, property == null ? new JetProperty[0] : new JetProperty[] { property }));
boolean hasReassignMethodReturningUnit = false;
JetSimpleNameExpression operationReference = null;
PsiElement parent = expression.getParent();
if (parent instanceof JetBinaryExpression) {
operationReference = ((JetBinaryExpression) parent).getOperationReference();
}
else if (parent instanceof JetUnaryExpression) {
operationReference = ((JetUnaryExpression) parent).getOperationSign();
}
if (operationReference != null) {
DeclarationDescriptor descriptor = trace.get(BindingContext.REFERENCE_TARGET, operationReference);
if (descriptor instanceof FunctionDescriptor) {
if (JetStandardClasses.isUnit(((FunctionDescriptor) descriptor).getReturnType())) {
hasReassignMethodReturningUnit = true;
}
}
}
if (!hasReassignMethodReturningUnit) {
trace.report(Errors.VAL_REASSIGNMENT.on(expression, variableDescriptor, property == null ? new JetProperty[0] : new JetProperty[]{property}));
}
}
if (inAnonymousInitializers && variableDescriptor instanceof PropertyDescriptor && !enterInitializationPoints.isInitialized() &&
exitInitializationPoints.isInitialized()) {
@@ -0,0 +1,27 @@
namespace kt469
//KT-512 plusAssign() : Unit does not work properly
import java.util.*
fun bar(list : List<Int>) {
for (i in 1..10) {
list += i // error
}
System.out?.println(list)
}
fun <T> List<T>.plusAssign(t : T) {
add(t)
}
//KT-469 Allow val-reassignment when appropriate functions are defined
fun foo() {
val m = MyNumber(2)
m -= MyNumber(3) //should not be error here
}
class MyNumber(var i: Int) {
fun minusAssign(m : MyNumber) {
i -= m.i
}
}