diff --git a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestWithStdlibGenerated.java b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestWithStdlibGenerated.java index 7d5f5a25bec..3e71ea75fee 100644 --- a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestWithStdlibGenerated.java +++ b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestWithStdlibGenerated.java @@ -1800,6 +1800,11 @@ public class FirOldFrontendDiagnosticsTestWithStdlibGenerated extends AbstractFi KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/testsWithStdLib/factoryPattern"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true); } + @TestMetadata("independentResolutionInLambda.kt") + public void testIndependentResolutionInLambda() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/factoryPattern/independentResolutionInLambda.kt"); + } + @TestMetadata("overloadByLambdaReturnType_disabled.kt") public void testOverloadByLambdaReturnType_disabled() throws Exception { runTest("compiler/testData/diagnostics/testsWithStdLib/factoryPattern/overloadByLambdaReturnType_disabled.kt"); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinResolutionCallbacksImpl.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinResolutionCallbacksImpl.kt index b4068034a6f..6719bfdcbea 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinResolutionCallbacksImpl.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinResolutionCallbacksImpl.kt @@ -95,7 +95,8 @@ class KotlinResolutionCallbacksImpl( parameters: List, expectedReturnType: UnwrappedType?, annotations: Annotations, - stubsForPostponedVariables: Map + stubsForPostponedVariables: Map, + shouldRunInIndependentContext: Boolean ): ReturnArgumentsAnalysisResult { val psiCallArgument = lambdaArgument.psiCallArgument as PSIFunctionKotlinCallArgument val outerCallContext = psiCallArgument.outerCallContext @@ -137,7 +138,7 @@ class KotlinResolutionCallbacksImpl( val lambdaInfo = LambdaInfo( expectedReturnType ?: TypeUtils.NO_EXPECTED_TYPE, - if (expectedReturnType == null) ContextDependency.DEPENDENT else ContextDependency.INDEPENDENT + if (expectedReturnType != null || shouldRunInIndependentContext) ContextDependency.INDEPENDENT else ContextDependency.DEPENDENT ) val builtIns = outerCallContext.scope.ownerDescriptor.builtIns diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ExternalComponents.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ExternalComponents.kt index 58db66cb149..4717e4f4a50 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ExternalComponents.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ExternalComponents.kt @@ -72,7 +72,8 @@ interface KotlinResolutionCallbacks { parameters: List, expectedReturnType: UnwrappedType?, // null means, that return type is not proper i.e. it depends on some type variables annotations: Annotations, - stubsForPostponedVariables: Map + stubsForPostponedVariables: Map, + shouldRunInIndependentContext: Boolean = false ): ReturnArgumentsAnalysisResult fun bindStubResolvedCallForCandidate(candidate: ResolvedCallAtom) 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 a2ff8d5cdba..b5553bd5c25 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 @@ -115,7 +115,8 @@ class KotlinCallCompleter( firstCandidate.getSystem().asPostponedArgumentsAnalyzerContext(), resolutionCallbacks, firstAtom, - diagnosticHolderForLambda + diagnosticHolderForLambda, + shouldRunInIndependentContext = true ) lambdas.getValue(firstCandidate).setAnalyzedResults(results.returnArgumentsInfo, listOf(firstAtom)) diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/PostponedArgumentsAnalyzer.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/PostponedArgumentsAnalyzer.kt index 86dc004cdf3..402f7969050 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/PostponedArgumentsAnalyzer.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/PostponedArgumentsAnalyzer.kt @@ -70,7 +70,8 @@ class PostponedArgumentsAnalyzer( c: Context, resolutionCallbacks: KotlinResolutionCallbacks, lambda: ResolvedLambdaAtom, - diagnosticHolder: KotlinDiagnosticsHolder + diagnosticHolder: KotlinDiagnosticsHolder, + shouldRunInIndependentContext: Boolean = false ): ReturnArgumentsAnalysisResult { val stubsForPostponedVariables = c.bindingStubsForPostponedVariables() val currentSubstitutor = c.buildCurrentSubstitutor(stubsForPostponedVariables.mapKeys { it.key.freshTypeConstructor(c) }) @@ -128,7 +129,8 @@ class PostponedArgumentsAnalyzer( parameters, expectedTypeForReturnArguments, convertedAnnotations ?: Annotations.EMPTY, - stubsForPostponedVariables.cast() + stubsForPostponedVariables.cast(), + shouldRunInIndependentContext ) val (returnArgumentsInfo, inferenceSession, inferedReturnType, hasInapplicableCallForBuilderInference) = returnArgumentsAnalysisResult diff --git a/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/independentResolutionInLambda.fir.kt b/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/independentResolutionInLambda.fir.kt new file mode 100644 index 00000000000..e2b2d2e2fd7 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/independentResolutionInLambda.fir.kt @@ -0,0 +1,41 @@ +// !LANGUAGE: +NewInference +FactoryPatternResolution +// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNUSED_EXPRESSION +// ISSUE: KT-11265 + +// FILE: FactoryPattern.kt + +package annotations + +annotation class FactoryPattern + +// FILE: main.kt + +import annotations.FactoryPattern + +@kotlin.jvm.JvmName("myFlatMapIterable") +@FactoryPattern +fun Sequence.myFlatMap(transform: (T) -> Iterable): Sequence { + TODO() +} + +fun Sequence.myFlatMap(transform: (T) -> Sequence): Sequence { + TODO() +} + +interface A { + val supertypes: Collection +} + +interface B { + val descriptors: Sequence? +} + +interface C + +fun elvis(x: K?, y: K): K = y + +fun test(a: A) { + a.supertypes.asSequence().myFlatMap { + elvis(it.descriptors, sequenceOf()) + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/independentResolutionInLambda.kt b/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/independentResolutionInLambda.kt new file mode 100644 index 00000000000..3a7b5c4ce38 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/independentResolutionInLambda.kt @@ -0,0 +1,41 @@ +// !LANGUAGE: +NewInference +FactoryPatternResolution +// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNUSED_EXPRESSION +// ISSUE: KT-11265 + +// FILE: FactoryPattern.kt + +package annotations + +annotation class FactoryPattern + +// FILE: main.kt + +import annotations.FactoryPattern + +@kotlin.jvm.JvmName("myFlatMapIterable") +@FactoryPattern +fun Sequence.myFlatMap(transform: (T) -> Iterable): Sequence { + TODO() +} + +fun Sequence.myFlatMap(transform: (T) -> Sequence): Sequence { + TODO() +} + +interface A { + val supertypes: Collection +} + +interface B { + val descriptors: Sequence? +} + +interface C + +fun elvis(x: K?, y: K): K = y + +fun test(a: A) { + a.supertypes.asSequence().myFlatMap { + elvis(it.descriptors, sequenceOf()) + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/independentResolutionInLambda.txt b/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/independentResolutionInLambda.txt new file mode 100644 index 00000000000..75fc73cb15c --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/factoryPattern/independentResolutionInLambda.txt @@ -0,0 +1,36 @@ +package + +public fun elvis(/*0*/ x: K?, /*1*/ y: K): K +public fun test(/*0*/ a: A): kotlin.Unit +@kotlin.jvm.JvmName(name = "myFlatMapIterable") @annotations.FactoryPattern public fun kotlin.sequences.Sequence.myFlatMap(/*0*/ transform: (T) -> kotlin.collections.Iterable): kotlin.sequences.Sequence +public fun kotlin.sequences.Sequence.myFlatMap(/*0*/ transform: (T) -> kotlin.sequences.Sequence): kotlin.sequences.Sequence + +public interface A { + public abstract val supertypes: kotlin.collections.Collection + 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 open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public interface B { + public abstract val descriptors: kotlin.sequences.Sequence? + 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 open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public interface C { + 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 open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +package annotations { + + public final annotation class FactoryPattern : kotlin.Annotation { + public constructor FactoryPattern() + 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 open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java index 565e76c3781..4a4caa521c7 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java @@ -2815,6 +2815,11 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/testsWithStdLib/factoryPattern"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true); } + @TestMetadata("independentResolutionInLambda.kt") + public void testIndependentResolutionInLambda() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/factoryPattern/independentResolutionInLambda.kt"); + } + @TestMetadata("overloadByLambdaReturnType_disabled.kt") public void testOverloadByLambdaReturnType_disabled() throws Exception { runTest("compiler/testData/diagnostics/testsWithStdLib/factoryPattern/overloadByLambdaReturnType_disabled.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java index d4ae24b5c84..67b6c376ce5 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java @@ -2815,6 +2815,11 @@ public class DiagnosticsTestWithStdLibUsingJavacGenerated extends AbstractDiagno KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/testsWithStdLib/factoryPattern"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true); } + @TestMetadata("independentResolutionInLambda.kt") + public void testIndependentResolutionInLambda() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/factoryPattern/independentResolutionInLambda.kt"); + } + @TestMetadata("overloadByLambdaReturnType_disabled.kt") public void testOverloadByLambdaReturnType_disabled() throws Exception { runTest("compiler/testData/diagnostics/testsWithStdLib/factoryPattern/overloadByLambdaReturnType_disabled.kt");