KT-2838 Type inference failed on passing null as a nullable argument
#KT-2838 fixed
This commit is contained in:
+7
@@ -204,6 +204,13 @@ public class ConstraintSystemImpl implements ConstraintSystem {
|
||||
if (constrainingTypeDescriptor instanceof TypeParameterDescriptor) {
|
||||
assert typeParameterConstraints.get(constrainingTypeDescriptor) == null : "Constraining type contains type variable " + constrainingTypeDescriptor.getName();
|
||||
}
|
||||
if (constraintKind == SUB_TYPE && KotlinBuiltIns.getInstance().isNothingOrNullableNothing(constrainingType)) {
|
||||
// following constraints are always true:
|
||||
// 'Nothing' is a subtype of any type
|
||||
if (!constrainingType.isNullable()) return;
|
||||
// 'Nothing?' is a subtype of nullable type
|
||||
if (subjectType.isNullable()) return;
|
||||
}
|
||||
if (!(constrainingTypeDescriptor instanceof ClassDescriptor) || !(subjectTypeDescriptor instanceof ClassDescriptor)) {
|
||||
errorConstraintPositions.add(constraintPosition);
|
||||
return;
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
//KT-2838 Type inference failed on passing null as a nullable argument
|
||||
package a
|
||||
|
||||
fun foo<T>(a: T, b: Map<T, String>?) = b?.get(a)
|
||||
fun bar<T>(a: T, b: Map<T, String>) = b.get(a)
|
||||
|
||||
fun test(a: Int) {
|
||||
foo(a, null)
|
||||
<!TYPE_INFERENCE_TYPE_CONSTRUCTOR_MISMATCH!>bar<!>(a, null)
|
||||
}
|
||||
fun test1(a: Int) {
|
||||
<!UNREACHABLE_CODE!>foo(a, throw Exception())<!>
|
||||
}
|
||||
|
||||
fun test2(a: Int) {
|
||||
<!UNREACHABLE_CODE!>bar(a, throw Exception())<!>
|
||||
}
|
||||
@@ -2197,6 +2197,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage
|
||||
doTest("compiler/testData/diagnostics/tests/inference/regressions/kt2514.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt2838.kt")
|
||||
public void testKt2838() throws Exception {
|
||||
doTest("compiler/testData/diagnostics/tests/inference/regressions/kt2838.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt2841.kt")
|
||||
public void testKt2841() throws Exception {
|
||||
doTest("compiler/testData/diagnostics/tests/inference/regressions/kt2841.kt");
|
||||
|
||||
Reference in New Issue
Block a user