From 04e567dbf9abc7fde9383267ec105febf83f38fb Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Thu, 26 Dec 2019 17:32:44 +0300 Subject: [PATCH] [NI] Don't replace context dependency for blocks with expected type #KT-35702 Fixed --- ...irOldFrontendDiagnosticsTestGenerated.java | 5 +++ .../expressions/ExpressionTypingServices.java | 7 +--- .../tests/inference/kt35702.fir.kt | 41 +++++++++++++++++++ .../diagnostics/tests/inference/kt35702.kt | 41 +++++++++++++++++++ .../diagnostics/tests/inference/kt35702.txt | 14 +++++++ .../checkers/DiagnosticsTestGenerated.java | 5 +++ .../DiagnosticsUsingJavacTestGenerated.java | 5 +++ 7 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/inference/kt35702.fir.kt create mode 100644 compiler/testData/diagnostics/tests/inference/kt35702.kt create mode 100644 compiler/testData/diagnostics/tests/inference/kt35702.txt diff --git a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java index b7433aef74c..a83e4b73972 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java @@ -9933,6 +9933,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte runTest("compiler/testData/diagnostics/tests/inference/kt33263.kt"); } + @TestMetadata("kt35702.kt") + public void testKt35702() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt35702.kt"); + } + @TestMetadata("kt6175.kt") public void testKt6175() throws Exception { runTest("compiler/testData/diagnostics/tests/inference/kt6175.kt"); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingServices.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingServices.java index db3baa8aec3..059615f0268 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingServices.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingServices.java @@ -327,12 +327,7 @@ public class ExpressionTypingServices { expectedType = context.expectedType; } - ContextDependency dependency = context.contextDependency; - if (getLanguageVersionSettings().supportsFeature(LanguageFeature.NewInference)) { - dependency = ContextDependency.INDEPENDENT; - } - - return blockLevelVisitor.getTypeInfo(statementExpression, context.replaceExpectedType(expectedType).replaceContextDependency(dependency), true); + return blockLevelVisitor.getTypeInfo(statementExpression, context.replaceExpectedType(expectedType), true); } if (context.languageVersionSettings.supportsFeature(LanguageFeature.NewInference) && statementExpression instanceof KtLambdaExpression) { diff --git a/compiler/testData/diagnostics/tests/inference/kt35702.fir.kt b/compiler/testData/diagnostics/tests/inference/kt35702.fir.kt new file mode 100644 index 00000000000..62717970106 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/kt35702.fir.kt @@ -0,0 +1,41 @@ +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNCHECKED_CAST -UNUSED_VARIABLE -UNUSED_ANONYMOUS_PARAMETER -UNUSED_PARAMETER +// !WITH_NEW_INFERENCE +// ISSUE: KT-35702 + +interface A + +fun uncheckedCast(value: Any?): T = value as T + +fun select(x: K, y: K): K = x + +fun test_1(b: Boolean, s: String) { + val result: (A) -> T = if (b) { + { a: A -> uncheckedCast(s) } + } else { + { a: A -> uncheckedCast(s) } + } +} + +fun test_2(b: Boolean, s: String) { + val result: (A) -> T = if (b) { + { a: A -> uncheckedCast(s) } + } else { + { a -> uncheckedCast(s) } + } +} + +fun test_3(b: Boolean, s: String) { + val result: (A) -> T = if (b) { + { a -> uncheckedCast(s) } + } else { + { a -> uncheckedCast(s) } + } +} + +fun test_4(s: String) { + val result: (A) -> T = select( + { a: A -> uncheckedCast(s) }, + { a: A -> uncheckedCast(s) } + ) +} diff --git a/compiler/testData/diagnostics/tests/inference/kt35702.kt b/compiler/testData/diagnostics/tests/inference/kt35702.kt new file mode 100644 index 00000000000..62717970106 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/kt35702.kt @@ -0,0 +1,41 @@ +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNCHECKED_CAST -UNUSED_VARIABLE -UNUSED_ANONYMOUS_PARAMETER -UNUSED_PARAMETER +// !WITH_NEW_INFERENCE +// ISSUE: KT-35702 + +interface A + +fun uncheckedCast(value: Any?): T = value as T + +fun select(x: K, y: K): K = x + +fun test_1(b: Boolean, s: String) { + val result: (A) -> T = if (b) { + { a: A -> uncheckedCast(s) } + } else { + { a: A -> uncheckedCast(s) } + } +} + +fun test_2(b: Boolean, s: String) { + val result: (A) -> T = if (b) { + { a: A -> uncheckedCast(s) } + } else { + { a -> uncheckedCast(s) } + } +} + +fun test_3(b: Boolean, s: String) { + val result: (A) -> T = if (b) { + { a -> uncheckedCast(s) } + } else { + { a -> uncheckedCast(s) } + } +} + +fun test_4(s: String) { + val result: (A) -> T = select( + { a: A -> uncheckedCast(s) }, + { a: A -> uncheckedCast(s) } + ) +} diff --git a/compiler/testData/diagnostics/tests/inference/kt35702.txt b/compiler/testData/diagnostics/tests/inference/kt35702.txt new file mode 100644 index 00000000000..80ecf04d9c4 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/kt35702.txt @@ -0,0 +1,14 @@ +package + +public fun select(/*0*/ x: K, /*1*/ y: K): K +public fun test_1(/*0*/ b: kotlin.Boolean, /*1*/ s: kotlin.String): kotlin.Unit +public fun test_2(/*0*/ b: kotlin.Boolean, /*1*/ s: kotlin.String): kotlin.Unit +public fun test_3(/*0*/ b: kotlin.Boolean, /*1*/ s: kotlin.String): kotlin.Unit +public fun test_4(/*0*/ s: kotlin.String): kotlin.Unit +public fun uncheckedCast(/*0*/ value: kotlin.Any?): T + +public interface A { + 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/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 14a62f9d20b..e7a4a176d57 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -9940,6 +9940,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { runTest("compiler/testData/diagnostics/tests/inference/kt33263.kt"); } + @TestMetadata("kt35702.kt") + public void testKt35702() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt35702.kt"); + } + @TestMetadata("kt6175.kt") public void testKt6175() throws Exception { runTest("compiler/testData/diagnostics/tests/inference/kt6175.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index 3efae0e6fa0..846d7d5e5ed 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -9935,6 +9935,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/inference/kt33263.kt"); } + @TestMetadata("kt35702.kt") + public void testKt35702() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/kt35702.kt"); + } + @TestMetadata("kt6175.kt") public void testKt6175() throws Exception { runTest("compiler/testData/diagnostics/tests/inference/kt6175.kt");