Support expected type from explicit cast

This commit support the following case.
Suppose we have such declaration:
  fun <T> foo(): T { ... }

Then in code we want to use it like this: `foo() as String`.
But in LV <= 1.1 we have type inference error: "Not enough
information for type parameter `T`". This error happened because we
do not use type from cast as expected type for call.

In this commit we fix this problem and use this type as expected type
in following cases:
 - our function has only one type parameter (this can be relaxed later)
 - function parameter types and extension receiver type not contains `T`

Also this fix problem with `findViewById`.
Already signature was: `fun findViewById(...): View`
and was used like: `findViewById() as MyView`.
New signature is `fun <T : View> findViewById(...): T`
and old usage was broken because of problem described above
This commit is contained in:
Stanislav Erokhin
2017-09-21 16:05:41 +03:00
committed by Ilya Gorbunov
parent ac508a510e
commit 4932fa1ddd
21 changed files with 412 additions and 1 deletions
@@ -10222,6 +10222,36 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("expectedTypeAdditionalTest.kt")
public void testExpectedTypeAdditionalTest() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/expectedTypeAdditionalTest.kt");
doTest(fileName);
}
@TestMetadata("expectedTypeFromCast.kt")
public void testExpectedTypeFromCast() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/expectedTypeFromCast.kt");
doTest(fileName);
}
@TestMetadata("expectedTypeFromCastComplexExpression.kt")
public void testExpectedTypeFromCastComplexExpression() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/expectedTypeFromCastComplexExpression.kt");
doTest(fileName);
}
@TestMetadata("expectedTypeWithGenerics.kt")
public void testExpectedTypeWithGenerics() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/expectedTypeWithGenerics.kt");
doTest(fileName);
}
@TestMetadata("findViewById.kt")
public void testFindViewById() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/findViewById.kt");
doTest(fileName);
}
@TestMetadata("fixVariableToNothing.kt")
public void testFixVariableToNothing() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/fixVariableToNothing.kt");
@@ -10222,6 +10222,36 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
doTest(fileName);
}
@TestMetadata("expectedTypeAdditionalTest.kt")
public void testExpectedTypeAdditionalTest() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/expectedTypeAdditionalTest.kt");
doTest(fileName);
}
@TestMetadata("expectedTypeFromCast.kt")
public void testExpectedTypeFromCast() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/expectedTypeFromCast.kt");
doTest(fileName);
}
@TestMetadata("expectedTypeFromCastComplexExpression.kt")
public void testExpectedTypeFromCastComplexExpression() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/expectedTypeFromCastComplexExpression.kt");
doTest(fileName);
}
@TestMetadata("expectedTypeWithGenerics.kt")
public void testExpectedTypeWithGenerics() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/expectedTypeWithGenerics.kt");
doTest(fileName);
}
@TestMetadata("findViewById.kt")
public void testFindViewById() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/findViewById.kt");
doTest(fileName);
}
@TestMetadata("fixVariableToNothing.kt")
public void testFixVariableToNothing() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/fixVariableToNothing.kt");
@@ -17825,6 +17825,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("expectedTypeFromCast.kt")
public void testExpectedTypeFromCast() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reified/expectedTypeFromCast.kt");
doTest(fileName);
}
@TestMetadata("filterIsInstance.kt")
public void testFilterIsInstance() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reified/filterIsInstance.kt");
@@ -17825,6 +17825,12 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
doTest(fileName);
}
@TestMetadata("expectedTypeFromCast.kt")
public void testExpectedTypeFromCast() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reified/expectedTypeFromCast.kt");
doTest(fileName);
}
@TestMetadata("filterIsInstance.kt")
public void testFilterIsInstance() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/reified/filterIsInstance.kt");