From e42f16c95c64849f78a9811a92876f7a1636c9f6 Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Tue, 28 Jan 2020 14:09:09 +0300 Subject: [PATCH] [NI] Fix construction of common system for builder inference --- .../tower/KotlinToResolvedCallTransformer.kt | 8 +++-- .../calls/components/KotlinCallCompleter.kt | 29 ++++++++++++------- .../resolve/calls/model/ResolutionAtoms.kt | 3 +- ...InferenceForMaterializeWithExpectedType.kt | 11 +++++++ ...nferenceForMaterializeWithExpectedType.txt | 5 ++++ .../coroutines/inference/kt35684.kt | 2 +- .../DiagnosticsTestWithStdLibGenerated.java | 5 ++++ ...ticsTestWithStdLibUsingJavacGenerated.java | 5 ++++ 8 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/builderInferenceForMaterializeWithExpectedType.kt create mode 100644 compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/builderInferenceForMaterializeWithExpectedType.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt index 456af5d9209..47e788c84ec 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt @@ -111,9 +111,11 @@ class KotlinToResolvedCallTransformer( context.trace.record(BindingContext.ONLY_RESOLVED_CALL, psiCall, PartialCallContainer(baseResolvedCall)) context.trace.record(BindingContext.PARTIAL_CALL_RESOLUTION_CONTEXT, psiCall, context) - context.inferenceSession.addPartialCallInfo( - PSIPartialCallInfo(baseResolvedCall, context, tracingStrategy) - ) + if (baseResolvedCall.forwardToInferenceSession) { + context.inferenceSession.addPartialCallInfo( + PSIPartialCallInfo(baseResolvedCall, context, tracingStrategy), + ) + } createStubResolvedCallAndWriteItToTrace(candidate, context.trace, baseResolvedCall.diagnostics, substitutor = null) } 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 5721f7741a2..340caeb8929 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 @@ -51,14 +51,22 @@ class KotlinCallCompleter( candidate.addExpectedTypeFromCastConstraint(returnType, resolutionCallbacks) candidate.checkSamWithVararg(diagnosticHolder) - return if (resolutionCallbacks.inferenceSession.shouldRunCompletion(candidate)) - candidate.runCompletion( - CompletionModeCalculator.computeCompletionMode(candidate, expectedType, returnType, trivialConstraintTypeInferenceOracle), - diagnosticHolder, - resolutionCallbacks - ) - else - candidate.asCallResolutionResult(ConstraintSystemCompletionMode.PARTIAL, diagnosticHolder) + val completionMode = + CompletionModeCalculator.computeCompletionMode(candidate, expectedType, returnType, trivialConstraintTypeInferenceOracle) + + return when (completionMode) { + ConstraintSystemCompletionMode.FULL -> { + if (resolutionCallbacks.inferenceSession.shouldRunCompletion(candidate)) { + candidate.runCompletion(completionMode, diagnosticHolder, resolutionCallbacks) + } else { + candidate.asCallResolutionResult( + ConstraintSystemCompletionMode.PARTIAL, diagnosticHolder, forwardToInferenceSession = true + ) + } + } + ConstraintSystemCompletionMode.PARTIAL -> + candidate.runCompletion(completionMode, diagnosticHolder, resolutionCallbacks) + } } private fun KotlinResolutionCandidate.checkSamWithVararg(diagnosticHolder: KotlinDiagnosticsHolder.SimpleHolder) { @@ -210,7 +218,8 @@ class KotlinCallCompleter( fun KotlinResolutionCandidate.asCallResolutionResult( type: ConstraintSystemCompletionMode, - diagnosticsHolder: KotlinDiagnosticsHolder.SimpleHolder + diagnosticsHolder: KotlinDiagnosticsHolder.SimpleHolder, + forwardToInferenceSession: Boolean = false ): CallResolutionResult { val systemStorage = getSystem().asReadOnlyStorage() val allDiagnostics = diagnosticsHolder.getDiagnostics() + this.diagnosticsFromResolutionParts @@ -222,7 +231,7 @@ class KotlinCallCompleter( return if (type == ConstraintSystemCompletionMode.FULL) { CompletedCallResolutionResult(resolvedCall, allDiagnostics, systemStorage) } else { - PartialCallResolutionResult(resolvedCall, allDiagnostics, systemStorage) + PartialCallResolutionResult(resolvedCall, allDiagnostics, systemStorage, forwardToInferenceSession) } } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/ResolutionAtoms.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/ResolutionAtoms.kt index 43685179196..5412e11ab12 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/ResolutionAtoms.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/ResolutionAtoms.kt @@ -225,7 +225,8 @@ open class SingleCallResolutionResult( class PartialCallResolutionResult( resultCallAtom: ResolvedCallAtom, diagnostics: List, - constraintSystem: ConstraintStorage + constraintSystem: ConstraintStorage, + val forwardToInferenceSession: Boolean = false ) : SingleCallResolutionResult(resultCallAtom, diagnostics, constraintSystem) class CompletedCallResolutionResult( diff --git a/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/builderInferenceForMaterializeWithExpectedType.kt b/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/builderInferenceForMaterializeWithExpectedType.kt new file mode 100644 index 00000000000..dbb757d1b64 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/builderInferenceForMaterializeWithExpectedType.kt @@ -0,0 +1,11 @@ +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE + +fun foo(x: List) {} +fun materialize(): K = TODO() + +fun test() { + val x: Sequence = sequence { + foo(materialize()) + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/builderInferenceForMaterializeWithExpectedType.txt b/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/builderInferenceForMaterializeWithExpectedType.txt new file mode 100644 index 00000000000..9c6aa01b68b --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/builderInferenceForMaterializeWithExpectedType.txt @@ -0,0 +1,5 @@ +package + +public fun foo(/*0*/ x: kotlin.collections.List): kotlin.Unit +public fun materialize(): K +public fun test(): kotlin.Unit diff --git a/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/kt35684.kt b/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/kt35684.kt index 3d8f58d55f6..cc35c3553ff 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/kt35684.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/kt35684.kt @@ -14,7 +14,7 @@ fun test_1() { fun test_2() { sequence { - yield(materialize()) + yield(materialize()) } } diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java index c81ef793617..659627a8ece 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java @@ -1882,6 +1882,11 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/applyInsideCoroutine.kt"); } + @TestMetadata("builderInferenceForMaterializeWithExpectedType.kt") + public void testBuilderInferenceForMaterializeWithExpectedType() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/builderInferenceForMaterializeWithExpectedType.kt"); + } + @TestMetadata("callableReferenceToASuspendFunction.kt") public void testCallableReferenceToASuspendFunction() throws Exception { runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/callableReferenceToASuspendFunction.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java index 4a6da1c3459..930073647a1 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java @@ -1882,6 +1882,11 @@ public class DiagnosticsTestWithStdLibUsingJavacGenerated extends AbstractDiagno runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/applyInsideCoroutine.kt"); } + @TestMetadata("builderInferenceForMaterializeWithExpectedType.kt") + public void testBuilderInferenceForMaterializeWithExpectedType() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/builderInferenceForMaterializeWithExpectedType.kt"); + } + @TestMetadata("callableReferenceToASuspendFunction.kt") public void testCallableReferenceToASuspendFunction() throws Exception { runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/callableReferenceToASuspendFunction.kt");