[NI] Don't always complete builder inference lambda in FULL mode

#KT-41164 Fixed
This commit is contained in:
Dmitriy Novozhilov
2020-08-20 15:04:52 +03:00
parent 6a15e0410f
commit e98cbf81cf
21 changed files with 181 additions and 10 deletions
@@ -2096,6 +2096,11 @@ public class FirOldFrontendDiagnosticsTestWithStdlibGenerated extends AbstractFi
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/kt38766.kt");
}
@TestMetadata("kt41164.kt")
public void testKt41164() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/kt41164.kt");
}
@TestMetadata("nestedLambdaInferenceWithListMap.kt")
public void testNestedLambdaInferenceWithListMap() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/nestedLambdaInferenceWithListMap.kt");
@@ -1708,6 +1708,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/builderInference"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@TestMetadata("kt41164.kt")
public void testKt41164() throws Exception {
runTest("compiler/testData/codegen/box/builderInference/kt41164.kt");
}
@TestMetadata("lackOfNullCheckOnNullableInsideBuild.kt")
public void testLackOfNullCheckOnNullableInsideBuild() throws Exception {
runTest("compiler/testData/codegen/box/builderInference/lackOfNullCheckOnNullableInsideBuild.kt");
@@ -82,6 +82,7 @@ class DelegatedPropertyInferenceSession(
override fun inferPostponedVariables(
lambda: ResolvedLambdaAtom,
initialStorage: ConstraintStorage,
completionMode: KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode,
diagnosticsHolder: KotlinDiagnosticsHolder
): Map<TypeConstructor, UnwrappedType> = emptyMap()
@@ -103,6 +104,7 @@ class InferenceSessionForExistingCandidates(private val resolveReceiverIndepende
override fun inferPostponedVariables(
lambda: ResolvedLambdaAtom,
initialStorage: ConstraintStorage,
completionMode: KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode,
diagnosticsHolder: KotlinDiagnosticsHolder
): Map<TypeConstructor, UnwrappedType> = emptyMap()
@@ -147,7 +147,8 @@ class CoroutineInferenceSession(
override fun inferPostponedVariables(
lambda: ResolvedLambdaAtom,
initialStorage: ConstraintStorage,
diagnosticsHolder: KotlinDiagnosticsHolder
completionMode: KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode,
diagnosticsHolder: KotlinDiagnosticsHolder,
): Map<TypeConstructor, UnwrappedType>? {
val (commonSystem, effectivelyEmptyConstraintSystem) = buildCommonSystem(initialStorage)
if (effectivelyEmptyConstraintSystem) {
@@ -160,6 +161,7 @@ class CoroutineInferenceSession(
context,
builtIns.unitType,
partiallyResolvedCallsInfo.map { it.callResolutionResult.resultCallAtom },
completionMode,
diagnosticsHolder
)
@@ -81,15 +81,20 @@ abstract class ManyCandidatesResolver<D : CallableDescriptor>(
}
fun runCompletion(constraintSystem: NewConstraintSystem, atoms: List<ResolvedAtom>) {
val completionMode = KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode.FULL
kotlinConstraintSystemCompleter.runCompletion(
constraintSystem.asConstraintSystemCompleterContext(),
KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode.FULL,
completionMode,
atoms,
builtIns.unitType,
diagnosticHolder
) {
postponedArgumentsAnalyzer.analyze(
constraintSystem.asPostponedArgumentsAnalyzerContext(), resolutionCallbacks, it, diagnosticHolder
constraintSystem.asPostponedArgumentsAnalyzerContext(),
resolutionCallbacks,
it,
completionMode,
diagnosticHolder
)
}
@@ -197,4 +202,4 @@ class PSICompletedCallInfo(
class PSIErrorCallInfo<D : CallableDescriptor>(
override val callResolutionResult: CallResolutionResult,
val result: OverloadResolutionResults<D>
) : ErrorCallInfo
) : ErrorCallInfo
@@ -22,6 +22,7 @@ interface InferenceSession {
override fun inferPostponedVariables(
lambda: ResolvedLambdaAtom,
initialStorage: ConstraintStorage,
completionMode: KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode,
diagnosticsHolder: KotlinDiagnosticsHolder
): Map<TypeConstructor, UnwrappedType> = emptyMap()
@@ -44,6 +45,7 @@ interface InferenceSession {
fun inferPostponedVariables(
lambda: ResolvedLambdaAtom,
initialStorage: ConstraintStorage,
completionMode: KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode,
diagnosticsHolder: KotlinDiagnosticsHolder
): Map<TypeConstructor, UnwrappedType>?
@@ -64,4 +66,4 @@ interface CompletedCallInfo {
interface ErrorCallInfo {
val callResolutionResult: CallResolutionResult
}
}
@@ -115,6 +115,7 @@ class KotlinCallCompleter(
firstCandidate.getSystem().asPostponedArgumentsAnalyzerContext(),
resolutionCallbacks,
firstAtom,
ConstraintSystemCompletionMode.FULL,
diagnosticHolderForLambda,
)
@@ -124,6 +125,7 @@ class KotlinCallCompleter(
candidate.getSystem().asPostponedArgumentsAnalyzerContext(),
atom,
results,
ConstraintSystemCompletionMode.FULL,
diagnosticHolderForLambda
)
}
@@ -228,6 +230,7 @@ class KotlinCallCompleter(
constraintSystem.asPostponedArgumentsAnalyzerContext(),
resolutionCallbacks,
it,
completionMode,
diagnosticsHolder
)
}
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.annotations.FilteredAnnotations
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilder
import org.jetbrains.kotlin.resolve.calls.inference.addSubsystemFromArgument
import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter
import org.jetbrains.kotlin.resolve.calls.inference.components.NewTypeSubstitutorByConstructorMap
import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage
import org.jetbrains.kotlin.resolve.calls.inference.model.CoroutinePosition
@@ -47,15 +48,20 @@ class PostponedArgumentsAnalyzer(
c: Context,
resolutionCallbacks: KotlinResolutionCallbacks,
argument: ResolvedAtom,
completionMode: KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode,
diagnosticsHolder: KotlinDiagnosticsHolder
) {
when (argument) {
is ResolvedLambdaAtom ->
analyzeLambda(c, resolutionCallbacks, argument, diagnosticsHolder)
analyzeLambda(c, resolutionCallbacks, argument, completionMode, diagnosticsHolder)
is LambdaWithTypeVariableAsExpectedTypeAtom ->
analyzeLambda(
c, resolutionCallbacks, argument.transformToResolvedLambda(c.getBuilder(), diagnosticsHolder), diagnosticsHolder
c,
resolutionCallbacks,
argument.transformToResolvedLambda(c.getBuilder(), diagnosticsHolder),
completionMode,
diagnosticsHolder
)
is ResolvedCallableReferenceAtom ->
@@ -84,6 +90,7 @@ class PostponedArgumentsAnalyzer(
c: Context,
resolutionCallbacks: KotlinResolutionCallbacks,
lambda: ResolvedLambdaAtom,
completionMode: KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode,
diagnosticHolder: KotlinDiagnosticsHolder,
): ReturnArgumentsAnalysisResult {
val substitutorAndStubsForLambdaAnalysis = c.createSubstituteFunctorForLambdaAnalysis()
@@ -142,7 +149,7 @@ class PostponedArgumentsAnalyzer(
convertedAnnotations ?: Annotations.EMPTY,
substitutorAndStubsForLambdaAnalysis.stubsForPostponedVariables.cast(),
)
applyResultsOfAnalyzedLambdaToCandidateSystem(c, lambda, returnArgumentsAnalysisResult, diagnosticHolder, substitute)
applyResultsOfAnalyzedLambdaToCandidateSystem(c, lambda, returnArgumentsAnalysisResult, completionMode, diagnosticHolder, substitute)
return returnArgumentsAnalysisResult
}
@@ -150,6 +157,7 @@ class PostponedArgumentsAnalyzer(
c: Context,
lambda: ResolvedLambdaAtom,
returnArgumentsAnalysisResult: ReturnArgumentsAnalysisResult,
completionMode: KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode,
diagnosticHolder: KotlinDiagnosticsHolder,
substitute: (KotlinType) -> UnwrappedType = c.createSubstituteFunctorForLambdaAnalysis().substitute
) {
@@ -191,7 +199,12 @@ class PostponedArgumentsAnalyzer(
if (inferenceSession != null && lambda.atom.hasBuilderInferenceAnnotation) {
val storageSnapshot = c.getBuilder().currentStorage()
val postponedVariables = inferenceSession.inferPostponedVariables(lambda, storageSnapshot, diagnosticHolder)
val postponedVariables = inferenceSession.inferPostponedVariables(
lambda,
storageSnapshot,
completionMode,
diagnosticHolder
)
if (postponedVariables == null) {
c.getBuilder().removePostponedVariables()
return
@@ -73,10 +73,11 @@ class KotlinConstraintSystemCompleter(
c: Context,
topLevelType: UnwrappedType,
topLevelAtoms: List<ResolvedAtom>,
completionMode: ConstraintSystemCompletionMode,
diagnosticsHolder: KotlinDiagnosticsHolder
) {
c.runCompletion(
ConstraintSystemCompletionMode.FULL,
completionMode,
topLevelAtoms,
topLevelType,
diagnosticsHolder,
@@ -0,0 +1,24 @@
// ISSUE: KT-41164
// WITH_RUNTIME
import kotlin.experimental.ExperimentalTypeInference
interface MyProducerScope<in E>
interface MyFlow<out T>
fun <K> select(x: K, y: K): K = x
@OptIn(ExperimentalTypeInference::class)
fun <T> myCallbackFlow(@BuilderInference block: MyProducerScope<T>.() -> Unit): MyFlow<T> = null!!
fun MyProducerScope<*>.myAwaitClose(block: () -> Unit = {}) {}
fun <E> myEmptyFlow(): MyFlow<E> = null!!
fun test(): MyFlow<Int> {
return select(
myCallbackFlow {
myAwaitClose {}
},
myEmptyFlow()
)
}
fun box(): String = "OK"
@@ -0,0 +1,25 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER -EXPERIMENTAL_IS_NOT_ENABLED
// ISSUE: KT-41164
import kotlin.experimental.ExperimentalTypeInference
interface MyProducerScope<in E>
interface MyFlow<out T>
fun <K> select(x: K, y: K): K = x
@OptIn(ExperimentalTypeInference::class)
fun <T> myCallbackFlow(@BuilderInference block: MyProducerScope<T>.() -> Unit): MyFlow<T> = null!!
fun MyProducerScope<*>.myAwaitClose(block: () -> Unit = {}) {}
fun <T> myEmptyFlow(): MyFlow<T> = null!!
fun test(): MyFlow<Int> {
return select(
<!DEBUG_INFO_EXPRESSION_TYPE("MyFlow<kotlin.Any?>")!>myCallbackFlow <!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Function1<MyProducerScope<kotlin.Any?>, kotlin.Unit>")!>{
myAwaitClose {}
}<!><!>,
myEmptyFlow()
)
}
@@ -0,0 +1,25 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER -EXPERIMENTAL_IS_NOT_ENABLED
// ISSUE: KT-41164
import kotlin.experimental.ExperimentalTypeInference
interface MyProducerScope<in E>
interface MyFlow<out T>
fun <K> select(x: K, y: K): K = x
@OptIn(ExperimentalTypeInference::class)
fun <T> myCallbackFlow(@BuilderInference block: MyProducerScope<T>.() -> Unit): MyFlow<T> = null!!
fun MyProducerScope<*>.myAwaitClose(block: () -> Unit = {}) {}
fun <T> myEmptyFlow(): MyFlow<T> = null!!
fun test(): MyFlow<Int> {
return select(
<!DEBUG_INFO_EXPRESSION_TYPE("MyFlow<kotlin.Int>")!>myCallbackFlow <!DEBUG_INFO_EXPRESSION_TYPE("MyProducerScope<kotlin.Int>.() -> kotlin.Unit")!>{
myAwaitClose {}
}<!><!>,
myEmptyFlow()
)
}
@@ -0,0 +1,19 @@
package
@kotlin.OptIn(markerClass = {kotlin.experimental.ExperimentalTypeInference::class}) public fun </*0*/ T> myCallbackFlow(/*0*/ @kotlin.BuilderInference block: MyProducerScope<T>.() -> kotlin.Unit): MyFlow<T>
public fun </*0*/ T> myEmptyFlow(): MyFlow<T>
public fun </*0*/ K> select(/*0*/ x: K, /*1*/ y: K): K
public fun test(): MyFlow<kotlin.Int>
public fun MyProducerScope<*>.myAwaitClose(/*0*/ block: () -> kotlin.Unit = ...): kotlin.Unit
public interface MyFlow</*0*/ out T> {
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 MyProducerScope</*0*/ in E> {
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
}
@@ -2136,6 +2136,11 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/kt38766.kt");
}
@TestMetadata("kt41164.kt")
public void testKt41164() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/kt41164.kt");
}
@TestMetadata("nestedLambdaInferenceWithListMap.kt")
public void testNestedLambdaInferenceWithListMap() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/nestedLambdaInferenceWithListMap.kt");
@@ -2136,6 +2136,11 @@ public class DiagnosticsTestWithStdLibUsingJavacGenerated extends AbstractDiagno
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/kt38766.kt");
}
@TestMetadata("kt41164.kt")
public void testKt41164() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/kt41164.kt");
}
@TestMetadata("nestedLambdaInferenceWithListMap.kt")
public void testNestedLambdaInferenceWithListMap() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/nestedLambdaInferenceWithListMap.kt");
@@ -1728,6 +1728,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/builderInference"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true);
}
@TestMetadata("kt41164.kt")
public void testKt41164() throws Exception {
runTest("compiler/testData/codegen/box/builderInference/kt41164.kt");
}
@TestMetadata("lackOfNullCheckOnNullableInsideBuild.kt")
public void testLackOfNullCheckOnNullableInsideBuild() throws Exception {
runTest("compiler/testData/codegen/box/builderInference/lackOfNullCheckOnNullableInsideBuild.kt");
@@ -1728,6 +1728,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/builderInference"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true);
}
@TestMetadata("kt41164.kt")
public void testKt41164() throws Exception {
runTest("compiler/testData/codegen/box/builderInference/kt41164.kt");
}
@TestMetadata("lackOfNullCheckOnNullableInsideBuild.kt")
public void testLackOfNullCheckOnNullableInsideBuild() throws Exception {
runTest("compiler/testData/codegen/box/builderInference/lackOfNullCheckOnNullableInsideBuild.kt");
@@ -1708,6 +1708,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/builderInference"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@TestMetadata("kt41164.kt")
public void testKt41164() throws Exception {
runTest("compiler/testData/codegen/box/builderInference/kt41164.kt");
}
@TestMetadata("lackOfNullCheckOnNullableInsideBuild.kt")
public void testLackOfNullCheckOnNullableInsideBuild() throws Exception {
runTest("compiler/testData/codegen/box/builderInference/lackOfNullCheckOnNullableInsideBuild.kt");
@@ -1328,6 +1328,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/builderInference"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR_ES6, true);
}
@TestMetadata("kt41164.kt")
public void testKt41164() throws Exception {
runTest("compiler/testData/codegen/box/builderInference/kt41164.kt");
}
@TestMetadata("lackOfNullCheckOnNullableInsideBuild.kt")
public void testLackOfNullCheckOnNullableInsideBuild() throws Exception {
runTest("compiler/testData/codegen/box/builderInference/lackOfNullCheckOnNullableInsideBuild.kt");
@@ -1328,6 +1328,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/builderInference"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true);
}
@TestMetadata("kt41164.kt")
public void testKt41164() throws Exception {
runTest("compiler/testData/codegen/box/builderInference/kt41164.kt");
}
@TestMetadata("lackOfNullCheckOnNullableInsideBuild.kt")
public void testLackOfNullCheckOnNullableInsideBuild() throws Exception {
runTest("compiler/testData/codegen/box/builderInference/lackOfNullCheckOnNullableInsideBuild.kt");
@@ -1328,6 +1328,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/builderInference"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS, true);
}
@TestMetadata("kt41164.kt")
public void testKt41164() throws Exception {
runTest("compiler/testData/codegen/box/builderInference/kt41164.kt");
}
@TestMetadata("lackOfNullCheckOnNullableInsideBuild.kt")
public void testLackOfNullCheckOnNullableInsideBuild() throws Exception {
runTest("compiler/testData/codegen/box/builderInference/lackOfNullCheckOnNullableInsideBuild.kt");