Improve diagnostic for unresolved reference when function expected
#KT-10657 Fixed
This commit is contained in:
+46
-2
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2015 JetBrains s.r.o.
|
* Copyright 2010-2017 JetBrains s.r.o.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -16,18 +16,27 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.resolve.calls.tasks;
|
package org.jetbrains.kotlin.resolve.calls.tasks;
|
||||||
|
|
||||||
|
import kotlin.collections.CollectionsKt;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.jetbrains.kotlin.builtins.FunctionTypesKt;
|
||||||
import org.jetbrains.kotlin.descriptors.CallableDescriptor;
|
import org.jetbrains.kotlin.descriptors.CallableDescriptor;
|
||||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
|
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
|
||||||
|
import org.jetbrains.kotlin.descriptors.FunctionDescriptor;
|
||||||
|
import org.jetbrains.kotlin.descriptors.VariableDescriptor;
|
||||||
|
import org.jetbrains.kotlin.diagnostics.Errors;
|
||||||
import org.jetbrains.kotlin.psi.Call;
|
import org.jetbrains.kotlin.psi.Call;
|
||||||
import org.jetbrains.kotlin.psi.KtReferenceExpression;
|
import org.jetbrains.kotlin.psi.KtReferenceExpression;
|
||||||
import org.jetbrains.kotlin.resolve.BindingTrace;
|
import org.jetbrains.kotlin.resolve.BindingTrace;
|
||||||
|
import org.jetbrains.kotlin.resolve.calls.callResolverUtil.CallResolverUtilKt;
|
||||||
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
|
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
|
||||||
import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall;
|
import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall;
|
||||||
import org.jetbrains.kotlin.resolve.calls.util.FakeCallableDescriptorForObject;
|
import org.jetbrains.kotlin.resolve.calls.util.FakeCallableDescriptorForObject;
|
||||||
import org.jetbrains.kotlin.types.ErrorUtils;
|
import org.jetbrains.kotlin.types.ErrorUtils;
|
||||||
|
import org.jetbrains.kotlin.types.KotlinType;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import static org.jetbrains.kotlin.diagnostics.Errors.UNRESOLVED_REFERENCE;
|
import static org.jetbrains.kotlin.diagnostics.Errors.UNRESOLVED_REFERENCE;
|
||||||
import static org.jetbrains.kotlin.diagnostics.Errors.UNRESOLVED_REFERENCE_WRONG_RECEIVER;
|
import static org.jetbrains.kotlin.diagnostics.Errors.UNRESOLVED_REFERENCE_WRONG_RECEIVER;
|
||||||
@@ -82,6 +91,41 @@ public class TracingStrategyImpl extends AbstractTracingStrategy {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <D extends CallableDescriptor> void unresolvedReferenceWrongReceiver(@NotNull BindingTrace trace, @NotNull Collection<? extends ResolvedCall<D>> candidates) {
|
public <D extends CallableDescriptor> void unresolvedReferenceWrongReceiver(@NotNull BindingTrace trace, @NotNull Collection<? extends ResolvedCall<D>> candidates) {
|
||||||
trace.report(UNRESOLVED_REFERENCE_WRONG_RECEIVER.on(reference, candidates));
|
VariableDescriptor variableDescriptor = isFunctionExpectedError(candidates);
|
||||||
|
if (variableDescriptor != null) {
|
||||||
|
trace.report(Errors.FUNCTION_EXPECTED.on(reference, reference, variableDescriptor.getType()));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
trace.report(UNRESOLVED_REFERENCE_WRONG_RECEIVER.on(reference, candidates));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private static <D extends CallableDescriptor> VariableDescriptor isFunctionExpectedError(
|
||||||
|
@NotNull Collection<? extends ResolvedCall<D>> candidates
|
||||||
|
) {
|
||||||
|
List<VariableDescriptor> variables = CollectionsKt.map(candidates, TracingStrategyImpl::variableIfFunctionExpectedError);
|
||||||
|
List<VariableDescriptor> distinctVariables = CollectionsKt.distinct(variables);
|
||||||
|
return CollectionsKt.singleOrNull(distinctVariables);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private static <D extends CallableDescriptor> VariableDescriptor variableIfFunctionExpectedError(
|
||||||
|
@NotNull ResolvedCall<D> candidate
|
||||||
|
) {
|
||||||
|
if (!(candidate instanceof VariableAsFunctionResolvedCall)) return null;
|
||||||
|
|
||||||
|
ResolvedCall<VariableDescriptor> variableCall = ((VariableAsFunctionResolvedCall) candidate).getVariableCall();
|
||||||
|
ResolvedCall<FunctionDescriptor> functionCall = ((VariableAsFunctionResolvedCall) candidate).getFunctionCall();
|
||||||
|
|
||||||
|
KotlinType type = variableCall.getCandidateDescriptor().getType();
|
||||||
|
|
||||||
|
boolean nonFunctionalVar = variableCall.getStatus().isSuccess() && !FunctionTypesKt.isFunctionType(type);
|
||||||
|
Call functionPsiCall = functionCall.getCall();
|
||||||
|
if (nonFunctionalVar && CallResolverUtilKt.isInvokeCallOnVariable(functionPsiCall) && functionPsiCall.getValueArguments().isEmpty()) {
|
||||||
|
return variableCall.getCandidateDescriptor();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Vendored
+13
@@ -0,0 +1,13 @@
|
|||||||
|
// !DIAGNOSTICS: -UNUSED_PARAMETER
|
||||||
|
|
||||||
|
fun Int.invoke(a: Int) {}
|
||||||
|
fun Int.invoke(a: Int, b: Int) {}
|
||||||
|
|
||||||
|
class SomeClass
|
||||||
|
|
||||||
|
fun test(identifier: SomeClass, fn: String.() -> Unit) {
|
||||||
|
<!FUNCTION_EXPECTED, DEBUG_INFO_MISSING_UNRESOLVED!>identifier<!>()
|
||||||
|
<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>identifier<!>(123)
|
||||||
|
<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>identifier<!>(1, 2)
|
||||||
|
1.<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>fn<!>()
|
||||||
|
}
|
||||||
Vendored
+12
@@ -0,0 +1,12 @@
|
|||||||
|
package
|
||||||
|
|
||||||
|
public fun test(/*0*/ identifier: SomeClass, /*1*/ fn: kotlin.String.() -> kotlin.Unit): kotlin.Unit
|
||||||
|
public fun kotlin.Int.invoke(/*0*/ a: kotlin.Int): kotlin.Unit
|
||||||
|
public fun kotlin.Int.invoke(/*0*/ a: kotlin.Int, /*1*/ b: kotlin.Int): kotlin.Unit
|
||||||
|
|
||||||
|
public final class SomeClass {
|
||||||
|
public constructor SomeClass()
|
||||||
|
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
|
||||||
|
}
|
||||||
Vendored
+12
@@ -0,0 +1,12 @@
|
|||||||
|
// !DIAGNOSTICS: -UNUSED_PARAMETER
|
||||||
|
|
||||||
|
fun Int.invoke() {}
|
||||||
|
|
||||||
|
class SomeClass
|
||||||
|
|
||||||
|
fun test(identifier: SomeClass, fn: String.() -> Unit) {
|
||||||
|
<!FUNCTION_EXPECTED, DEBUG_INFO_MISSING_UNRESOLVED!>identifier<!>()
|
||||||
|
<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>identifier<!>(123)
|
||||||
|
<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>identifier<!>(1, 2)
|
||||||
|
1.<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>fn<!>()
|
||||||
|
}
|
||||||
Vendored
+11
@@ -0,0 +1,11 @@
|
|||||||
|
package
|
||||||
|
|
||||||
|
public fun test(/*0*/ identifier: SomeClass, /*1*/ fn: kotlin.String.() -> kotlin.Unit): kotlin.Unit
|
||||||
|
public fun kotlin.Int.invoke(): kotlin.Unit
|
||||||
|
|
||||||
|
public final class SomeClass {
|
||||||
|
public constructor SomeClass()
|
||||||
|
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
|
||||||
|
}
|
||||||
@@ -17607,6 +17607,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("functionExpectedWhenSeveralInvokesExist.kt")
|
||||||
|
public void testFunctionExpectedWhenSeveralInvokesExist() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/resolve/invoke/functionExpectedWhenSeveralInvokesExist.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("implicitInvoke.kt")
|
@TestMetadata("implicitInvoke.kt")
|
||||||
public void testImplicitInvoke() throws Exception {
|
public void testImplicitInvoke() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/resolve/invoke/implicitInvoke.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/resolve/invoke/implicitInvoke.kt");
|
||||||
@@ -17697,6 +17703,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("reportFunctionExpectedWhenOneInvokeExist.kt")
|
||||||
|
public void testReportFunctionExpectedWhenOneInvokeExist() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/resolve/invoke/reportFunctionExpectedWhenOneInvokeExist.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("valNamedInvoke.kt")
|
@TestMetadata("valNamedInvoke.kt")
|
||||||
public void testValNamedInvoke() throws Exception {
|
public void testValNamedInvoke() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/resolve/invoke/valNamedInvoke.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/resolve/invoke/valNamedInvoke.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user