KT-469 Allow val-reassignment when appropriate functions are defined
This commit is contained in:
@@ -283,7 +283,26 @@ public class JetFlowInformationProvider {
|
|||||||
PsiElement psiElement = trace.get(BindingContext.DESCRIPTOR_TO_DECLARATION, variableDescriptor);
|
PsiElement psiElement = trace.get(BindingContext.DESCRIPTOR_TO_DECLARATION, variableDescriptor);
|
||||||
JetProperty property = psiElement instanceof JetProperty ? (JetProperty) psiElement : null;
|
JetProperty property = psiElement instanceof JetProperty ? (JetProperty) psiElement : null;
|
||||||
varWithValReassignErrorGenerated.add(variableDescriptor);
|
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() &&
|
if (inAnonymousInitializers && variableDescriptor instanceof PropertyDescriptor && !enterInitializationPoints.isInitialized() &&
|
||||||
exitInitializationPoints.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
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user