diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.kt index 176ee9bf9ff..9c1cb32de77 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.kt @@ -323,6 +323,7 @@ abstract class CommonCompilerArguments : CommonToolArguments() { if (newInference) { put(LanguageFeature.NewInference, LanguageFeature.State.ENABLED) + put(LanguageFeature.SamConversionPerArgument, LanguageFeature.State.ENABLED) } if (legacySmartCastAfterTry) { @@ -360,11 +361,20 @@ abstract class CommonCompilerArguments : CommonToolArguments() { private fun HashMap.configureLanguageFeaturesFromInternalArgs(collector: MessageCollector) { val featuresThatForcePreReleaseBinaries = mutableListOf() + var standaloneSamConversionFeaturePassedExplicitly = false for ((feature, state) in internalArguments.filterIsInstance()) { put(feature, state) if (state == LanguageFeature.State.ENABLED && feature.forcesPreReleaseBinariesIfEnabled()) { featuresThatForcePreReleaseBinaries += feature } + + if (feature == LanguageFeature.SamConversionPerArgument) { + standaloneSamConversionFeaturePassedExplicitly = true + } + } + + if (!standaloneSamConversionFeaturePassedExplicitly && this[LanguageFeature.NewInference] == LanguageFeature.State.ENABLED) { + put(LanguageFeature.SamConversionPerArgument, LanguageFeature.State.ENABLED) } if (featuresThatForcePreReleaseBinaries.isNotEmpty()) { diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/JavaSyntheticScopes.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/JavaSyntheticScopes.kt index 2fdfb97b8e5..914dea47fd4 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/JavaSyntheticScopes.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/JavaSyntheticScopes.kt @@ -45,7 +45,8 @@ class JavaSyntheticScopes( val scopesWithForceEnabledSamAdapters: Collection init { - val newInferenceEnabled = languageVersionSettings.supportsFeature(LanguageFeature.NewInference) + val samConversionPerArgumentIsEnabled = + languageVersionSettings.supportsFeature(LanguageFeature.SamConversionPerArgument) val javaSyntheticPropertiesScope = JavaSyntheticPropertiesScope(storageManager, lookupTracker) val scopesFromExtensions = SyntheticScopeProviderExtension @@ -58,12 +59,12 @@ class JavaSyntheticScopes( samConventionResolver, deprecationResolver, lookupTracker, - samViaSyntheticScopeDisabled = newInferenceEnabled + samViaSyntheticScopeDisabled = samConversionPerArgumentIsEnabled ) scopes = listOf(javaSyntheticPropertiesScope, samAdapterFunctionsScope) + scopesFromExtensions - if (newInferenceEnabled) { + if (samConversionPerArgumentIsEnabled) { val forceEnabledSamAdapterFunctionsScope = SamAdapterFunctionsScope( storageManager, samConventionResolver, diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/syntheticExtensionsUtils.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/syntheticExtensionsUtils.kt index bc8dc1263f6..f6cb6a573f8 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/syntheticExtensionsUtils.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/syntheticExtensionsUtils.kt @@ -17,13 +17,11 @@ package org.jetbrains.kotlin.synthetic import org.jetbrains.kotlin.descriptors.* -import org.jetbrains.kotlin.descriptors.synthetic.SyntheticMemberDescriptor import org.jetbrains.kotlin.load.java.descriptors.JavaCallableMemberDescriptor import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor import org.jetbrains.kotlin.load.java.sam.SamAdapterDescriptor import org.jetbrains.kotlin.load.java.sam.SamConstructorDescriptor import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall -import org.jetbrains.kotlin.resolve.calls.model.ResolvedCallImpl import org.jetbrains.kotlin.resolve.calls.tower.NewResolvedCallImpl import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue @@ -63,13 +61,12 @@ fun syntheticVisibility(originalDescriptor: DeclarationDescriptorWithVisibility, } fun ResolvedCall.isResolvedWithSamConversions(): Boolean { - return if (this is NewResolvedCallImpl) { - // New inference - this.resolvedCallAtom.argumentsWithConversion.isNotEmpty() - } else { - // Old Inference - this.resultingDescriptor is SamAdapterDescriptor<*> || - this.resultingDescriptor is SamConstructorDescriptor || - this.resultingDescriptor is SamAdapterExtensionFunctionDescriptor + if (this is NewResolvedCallImpl && resolvedCallAtom.argumentsWithConversion.isNotEmpty()) { + return true } + + // Feature SamConversionPerArgument is disabled + return this.resultingDescriptor is SamAdapterDescriptor<*> || + this.resultingDescriptor is SamConstructorDescriptor || + this.resultingDescriptor is SamAdapterExtensionFunctionDescriptor } \ No newline at end of file diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ResolutionParts.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ResolutionParts.kt index 516efead521..e658ac81c9f 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ResolutionParts.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ResolutionParts.kt @@ -7,6 +7,7 @@ package org.jetbrains.kotlin.resolve.calls.components import org.jetbrains.kotlin.builtins.getReceiverTypeFromFunctionType import org.jetbrains.kotlin.builtins.isFunctionType +import org.jetbrains.kotlin.config.LanguageFeature import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptor import org.jetbrains.kotlin.resolve.calls.components.TypeArgumentsToParametersMapper.TypeArgumentsMapping.NoExplicitArguments @@ -295,6 +296,7 @@ private fun KotlinResolutionCandidate.getExpectedTypeWithSAMConversion( argument: KotlinCallArgument, candidateParameter: ParameterDescriptor ): UnwrappedType? { + if (!callComponents.languageVersionSettings.supportsFeature(LanguageFeature.SamConversionPerArgument)) return null if (!callComponents.samConversionTransformer.shouldRunSamConversionForFunction(resolvedCall.candidateDescriptor)) return null val argumentIsFunctional = when (argument) { diff --git a/compiler/testData/cli/jvm/standaloneSamConversionsAreDisabledExplicitlyWithNewInference.args b/compiler/testData/cli/jvm/standaloneSamConversionsAreDisabledExplicitlyWithNewInference.args new file mode 100644 index 00000000000..be57e4649ee --- /dev/null +++ b/compiler/testData/cli/jvm/standaloneSamConversionsAreDisabledExplicitlyWithNewInference.args @@ -0,0 +1,4 @@ +$TESTDATA_DIR$/standaloneSamConversionsAreEnabledWithNewInference.kt +-d +$TEMP_DIR$ +-Xnew-inference -XXLanguage:-SamConversionPerArgument \ No newline at end of file diff --git a/compiler/testData/cli/jvm/standaloneSamConversionsAreDisabledExplicitlyWithNewInference.out b/compiler/testData/cli/jvm/standaloneSamConversionsAreDisabledExplicitlyWithNewInference.out new file mode 100644 index 00000000000..d83ef45f7de --- /dev/null +++ b/compiler/testData/cli/jvm/standaloneSamConversionsAreDisabledExplicitlyWithNewInference.out @@ -0,0 +1,8 @@ +warning: flag is not supported by this version of the compiler: -Xnew-inference -XXLanguage:-SamConversionPerArgument +compiler/testData/cli/jvm/standaloneSamConversionsAreEnabledWithNewInference.kt:9:25: error: type mismatch: inferred type is () -> Unit but Runnable was expected + ForceSam.compare(r, {}) + ^ +compiler/testData/cli/jvm/standaloneSamConversionsAreEnabledWithNewInference.kt:10:22: error: type mismatch: inferred type is () -> Unit but Runnable was expected + ForceSam.compare({}, r) + ^ +COMPILATION_ERROR diff --git a/compiler/testData/cli/jvm/standaloneSamConversionsAreEnabledWithNewInference.args b/compiler/testData/cli/jvm/standaloneSamConversionsAreEnabledWithNewInference.args new file mode 100644 index 00000000000..0d78ff676ac --- /dev/null +++ b/compiler/testData/cli/jvm/standaloneSamConversionsAreEnabledWithNewInference.args @@ -0,0 +1,4 @@ +$TESTDATA_DIR$/standaloneSamConversionsAreEnabledWithNewInference.kt +-d +$TEMP_DIR$ +-Xnew-inference \ No newline at end of file diff --git a/compiler/testData/cli/jvm/standaloneSamConversionsAreEnabledWithNewInference.kt b/compiler/testData/cli/jvm/standaloneSamConversionsAreEnabledWithNewInference.kt new file mode 100644 index 00000000000..2b130e75adf --- /dev/null +++ b/compiler/testData/cli/jvm/standaloneSamConversionsAreEnabledWithNewInference.kt @@ -0,0 +1,11 @@ +object ForceSam : java.util.Comparator { + override fun compare(o1: Runnable, o2: Runnable): Int = 0 +} + +fun test(r: Runnable) { + ForceSam.compare(r, r) + ForceSam.compare({}, {}) + + ForceSam.compare(r, {}) + ForceSam.compare({}, r) +} diff --git a/compiler/testData/cli/jvm/standaloneSamConversionsAreEnabledWithNewInference.out b/compiler/testData/cli/jvm/standaloneSamConversionsAreEnabledWithNewInference.out new file mode 100644 index 00000000000..d86bac9de59 --- /dev/null +++ b/compiler/testData/cli/jvm/standaloneSamConversionsAreEnabledWithNewInference.out @@ -0,0 +1 @@ +OK diff --git a/compiler/testData/cli/jvm/standaloneSamConversionsAreEnabledWithNewInferenceInternalFlag.args b/compiler/testData/cli/jvm/standaloneSamConversionsAreEnabledWithNewInferenceInternalFlag.args new file mode 100644 index 00000000000..a0dff7a91fb --- /dev/null +++ b/compiler/testData/cli/jvm/standaloneSamConversionsAreEnabledWithNewInferenceInternalFlag.args @@ -0,0 +1,4 @@ +$TESTDATA_DIR$/standaloneSamConversionsAreEnabledWithNewInference.kt +-d +$TEMP_DIR$ +-XXLanguage:+NewInference \ No newline at end of file diff --git a/compiler/testData/cli/jvm/standaloneSamConversionsAreEnabledWithNewInferenceInternalFlag.out b/compiler/testData/cli/jvm/standaloneSamConversionsAreEnabledWithNewInferenceInternalFlag.out new file mode 100644 index 00000000000..b44197449b5 --- /dev/null +++ b/compiler/testData/cli/jvm/standaloneSamConversionsAreEnabledWithNewInferenceInternalFlag.out @@ -0,0 +1,10 @@ +warning: ATTENTION! +This build uses unsafe internal compiler arguments: + +-XXLanguage:+NewInference + +This mode is not recommended for production use, +as no stability/compatibility guarantees are given on +compiler or generated code. Use it at your own risk! + +OK diff --git a/compiler/testData/cli/jvm/standaloneSamConversionsBaseline_1_3.args b/compiler/testData/cli/jvm/standaloneSamConversionsBaseline_1_3.args new file mode 100644 index 00000000000..27788462fdc --- /dev/null +++ b/compiler/testData/cli/jvm/standaloneSamConversionsBaseline_1_3.args @@ -0,0 +1,3 @@ +$TESTDATA_DIR$/standaloneSamConversionsAreEnabledWithNewInference.kt +-d +$TEMP_DIR$ \ No newline at end of file diff --git a/compiler/testData/cli/jvm/standaloneSamConversionsBaseline_1_3.out b/compiler/testData/cli/jvm/standaloneSamConversionsBaseline_1_3.out new file mode 100644 index 00000000000..421a361ef02 --- /dev/null +++ b/compiler/testData/cli/jvm/standaloneSamConversionsBaseline_1_3.out @@ -0,0 +1,7 @@ +compiler/testData/cli/jvm/standaloneSamConversionsAreEnabledWithNewInference.kt:9:25: error: type mismatch: inferred type is () -> Unit but Runnable was expected + ForceSam.compare(r, {}) + ^ +compiler/testData/cli/jvm/standaloneSamConversionsAreEnabledWithNewInference.kt:10:22: error: type mismatch: inferred type is () -> Unit but Runnable was expected + ForceSam.compare({}, r) + ^ +COMPILATION_ERROR diff --git a/compiler/testData/codegen/box/javaInterop/genericSamProjectedOutWithNewInference.kt b/compiler/testData/codegen/box/javaInterop/genericSamProjectedOutWithNewInference.kt index 892d86441ea..1a779e1afe8 100644 --- a/compiler/testData/codegen/box/javaInterop/genericSamProjectedOutWithNewInference.kt +++ b/compiler/testData/codegen/box/javaInterop/genericSamProjectedOutWithNewInference.kt @@ -1,6 +1,5 @@ // !LANGUAGE: +NewInference // TARGET_BACKEND: JVM -// IGNORE_BACKEND: JVM_IR // FILE: example/Hello.java diff --git a/compiler/testData/codegen/box/sam/partialSam.kt b/compiler/testData/codegen/box/sam/partialSam.kt index a18b4198afa..b7345250da1 100644 --- a/compiler/testData/codegen/box/sam/partialSam.kt +++ b/compiler/testData/codegen/box/sam/partialSam.kt @@ -1,4 +1,4 @@ -// !LANGUAGE: +NewInference +// !LANGUAGE: +NewInference +SamConversionPerArgument // IGNORE_BACKEND: JVM_IR // TARGET_BACKEND: JVM // WITH_RUNTIME diff --git a/compiler/testData/codegen/box/sam/partialSamKT.kt b/compiler/testData/codegen/box/sam/partialSamKT.kt index 37e9d9d7af7..6202063e241 100644 --- a/compiler/testData/codegen/box/sam/partialSamKT.kt +++ b/compiler/testData/codegen/box/sam/partialSamKT.kt @@ -1,4 +1,4 @@ -// !LANGUAGE: +NewInference +SamConversionForKotlinFunctions +// !LANGUAGE: +NewInference +SamConversionForKotlinFunctions +SamConversionPerArgument // IGNORE_BACKEND: JVM_IR // TARGET_BACKEND: JVM // WITH_RUNTIME diff --git a/compiler/testData/diagnostics/tests/callableReference/generic/dependOnArgumentType.kt b/compiler/testData/diagnostics/tests/callableReference/generic/dependOnArgumentType.kt index fbe342c6e36..b7e690523a1 100644 --- a/compiler/testData/diagnostics/tests/callableReference/generic/dependOnArgumentType.kt +++ b/compiler/testData/diagnostics/tests/callableReference/generic/dependOnArgumentType.kt @@ -1,4 +1,4 @@ -// !WITH_NEW_INFERENCE +// !LANGUAGE: +SamConversionPerArgument +NewInference // !DIAGNOSTICS: -UNUSED_PARAMETER // FILE: A.java @@ -16,14 +16,14 @@ fun bar(s: T) {} fun complex(t: T, f: (T) -> Unit) {} fun test1() { - foo(1, A::invokeLater) // KT-24507 SAM conversion accidentally applied to callable reference and incorrectly handled via BE + foo(1, A::invokeLater) // KT-24507 SAM conversion accidentally applied to callable reference and incorrectly handled via BE foo(1, ::bar) complex(1, ::bar) } fun test2(x: R) { - foo(x, A::invokeLater) // KT-24507 SAM conversion accidentally applied to callable reference and incorrectly handled via BE + foo(x, A::invokeLater) // KT-24507 SAM conversion accidentally applied to callable reference and incorrectly handled via BE foo(x, ::bar) complex(x, ::bar) diff --git a/compiler/testData/diagnostics/tests/namedArguments/disallowForSamAdapterConstructor.kt b/compiler/testData/diagnostics/tests/namedArguments/disallowForSamAdapterConstructor.kt index 7ec20ec9c19..a66c092f403 100644 --- a/compiler/testData/diagnostics/tests/namedArguments/disallowForSamAdapterConstructor.kt +++ b/compiler/testData/diagnostics/tests/namedArguments/disallowForSamAdapterConstructor.kt @@ -13,5 +13,5 @@ public class J { package test fun test() { - J("", r = { }, z = false) + J("", r = { }, z = false) } diff --git a/compiler/testData/diagnostics/tests/namedArguments/disallowForSamAdapterFunction.kt b/compiler/testData/diagnostics/tests/namedArguments/disallowForSamAdapterFunction.kt index 5d9af118432..d50c2a49f28 100644 --- a/compiler/testData/diagnostics/tests/namedArguments/disallowForSamAdapterFunction.kt +++ b/compiler/testData/diagnostics/tests/namedArguments/disallowForSamAdapterFunction.kt @@ -13,5 +13,5 @@ public class J { package test fun test() { - J.foo("", r = { }, z = false) + J.foo("", r = { }, z = false) } diff --git a/compiler/testData/diagnostics/tests/platformTypes/rawTypes/samRaw.kt b/compiler/testData/diagnostics/tests/platformTypes/rawTypes/samRaw.kt index 0f43cdde327..cb66b6ba475 100644 --- a/compiler/testData/diagnostics/tests/platformTypes/rawTypes/samRaw.kt +++ b/compiler/testData/diagnostics/tests/platformTypes/rawTypes/samRaw.kt @@ -21,7 +21,7 @@ class B { fun main() { fun println() {} // All parameters in SAM adapter of `foo` have functional types - B().foo({ println() }, B.bar()) + B().foo({ println() }, B.bar()) // So you should use SAM constructors when you want to use mix lambdas and Java objects B().foo(Runnable { println() }, B.bar()) B().foo({ println() }, { it: Any? -> it == null } ) diff --git a/compiler/testData/diagnostics/tests/samConversions/GenericSubstitution.kt b/compiler/testData/diagnostics/tests/samConversions/GenericSubstitution.kt index bdaa10de4c9..85ccf5612eb 100644 --- a/compiler/testData/diagnostics/tests/samConversions/GenericSubstitution.kt +++ b/compiler/testData/diagnostics/tests/samConversions/GenericSubstitution.kt @@ -1,4 +1,4 @@ -// !LANGUAGE: +NewInference +// !LANGUAGE: +NewInference +SamConversionPerArgument // !CHECK_TYPE // FILE: J.java public interface J { diff --git a/compiler/testData/diagnostics/tests/samConversions/GenericSubstitutionKT.kt b/compiler/testData/diagnostics/tests/samConversions/GenericSubstitutionKT.kt index 15ff40e4c46..0bf63f43541 100644 --- a/compiler/testData/diagnostics/tests/samConversions/GenericSubstitutionKT.kt +++ b/compiler/testData/diagnostics/tests/samConversions/GenericSubstitutionKT.kt @@ -1,4 +1,4 @@ -// !LANGUAGE: +NewInference +SamConversionForKotlinFunctions +// !LANGUAGE: +NewInference +SamConversionForKotlinFunctions +SamConversionPerArgument // !CHECK_TYPE // FILE: F.java public interface F { diff --git a/compiler/testData/diagnostics/tests/samConversions/OverloadPriorityKT.kt b/compiler/testData/diagnostics/tests/samConversions/OverloadPriorityKT.kt index 100df86b001..3c30a5bc341 100644 --- a/compiler/testData/diagnostics/tests/samConversions/OverloadPriorityKT.kt +++ b/compiler/testData/diagnostics/tests/samConversions/OverloadPriorityKT.kt @@ -1,4 +1,4 @@ -// !LANGUAGE: +NewInference +SamConversionForKotlinFunctions +// !LANGUAGE: +NewInference +SamConversionForKotlinFunctions +SamConversionPerArgument // !CHECK_TYPE // FILE: Fn.java public interface Fn { diff --git a/compiler/testData/diagnostics/tests/samConversions/SimpleCorrect.kt b/compiler/testData/diagnostics/tests/samConversions/SimpleCorrect.kt index 3da568b283f..977da70c817 100644 --- a/compiler/testData/diagnostics/tests/samConversions/SimpleCorrect.kt +++ b/compiler/testData/diagnostics/tests/samConversions/SimpleCorrect.kt @@ -1,4 +1,4 @@ -// !LANGUAGE: +NewInference +SamConversionForKotlinFunctions +// !LANGUAGE: +NewInference +SamConversionPerArgument // FILE: J.java public interface J { public void foo1(Runnable r); diff --git a/compiler/testData/diagnostics/tests/samConversions/SimpleCorrectKT.kt b/compiler/testData/diagnostics/tests/samConversions/SimpleCorrectKT.kt index a5a8b0920f2..dfafa0c65a8 100644 --- a/compiler/testData/diagnostics/tests/samConversions/SimpleCorrectKT.kt +++ b/compiler/testData/diagnostics/tests/samConversions/SimpleCorrectKT.kt @@ -1,4 +1,4 @@ -// !LANGUAGE: +NewInference +SamConversionForKotlinFunctions +// !LANGUAGE: +NewInference +SamConversionForKotlinFunctions +SamConversionPerArgument // FILE: Runnable.java public interface Runnable { void run(); diff --git a/compiler/testData/diagnostics/tests/scopes/protectedVisibility/syntheticSAMExtensions.kt b/compiler/testData/diagnostics/tests/scopes/protectedVisibility/syntheticSAMExtensions.kt index 308acfa0c82..f0e62c09462 100644 --- a/compiler/testData/diagnostics/tests/scopes/protectedVisibility/syntheticSAMExtensions.kt +++ b/compiler/testData/diagnostics/tests/scopes/protectedVisibility/syntheticSAMExtensions.kt @@ -21,7 +21,7 @@ class B : A() { } if (d.x is B) { - d.x.foo {} + d.x.foo {} } } } diff --git a/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/InnerClassInGeneric.kt b/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/InnerClassInGeneric.kt index 680412ce5dd..0f3160e2a19 100644 --- a/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/InnerClassInGeneric.kt +++ b/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/InnerClassInGeneric.kt @@ -2,7 +2,7 @@ // FILE: KotlinFile.kt fun foo(javaClass: JavaClass): Int { val inner = javaClass.createInner() - return inner.doSomething(1, "") { } + return inner.doSomething(1, "") { } } // FILE: JavaClass.java diff --git a/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/NoNamedArgsAllowed.kt b/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/NoNamedArgsAllowed.kt index c0f3ecee47e..3add3166896 100644 --- a/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/NoNamedArgsAllowed.kt +++ b/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/NoNamedArgsAllowed.kt @@ -1,9 +1,9 @@ // !WITH_NEW_INFERENCE // FILE: KotlinFile.kt fun foo(javaClass: JavaClass) { - javaClass.doSomething(p = 1) { + javaClass.doSomething(p = 1) { bar() - } + } } fun bar(){} diff --git a/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/Private.kt b/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/Private.kt index 044299fd325..3da385c170f 100644 --- a/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/Private.kt +++ b/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/Private.kt @@ -1,7 +1,7 @@ // !WITH_NEW_INFERENCE // FILE: KotlinFile.kt fun foo(javaClass: JavaClass) { - javaClass.doSomething { } + javaClass.doSomething { } } // FILE: JavaClass.java diff --git a/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java index b2448527782..3e716bb0bc8 100644 --- a/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java @@ -576,6 +576,26 @@ public class CliTestGenerated extends AbstractCliTest { runTest("compiler/testData/cli/jvm/singleJavaFileRoots.args"); } + @TestMetadata("standaloneSamConversionsAreDisabledExplicitlyWithNewInference.args") + public void testStandaloneSamConversionsAreDisabledExplicitlyWithNewInference() throws Exception { + runTest("compiler/testData/cli/jvm/standaloneSamConversionsAreDisabledExplicitlyWithNewInference.args"); + } + + @TestMetadata("standaloneSamConversionsAreEnabledWithNewInference.args") + public void testStandaloneSamConversionsAreEnabledWithNewInference() throws Exception { + runTest("compiler/testData/cli/jvm/standaloneSamConversionsAreEnabledWithNewInference.args"); + } + + @TestMetadata("standaloneSamConversionsAreEnabledWithNewInferenceInternalFlag.args") + public void testStandaloneSamConversionsAreEnabledWithNewInferenceInternalFlag() throws Exception { + runTest("compiler/testData/cli/jvm/standaloneSamConversionsAreEnabledWithNewInferenceInternalFlag.args"); + } + + @TestMetadata("standaloneSamConversionsBaseline_1_3.args") + public void testStandaloneSamConversionsBaseline_1_3() throws Exception { + runTest("compiler/testData/cli/jvm/standaloneSamConversionsBaseline_1_3.args"); + } + @TestMetadata("suppressAllWarningsJvm.args") public void testSuppressAllWarningsJvm() throws Exception { runTest("compiler/testData/cli/jvm/suppressAllWarningsJvm.args"); diff --git a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt index 3b091d1bca8..ee4e655e623 100644 --- a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt +++ b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt @@ -125,6 +125,8 @@ enum class LanguageFeature( SamConversionForKotlinFunctions(sinceVersion = KOTLIN_1_3, defaultState = State.DISABLED), + SamConversionPerArgument(sinceVersion = KOTLIN_1_3, defaultState = State.DISABLED), + // can be used only with NewInference feature NewDataFlowForTryExpressions(sinceVersion = KOTLIN_1_3, defaultState = State.DISABLED), diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantSamConstructorInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantSamConstructorInspection.kt index c6e0d79a7ab..5e6f1614a21 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantSamConstructorInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantSamConstructorInspection.kt @@ -10,11 +10,13 @@ import com.intellij.codeInspection.* import com.intellij.openapi.project.Project import com.intellij.psi.PsiElementVisitor import org.jetbrains.kotlin.codegen.SamCodegenUtil +import org.jetbrains.kotlin.config.LanguageFeature import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade import org.jetbrains.kotlin.idea.codeInsight.forceEnableSamAdapters +import org.jetbrains.kotlin.idea.project.languageVersionSettings import org.jetbrains.kotlin.idea.resolve.frontendService import org.jetbrains.kotlin.idea.util.getResolutionScope import org.jetbrains.kotlin.incremental.components.NoLookupLocation @@ -131,10 +133,13 @@ class RedundantSamConstructorInspection : AbstractKotlinInspection() { if (!resolutionResults.isSuccess) return false val samAdapterOriginalDescriptor = - if (resolutionResults.resultingCall is NewResolvedCallImpl<*>) + if (parentCall.languageVersionSettings.supportsFeature(LanguageFeature.SamConversionPerArgument) && + resolutionResults.resultingCall is NewResolvedCallImpl<*> + ) { resolutionResults.resultingDescriptor - else + } else { SamCodegenUtil.getOriginalIfSamAdapter(resolutionResults.resultingDescriptor) ?: return false + } return samAdapterOriginalDescriptor.original == originalCall.resultingDescriptor.original } diff --git a/idea/testData/checker/diagnosticsMessage/standaloneSamConversionIsDisabledInIDE.kt b/idea/testData/checker/diagnosticsMessage/standaloneSamConversionIsDisabledInIDE.kt new file mode 100644 index 00000000000..7081d58f9c9 --- /dev/null +++ b/idea/testData/checker/diagnosticsMessage/standaloneSamConversionIsDisabledInIDE.kt @@ -0,0 +1,24 @@ +object ForceSam : java.util.Comparator { + override fun compare(o1: Runnable, o2: Runnable): Int = 0 +} + +fun test(r: Runnable) { + ForceSam.compare(r, r) + ForceSam.compare({}, {}) + + ForceSam.compare(r, {}) + ForceSam.compare({}, r) +} + +// Check that new inference is enabled +object Scope { + interface A + interface B + class C + + fun foo(c: C) where K : A, K : B {} + + fun usage(c: C) { + foo(c) // should compile only in NI + } +} diff --git a/idea/tests/org/jetbrains/kotlin/checkers/PsiCheckerTestGenerated.java b/idea/tests/org/jetbrains/kotlin/checkers/PsiCheckerTestGenerated.java index 7cf03400934..2cbfb00ae6d 100644 --- a/idea/tests/org/jetbrains/kotlin/checkers/PsiCheckerTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/checkers/PsiCheckerTestGenerated.java @@ -930,5 +930,10 @@ public class PsiCheckerTestGenerated extends AbstractPsiCheckerTest { public void testOperatorCallDiagnosticsOnInOperator() throws Exception { runTest("idea/testData/checker/diagnosticsMessage/operatorCallDiagnosticsOnInOperator.kt"); } + + @TestMetadata("standaloneSamConversionIsDisabledInIDE.kt") + public void testStandaloneSamConversionIsDisabledInIDE() throws Exception { + runTest("idea/testData/checker/diagnosticsMessage/standaloneSamConversionIsDisabledInIDE.kt"); + } } }