diff --git a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java index 06d134a2a40..474e5cb066c 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java @@ -7824,6 +7824,11 @@ public class FirDiagnosticsSmokeTestGenerated extends AbstractFirDiagnosticsSmok runTest("compiler/testData/diagnostics/tests/funInterface/basicFunInterfaceDisabled.kt"); } + @TestMetadata("funInterfaceSyntheticConstructors.kt") + public void testFunInterfaceSyntheticConstructors() throws Exception { + runTest("compiler/testData/diagnostics/tests/funInterface/funInterfaceSyntheticConstructors.kt"); + } + @TestMetadata("genericSubstitutionForFunInterface.kt") public void testGenericSubstitutionForFunInterface() throws Exception { runTest("compiler/testData/diagnostics/tests/funInterface/genericSubstitutionForFunInterface.kt"); diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SamConstructorDescriptor.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SamConstructorDescriptor.kt index f16b8ae0569..d524b402a6a 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SamConstructorDescriptor.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SamConstructorDescriptor.kt @@ -17,18 +17,18 @@ package org.jetbrains.kotlin.load.java.sam import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor +import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl import org.jetbrains.kotlin.descriptors.synthetic.SyntheticMemberDescriptor -import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor import org.jetbrains.kotlin.resolve.scopes.DescriptorKindExclude -interface SamConstructorDescriptor : SimpleFunctionDescriptor, SyntheticMemberDescriptor +interface SamConstructorDescriptor : SimpleFunctionDescriptor, SyntheticMemberDescriptor class SamConstructorDescriptorImpl( containingDeclaration: DeclarationDescriptor, - private val samInterface: JavaClassDescriptor + private val samInterface: ClassDescriptor ) : SimpleFunctionDescriptorImpl( containingDeclaration, null, @@ -37,7 +37,7 @@ class SamConstructorDescriptorImpl( CallableMemberDescriptor.Kind.SYNTHESIZED, samInterface.source ), SamConstructorDescriptor { - override val baseDescriptorForSynthetic: JavaClassDescriptor + override val baseDescriptorForSynthetic: ClassDescriptor get() = samInterface } diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SamTypeAliasConstructorDescriptor.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SamTypeAliasConstructorDescriptor.kt index c08bb01e0e1..7dd0d4ea7b3 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SamTypeAliasConstructorDescriptor.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SamTypeAliasConstructorDescriptor.kt @@ -16,12 +16,8 @@ package org.jetbrains.kotlin.load.java.sam -import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor -import org.jetbrains.kotlin.descriptors.DeclarationDescriptor -import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithNavigationSubstitute -import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor +import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl -import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor interface SamTypeAliasConstructorDescriptor : SamConstructorDescriptor, DeclarationDescriptorWithNavigationSubstitute { val typeAliasDescriptor: TypeAliasDescriptor @@ -41,6 +37,6 @@ class SamTypeAliasConstructorDescriptorImpl( CallableMemberDescriptor.Kind.SYNTHESIZED, typeAliasDescriptor.source ), SamTypeAliasConstructorDescriptor { - override val baseDescriptorForSynthetic: JavaClassDescriptor + override val baseDescriptorForSynthetic: ClassDescriptor get() = samInterfaceConstructorDescriptor.baseDescriptorForSynthetic } diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SingleAbstractMethodUtils.java b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SingleAbstractMethodUtils.java index 307ead36178..fbbb6feec40 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SingleAbstractMethodUtils.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SingleAbstractMethodUtils.java @@ -28,6 +28,7 @@ import org.jetbrains.kotlin.resolve.SamConversionResolver; import org.jetbrains.kotlin.load.java.descriptors.JavaClassConstructorDescriptor; import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor; import org.jetbrains.kotlin.load.java.descriptors.JavaMethodDescriptor; +import org.jetbrains.kotlin.load.java.lazy.descriptors.LazyJavaClassDescriptor; import org.jetbrains.kotlin.name.Name; import org.jetbrains.kotlin.name.SpecialNames; import org.jetbrains.kotlin.resolve.DescriptorUtils; @@ -167,7 +168,7 @@ public class SingleAbstractMethodUtils { @NotNull public static SamConstructorDescriptor createSamConstructorFunction( @NotNull DeclarationDescriptor owner, - @NotNull JavaClassDescriptor samInterface, + @NotNull ClassDescriptor samInterface, @NotNull SamConversionResolver samResolver ) { assert getSingleAbstractMethodOrNull(samInterface) != null : samInterface; @@ -182,7 +183,7 @@ public class SingleAbstractMethodUtils { } private static void initializeSamConstructorDescriptor( - @NotNull JavaClassDescriptor samInterface, + @NotNull ClassDescriptor samInterface, @NotNull SimpleFunctionDescriptorImpl samConstructor, @NotNull List samTypeParameters, @NotNull KotlinType unsubstitutedSamType, @@ -224,7 +225,7 @@ public class SingleAbstractMethodUtils { ) { SamTypeAliasConstructorDescriptorImpl result = new SamTypeAliasConstructorDescriptorImpl(typeAliasDescriptor, underlyingSamConstructor); - JavaClassDescriptor samInterface = underlyingSamConstructor.getBaseDescriptorForSynthetic(); + ClassDescriptor samInterface = underlyingSamConstructor.getBaseDescriptorForSynthetic(); List samTypeParameters = typeAliasDescriptor.getTypeConstructor().getParameters(); SimpleType unsubstitutedSamType = typeAliasDescriptor.getExpandedType(); initializeSamConstructorDescriptor(samInterface, result, samTypeParameters, unsubstitutedSamType, samResolver); @@ -232,6 +233,15 @@ public class SingleAbstractMethodUtils { return result; } + public static boolean isSamClassDescriptor(@NotNull ClassDescriptor descriptor) { + if (descriptor.isFun()) return true; + if (descriptor instanceof LazyJavaClassDescriptor && + ((LazyJavaClassDescriptor) descriptor).getDefaultFunctionTypeForSamInterface() != null + ) return true; + + return false; + } + public static boolean isSamType(@NotNull KotlinType type) { ClassifierDescriptor descriptor = type.getConstructor().getDeclarationDescriptor(); if (descriptor instanceof ClassDescriptor && ((ClassDescriptor) descriptor).isFun()) return true; 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 bece801d1c1..8ea04e356e6 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SamAdapterFunctionsScope.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SamAdapterFunctionsScope.kt @@ -30,7 +30,6 @@ import org.jetbrains.kotlin.resolve.SamConversionResolver import org.jetbrains.kotlin.load.java.descriptors.JavaClassConstructorDescriptor import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor import org.jetbrains.kotlin.load.java.descriptors.JavaMethodDescriptor -import org.jetbrains.kotlin.load.java.lazy.descriptors.LazyJavaClassDescriptor import org.jetbrains.kotlin.load.java.sam.SamAdapterDescriptor import org.jetbrains.kotlin.load.java.sam.SamConstructorDescriptor import org.jetbrains.kotlin.load.java.sam.SingleAbstractMethodUtils @@ -73,7 +72,7 @@ class SamAdapterFunctionsScope( } private val samConstructorForClassifier = - storageManager.createMemoizedFunction { classifier -> + storageManager.createMemoizedFunction { classifier -> SingleAbstractMethodUtils.createSamConstructorFunction(classifier.containingDeclaration, classifier, samResolver) } @@ -259,13 +258,15 @@ class SamAdapterFunctionsScope( return getTypeAliasSamConstructor(classifier) } - if (classifier !is LazyJavaClassDescriptor || classifier.defaultFunctionTypeForSamInterface == null) return null + if (classifier !is ClassDescriptor) return null + if (!SingleAbstractMethodUtils.isSamClassDescriptor(classifier)) return null + return samConstructorForClassifier(classifier) } private fun getTypeAliasSamConstructor(classifier: TypeAliasDescriptor): SamConstructorDescriptor? { val classDescriptor = classifier.classDescriptor ?: return null - if (classDescriptor !is LazyJavaClassDescriptor || classDescriptor.defaultFunctionTypeForSamInterface == null) return null + if (!SingleAbstractMethodUtils.isSamClassDescriptor(classDescriptor)) return null return SingleAbstractMethodUtils.createTypeAliasSamConstructorFunction( classifier, samConstructorForClassifier(classDescriptor), samResolver) diff --git a/compiler/testData/diagnostics/tests/funInterface/funInterfaceSyntheticConstructors.kt b/compiler/testData/diagnostics/tests/funInterface/funInterfaceSyntheticConstructors.kt new file mode 100644 index 00000000000..c4b9340ea17 --- /dev/null +++ b/compiler/testData/diagnostics/tests/funInterface/funInterfaceSyntheticConstructors.kt @@ -0,0 +1,15 @@ +// !LANGUAGE: +NewInference +SamConversionForKotlinFunctions +SamConversionPerArgument +FunctionInterfaceConversion +// !DIAGNOSTICS: -UNUSED_PARAMETER + +fun interface KRunnable { + fun invoke() +} + +typealias KRunnableAlias = KRunnable + +fun foo(f: KRunnable) {} + +fun test() { + foo(KRunnable {}) + foo(KRunnableAlias {}) +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/funInterface/funInterfaceSyntheticConstructors.txt b/compiler/testData/diagnostics/tests/funInterface/funInterfaceSyntheticConstructors.txt new file mode 100644 index 00000000000..6f55a69343d --- /dev/null +++ b/compiler/testData/diagnostics/tests/funInterface/funInterfaceSyntheticConstructors.txt @@ -0,0 +1,12 @@ +package + +public fun foo(/*0*/ f: KRunnable): kotlin.Unit +public fun test(): kotlin.Unit + +public interface KRunnable { + 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 abstract fun invoke(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} +public typealias KRunnableAlias = KRunnable diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 4e7d1282236..c8887492624 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -7906,6 +7906,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { runTest("compiler/testData/diagnostics/tests/funInterface/basicFunInterfaceDisabled.kt"); } + @TestMetadata("funInterfaceSyntheticConstructors.kt") + public void testFunInterfaceSyntheticConstructors() throws Exception { + runTest("compiler/testData/diagnostics/tests/funInterface/funInterfaceSyntheticConstructors.kt"); + } + @TestMetadata("genericSubstitutionForFunInterface.kt") public void testGenericSubstitutionForFunInterface() throws Exception { runTest("compiler/testData/diagnostics/tests/funInterface/genericSubstitutionForFunInterface.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index 32998ff0510..38548b04cf0 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -7901,6 +7901,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/funInterface/basicFunInterfaceDisabled.kt"); } + @TestMetadata("funInterfaceSyntheticConstructors.kt") + public void testFunInterfaceSyntheticConstructors() throws Exception { + runTest("compiler/testData/diagnostics/tests/funInterface/funInterfaceSyntheticConstructors.kt"); + } + @TestMetadata("genericSubstitutionForFunInterface.kt") public void testGenericSubstitutionForFunInterface() throws Exception { runTest("compiler/testData/diagnostics/tests/funInterface/genericSubstitutionForFunInterface.kt");