diff --git a/compiler/fir/analysis-tests/testData/resolveWithStdlib/problems/KtFirCompositeScope.fir.txt b/compiler/fir/analysis-tests/testData/resolveWithStdlib/problems/KtFirCompositeScope.fir.txt index ccfb90227b4..17edd1a0953 100644 --- a/compiler/fir/analysis-tests/testData/resolveWithStdlib/problems/KtFirCompositeScope.fir.txt +++ b/compiler/fir/analysis-tests/testData/resolveWithStdlib/problems/KtFirCompositeScope.fir.txt @@ -20,7 +20,7 @@ FILE: KtFirCompositeScope.kt public final fun getAllNames(): R|kotlin/collections/Set| { ^getAllNames R|/withValidityAssertion||>( = withValidityAssertion@fun (): R|kotlin/collections/Set| { ^ R|/buildSet|( = buildSet@fun R|kotlin/collections/MutableSet|.(): R|kotlin/Unit| { - this@R|/KtFirCompositeScope|.R|/KtFirCompositeScope.subScopes|.R|kotlin/collections/flatMapTo||>(this@R|special/anonymous|, = flatMapTo@fun (it: R|KtScope|): R|kotlin/collections/Iterable| { + this@R|/KtFirCompositeScope|.R|/KtFirCompositeScope.subScopes|.R|kotlin/collections/flatMapTo||>(this@R|special/anonymous|, = flatMapTo@fun (it: R|KtScope|): R|kotlin/collections/Iterable| { ^ R|/it|.R|/KtScope.getAllNames|() } ) diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index 379b8e15c51..09aa2cd302d 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -17677,6 +17677,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/inference/builderInference/constraintsBetweenTwoStubVariables.kt"); } + @Test + @TestMetadata("inferFromExpectedType.kt") + public void testInferFromExpectedType() throws Exception { + runTest("compiler/testData/codegen/box/inference/builderInference/inferFromExpectedType.kt"); + } + @Test @TestMetadata("intersect.kt") public void testIntersect() throws Exception { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/FirOverloadByLambdaReturnTypeResolver.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/FirOverloadByLambdaReturnTypeResolver.kt index d9a083f8d54..bda41af7db8 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/FirOverloadByLambdaReturnTypeResolver.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/FirOverloadByLambdaReturnTypeResolver.kt @@ -5,7 +5,6 @@ package org.jetbrains.kotlin.fir -import org.jetbrains.kotlin.config.LanguageFeature import org.jetbrains.kotlin.fir.expressions.FirResolvable import org.jetbrains.kotlin.fir.expressions.FirStatement import org.jetbrains.kotlin.fir.resolve.calls.Candidate @@ -138,7 +137,8 @@ class FirOverloadByLambdaReturnTypeResolver( val results = postponedArgumentsAnalyzer.analyzeLambda( firstCandidate.system.asPostponedArgumentsAnalyzerContext(), firstAtom, - firstCandidate + firstCandidate, + ConstraintSystemCompletionMode.FULL, ) while (iterator.hasNext()) { val (candidate, atom) = iterator.next() @@ -147,7 +147,8 @@ class FirOverloadByLambdaReturnTypeResolver( candidate.system.asPostponedArgumentsAnalyzerContext(), atom, candidate, - results + results, + ConstraintSystemCompletionMode.FULL, ) } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirBuilderInferenceSession.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirBuilderInferenceSession.kt index 550d2899ada..be0d8010fc9 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirBuilderInferenceSession.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirBuilderInferenceSession.kt @@ -108,7 +108,8 @@ class FirBuilderInferenceSession( override fun inferPostponedVariables( lambda: ResolvedLambdaAtom, - initialStorage: ConstraintStorage + initialStorage: ConstraintStorage, + completionMode: ConstraintSystemCompletionMode ): Map? { val (commonSystem, effectivelyEmptyConstraintSystem) = buildCommonSystem(initialStorage) if (effectivelyEmptyConstraintSystem) { @@ -120,7 +121,7 @@ class FirBuilderInferenceSession( @Suppress("UNCHECKED_CAST") components.callCompleter.completer.complete( context, - ConstraintSystemCompletionMode.FULL, + completionMode, partiallyResolvedCalls.map { it.first as FirStatement }, components.session.builtinTypes.unitType.type, resolutionContext, collectVariablesFromContext = true diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirCallCompleter.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirCallCompleter.kt index 8af3cb2017c..31f501e618f 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirCallCompleter.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirCallCompleter.kt @@ -167,7 +167,7 @@ class FirCallCompleter( initialType, transformer.resolutionContext ) { - analyzer.analyze(candidate.system.asPostponedArgumentsAnalyzerContext(), it, candidate) + analyzer.analyze(candidate.system.asPostponedArgumentsAnalyzerContext(), it, candidate, completionMode) } } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirDelegatedPropertyInferenceSession.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirDelegatedPropertyInferenceSession.kt index f6c196092f9..1b16527233d 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirDelegatedPropertyInferenceSession.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirDelegatedPropertyInferenceSession.kt @@ -53,7 +53,8 @@ class FirDelegatedPropertyInferenceSession( override fun inferPostponedVariables( lambda: ResolvedLambdaAtom, - initialStorage: ConstraintStorage + initialStorage: ConstraintStorage, + completionMode: ConstraintSystemCompletionMode ): Map? = null override fun shouldCompleteResolvedSubAtomsOf(call: T): Boolean where T : FirResolvable, T : FirStatement = true @@ -76,7 +77,8 @@ class FirDelegatedPropertyInferenceSession( postponedArgumentsAnalyzer.analyze( commonSystem.asPostponedArgumentsAnalyzerContext(), it, - resolvedCalls.first().candidate + resolvedCalls.first().candidate, + ConstraintSystemCompletionMode.FULL, ) } } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirInferenceSession.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirInferenceSession.kt index 537c9210f81..547e1496122 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirInferenceSession.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirInferenceSession.kt @@ -10,6 +10,7 @@ import org.jetbrains.kotlin.fir.expressions.FirStatement import org.jetbrains.kotlin.fir.resolve.calls.Candidate import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.types.ConeTypeVariableTypeConstructor +import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintSystemCompletionMode import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage abstract class FirInferenceSession { @@ -27,6 +28,7 @@ abstract class FirInferenceSession { abstract fun inferPostponedVariables( lambda: ResolvedLambdaAtom, initialStorage: ConstraintStorage, + completionMode: ConstraintSystemCompletionMode, // TODO: diagnostic holder ): Map? @@ -47,7 +49,8 @@ abstract class FirStubInferenceSession : FirInferenceSession() { override fun inferPostponedVariables( lambda: ResolvedLambdaAtom, - initialStorage: ConstraintStorage + initialStorage: ConstraintStorage, + completionMode: ConstraintSystemCompletionMode ): Map? = null override fun writeOnlyStubs(call: T): Boolean where T : FirResolvable, T : FirStatement = false diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArgumentsAnalyzer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArgumentsAnalyzer.kt index e0846c05a03..10bbea5bfb8 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArgumentsAnalyzer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArgumentsAnalyzer.kt @@ -16,11 +16,15 @@ import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeUnresolvedReferenceError import org.jetbrains.kotlin.fir.resolve.inference.model.ConeLambdaArgumentConstraintPosition import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor import org.jetbrains.kotlin.fir.resolve.transformers.StoreNameReference -import org.jetbrains.kotlin.fir.types.* +import org.jetbrains.kotlin.fir.types.ConeKotlinType +import org.jetbrains.kotlin.fir.types.ConeTypeVariable import org.jetbrains.kotlin.fir.types.builder.buildErrorTypeRef import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef +import org.jetbrains.kotlin.fir.types.coneType +import org.jetbrains.kotlin.fir.types.isMarkedNullable import org.jetbrains.kotlin.resolve.calls.components.PostponedArgumentsAnalyzerContext import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilder +import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintSystemCompletionMode import org.jetbrains.kotlin.resolve.calls.inference.model.CoroutinePosition import org.jetbrains.kotlin.types.model.StubTypeMarker import org.jetbrains.kotlin.types.model.TypeVariableMarker @@ -52,14 +56,15 @@ class PostponedArgumentsAnalyzer( fun analyze( c: PostponedArgumentsAnalyzerContext, argument: PostponedResolvedAtom, - candidate: Candidate + candidate: Candidate, + completionMode: ConstraintSystemCompletionMode ) { when (argument) { is ResolvedLambdaAtom -> - analyzeLambda(c, argument, candidate) + analyzeLambda(c, argument, candidate, completionMode) is LambdaWithTypeVariableAsExpectedTypeAtom -> - analyzeLambda(c, argument.transformToResolvedLambda(c.getBuilder(), resolutionContext), candidate) + analyzeLambda(c, argument.transformToResolvedLambda(c.getBuilder(), resolutionContext), candidate, completionMode) is ResolvedCallableReferenceAtom -> processCallableReference(argument, candidate) @@ -106,7 +111,8 @@ class PostponedArgumentsAnalyzer( fun analyzeLambda( c: PostponedArgumentsAnalyzerContext, lambda: ResolvedLambdaAtom, - candidate: Candidate + candidate: Candidate, + completionMode: ConstraintSystemCompletionMode, //diagnosticHolder: KotlinDiagnosticsHolder ): ReturnArgumentsAnalysisResult { val unitType = components.session.builtinTypes.unitType.type @@ -135,7 +141,15 @@ class PostponedArgumentsAnalyzer( expectedTypeForReturnArguments, stubsForPostponedVariables ) - applyResultsOfAnalyzedLambdaToCandidateSystem(c, lambda, candidate, results, expectedTypeForReturnArguments, ::substitute) + applyResultsOfAnalyzedLambdaToCandidateSystem( + c, + lambda, + candidate, + results, + completionMode, + expectedTypeForReturnArguments, + ::substitute + ) return results } @@ -144,6 +158,7 @@ class PostponedArgumentsAnalyzer( lambda: ResolvedLambdaAtom, candidate: Candidate, results: ReturnArgumentsAnalysisResult, + completionMode: ConstraintSystemCompletionMode, expectedReturnType: ConeKotlinType? = null, substitute: (ConeKotlinType) -> ConeKotlinType = c.createSubstituteFunctorForLambdaAnalysis() ) { @@ -151,7 +166,7 @@ class PostponedArgumentsAnalyzer( if (inferenceSession != null) { val storageSnapshot = c.getBuilder().currentStorage() - val postponedVariables = inferenceSession.inferPostponedVariables(lambda, storageSnapshot) + val postponedVariables = inferenceSession.inferPostponedVariables(lambda, storageSnapshot, completionMode) if (postponedVariables == null) { c.getBuilder().removePostponedVariables() diff --git a/compiler/testData/codegen/box/inference/builderInference/inferFromExpectedType.kt b/compiler/testData/codegen/box/inference/builderInference/inferFromExpectedType.kt new file mode 100644 index 00000000000..7208134789e --- /dev/null +++ b/compiler/testData/codegen/box/inference/builderInference/inferFromExpectedType.kt @@ -0,0 +1,17 @@ +// WITH_RUNTIME +// TARGET_BACKEND: JVM_IR + +@OptIn(ExperimentalStdlibApi::class) +fun getAllPossibleNames(subScopes: List>): Set = withValidityAssertion { + buildSet { + subScopes.flatMapTo(this) { it } + } +} + +inline fun withValidityAssertion(action: () -> R): R { + return action() +} + +fun box(): String { + return getAllPossibleNames(listOf(listOf("O"), listOf("K"))).joinToString("") +} diff --git a/compiler/testData/codegen/box/inference/builderInference/kt41164.kt b/compiler/testData/codegen/box/inference/builderInference/kt41164.kt index d4a4ba991f0..21d312f4feb 100644 --- a/compiler/testData/codegen/box/inference/builderInference/kt41164.kt +++ b/compiler/testData/codegen/box/inference/builderInference/kt41164.kt @@ -1,6 +1,5 @@ // ISSUE: KT-41164 // WITH_RUNTIME -// IGNORE_BACKEND_FIR: JVM_IR // DONT_TARGET_EXACT_BACKEND: WASM import kotlin.experimental.ExperimentalTypeInference diff --git a/compiler/testData/diagnostics/testsWithStdLib/builderInference/resolveUsualCallWithBuilderInference.fir.kt b/compiler/testData/diagnostics/testsWithStdLib/builderInference/resolveUsualCallWithBuilderInference.fir.kt deleted file mode 100644 index 8d193179a4e..00000000000 --- a/compiler/testData/diagnostics/testsWithStdLib/builderInference/resolveUsualCallWithBuilderInference.fir.kt +++ /dev/null @@ -1,37 +0,0 @@ -// !LANGUAGE: +UnrestrictedBuilderInference -// !DIAGNOSTICS: -UNUSED_PARAMETER -// FILE: annotation.kt - -package kotlin - -annotation class BuilderInference - -// FILE: test.kt - -class Builder { - fun add(t: T) {} -} - -fun build(@BuilderInference g: Builder.() -> Unit): List = TODO() -fun wrongBuild(g: Builder.() -> Unit): List = TODO() - -fun Builder.extensionAdd(s: S) {} - -@BuilderInference -fun Builder.safeExtensionAdd(s: S) {} - -val member = build { - add(42) -} - -val memberWithoutAnn = wrongBuild { - add(42) -} - -val extension = build { - extensionAdd("foo") -} - -val safeExtension = build { - safeExtensionAdd("foo") -} diff --git a/compiler/testData/diagnostics/testsWithStdLib/builderInference/resolveUsualCallWithBuilderInference.kt b/compiler/testData/diagnostics/testsWithStdLib/builderInference/resolveUsualCallWithBuilderInference.kt index 1e0dc7cb6dc..a2f0adc0504 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/builderInference/resolveUsualCallWithBuilderInference.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/builderInference/resolveUsualCallWithBuilderInference.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // !LANGUAGE: +UnrestrictedBuilderInference // !DIAGNOSTICS: -UNUSED_PARAMETER // FILE: annotation.kt diff --git a/compiler/testData/ir/irText/expressions/kt47082.fir.txt b/compiler/testData/ir/irText/expressions/kt47082.fir.txt index 06d4bd57cbd..39377efad42 100644 --- a/compiler/testData/ir/irText/expressions/kt47082.fir.txt +++ b/compiler/testData/ir/irText/expressions/kt47082.fir.txt @@ -77,11 +77,11 @@ FILE fqName: fileName:/kt47082.kt $receiver: VALUE_PARAMETER name: type:.Derived.foo> BLOCK_BODY TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit - CALL 'public final fun toChannel (destination: C of .toChannel): C of .toChannel declared in ' type=.Derived.foo> origin=null + CALL 'public final fun toChannel (destination: C of .toChannel): C of .toChannel declared in ' type=.Derived origin=null : R of .foo - : .Derived.foo> + : .Derived $receiver: GET_VAR 'r: .Receiver.foo> declared in .foo' type=.Receiver.foo> origin=null - destination: GET_VAR ': .Derived.foo> declared in .foo.' type=.Derived.foo> origin=null + destination: GET_VAR ': .Derived.foo> declared in .foo.' type=.Derived origin=null FUN name:box visibility:public modality:FINAL <> () returnType:kotlin.String BLOCK_BODY RETURN type=kotlin.Nothing from='public final fun box (): kotlin.String declared in ' diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index bb7eeb1bfe1..d2017d947e0 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -17677,6 +17677,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/inference/builderInference/constraintsBetweenTwoStubVariables.kt"); } + @Test + @TestMetadata("inferFromExpectedType.kt") + public void testInferFromExpectedType() throws Exception { + runTest("compiler/testData/codegen/box/inference/builderInference/inferFromExpectedType.kt"); + } + @Test @TestMetadata("intersect.kt") public void testIntersect() throws Exception {