From 3de6289c6c584f1d62b5b3574beceda007c88371 Mon Sep 17 00:00:00 2001 From: Pavel Kirpichenkov Date: Mon, 2 Dec 2019 20:07:17 +0300 Subject: [PATCH] [NI] Run completion on subatoms of error calls Call transformer previously ran completion of argument atoms only for non-error candidates. This led to missing diagnostics, i.e. from collection literal resolver. Now arguments of calls resolved to error descriptor are completed, with exception to not found provideDelegate calls. provideDelegate's subatoms are not completed after failure, because it is a part of delegate competion, which does not end with unresolved provideDelegate. Completing after provideDelegate failure removes constraint system from resolved arguments, which breaks resolve for get/setValue. ^KT-33592 Fixed --- .../fir/FirDiagnosticsSmokeTestGenerated.java | 5 +++++ .../DelegatedPropertyInferenceSession.kt | 9 ++++++-- .../inference/CoroutineInferenceSession.kt | 2 ++ .../tower/KotlinToResolvedCallTransformer.kt | 2 +- .../calls/tower/ManyCandidatesResolver.kt | 2 ++ .../calls/tower/ResolvedAtomCompleter.kt | 2 ++ .../calls/components/InferenceSession.kt | 2 ++ .../function/extensionToSupertype.kt | 2 +- ...ersectionTypeOverloadWithWrongParameter.kt | 2 +- .../resolve/withPlaceholderTypes.kt | 2 +- .../collectionLiteralsOutsideOfAnnotations.kt | 22 +++++++++++++++++++ ...collectionLiteralsOutsideOfAnnotations.txt | 13 +++++++++++ .../checkers/DiagnosticsTestGenerated.java | 5 +++++ .../DiagnosticsUsingJavacTestGenerated.java | 5 +++++ ...renceInsideUnresolvedConstructor.types.txt | 4 ++-- .../InnerNonFixedTypeVariable.types.txt | 4 ++-- 16 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/collectionLiterals/collectionLiteralsOutsideOfAnnotations.kt create mode 100644 compiler/testData/diagnostics/tests/collectionLiterals/collectionLiteralsOutsideOfAnnotations.txt diff --git a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java index 4a00ec72b01..396a86956b5 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java @@ -3430,6 +3430,11 @@ public class FirDiagnosticsSmokeTestGenerated extends AbstractFirDiagnosticsSmok runTest("compiler/testData/diagnostics/tests/collectionLiterals/collectionLiteralsAsPrimitiveArrays.kt"); } + @TestMetadata("collectionLiteralsOutsideOfAnnotations.kt") + public void testCollectionLiteralsOutsideOfAnnotations() throws Exception { + runTest("compiler/testData/diagnostics/tests/collectionLiterals/collectionLiteralsOutsideOfAnnotations.kt"); + } + @TestMetadata("collectionLiteralsWithVarargs.kt") public void testCollectionLiteralsWithVarargs() throws Exception { runTest("compiler/testData/diagnostics/tests/collectionLiterals/collectionLiteralsWithVarargs.kt"); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DelegatedPropertyInferenceSession.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DelegatedPropertyInferenceSession.kt index 32fe600a1bb..aa3c09be762 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DelegatedPropertyInferenceSession.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DelegatedPropertyInferenceSession.kt @@ -49,8 +49,8 @@ class DelegatedPropertyInferenceSession( private fun ResolvedCallAtom.addConstraintForThis(descriptor: CallableDescriptor, commonSystem: ConstraintSystemBuilder) { val typeOfThis = variableDescriptor.extensionReceiverParameter?.type - ?: variableDescriptor.dispatchReceiverParameter?.type - ?: builtIns.nullableNothingType + ?: variableDescriptor.dispatchReceiverParameter?.type + ?: builtIns.nullableNothingType val valueParameterForThis = descriptor.valueParameters.getOrNull(0) ?: return val substitutedType = freshVariablesSubstitutor.safeSubstitute(valueParameterForThis.type.unwrap()) @@ -85,6 +85,8 @@ class DelegatedPropertyInferenceSession( ): Map = emptyMap() override fun writeOnlyStubs(callInfo: SingleCallResolutionResult): Boolean = false + + override fun shouldCompleteResolvedSubAtomsOf(resolvedCallAtom: ResolvedCallAtom) = true } object InferenceSessionForExistingCandidates : InferenceSession { @@ -104,4 +106,7 @@ object InferenceSessionForExistingCandidates : InferenceSession { override fun writeOnlyStubs(callInfo: SingleCallResolutionResult): Boolean = false override fun callCompleted(resolvedAtom: ResolvedAtom): Boolean = false + override fun shouldCompleteResolvedSubAtomsOf(resolvedCallAtom: ResolvedCallAtom): Boolean { + return !ErrorUtils.isError(resolvedCallAtom.candidateDescriptor) + } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/CoroutineInferenceSession.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/CoroutineInferenceSession.kt index 49e1860469f..bc123cb984b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/CoroutineInferenceSession.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/CoroutineInferenceSession.kt @@ -107,6 +107,8 @@ class CoroutineInferenceSession( return commonSystem.fixedTypeVariables.cast() // TODO: SUB } + override fun shouldCompleteResolvedSubAtomsOf(resolvedCallAtom: ResolvedCallAtom) = true + private fun createNonFixedTypeToVariableSubstitutor(): NewTypeSubstitutorByConstructorMap { val bindings = hashMapOf() for ((variable, nonFixedType) in stubsForPostponedVariables) { 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 8205c13664a..afe704aecd1 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 @@ -138,7 +138,7 @@ class KotlinToResolvedCallTransformer( typeApproximator, missingSupertypesResolver ) - if (!ErrorUtils.isError(candidate.candidateDescriptor)) { + if (context.inferenceSession.shouldCompleteResolvedSubAtomsOf(candidate)) { candidate.subResolvedAtoms?.forEach { subKtPrimitive -> ktPrimitiveCompleter.completeAll(subKtPrimitive) } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ManyCandidatesResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ManyCandidatesResolver.kt index 71504379e6b..2bc6f202af5 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ManyCandidatesResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ManyCandidatesResolver.kt @@ -61,6 +61,8 @@ abstract class ManyCandidatesResolver( override fun callCompleted(resolvedAtom: ResolvedAtom): Boolean = !completedCalls.add(resolvedAtom) + override fun shouldCompleteResolvedSubAtomsOf(resolvedCallAtom: ResolvedCallAtom) = true + fun resolveCandidates(resolutionCallbacks: KotlinResolutionCallbacks): List> { val resolvedCallsInfo = partiallyResolvedCallsInfo.toList() diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt index 6b5be5b10ed..648ab4b724b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/ResolvedAtomCompleter.kt @@ -73,6 +73,8 @@ class ResolvedAtomCompleter( } fun completeAll(resolvedAtom: ResolvedAtom) { + if (!resolvedAtom.analyzed) + return resolvedAtom.subResolvedAtoms?.forEach { subKtPrimitive -> completeAll(subKtPrimitive) } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/InferenceSession.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/InferenceSession.kt index 96b8f5e952d..9f3a75dba05 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/InferenceSession.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/InferenceSession.kt @@ -26,6 +26,7 @@ interface InferenceSession { override fun writeOnlyStubs(callInfo: SingleCallResolutionResult): Boolean = false override fun callCompleted(resolvedAtom: ResolvedAtom): Boolean = false + override fun shouldCompleteResolvedSubAtomsOf(resolvedCallAtom: ResolvedCallAtom) = true } } @@ -37,6 +38,7 @@ interface InferenceSession { fun inferPostponedVariables(lambda: ResolvedLambdaAtom, initialStorage: ConstraintStorage): Map fun writeOnlyStubs(callInfo: SingleCallResolutionResult): Boolean fun callCompleted(resolvedAtom: ResolvedAtom): Boolean + fun shouldCompleteResolvedSubAtomsOf(resolvedCallAtom: ResolvedCallAtom): Boolean } interface PartialCallInfo { diff --git a/compiler/testData/diagnostics/tests/callableReference/function/extensionToSupertype.kt b/compiler/testData/diagnostics/tests/callableReference/function/extensionToSupertype.kt index 96bce839d55..4f27d448e65 100644 --- a/compiler/testData/diagnostics/tests/callableReference/function/extensionToSupertype.kt +++ b/compiler/testData/diagnostics/tests/callableReference/function/extensionToSupertype.kt @@ -15,5 +15,5 @@ fun take(f: () -> Unit) {} fun test() { B::foo checkType { _>() } - take(B::foo) + take(B::foo) } diff --git a/compiler/testData/diagnostics/tests/callableReference/resolve/intersectionTypeOverloadWithWrongParameter.kt b/compiler/testData/diagnostics/tests/callableReference/resolve/intersectionTypeOverloadWithWrongParameter.kt index e6bb894c11b..9b00eef59ed 100644 --- a/compiler/testData/diagnostics/tests/callableReference/resolve/intersectionTypeOverloadWithWrongParameter.kt +++ b/compiler/testData/diagnostics/tests/callableReference/resolve/intersectionTypeOverloadWithWrongParameter.kt @@ -11,7 +11,7 @@ class Foo { } fun foo() { - installRoute(::route) + installRoute(::route) } } diff --git a/compiler/testData/diagnostics/tests/callableReference/resolve/withPlaceholderTypes.kt b/compiler/testData/diagnostics/tests/callableReference/resolve/withPlaceholderTypes.kt index 8a35e743d43..3e2251bed0b 100644 --- a/compiler/testData/diagnostics/tests/callableReference/resolve/withPlaceholderTypes.kt +++ b/compiler/testData/diagnostics/tests/callableReference/resolve/withPlaceholderTypes.kt @@ -22,7 +22,7 @@ val x1 = fn1(1, ::foo, ::foo) val x2 = fn1(1, ::foo, ::bar) val x3 = fn2(::bar, ::foo) -val x4 = fn2(::foo, ::bar) +val x4 = fn2(::foo, ::bar) val x5 = fn2(::foo, ::foo) val x6 = fn3(1, ::qux) \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/collectionLiterals/collectionLiteralsOutsideOfAnnotations.kt b/compiler/testData/diagnostics/tests/collectionLiterals/collectionLiteralsOutsideOfAnnotations.kt new file mode 100644 index 00000000000..7002f6fad0f --- /dev/null +++ b/compiler/testData/diagnostics/tests/collectionLiterals/collectionLiteralsOutsideOfAnnotations.kt @@ -0,0 +1,22 @@ +// !LANGUAGE: +NewInference +// !WITH_NEW_INFERENCE +// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER + +fun takeArray(array: Array) {} + +fun test() { + "foo bar".split([""]) + unresolved([""]) + takeArray([""]) + val v = [""] + [""] + [1, 2, 3].size +} + +fun baz(arg: Array = []) { + if (true) ["yes"] else {["no"]} +} + +class Foo( + val v: Array = [] +) diff --git a/compiler/testData/diagnostics/tests/collectionLiterals/collectionLiteralsOutsideOfAnnotations.txt b/compiler/testData/diagnostics/tests/collectionLiterals/collectionLiteralsOutsideOfAnnotations.txt new file mode 100644 index 00000000000..d95571c703f --- /dev/null +++ b/compiler/testData/diagnostics/tests/collectionLiterals/collectionLiteralsOutsideOfAnnotations.txt @@ -0,0 +1,13 @@ +package + +public fun baz(/*0*/ arg: kotlin.Array = ...): kotlin.Unit +public fun takeArray(/*0*/ array: kotlin.Array): kotlin.Unit +public fun test(): kotlin.Unit + +public final class Foo { + public constructor Foo(/*0*/ v: kotlin.Array = ...) + public final val v: kotlin.Array + 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/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 2091357c9b6..899d43083eb 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -3437,6 +3437,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { runTest("compiler/testData/diagnostics/tests/collectionLiterals/collectionLiteralsAsPrimitiveArrays.kt"); } + @TestMetadata("collectionLiteralsOutsideOfAnnotations.kt") + public void testCollectionLiteralsOutsideOfAnnotations() throws Exception { + runTest("compiler/testData/diagnostics/tests/collectionLiterals/collectionLiteralsOutsideOfAnnotations.kt"); + } + @TestMetadata("collectionLiteralsWithVarargs.kt") public void testCollectionLiteralsWithVarargs() throws Exception { runTest("compiler/testData/diagnostics/tests/collectionLiterals/collectionLiteralsWithVarargs.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index 435510097fb..be712a8a42d 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -3432,6 +3432,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/collectionLiterals/collectionLiteralsAsPrimitiveArrays.kt"); } + @TestMetadata("collectionLiteralsOutsideOfAnnotations.kt") + public void testCollectionLiteralsOutsideOfAnnotations() throws Exception { + runTest("compiler/testData/diagnostics/tests/collectionLiterals/collectionLiteralsOutsideOfAnnotations.kt"); + } + @TestMetadata("collectionLiteralsWithVarargs.kt") public void testCollectionLiteralsWithVarargs() throws Exception { runTest("compiler/testData/diagnostics/tests/collectionLiterals/collectionLiteralsWithVarargs.kt"); diff --git a/plugins/uast-kotlin/testData/InferenceInsideUnresolvedConstructor.types.txt b/plugins/uast-kotlin/testData/InferenceInsideUnresolvedConstructor.types.txt index 877b1d12428..dce06a46fef 100644 --- a/plugins/uast-kotlin/testData/InferenceInsideUnresolvedConstructor.types.txt +++ b/plugins/uast-kotlin/testData/InferenceInsideUnresolvedConstructor.types.txt @@ -6,9 +6,9 @@ UFile (package = ) [public final class InferenceInsideUnresolvedConstructorKt {. UCallExpression (kind = UastCallKind(name='method_call'), argCount = 1)) [(id(42))] UIdentifier (Identifier (Unresolved)) [UIdentifier (Identifier (Unresolved))] USimpleNameReferenceExpression (identifier = , resolvesTo = null) [] - UCallExpression (kind = UastCallKind(name='method_call'), argCount = 1)) [id(42)] : PsiType: + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 1)) [id(42)] : PsiType:int UIdentifier (Identifier (id)) [UIdentifier (Identifier (id))] - USimpleNameReferenceExpression (identifier = id, resolvesTo = null) [id] : PsiType: + USimpleNameReferenceExpression (identifier = id, resolvesTo = null) [id] : PsiType:int ULiteralExpression (value = 42) [42] : PsiType:int UMethod (name = id) [public static final fun id(@org.jetbrains.annotations.Nullable x: T) : T {...}] UParameter (name = x) [@org.jetbrains.annotations.Nullable var x: T] diff --git a/plugins/uast-kotlin/testData/InnerNonFixedTypeVariable.types.txt b/plugins/uast-kotlin/testData/InnerNonFixedTypeVariable.types.txt index 20f2deb7508..754530b1c46 100644 --- a/plugins/uast-kotlin/testData/InnerNonFixedTypeVariable.types.txt +++ b/plugins/uast-kotlin/testData/InnerNonFixedTypeVariable.types.txt @@ -13,9 +13,9 @@ UFile (package = ) [public final class InnerNonFixedTypeVariableKt {...] UCallExpression (kind = UastCallKind(name='method_call'), argCount = 2)) [(mutableSetOf(), { ...})] : PsiType: UIdentifier (Identifier (mapTo)) [UIdentifier (Identifier (mapTo))] USimpleNameReferenceExpression (identifier = , resolvesTo = null) [] : PsiType: - UCallExpression (kind = UastCallKind(name='method_call'), argCount = 0)) [mutableSetOf()] : PsiType: + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 0)) [mutableSetOf()] : PsiType:Set UIdentifier (Identifier (mutableSetOf)) [UIdentifier (Identifier (mutableSetOf))] - USimpleNameReferenceExpression (identifier = mutableSetOf, resolvesTo = null) [mutableSetOf] : PsiType: + USimpleNameReferenceExpression (identifier = mutableSetOf, resolvesTo = null) [mutableSetOf] : PsiType:Set ULambdaExpression [{ ...}] : PsiType:Function0 UBlockExpression [{...}] UClass (name = Some) [public final class Some {...}]