From bf1e68a53fc62186c02b53de9f6aae428249e0fc Mon Sep 17 00:00:00 2001 From: Victor Petukhov Date: Thu, 19 Aug 2021 09:08:39 +0300 Subject: [PATCH] Resolve calls using the builder inference despite the presence of the annotation if there are uninferred type variables ^KT-48194 Fixed --- .../FirOldFrontendDiagnosticsTestGenerated.java | 6 ++++++ ...ontendDiagnosticsWithLightTreeTestGenerated.java | 6 ++++++ .../codegen/FirBlackBoxCodegenTestGenerated.java | 6 ------ .../calls/components/PostponedArgumentsAnalyzer.kt | 10 ++++++++-- .../components/KotlinConstraintSystemCompleter.kt | 4 +++- .../inference/builderInference/withoutAnnotation.kt | 13 +++++++++++++ .../withoutAnnotationDisabledFeature.fir.kt | 12 ++++++++++++ .../withoutAnnotationDisabledFeature.kt | 12 ++++++++++++ .../withoutAnnotationDisabledFeature.txt | 5 +++++ .../test/runners/DiagnosticTestGenerated.java | 6 ++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 ++++++ .../codegen/IrBlackBoxCodegenTestGenerated.java | 6 ++++++ .../codegen/LightAnalysisModeTestGenerated.java | 5 +++++ .../kotlin/config/LanguageVersionSettings.kt | 1 + .../semantics/IrJsCodegenBoxES6TestGenerated.java | 5 +++++ .../ir/semantics/IrJsCodegenBoxTestGenerated.java | 5 +++++ .../test/semantics/JsCodegenBoxTestGenerated.java | 5 +++++ 17 files changed, 104 insertions(+), 9 deletions(-) create mode 100644 compiler/testData/codegen/box/inference/builderInference/withoutAnnotation.kt create mode 100644 compiler/testData/diagnostics/tests/inference/builderInference/withoutAnnotationDisabledFeature.fir.kt create mode 100644 compiler/testData/diagnostics/tests/inference/builderInference/withoutAnnotationDisabledFeature.kt create mode 100644 compiler/testData/diagnostics/tests/inference/builderInference/withoutAnnotationDisabledFeature.txt diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java index 297302d2acb..b87a7ad3d14 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java @@ -13404,6 +13404,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/inference/builderInference/specialCallsWithLambdas.kt"); } + @Test + @TestMetadata("withoutAnnotationDisabledFeature.kt") + public void testWithoutAnnotationDisabledFeature() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/builderInference/withoutAnnotationDisabledFeature.kt"); + } + @Nested @TestMetadata("compiler/testData/diagnostics/tests/inference/builderInference/constraints") @TestDataPath("$PROJECT_ROOT") diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java index 6b5627f961d..6700477e5a2 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java @@ -13404,6 +13404,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/inference/builderInference/specialCallsWithLambdas.kt"); } + @Test + @TestMetadata("withoutAnnotationDisabledFeature.kt") + public void testWithoutAnnotationDisabledFeature() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/builderInference/withoutAnnotationDisabledFeature.kt"); + } + @Nested @TestMetadata("compiler/testData/diagnostics/tests/inference/builderInference/constraints") @TestDataPath("$PROJECT_ROOT") diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index 6a4e1b455fe..ef5f57f37fd 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -18234,12 +18234,6 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT public void testWithoutAnnotation() throws Exception { runTest("compiler/testData/codegen/box/inference/builderInference/withoutAnnotation.kt"); } - - @Test - @TestMetadata("withoutAnnotationDisabledFeature.kt") - public void testWithoutAnnotationDisabledFeature() throws Exception { - runTest("compiler/testData/codegen/box/inference/builderInference/withoutAnnotationDisabledFeature.kt"); - } } } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/PostponedArgumentsAnalyzer.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/PostponedArgumentsAnalyzer.kt index f5b1d8a0223..5cbbf2e05f7 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/PostponedArgumentsAnalyzer.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/PostponedArgumentsAnalyzer.kt @@ -9,6 +9,8 @@ import org.jetbrains.kotlin.builtins.StandardNames import org.jetbrains.kotlin.builtins.getReceiverTypeFromFunctionType import org.jetbrains.kotlin.builtins.getValueParameterTypesFromFunctionType import org.jetbrains.kotlin.builtins.isBuiltinFunctionalType +import org.jetbrains.kotlin.config.LanguageFeature +import org.jetbrains.kotlin.config.LanguageVersionSettings import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.descriptors.annotations.FilteredAnnotations import org.jetbrains.kotlin.resolve.calls.inference.addSubsystemFromArgument @@ -23,7 +25,8 @@ import org.jetbrains.kotlin.types.typeUtil.builtIns import org.jetbrains.kotlin.utils.addToStdlib.cast class PostponedArgumentsAnalyzer( - private val callableReferenceResolver: CallableReferenceResolver + private val callableReferenceResolver: CallableReferenceResolver, + private val languageVersionSettings: LanguageVersionSettings ) { fun analyze( @@ -180,7 +183,10 @@ class PostponedArgumentsAnalyzer( lambda.setAnalyzedResults(returnArgumentsInfo, subResolvedKtPrimitives) - if (inferenceSession != null && lambda.atom.hasBuilderInferenceAnnotation) { + val shouldUseBuilderInference = lambda.atom.hasBuilderInferenceAnnotation + || languageVersionSettings.supportsFeature(LanguageFeature.UseBuilderInferenceWithoutAnnotation) + + if (inferenceSession != null && shouldUseBuilderInference) { val storageSnapshot = c.getBuilder().currentStorage() val postponedVariables = inferenceSession.inferPostponedVariables( diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt index 59ac07cbdb7..5f4ce7883a0 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt @@ -176,9 +176,11 @@ class KotlinConstraintSystemCompleter( if (!useBuilderInferenceOnlyIfNeeded) return false val lambdaArguments = postponedArguments.filterIsInstance().takeIf { it.isNotEmpty() } ?: return false + val useBuilderInferenceWithoutAnnotation = + languageVersionSettings.supportsFeature(LanguageFeature.UseBuilderInferenceWithoutAnnotation) return lambdaArguments.any { argument -> - if (!argument.atom.hasBuilderInferenceAnnotation) + if (!argument.atom.hasBuilderInferenceAnnotation && !useBuilderInferenceWithoutAnnotation) return@any false val notFixedInputTypeVariables = argument.inputTypes diff --git a/compiler/testData/codegen/box/inference/builderInference/withoutAnnotation.kt b/compiler/testData/codegen/box/inference/builderInference/withoutAnnotation.kt new file mode 100644 index 00000000000..e9b5e9f1e20 --- /dev/null +++ b/compiler/testData/codegen/box/inference/builderInference/withoutAnnotation.kt @@ -0,0 +1,13 @@ +// WITH_RUNTIME +// DONT_TARGET_EXACT_BACKEND: WASM +// IGNORE_BACKEND_FIR: JVM_IR +// !LANGUAGE: +UseBuilderInferenceWithoutAnnotation + +fun buildMap(builderAction: MutableMap.() -> Unit): Map = mapOf() + +fun box(): String { + val x = buildMap { + put("", "") + } + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/withoutAnnotationDisabledFeature.fir.kt b/compiler/testData/diagnostics/tests/inference/builderInference/withoutAnnotationDisabledFeature.fir.kt new file mode 100644 index 00000000000..5cfc537c523 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/builderInference/withoutAnnotationDisabledFeature.fir.kt @@ -0,0 +1,12 @@ +// WITH_RUNTIME +// DONT_TARGET_EXACT_BACKEND: WASM +// !LANGUAGE: -UseBuilderInferenceWithoutAnnotation + +fun buildMap(builderAction: MutableMap.() -> Unit): Map = mapOf() + +fun box(): String { + val x = buildMap { + put("", "") + } + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/withoutAnnotationDisabledFeature.kt b/compiler/testData/diagnostics/tests/inference/builderInference/withoutAnnotationDisabledFeature.kt new file mode 100644 index 00000000000..553fbcff7a4 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/builderInference/withoutAnnotationDisabledFeature.kt @@ -0,0 +1,12 @@ +// WITH_RUNTIME +// DONT_TARGET_EXACT_BACKEND: WASM +// !LANGUAGE: -UseBuilderInferenceWithoutAnnotation + +fun buildMap(builderAction: MutableMap.() -> Unit): Map = mapOf() + +fun box(): String { + val x = buildMap { + put("", "") + } + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/builderInference/withoutAnnotationDisabledFeature.txt b/compiler/testData/diagnostics/tests/inference/builderInference/withoutAnnotationDisabledFeature.txt new file mode 100644 index 00000000000..ca70816bf40 --- /dev/null +++ b/compiler/testData/diagnostics/tests/inference/builderInference/withoutAnnotationDisabledFeature.txt @@ -0,0 +1,5 @@ +package + +public fun box(): kotlin.String +public fun buildMap(/*0*/ builderAction: kotlin.collections.MutableMap.() -> kotlin.Unit): kotlin.collections.Map + diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index 7780befc12b..29f9e03d08b 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -13410,6 +13410,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/inference/builderInference/specialCallsWithLambdas.kt"); } + @Test + @TestMetadata("withoutAnnotationDisabledFeature.kt") + public void testWithoutAnnotationDisabledFeature() throws Exception { + runTest("compiler/testData/diagnostics/tests/inference/builderInference/withoutAnnotationDisabledFeature.kt"); + } + @Nested @TestMetadata("compiler/testData/diagnostics/tests/inference/builderInference/constraints") @TestDataPath("$PROJECT_ROOT") diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java index 35b2b728678..44cfbf55b3a 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java @@ -18078,6 +18078,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { public void testWithExpectedType() throws Exception { runTest("compiler/testData/codegen/box/inference/builderInference/withExpectedType.kt"); } + + @Test + @TestMetadata("withoutAnnotation.kt") + public void testWithoutAnnotation() throws Exception { + runTest("compiler/testData/codegen/box/inference/builderInference/withoutAnnotation.kt"); + } } } diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index 22c8a81baa9..863005afa1d 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -18222,6 +18222,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes public void testWithExpectedType() throws Exception { runTest("compiler/testData/codegen/box/inference/builderInference/withExpectedType.kt"); } + + @Test + @TestMetadata("withoutAnnotation.kt") + public void testWithoutAnnotation() throws Exception { + runTest("compiler/testData/codegen/box/inference/builderInference/withoutAnnotation.kt"); + } } } diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 2a4de17e8e5..b1c46cd95b8 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -14979,6 +14979,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes public void testWithExpectedType() throws Exception { runTest("compiler/testData/codegen/box/inference/builderInference/withExpectedType.kt"); } + + @TestMetadata("withoutAnnotation.kt") + public void testWithoutAnnotation() throws Exception { + runTest("compiler/testData/codegen/box/inference/builderInference/withoutAnnotation.kt"); + } } } diff --git a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt index 2c35e07cbb0..61bb3134be5 100644 --- a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt +++ b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt @@ -226,6 +226,7 @@ enum class LanguageFeature( OptInRelease(KOTLIN_1_7), ProhibitNonExhaustiveWhenOnAlgebraicTypes(KOTLIN_1_7, kind = BUG_FIX), + UseBuilderInferenceWithoutAnnotation(KOTLIN_1_7), // Temporarily disabled, see KT-27084/KT-22379 SoundSmartcastFromLoopConditionForLoopAssignedVariables(sinceVersion = null, kind = BUG_FIX), diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java index 53644c3816f..e2ea92f5efb 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java @@ -13058,6 +13058,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes public void testWithExpectedType() throws Exception { runTest("compiler/testData/codegen/box/inference/builderInference/withExpectedType.kt"); } + + @TestMetadata("withoutAnnotation.kt") + public void testWithoutAnnotation() throws Exception { + runTest("compiler/testData/codegen/box/inference/builderInference/withoutAnnotation.kt"); + } } } diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index 9cae988420d..9c8ad21de8a 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -12464,6 +12464,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { public void testWithExpectedType() throws Exception { runTest("compiler/testData/codegen/box/inference/builderInference/withExpectedType.kt"); } + + @TestMetadata("withoutAnnotation.kt") + public void testWithoutAnnotation() throws Exception { + runTest("compiler/testData/codegen/box/inference/builderInference/withoutAnnotation.kt"); + } } } diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index d87f835237c..7e9ddd52ae7 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -12529,6 +12529,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { public void testWithExpectedType() throws Exception { runTest("compiler/testData/codegen/box/inference/builderInference/withExpectedType.kt"); } + + @TestMetadata("withoutAnnotation.kt") + public void testWithoutAnnotation() throws Exception { + runTest("compiler/testData/codegen/box/inference/builderInference/withoutAnnotation.kt"); + } } }