From fa50d66afee252359d18b5eea94fe6e512c54b21 Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Mon, 27 Apr 2020 03:41:12 +0300 Subject: [PATCH] [NI] Fix SAM conversion for projected-out members #KT-25290 Fixed --- .../kotlin/synthetic/SamAdapterFunctionsScope.kt | 8 +++++++- .../kotlin/resolve/calls/components/ResolutionParts.kt | 3 +++ .../javaInterop/genericSamProjectedOutWithNewInference.kt | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SamAdapterFunctionsScope.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SamAdapterFunctionsScope.kt index 40feaca70d4..5390b03391e 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SamAdapterFunctionsScope.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SamAdapterFunctionsScope.kt @@ -43,6 +43,7 @@ import org.jetbrains.kotlin.resolve.scopes.SyntheticScope import org.jetbrains.kotlin.storage.StorageManager import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.checker.findCorrespondingSupertype +import org.jetbrains.kotlin.types.typeUtil.isNothing import kotlin.properties.Delegates interface SamAdapterExtensionFunctionDescriptor : FunctionDescriptor, FunctionInterfaceAdapterExtensionFunctionDescriptor { @@ -106,7 +107,9 @@ class SamAdapterFunctionsScope( var result: SmartList? = null for (type in receiverTypes) { for (function in type.memberScope.getContributedFunctions(name, location)) { - if (samViaSyntheticScopeDisabled && !function.shouldGenerateCandidateForVarargAfterSamAndHasVararg) continue + if (samViaSyntheticScopeDisabled && !function.hasNothingTypeInParameters) { + if (!function.shouldGenerateCandidateForVarargAfterSamAndHasVararg) continue + } val extension = extensionForFunction(function.original)?.substituteForReceiverType(type) if (extension != null) { @@ -134,6 +137,9 @@ class SamAdapterFunctionsScope( private val FunctionDescriptor.shouldGenerateCandidateForVarargAfterSamAndHasVararg get() = shouldGenerateCandidateForVarargAfterSam && valueParameters.lastOrNull()?.isVararg == true + private val FunctionDescriptor.hasNothingTypeInParameters + get() = valueParameters.any { it.type.isNothing() && !it.original.type.isNothing() } + private fun FunctionDescriptor.substituteForReceiverType(receiverType: KotlinType): FunctionDescriptor? { val containingClass = containingDeclaration as? ClassDescriptor ?: return null val correspondingSupertype = findCorrespondingSupertype(receiverType, containingClass.defaultType) ?: return null 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 1580ca1b965..71194de956d 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 @@ -31,6 +31,7 @@ import org.jetbrains.kotlin.types.model.KotlinTypeMarker import org.jetbrains.kotlin.types.model.TypeConstructorMarker import org.jetbrains.kotlin.types.model.typeConstructor import org.jetbrains.kotlin.types.typeUtil.contains +import org.jetbrains.kotlin.types.typeUtil.isNothing import org.jetbrains.kotlin.types.typeUtil.makeNotNullable import org.jetbrains.kotlin.types.typeUtil.makeNullable import org.jetbrains.kotlin.utils.addToStdlib.safeAs @@ -500,6 +501,8 @@ private fun KotlinResolutionCandidate.getExpectedTypeWithSAMConversion( !callComponents.languageVersionSettings.supportsFeature(LanguageFeature.ProhibitVarargAsArrayAfterSamArgument) if (generatingAdditionalSamCandidateIsEnabled) return null + if (candidateParameter.type.isNothing()) return null + val samConversionOracle = callComponents.samConversionOracle if (!callComponents.languageVersionSettings.supportsFeature(LanguageFeature.SamConversionForKotlinFunctions)) { if (!samConversionOracle.shouldRunSamConversionForFunction(resolvedCall.candidateDescriptor)) return null diff --git a/compiler/testData/codegen/box/javaInterop/genericSamProjectedOutWithNewInference.kt b/compiler/testData/codegen/box/javaInterop/genericSamProjectedOutWithNewInference.kt index e5020c1761a..1675074a8c6 100644 --- a/compiler/testData/codegen/box/javaInterop/genericSamProjectedOutWithNewInference.kt +++ b/compiler/testData/codegen/box/javaInterop/genericSamProjectedOutWithNewInference.kt @@ -1,6 +1,6 @@ -// !LANGUAGE: -NewInference // IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM +// IGNORE_BACKEND: JVM_IR // FILE: example/Hello.java