KT-2484 Type inferred for function literal is (...) -> Int instead of (...) -> Unit, when function literal parameter is explicit

#KT-2484 fixed
This commit is contained in:
Svetlana Isakova
2012-08-06 18:08:26 +04:00
parent 679a678e7c
commit f730c1357c
3 changed files with 29 additions and 2 deletions
@@ -135,8 +135,7 @@ public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
JetType safeReturnType = returnType == null ? ErrorUtils.createErrorType("<return type>") : returnType;
functionDescriptor.setReturnType(safeReturnType);
boolean hasDeclaredValueParameters = functionLiteral.getValueParameterList() != null;
if (!hasDeclaredValueParameters && functionTypeExpected) {
if (!functionLiteral.hasDeclaredReturnType() && functionTypeExpected) {
JetType expectedReturnType = JetStandardClasses.getReturnTypeFromFunctionType(expectedType);
if (JetStandardClasses.isUnit(expectedReturnType)) {
functionDescriptor.setReturnType(JetStandardClasses.getUnitType());
@@ -0,0 +1,23 @@
//KT-2484 Type inferred for function literal is (...) -> Int instead of (...) -> Unit, when function literal parameter is explicit
package a
//+JDK
import java.util.List
fun <T> Array<T>.forEach(operation: (T) -> Unit) : Unit = for (element in this) operation(element)
fun bar(operation: (String) -> Unit) = operation("")
fun main(args: Array<String>) {
args.forEach { (a : String) : Unit -> a.length } // Type mismatch: (String) -> Unit required, (String) -> Int found
args.forEach { (a) : Unit -> a.length } // Type mismatch: (String) -> Unit required, (String) -> Int found
args.forEach { (a : String) -> a.length } // Type mismatch: (String) -> Unit required, (String) -> Int found
args.forEach { a -> a.length } // Type mismatch: (String) -> Unit required, (String) -> Int found
args.forEach { it.length } // This works!
bar { (a: String) : Unit -> a.length }
bar { (a) : Unit -> a.length }
bar { (a: String) -> a.length }
bar { a -> a.length }
bar { it.length }
}
@@ -1380,6 +1380,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage
doTest("compiler/testData/diagnostics/tests/inference/regressions/kt2324.kt");
}
@TestMetadata("kt2484.kt")
public void testKt2484() throws Exception {
doTest("compiler/testData/diagnostics/tests/inference/regressions/kt2484.kt");
}
@TestMetadata("kt2514.kt")
public void testKt2514() throws Exception {
doTest("compiler/testData/diagnostics/tests/inference/regressions/kt2514.kt");