From 9b645589dfc85b146395778f6d547c8e046e38fa Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Wed, 26 Dec 2012 14:38:53 +0400 Subject: [PATCH] KT-2838 Type inference failed on passing null as a nullable argument #KT-2838 fixed --- .../calls/inference/ConstraintSystemImpl.java | 7 +++++++ .../tests/inference/regressions/kt2838.kt | 17 +++++++++++++++++ .../checkers/JetDiagnosticsTestGenerated.java | 5 +++++ 3 files changed, 29 insertions(+) create mode 100644 compiler/testData/diagnostics/tests/inference/regressions/kt2838.kt diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintSystemImpl.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintSystemImpl.java index 80bd161f20a..8ee4904477d 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintSystemImpl.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintSystemImpl.java @@ -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; diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt2838.kt b/compiler/testData/diagnostics/tests/inference/regressions/kt2838.kt new file mode 100644 index 00000000000..62b6addd8c6 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/regressions/kt2838.kt @@ -0,0 +1,17 @@ +//KT-2838 Type inference failed on passing null as a nullable argument +package a + +fun foo(a: T, b: Map?) = b?.get(a) +fun bar(a: T, b: Map) = b.get(a) + +fun test(a: Int) { + foo(a, null) + bar(a, null) +} +fun test1(a: Int) { + foo(a, throw Exception()) +} + +fun test2(a: Int) { + bar(a, throw Exception()) +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index 8355a856709..f8053d332e1 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -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");