K2: Avoid using Nothing? as inference result in the majority of cases

Namely, do not choose `Nothing?` result type when fixing a variable
that has other constraints besides the ones that came from
the relevant type parameter's upper bounds.

See more details in KT-55691.

In K1, the case from specialCallWithMaterializeAndExpectedType.kt
was working (inferred to String?) just because the branches
were analyzed independently with `String?` expected type.

This change became necessary after the previous commit when we united
inference subsystems for if/when branches (see motivation there).

NB: For K1, the behavior is left the same, but the code
was refactored a bit.

^KT-55691 Fixed
^KT-56448 Fixed
This commit is contained in:
Denis.Zharkov
2022-12-02 19:28:18 +01:00
committed by Space Team
parent f12a4e08cf
commit 2bafcddf7a
18 changed files with 289 additions and 68 deletions
@@ -3120,7 +3120,7 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag
@Test
public void testAllFilesPresentInAdapted() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/callableReference/adapted"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/callableReference/adapted"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.(fir|ll)\\.kts?$"), true);
}
@Test
@@ -14486,6 +14486,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag
runTest("compiler/testData/diagnostics/tests/inference/nonFunctionalExpectedTypeForLambdaArgument.kt");
}
@Test
@TestMetadata("nothingVsParameterBound.kt")
public void testNothingVsParameterBound() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/nothingVsParameterBound.kt");
}
@Test
@TestMetadata("nullableTypeArgumentWithNotNullUpperBound.kt")
public void testNullableTypeArgumentWithNotNullUpperBound() throws Exception {
@@ -16533,6 +16539,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag
runTest("compiler/testData/diagnostics/tests/inference/nothingType/kt34335.kt");
}
@Test
@TestMetadata("kt56448.kt")
public void testKt56448() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/nothingType/kt56448.kt");
}
@Test
@TestMetadata("lambdaNothingAndExpectedType.kt")
public void testLambdaNothingAndExpectedType() throws Exception {
@@ -16587,6 +16599,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag
runTest("compiler/testData/diagnostics/tests/inference/nothingType/reportImplicitNothingOnlyForOwnTypeParameters.kt");
}
@Test
@TestMetadata("selectWithNull.kt")
public void testSelectWithNull() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/nothingType/selectWithNull.kt");
}
@Test
@TestMetadata("specialCallWithMaterializeAndExpectedType.kt")
public void testSpecialCallWithMaterializeAndExpectedType() throws Exception {