diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/Arguments.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/Arguments.kt index 1f7b246ea36..04a5f69e9dd 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/Arguments.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/Arguments.kt @@ -207,7 +207,7 @@ fun Candidate.resolvePlainArgumentType( val position = SimpleConstraintSystemConstraintPosition //TODO val session = sink.components.session - val capturedType = prepareCapturedType(argumentType, session) + val capturedType = prepareCapturedType(argumentType) val nullableExpectedType = expectedType.withNullability(ConeNullability.NULLABLE, session.inferenceContext) if (isReceiver && isSafeCall) { @@ -220,12 +220,20 @@ fun Candidate.resolvePlainArgumentType( checkApplicabilityForArgumentType(csBuilder, capturedType, expectedType, position, isReceiver, isDispatch, nullableExpectedType, sink) } -fun Candidate.prepareCapturedType(argumentType: ConeKotlinType, session: FirSession): ConeKotlinType { - if (argumentType.typeArguments.isEmpty() || argumentType !is ConeClassLikeType) return argumentType +fun Candidate.prepareCapturedType(argumentType: ConeKotlinType): ConeKotlinType { + return captureTypeFromExpressionOrNull(argumentType) ?: argumentType +} + +private fun Candidate.captureTypeFromExpressionOrNull(argumentType: ConeKotlinType): ConeKotlinType? { + if (argumentType is ConeFlexibleType) { + return captureTypeFromExpressionOrNull(argumentType.lowerBound) + } + + if (argumentType.typeArguments.isEmpty() || argumentType !is ConeClassLikeType) return null return bodyResolveComponents.inferenceComponents.ctx.captureFromArguments( argumentType, CaptureStatus.FROM_EXPRESSION - ) as? ConeKotlinType ?: argumentType + ) as? ConeKotlinType } private fun checkApplicabilityForArgumentType( diff --git a/compiler/fir/resolve/testData/resolve/stdlib/j+k/capturedFlexible.kt b/compiler/fir/resolve/testData/resolve/stdlib/j+k/capturedFlexible.kt new file mode 100644 index 00000000000..4b559137c9c --- /dev/null +++ b/compiler/fir/resolve/testData/resolve/stdlib/j+k/capturedFlexible.kt @@ -0,0 +1,3 @@ +fun foo(z: java.util.zip.ZipFile) { + z.entries().asSequence() +} diff --git a/compiler/fir/resolve/testData/resolve/stdlib/j+k/capturedFlexible.txt b/compiler/fir/resolve/testData/resolve/stdlib/j+k/capturedFlexible.txt new file mode 100644 index 00000000000..2af7e3f7089 --- /dev/null +++ b/compiler/fir/resolve/testData/resolve/stdlib/j+k/capturedFlexible.txt @@ -0,0 +1,4 @@ +FILE: capturedFlexible.kt + public final fun foo(z: R|java/util/zip/ZipFile|): R|kotlin/Unit| { + R|/z|.R|java/util/zip/ZipFile.entries|().R|kotlin/sequences/asSequence|() + } diff --git a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithStdlibTestGenerated.java b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithStdlibTestGenerated.java index 0c7571b94fd..9af89027099 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithStdlibTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithStdlibTestGenerated.java @@ -464,6 +464,11 @@ public class FirDiagnosticsWithStdlibTestGenerated extends AbstractFirDiagnostic KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/resolve/testData/resolve/stdlib/j+k"), Pattern.compile("^([^.]+)\\.kt$"), null, true); } + @TestMetadata("capturedFlexible.kt") + public void testCapturedFlexible() throws Exception { + runTest("compiler/fir/resolve/testData/resolve/stdlib/j+k/capturedFlexible.kt"); + } + @TestMetadata("complexFlexibleInference.kt") public void testComplexFlexibleInference() throws Exception { runTest("compiler/fir/resolve/testData/resolve/stdlib/j+k/complexFlexibleInference.kt"); diff --git a/compiler/testData/diagnostics/tests/generics/kt9203.fir.kt b/compiler/testData/diagnostics/tests/generics/kt9203.fir.kt index 01d1f6f9ee0..7e4694a76ee 100644 --- a/compiler/testData/diagnostics/tests/generics/kt9203.fir.kt +++ b/compiler/testData/diagnostics/tests/generics/kt9203.fir.kt @@ -6,6 +6,6 @@ import java.util.stream.IntStream fun main() { val xs = IntStream.range(0, 10).mapToObj { it.toString() } - .collect(Collectors.toList()) + .collect(Collectors.toList()) xs[0] -} \ No newline at end of file +} diff --git a/compiler/testData/diagnostics/tests/j+k/collectorInference.fir.kt b/compiler/testData/diagnostics/tests/j+k/collectorInference.fir.kt index 6ae7cf7606e..b39362a17a7 100644 --- a/compiler/testData/diagnostics/tests/j+k/collectorInference.fir.kt +++ b/compiler/testData/diagnostics/tests/j+k/collectorInference.fir.kt @@ -7,8 +7,8 @@ import java.util.stream.Collectors import java.util.stream.Stream fun test(a: Stream) { - a.collect(Collectors.toList()) checkType { _>() } + a.collect(Collectors.toList()) checkType { _>() } // actually the inferred type is platform - a.collect(Collectors.toList()) checkType { _>() } + a.collect(Collectors.toList()) checkType { _>() } } diff --git a/compiler/testData/diagnostics/tests/targetedBuiltIns/stream.fir.kt b/compiler/testData/diagnostics/tests/targetedBuiltIns/stream.fir.kt index ba756b281d9..0073efb8034 100644 --- a/compiler/testData/diagnostics/tests/targetedBuiltIns/stream.fir.kt +++ b/compiler/testData/diagnostics/tests/targetedBuiltIns/stream.fir.kt @@ -8,6 +8,6 @@ interface A : Collection { } fun foo(x: List, y: A) { - x.stream().filter { it.length > 0 }.collect(Collectors.toList()) + x.stream().filter { it.length > 0 }.collect(Collectors.toList()) y.stream().filter { it.length > 0 } }