diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingVisitorForStatements.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingVisitorForStatements.java index 0e8b750ebc6..4308d3ef8ba 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingVisitorForStatements.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingVisitorForStatements.java @@ -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; } diff --git a/compiler/testData/diagnostics/tests/functionLiterals/assignmentOperationInLambda.kt b/compiler/testData/diagnostics/tests/functionLiterals/assignmentOperationInLambda.kt new file mode 100644 index 00000000000..2661582261b --- /dev/null +++ b/compiler/testData/diagnostics/tests/functionLiterals/assignmentOperationInLambda.kt @@ -0,0 +1,16 @@ +fun test(bal: Array) { + 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 run(f: () -> T): T = f() \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/functionLiterals/assignmentOperationInLambda.txt b/compiler/testData/diagnostics/tests/functionLiterals/assignmentOperationInLambda.txt new file mode 100644 index 00000000000..e4801ceb263 --- /dev/null +++ b/compiler/testData/diagnostics/tests/functionLiterals/assignmentOperationInLambda.txt @@ -0,0 +1,4 @@ +package + +internal fun run(/*0*/ f: () -> T): T +internal fun test(/*0*/ bal: kotlin.Array): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/functionLiterals/assignmentOperationInLambdaWithExpectedType.kt b/compiler/testData/diagnostics/tests/functionLiterals/assignmentOperationInLambdaWithExpectedType.kt new file mode 100644 index 00000000000..f054b9ca105 --- /dev/null +++ b/compiler/testData/diagnostics/tests/functionLiterals/assignmentOperationInLambdaWithExpectedType.kt @@ -0,0 +1,16 @@ +fun test(bal: Array) { + var bar = 4 + + val a: () -> Unit = { bar += 4 } + + val b: () -> Int = { bar = 4 } + + val c: () -> UNRESOLVED = { bal[2] = 3 } + + val d: () -> Int = { bar += 4 } + + val e: Unit = run { bar += 4 } + + val f: Int = run { bar += 4 } +} +fun run(f: () -> T): T = f() \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/functionLiterals/assignmentOperationInLambdaWithExpectedType.txt b/compiler/testData/diagnostics/tests/functionLiterals/assignmentOperationInLambdaWithExpectedType.txt new file mode 100644 index 00000000000..e4801ceb263 --- /dev/null +++ b/compiler/testData/diagnostics/tests/functionLiterals/assignmentOperationInLambdaWithExpectedType.txt @@ -0,0 +1,4 @@ +package + +internal fun run(/*0*/ f: () -> T): T +internal fun test(/*0*/ bal: kotlin.Array): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/functionLiterals/kt6869.kt b/compiler/testData/diagnostics/tests/functionLiterals/kt6869.kt new file mode 100644 index 00000000000..ac69102251b --- /dev/null +++ b/compiler/testData/diagnostics/tests/functionLiterals/kt6869.kt @@ -0,0 +1,11 @@ +fun main(args : Array) { + var list = listOf(1) + + val a: Int? = 2 + + a?.let { list += it } +} + +fun T.let(f: (T) -> R): R = f(this) +fun Iterable.plus(element: T): List = null!! +fun listOf(vararg values: T): List = null!! \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/functionLiterals/kt6869.txt b/compiler/testData/diagnostics/tests/functionLiterals/kt6869.txt new file mode 100644 index 00000000000..3f022ebdf7c --- /dev/null +++ b/compiler/testData/diagnostics/tests/functionLiterals/kt6869.txt @@ -0,0 +1,6 @@ +package + +internal fun listOf(/*0*/ vararg values: T /*kotlin.Array*/): kotlin.List +internal fun main(/*0*/ args: kotlin.Array): kotlin.Unit +internal fun T.let(/*0*/ f: (T) -> R): R +internal fun kotlin.Iterable.plus(/*0*/ element: T): kotlin.List diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java index 9a4fc2ab70c..af471419a1f 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java @@ -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");