diff --git a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/KotlinK2QuickFixRegistrar.kt b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/KotlinK2QuickFixRegistrar.kt index b5aab2ae7fdb..6fc6f677f3b4 100644 --- a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/KotlinK2QuickFixRegistrar.kt +++ b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/KotlinK2QuickFixRegistrar.kt @@ -582,6 +582,7 @@ class KotlinK2QuickFixRegistrar : KotlinQuickFixRegistrar() { override val importOnTheFlyList: KotlinQuickFixesList = KtQuickFixesListBuilder.registerPsiQuickFix { registerFactory(ImportQuickFixFactories.unresolvedReferenceFactory) + registerFactory(ImportQuickFixFactories.unresolvedReferenceWrongReceiverFactory) registerFactory(ImportQuickFixFactories.invisibleReferenceFactory) } } diff --git a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/imprt/ImportQuickFixFactories.kt b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/imprt/ImportQuickFixFactories.kt index 2f6e4f709e27..643282bbc829 100644 --- a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/imprt/ImportQuickFixFactories.kt +++ b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/imprt/ImportQuickFixFactories.kt @@ -16,4 +16,11 @@ object ImportQuickFixFactories { val unresolvedReferenceFactory = KotlinQuickFixFactory.IntentionBased { diagnostic: KaFirDiagnostic.UnresolvedReference -> ImportQuickFixProvider.getFixes(diagnostic.psi) } + + /** + * See KDoc for [unresolvedReferenceFactory]. + */ + val unresolvedReferenceWrongReceiverFactory = KotlinQuickFixFactory.IntentionBased { diagnostic: KaFirDiagnostic.UnresolvedReferenceWrongReceiver -> + ImportQuickFixProvider.getFixes(diagnostic.psi) + } } \ No newline at end of file diff --git a/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixMultiFileTestGenerated.java b/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixMultiFileTestGenerated.java index 564e01d09797..6b3654bc265f 100644 --- a/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixMultiFileTestGenerated.java +++ b/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixMultiFileTestGenerated.java @@ -999,6 +999,11 @@ public abstract class HighLevelQuickFixMultiFileTestGenerated extends AbstractHi public void testWithSmartCastQualifier() throws Exception { runTest("../../../idea/tests/testData/quickfix/autoImports/withSmartCastQualifier.before.Main.kt"); } + + @TestMetadata("wrongReceiverType.test") + public void testWrongReceiverType() throws Exception { + runTest("../../../idea/tests/testData/quickfix/autoImports/wrongReceiverType.test"); + } } } diff --git a/plugins/kotlin/fir/tests/test/org/jetbrains/kotlin/idea/fir/imports/K2AutoImportTestGenerated.java b/plugins/kotlin/fir/tests/test/org/jetbrains/kotlin/idea/fir/imports/K2AutoImportTestGenerated.java index dcbb723a8f7b..fa3ae4232e4c 100644 --- a/plugins/kotlin/fir/tests/test/org/jetbrains/kotlin/idea/fir/imports/K2AutoImportTestGenerated.java +++ b/plugins/kotlin/fir/tests/test/org/jetbrains/kotlin/idea/fir/imports/K2AutoImportTestGenerated.java @@ -184,4 +184,9 @@ public class K2AutoImportTestGenerated extends AbstractK2AutoImportTest { public void testWithoutAutoImport() throws Exception { runTest("../../idea/tests/testData/editor/autoImport/withoutAutoImport/"); } + + @TestMetadata("wrongReceiverType") + public void testWrongReceiverType() throws Exception { + runTest("../../idea/tests/testData/editor/autoImport/wrongReceiverType/"); + } } diff --git a/plugins/kotlin/highlighting/highlighting-k2/src/org/jetbrains/kotlin/idea/highlighting/KotlinDiagnosticHighlightVisitor.kt b/plugins/kotlin/highlighting/highlighting-k2/src/org/jetbrains/kotlin/idea/highlighting/KotlinDiagnosticHighlightVisitor.kt index f7c94a60dfcc..2093f251c45b 100644 --- a/plugins/kotlin/highlighting/highlighting-k2/src/org/jetbrains/kotlin/idea/highlighting/KotlinDiagnosticHighlightVisitor.kt +++ b/plugins/kotlin/highlighting/highlighting-k2/src/org/jetbrains/kotlin/idea/highlighting/KotlinDiagnosticHighlightVisitor.kt @@ -159,6 +159,7 @@ class KotlinDiagnosticHighlightVisitor : HighlightVisitor { if ( diagnostic is KaFirDiagnostic.UnresolvedReference || + diagnostic is KaFirDiagnostic.UnresolvedReferenceWrongReceiver || diagnostic is KaFirDiagnostic.UnresolvedImport ) { psiElement.registerKotlinUnresolvedReferenceKind(KotlinUnresolvedReferenceKind.Regular) @@ -167,6 +168,7 @@ class KotlinDiagnosticHighlightVisitor : HighlightVisitor { if ( diagnostic is KaFirDiagnostic.UnresolvedImport || diagnostic is KaFirDiagnostic.UnresolvedReference || + diagnostic is KaFirDiagnostic.UnresolvedReferenceWrongReceiver || diagnostic is KaFirDiagnostic.DelegateSpecialFunctionMissing || diagnostic is KaFirDiagnostic.DelegateSpecialFunctionNoneApplicable ) { diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/imports/K1AutoImportTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/imports/K1AutoImportTestGenerated.java index 1c83f3ed583d..4a4446e66d59 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/imports/K1AutoImportTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/imports/K1AutoImportTestGenerated.java @@ -184,4 +184,9 @@ public class K1AutoImportTestGenerated extends AbstractK1AutoImportTest { public void testWithoutAutoImport() throws Exception { runTest("testData/editor/autoImport/withoutAutoImport/"); } + + @TestMetadata("wrongReceiverType") + public void testWrongReceiverType() throws Exception { + runTest("testData/editor/autoImport/wrongReceiverType/"); + } } diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java index 16a7260275de..0422db4c2a72 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java @@ -1111,6 +1111,11 @@ public abstract class QuickFixMultiFileTestGenerated extends AbstractQuickFixMul public void testWithSmartCastQualifier() throws Exception { runTest("testData/quickfix/autoImports/withSmartCastQualifier.before.Main.kt"); } + + @TestMetadata("wrongReceiverType.test") + public void testWrongReceiverType() throws Exception { + runTest("testData/quickfix/autoImports/wrongReceiverType.test"); + } } } diff --git a/plugins/kotlin/idea/tests/testData/editor/autoImport/wrongReceiverType/dependency/dependency.kt b/plugins/kotlin/idea/tests/testData/editor/autoImport/wrongReceiverType/dependency/dependency.kt new file mode 100644 index 000000000000..2b19325821e3 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/editor/autoImport/wrongReceiverType/dependency/dependency.kt @@ -0,0 +1,5 @@ +package dependency + +class FromDependency + +fun FromDependency.extension() {} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/editor/autoImport/wrongReceiverType/wrongReceiverType.kt b/plugins/kotlin/idea/tests/testData/editor/autoImport/wrongReceiverType/wrongReceiverType.kt new file mode 100644 index 000000000000..e7e80fd3b2f9 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/editor/autoImport/wrongReceiverType/wrongReceiverType.kt @@ -0,0 +1,11 @@ +package main + +import dependency.FromDependency + +class UnrelatedClass + +fun UnrelatedClass.extension() + +fun test(f: FromDependency) { + f.extension() // UNRESOLVED_REFERENCE_WRONG_RECEIVER +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/editor/autoImport/wrongReceiverType/wrongReceiverType.kt.after b/plugins/kotlin/idea/tests/testData/editor/autoImport/wrongReceiverType/wrongReceiverType.kt.after new file mode 100644 index 000000000000..57b8ef73ada9 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/editor/autoImport/wrongReceiverType/wrongReceiverType.kt.after @@ -0,0 +1,12 @@ +package main + +import dependency.FromDependency +import dependency.extension + +class UnrelatedClass + +fun UnrelatedClass.extension() + +fun test(f: FromDependency) { + f.extension() // UNRESOLVED_REFERENCE_WRONG_RECEIVER +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/autoImports/wrongReceiverType.test b/plugins/kotlin/idea/tests/testData/quickfix/autoImports/wrongReceiverType.test new file mode 100644 index 000000000000..c890a5646170 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/quickfix/autoImports/wrongReceiverType.test @@ -0,0 +1,30 @@ +// FILE: first.before.kt +// "Import extension function 'String.extension'" "true" +// ERROR: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public fun Int.extension(): Unit defined in test in file first.before.kt + +package test + +fun Int.extension() {} + +fun test(str: String) { + str.extension() +} + +// FILE: second.kt +package dependency + +fun String.extension() {} + +// FILE: first.after.kt +// "Import extension function 'String.extension'" "true" +// ERROR: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public fun Int.extension(): Unit defined in test in file first.before.kt + +package test + +import dependency.extension + +fun Int.extension() {} + +fun test(str: String) { + str.extension() +}