From 5e09a0c2c97812fa6cbe313d97cb724def1d5522 Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Thu, 27 Aug 2015 17:45:02 +0300 Subject: [PATCH] Add some test cases after review --- .../generics/nullability/functionalBound.kt | 13 ++++ .../generics/nullability/functionalBound.txt | 3 + .../nullability/smartCastRefinedClass.kt | 13 ++++ .../nullability/smartCastRefinedClass.txt | 3 + .../nullability/smartCastsValueArgument.kt | 2 + .../generics/nullability/tpBoundsViolation.kt | 35 +++++++--- .../nullability/tpBoundsViolation.txt | 4 +- .../nullability/tpBoundsViolationVariance.kt | 66 +++++++++++++++++++ .../nullability/tpBoundsViolationVariance.txt | 36 ++++++++++ .../checkers/JetDiagnosticsTestGenerated.java | 18 +++++ 10 files changed, 182 insertions(+), 11 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/generics/nullability/functionalBound.kt create mode 100644 compiler/testData/diagnostics/tests/generics/nullability/functionalBound.txt create mode 100644 compiler/testData/diagnostics/tests/generics/nullability/smartCastRefinedClass.kt create mode 100644 compiler/testData/diagnostics/tests/generics/nullability/smartCastRefinedClass.txt create mode 100644 compiler/testData/diagnostics/tests/generics/nullability/tpBoundsViolationVariance.kt create mode 100644 compiler/testData/diagnostics/tests/generics/nullability/tpBoundsViolationVariance.txt diff --git a/compiler/testData/diagnostics/tests/generics/nullability/functionalBound.kt b/compiler/testData/diagnostics/tests/generics/nullability/functionalBound.kt new file mode 100644 index 00000000000..572060a3510 --- /dev/null +++ b/compiler/testData/diagnostics/tests/generics/nullability/functionalBound.kt @@ -0,0 +1,13 @@ +fun Unit)?> foo(x: E, y: T) { + if (x != null) { + x.y() + } + + if (y != null) { + x.y() + } + + if (x != null && y != null) { + x.y() + } +} diff --git a/compiler/testData/diagnostics/tests/generics/nullability/functionalBound.txt b/compiler/testData/diagnostics/tests/generics/nullability/functionalBound.txt new file mode 100644 index 00000000000..ba6fa93f97e --- /dev/null +++ b/compiler/testData/diagnostics/tests/generics/nullability/functionalBound.txt @@ -0,0 +1,3 @@ +package + +internal fun kotlin.Unit)?> foo(/*0*/ x: E, /*1*/ y: T): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/generics/nullability/smartCastRefinedClass.kt b/compiler/testData/diagnostics/tests/generics/nullability/smartCastRefinedClass.kt new file mode 100644 index 00000000000..5049a26602a --- /dev/null +++ b/compiler/testData/diagnostics/tests/generics/nullability/smartCastRefinedClass.kt @@ -0,0 +1,13 @@ +fun foo(x: T) { + if (x is String?) { + x.length() + + if (x != null) { + x.length() + } + } + + if (x is String) { + x.length() + } +} diff --git a/compiler/testData/diagnostics/tests/generics/nullability/smartCastRefinedClass.txt b/compiler/testData/diagnostics/tests/generics/nullability/smartCastRefinedClass.txt new file mode 100644 index 00000000000..4ef0c871d84 --- /dev/null +++ b/compiler/testData/diagnostics/tests/generics/nullability/smartCastRefinedClass.txt @@ -0,0 +1,3 @@ +package + +internal fun foo(/*0*/ x: T): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/generics/nullability/smartCastsValueArgument.kt b/compiler/testData/diagnostics/tests/generics/nullability/smartCastsValueArgument.kt index b4497d20f89..44a0cc89a9f 100644 --- a/compiler/testData/diagnostics/tests/generics/nullability/smartCastsValueArgument.kt +++ b/compiler/testData/diagnostics/tests/generics/nullability/smartCastsValueArgument.kt @@ -14,6 +14,7 @@ fun foo(x: T) { y2 = x bar1(x) + bar1(x) bar2(x) bar3(x) } @@ -39,6 +40,7 @@ fun foo(x: T) { if (1 == 1) { val y = x!! bar1(x) + bar1(x) bar2(x) bar3(x) diff --git a/compiler/testData/diagnostics/tests/generics/nullability/tpBoundsViolation.kt b/compiler/testData/diagnostics/tests/generics/nullability/tpBoundsViolation.kt index a2ae506610a..66570e97cd7 100644 --- a/compiler/testData/diagnostics/tests/generics/nullability/tpBoundsViolation.kt +++ b/compiler/testData/diagnostics/tests/generics/nullability/tpBoundsViolation.kt @@ -1,31 +1,48 @@ -// !DIAGNOSTICS: -UNUSED_PARAMETER, -BASE_WITH_NULLABLE_UPPER_BOUND +// !CHECK_TYPE +// !DIAGNOSTICS: -UNUSED_PARAMETER, -BASE_WITH_NULLABLE_UPPER_BOUND, -UNUSED_VARIABLE class A { - fun foo1(x: E) {} - fun foo2(x: E) {} + fun foo1(x: E) = x + fun foo2(x: E) = x fun bar(x: F, y: F?, z: Z, w: W) { foo1(x) - foo1(x) + + val x1 = foo1(x) + x1.checkType { _() } + foo2(x) - foo2(x) + + val x2 = foo2(x) + x2.checkType { _() } foo1<F?>(y) foo1(y) foo2(y) - foo2(y) + + val x3 = foo2(y) + x3.checkType { _() } + foo1(y) foo2(y) foo1(z) - foo1(z) + + val x4 = foo1(z) + x4.checkType { _() } + foo2(z) - foo2(z) + + val x5 = foo2(z) + x4.checkType { _() } foo1<W>(w) foo1(w) foo2(w) - foo2(w) + + val x6 = foo2(w) + x6.checkType { _() } + foo1<W>(w) } } diff --git a/compiler/testData/diagnostics/tests/generics/nullability/tpBoundsViolation.txt b/compiler/testData/diagnostics/tests/generics/nullability/tpBoundsViolation.txt index ed21e4d1a31..ea738485998 100644 --- a/compiler/testData/diagnostics/tests/generics/nullability/tpBoundsViolation.txt +++ b/compiler/testData/diagnostics/tests/generics/nullability/tpBoundsViolation.txt @@ -4,8 +4,8 @@ internal final class A { public constructor A() internal final fun bar(/*0*/ x: F, /*1*/ y: F?, /*2*/ z: Z, /*3*/ w: W): kotlin.Unit public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean - internal final fun foo1(/*0*/ x: E): kotlin.Unit - internal final fun foo2(/*0*/ x: E): kotlin.Unit + internal final fun foo1(/*0*/ x: E): E + internal final fun foo2(/*0*/ x: E): E public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String } diff --git a/compiler/testData/diagnostics/tests/generics/nullability/tpBoundsViolationVariance.kt b/compiler/testData/diagnostics/tests/generics/nullability/tpBoundsViolationVariance.kt new file mode 100644 index 00000000000..6e50f104289 --- /dev/null +++ b/compiler/testData/diagnostics/tests/generics/nullability/tpBoundsViolationVariance.kt @@ -0,0 +1,66 @@ +// !CHECK_TYPE +// !DIAGNOSTICS: -UNUSED_PARAMETER, -BASE_WITH_NULLABLE_UPPER_BOUND, -UNUSED_VARIABLE + +class A { + class Inv + fun > fooInv1(x: E) = x + fun > fooInv2(x: E) = x + + class In + fun > fooIn1(x: E) = x + fun > fooIn2(x: E) = x + + class Out + fun > fooOut1(x: E) = x + fun > fooOut2(x: E) = x + + fun bar() { + // F + fooInv1>(Inv()) + fooInv2<Inv>(Inv()) + fooInv1(Inv()) + fooInv2(Inv()) + + fooIn1>(In()) + fooIn2>(In()) + fooIn1(In()) + fooIn2(In()) + + fooOut1>(Out()) + fooOut2>(Out()) + fooOut1(Out()) + fooOut2(Out()) + + // Z + fooInv1<Inv>(Inv()) + fooInv2<Inv>(Inv()) + fooInv1(Inv()) + fooInv2(Inv()) + + fooIn1<In>(In()) + fooIn2<In>(In()) + fooIn1(In()) + fooIn2(In()) + + fooOut1>(Out()) + fooOut2>(Out()) + fooOut1(Out()) + fooOut2(Out()) + + // W + fooInv1<Inv>(Inv()) + fooInv2<Inv>(Inv()) + fooInv1(Inv()) + fooInv2(Inv()) + + fooIn1<In>(In()) + fooIn2<In>(In()) + fooIn1(In()) + fooIn2(In()) + + fooOut1<Out>(Out()) + fooOut2>(Out()) + fooOut1(Out()) + fooOut2(Out()) + } +} diff --git a/compiler/testData/diagnostics/tests/generics/nullability/tpBoundsViolationVariance.txt b/compiler/testData/diagnostics/tests/generics/nullability/tpBoundsViolationVariance.txt new file mode 100644 index 00000000000..65e12cc5169 --- /dev/null +++ b/compiler/testData/diagnostics/tests/generics/nullability/tpBoundsViolationVariance.txt @@ -0,0 +1,36 @@ +package + +internal final class A { + public constructor A() + internal final fun bar(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + internal final fun > fooIn1(/*0*/ x: E): E + internal final fun > fooIn2(/*0*/ x: E): E + internal final fun > fooInv1(/*0*/ x: E): E + internal final fun > fooInv2(/*0*/ x: E): E + internal final fun > fooOut1(/*0*/ x: E): E + internal final fun > fooOut2(/*0*/ x: E): E + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + internal final class In { + public constructor In() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } + + internal final class Inv { + public constructor Inv() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } + + internal final class Out { + public constructor Out() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java index 88631817c94..63cb0343efe 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java @@ -6149,12 +6149,24 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("functionalBound.kt") + public void testFunctionalBound() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/nullability/functionalBound.kt"); + doTest(fileName); + } + @TestMetadata("nullToGeneric.kt") public void testNullToGeneric() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/nullability/nullToGeneric.kt"); doTest(fileName); } + @TestMetadata("smartCastRefinedClass.kt") + public void testSmartCastRefinedClass() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/nullability/smartCastRefinedClass.kt"); + doTest(fileName); + } + @TestMetadata("smartCasts.kt") public void testSmartCasts() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/nullability/smartCasts.kt"); @@ -6179,6 +6191,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("tpBoundsViolationVariance.kt") + public void testTpBoundsViolationVariance() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/nullability/tpBoundsViolationVariance.kt"); + doTest(fileName); + } + @TestMetadata("tpInBounds.kt") public void testTpInBounds() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/nullability/tpInBounds.kt");