Wrong diagnostic when last expression in lambda contains assignment operation

#KT-6769 Fixed
This commit is contained in:
Stanislav Erokhin
2015-02-26 22:50:55 +03:00
parent 09435ad0e3
commit 602689892e
8 changed files with 76 additions and 1 deletions
@@ -84,7 +84,7 @@ public class ExpressionTypingVisitorForStatements extends ExpressionTypingVisito
@NotNull ExpressionTypingContext context
) {
if (assignmentType != null && !KotlinBuiltIns.isUnit(assignmentType) && !noExpectedType(context.expectedType) &&
TypeUtils.equalTypes(context.expectedType, assignmentType)) {
!context.expectedType.isError() && TypeUtils.equalTypes(context.expectedType, assignmentType)) {
context.trace.report(Errors.ASSIGNMENT_TYPE_MISMATCH.on(expression, context.expectedType));
return null;
}
@@ -0,0 +1,16 @@
fun test(bal: Array<Int>) {
var bar = 4
val a = { bar += 4 }
a : () -> Unit
val b = { bar = 4 }
b : () -> Unit
val c = { bal[2] = 3 }
c : () -> Unit
val d = run { bar += 4 }
d : Unit
}
fun <T> run(f: () -> T): T = f()
@@ -0,0 +1,4 @@
package
internal fun </*0*/ T> run(/*0*/ f: () -> T): T
internal fun test(/*0*/ bal: kotlin.Array<kotlin.Int>): kotlin.Unit
@@ -0,0 +1,16 @@
fun test(bal: Array<Int>) {
var bar = 4
val <!UNUSED_VARIABLE!>a<!>: () -> Unit = { bar += 4 }
val <!UNUSED_VARIABLE!>b<!>: () -> Int = { <!EXPECTED_TYPE_MISMATCH!>bar = 4<!> }
val <!UNUSED_VARIABLE!>c<!>: () -> <!UNRESOLVED_REFERENCE!>UNRESOLVED<!> = { bal[2] = 3 }
val <!UNUSED_VARIABLE!>d<!>: () -> Int = { <!ASSIGNMENT_TYPE_MISMATCH(kotlin.Int)!>bar += 4<!> }
val <!UNUSED_VARIABLE!>e<!>: Unit = run { bar += 4 }
val <!UNUSED_VARIABLE!>f<!>: Int = <!TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH!>run<!> { bar += 4 }
}
fun <T> run(f: () -> T): T = f()
@@ -0,0 +1,4 @@
package
internal fun </*0*/ T> run(/*0*/ f: () -> T): T
internal fun test(/*0*/ bal: kotlin.Array<kotlin.Int>): kotlin.Unit
@@ -0,0 +1,11 @@
fun main(args : Array<String>) {
var list = listOf(1)
val a: Int? = 2
a?.let { list += it }
}
fun <T : Any, R> T.let(f: (T) -> R): R = f(this)
fun <T> Iterable<T>.plus(<!UNUSED_PARAMETER!>element<!>: T): List<T> = null!!
fun listOf<T>(vararg <!UNUSED_PARAMETER!>values<!>: T): List<T> = null!!
@@ -0,0 +1,6 @@
package
internal fun </*0*/ T> listOf(/*0*/ vararg values: T /*kotlin.Array<out T>*/): kotlin.List<T>
internal fun main(/*0*/ args: kotlin.Array<kotlin.String>): kotlin.Unit
internal fun </*0*/ T : kotlin.Any, /*1*/ R> T.let(/*0*/ f: (T) -> R): R
internal fun </*0*/ T> kotlin.Iterable<T>.plus(/*0*/ element: T): kotlin.List<T>
@@ -4521,6 +4521,18 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/tests/functionLiterals"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("assignmentOperationInLambda.kt")
public void testAssignmentOperationInLambda() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/functionLiterals/assignmentOperationInLambda.kt");
doTest(fileName);
}
@TestMetadata("assignmentOperationInLambdaWithExpectedType.kt")
public void testAssignmentOperationInLambdaWithExpectedType() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/functionLiterals/assignmentOperationInLambdaWithExpectedType.kt");
doTest(fileName);
}
@TestMetadata("DanglingFunctionLiteral.kt")
public void testDanglingFunctionLiteral() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/functionLiterals/DanglingFunctionLiteral.kt");
@@ -4569,6 +4581,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("kt6869.kt")
public void testKt6869() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/functionLiterals/kt6869.kt");
doTest(fileName);
}
@TestMetadata("LabeledFunctionLiterals.kt")
public void testLabeledFunctionLiterals() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/functionLiterals/LabeledFunctionLiterals.kt");