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 7ae9259d232..680f3b6a4f4 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/cfg/JetFlowInformationProvider.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/cfg/JetFlowInformationProvider.java @@ -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()) { diff --git a/compiler/testData/checkerWithErrorTypes/full/regression/kt469.jet b/compiler/testData/checkerWithErrorTypes/full/regression/kt469.jet new file mode 100644 index 00000000000..3d01d2efee9 --- /dev/null +++ b/compiler/testData/checkerWithErrorTypes/full/regression/kt469.jet @@ -0,0 +1,27 @@ +namespace kt469 + +//KT-512 plusAssign() : Unit does not work properly +import java.util.* + +fun bar(list : List) { + for (i in 1..10) { + list += i // error + } + System.out?.println(list) +} + +fun List.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 + } +}