From 907ebb36d02a763d54c18fb18e532aa1dda0061e Mon Sep 17 00:00:00 2001 From: Kirill Rakhman Date: Tue, 24 Oct 2023 14:27:02 +0200 Subject: [PATCH] [FIR] Filter out SAM constructors from MemberScopeTowerLevel This tower level is for calls on a dispatch receiver, and it's not allowed to call SAM constructors on a dispatch receiver. #KT-60983 Fixed --- ...CompilerTestFE10TestdataTestGenerated.java | 6 ++ ...sticCompilerFE10TestDataTestGenerated.java | 6 ++ ...eeOldFrontendDiagnosticsTestGenerated.java | 6 ++ ...siOldFrontendDiagnosticsTestGenerated.java | 6 ++ .../fir/resolve/calls/tower/TowerLevels.kt | 10 ++-- .../tests/samConversions/kt60983.fir.kt | 58 +++++++++++++++++++ .../tests/samConversions/kt60983.kt | 58 +++++++++++++++++++ .../test/runners/DiagnosticTestGenerated.java | 6 ++ 8 files changed, 151 insertions(+), 5 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/samConversions/kt60983.fir.kt create mode 100644 compiler/testData/diagnostics/tests/samConversions/kt60983.kt diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java index d78da2a9212..91ee88a55cc 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java @@ -29218,6 +29218,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia runTest("compiler/testData/diagnostics/tests/samConversions/kt50477.kt"); } + @Test + @TestMetadata("kt60983.kt") + public void testKt60983() throws Exception { + runTest("compiler/testData/diagnostics/tests/samConversions/kt60983.kt"); + } + @Test @TestMetadata("OverloadPriority.kt") public void testOverloadPriority() throws Exception { diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java index 566643ebbdc..7ff01d13fce 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java @@ -29218,6 +29218,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated runTest("compiler/testData/diagnostics/tests/samConversions/kt50477.kt"); } + @Test + @TestMetadata("kt60983.kt") + public void testKt60983() throws Exception { + runTest("compiler/testData/diagnostics/tests/samConversions/kt60983.kt"); + } + @Test @TestMetadata("OverloadPriority.kt") public void testOverloadPriority() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java index 3852eb6c286..014e456ebda 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java @@ -29206,6 +29206,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir runTest("compiler/testData/diagnostics/tests/samConversions/kt50477.kt"); } + @Test + @TestMetadata("kt60983.kt") + public void testKt60983() throws Exception { + runTest("compiler/testData/diagnostics/tests/samConversions/kt60983.kt"); + } + @Test @TestMetadata("OverloadPriority.kt") public void testOverloadPriority() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java index e72f10027a2..38a5f1a322f 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java @@ -29218,6 +29218,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia runTest("compiler/testData/diagnostics/tests/samConversions/kt50477.kt"); } + @Test + @TestMetadata("kt60983.kt") + public void testKt60983() throws Exception { + runTest("compiler/testData/diagnostics/tests/samConversions/kt60983.kt"); + } + @Test @TestMetadata("OverloadPriority.kt") public void testOverloadPriority() throws Exception { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/TowerLevels.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/TowerLevels.kt index eb49c8b9bb2..d7496ad4cb5 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/TowerLevels.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/TowerLevels.kt @@ -9,10 +9,7 @@ import org.jetbrains.kotlin.KtFakeSourceElementKind import org.jetbrains.kotlin.KtSourceElement import org.jetbrains.kotlin.fakeElement import org.jetbrains.kotlin.fir.* -import org.jetbrains.kotlin.fir.declarations.ContextReceiverGroup -import org.jetbrains.kotlin.fir.declarations.FirConstructor -import org.jetbrains.kotlin.fir.declarations.FirResolvePhase -import org.jetbrains.kotlin.fir.declarations.getAnnotationByClassId +import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.declarations.utils.isInner import org.jetbrains.kotlin.fir.declarations.utils.isStatic import org.jetbrains.kotlin.fir.expressions.FirExpression @@ -174,7 +171,10 @@ class MemberScopeTowerLevel( empty = false if (candidate.hasConsistentExtensionReceiver(givenExtensionReceiverOptions)) { val fir = candidate.fir - if ((fir as? FirConstructor)?.isInner == false) { + // Calls on a dispatch receiver cannot be: + // - a constructor call unless it's an inner class + // - a SAM constructor call + if ((fir as? FirConstructor)?.isInner == false || fir.origin == FirDeclarationOrigin.SamConstructor) { return@processScopeMembers } result += MemberWithBaseScope(candidate, this) diff --git a/compiler/testData/diagnostics/tests/samConversions/kt60983.fir.kt b/compiler/testData/diagnostics/tests/samConversions/kt60983.fir.kt new file mode 100644 index 00000000000..d4499438a66 --- /dev/null +++ b/compiler/testData/diagnostics/tests/samConversions/kt60983.fir.kt @@ -0,0 +1,58 @@ +// MODULE: m1 +// FILE: J.java +public class J { + public static class C {} + public class C2 {} + public interface I { + void x(); + } +} + +// FILE: test.kt +class K { + open class C + inner class C2 + fun interface I { + fun x() + } +} + +fun J.testJ() { + C() + C2() + I {} +} + +fun testJ2(j: J) { + j.C() + j.C2() + j.I {} +} + +fun K.testK() { + C() + C2() + I {} +} + +fun testK2(k: K) { + k.C() + k.C2() + k.I {} +} + +// MODULE: m2(m1) +// FILE: testResolutionContinues.kt +fun J.testResolutionContinues() { + acceptI(I {}) +} + +fun K.testResolutionContinues() { + acceptI(I {}) +} + +fun interface I { + fun x() +} + +fun acceptI(i: I) {} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/samConversions/kt60983.kt b/compiler/testData/diagnostics/tests/samConversions/kt60983.kt new file mode 100644 index 00000000000..abf08e22b7b --- /dev/null +++ b/compiler/testData/diagnostics/tests/samConversions/kt60983.kt @@ -0,0 +1,58 @@ +// MODULE: m1 +// FILE: J.java +public class J { + public static class C {} + public class C2 {} + public interface I { + void x(); + } +} + +// FILE: test.kt +class K { + open class C + inner class C2 + fun interface I { + fun x() + } +} + +fun J.testJ() { + C() + C2() + I {} +} + +fun testJ2(j: J) { + j.C() + j.C2() + j.I {} +} + +fun K.testK() { + C() + C2() + I {} +} + +fun testK2(k: K) { + k.C() + k.C2() + k.I {} +} + +// MODULE: m2(m1) +// FILE: testResolutionContinues.kt +fun J.testResolutionContinues() { + acceptI(I {}) +} + +fun K.testResolutionContinues() { + acceptI(I {}) +} + +fun interface I { + fun x() +} + +fun acceptI(i: I) {} \ No newline at end of file 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 197344637af..79c8827e71f 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 @@ -31152,6 +31152,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/samConversions/kt50477.kt"); } + @Test + @TestMetadata("kt60983.kt") + public void testKt60983() throws Exception { + runTest("compiler/testData/diagnostics/tests/samConversions/kt60983.kt"); + } + @Test @TestMetadata("OverloadPriority.kt") public void testOverloadPriority() throws Exception {