From 09b44b31898f2aa7b4794deb19ac8b8db02b4b4d Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Mon, 13 Jul 2020 15:45:50 +0300 Subject: [PATCH] Fix rewrite at slice exception for callable references inside `flatMap` #KT-40254 Fixed --- .../fir/FirOldFrontendDiagnosticsTestGenerated.java | 5 +++++ .../resolve/calls/components/KotlinCallCompleter.kt | 7 ++----- .../resolveTwoReferencesAgainstGenerics.fir.kt | 10 ++++++++++ .../resolve/resolveTwoReferencesAgainstGenerics.kt | 10 ++++++++++ .../resolve/resolveTwoReferencesAgainstGenerics.txt | 11 +++++++++++ .../kotlin/checkers/DiagnosticsTestGenerated.java | 5 +++++ .../javac/DiagnosticsUsingJavacTestGenerated.java | 5 +++++ 7 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/callableReference/resolve/resolveTwoReferencesAgainstGenerics.fir.kt create mode 100644 compiler/testData/diagnostics/tests/callableReference/resolve/resolveTwoReferencesAgainstGenerics.kt create mode 100644 compiler/testData/diagnostics/tests/callableReference/resolve/resolveTwoReferencesAgainstGenerics.txt diff --git a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java index 17bd5399183..bf709b714f5 100644 --- a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java @@ -2803,6 +2803,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte runTest("compiler/testData/diagnostics/tests/callableReference/resolve/resolveReferenceAgainstKFunctionAndKPrpoerty.kt"); } + @TestMetadata("resolveTwoReferencesAgainstGenerics.kt") + public void testResolveTwoReferencesAgainstGenerics() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/resolve/resolveTwoReferencesAgainstGenerics.kt"); + } + @TestMetadata("valVsFun.kt") public void testValVsFun() throws Exception { runTest("compiler/testData/diagnostics/tests/callableReference/resolve/valVsFun.kt"); diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/KotlinCallCompleter.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/KotlinCallCompleter.kt index 0ccf8061812..43b884e7d12 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/KotlinCallCompleter.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/KotlinCallCompleter.kt @@ -101,11 +101,8 @@ class KotlinCallCompleter( return candidates } - val newAtoms = mutableMapOf() - for ((candidate, atom) in lambdas.entries) { - val newAtom = kotlinConstraintSystemCompleter.prepareLambdaAtomForFactoryPattern(atom, candidate, candidate) - newAtoms[candidate] = newAtom - candidate.addResolvedKtPrimitive(newAtom) + val newAtoms = lambdas.mapValues { (candidate, atom) -> + kotlinConstraintSystemCompleter.prepareLambdaAtomForFactoryPattern(atom, candidate, candidate) } val diagnosticHolderForLambda = KotlinDiagnosticsHolder.SimpleHolder() diff --git a/compiler/testData/diagnostics/tests/callableReference/resolve/resolveTwoReferencesAgainstGenerics.fir.kt b/compiler/testData/diagnostics/tests/callableReference/resolve/resolveTwoReferencesAgainstGenerics.fir.kt new file mode 100644 index 00000000000..9ec54a74492 --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/resolve/resolveTwoReferencesAgainstGenerics.fir.kt @@ -0,0 +1,10 @@ +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNUSED_PARAMETER +// WITH_RUNTIME + + +fun List>.flatten(): List = flatMap { it.fold(::emptyList, ::listOf) } + +class Option { + fun fold(ifEmpty: () -> R, ifSome: (T) -> R): R = TODO() +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/callableReference/resolve/resolveTwoReferencesAgainstGenerics.kt b/compiler/testData/diagnostics/tests/callableReference/resolve/resolveTwoReferencesAgainstGenerics.kt new file mode 100644 index 00000000000..3dcd7353331 --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/resolve/resolveTwoReferencesAgainstGenerics.kt @@ -0,0 +1,10 @@ +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNUSED_PARAMETER +// WITH_RUNTIME + + +fun List>.flatten(): List = flatMap { it.fold(::emptyList, ::listOf) } + +class Option { + fun fold(ifEmpty: () -> R, ifSome: (T) -> R): R = TODO() +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/callableReference/resolve/resolveTwoReferencesAgainstGenerics.txt b/compiler/testData/diagnostics/tests/callableReference/resolve/resolveTwoReferencesAgainstGenerics.txt new file mode 100644 index 00000000000..c7a22a24bdb --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/resolve/resolveTwoReferencesAgainstGenerics.txt @@ -0,0 +1,11 @@ +package + +public fun kotlin.collections.List>.flatten(): kotlin.collections.List + +public final class Option { + public constructor Option() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public final fun fold(/*0*/ ifEmpty: () -> R, /*1*/ ifSome: (T) -> R): R + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index b03c1bd07dc..fb559b1573a 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -2810,6 +2810,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTestWithFirVali runTest("compiler/testData/diagnostics/tests/callableReference/resolve/resolveReferenceAgainstKFunctionAndKPrpoerty.kt"); } + @TestMetadata("resolveTwoReferencesAgainstGenerics.kt") + public void testResolveTwoReferencesAgainstGenerics() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/resolve/resolveTwoReferencesAgainstGenerics.kt"); + } + @TestMetadata("valVsFun.kt") public void testValVsFun() throws Exception { runTest("compiler/testData/diagnostics/tests/callableReference/resolve/valVsFun.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index 9de8a884f06..883344af4ef 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -2805,6 +2805,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/callableReference/resolve/resolveReferenceAgainstKFunctionAndKPrpoerty.kt"); } + @TestMetadata("resolveTwoReferencesAgainstGenerics.kt") + public void testResolveTwoReferencesAgainstGenerics() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/resolve/resolveTwoReferencesAgainstGenerics.kt"); + } + @TestMetadata("valVsFun.kt") public void testValVsFun() throws Exception { runTest("compiler/testData/diagnostics/tests/callableReference/resolve/valVsFun.kt");