Fix invalid usage of descriptorToDeclaration in QuickFixUtil

This commit is contained in:
Pavel V. Talanov
2014-06-23 19:34:04 +04:00
parent 41dd12d46b
commit e3c170183e
4 changed files with 33 additions and 10 deletions
@@ -26,7 +26,6 @@ import org.jetbrains.jet.lang.diagnostics.DiagnosticWithParameters2;
import org.jetbrains.jet.lang.diagnostics.Errors;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.plugin.caches.resolve.ResolvePackage;
@@ -34,6 +33,7 @@ import org.jetbrains.jet.plugin.caches.resolve.ResolvePackage;
import java.util.LinkedList;
import java.util.List;
//TODO: should use change signature to deal with cases of multiple overridden descriptors
public class QuickFixFactoryForTypeMismatchError implements JetIntentionActionsFactory {
@NotNull
@Override
@@ -132,14 +132,9 @@ public class QuickFixFactoryForTypeMismatchError implements JetIntentionActionsF
@Nullable
private static JetFunction getFunctionDeclaration(@NotNull BindingContext context, @NotNull ResolvedCall<?> resolvedCall) {
List<PsiElement> declarations = BindingContextUtils.descriptorToDeclarations(context, resolvedCall.getResultingDescriptor());
//do not create fix if descriptor has more than one overridden declarations
//TODO: use change signature to deal with this case correctly
if (declarations.size() == 1) {
PsiElement result = declarations.iterator().next();
if (result instanceof JetFunction) {
return (JetFunction) result;
}
PsiElement result = QuickFixUtil.safeGetDeclaration(context, resolvedCall);
if (result instanceof JetFunction) {
return (JetFunction) result;
}
return null;
}
@@ -109,7 +109,7 @@ public class QuickFixUtil {
BindingContext context = ResolvePackage.getBindingContext(callExpression.getContainingJetFile());
ResolvedCall<?> resolvedCall = context.get(BindingContext.RESOLVED_CALL, callExpression.getCalleeExpression());
if (resolvedCall == null) return null;
PsiElement declaration = BindingContextUtils.descriptorToDeclaration(context, resolvedCall.getCandidateDescriptor());
PsiElement declaration = safeGetDeclaration(context, resolvedCall);
if (declaration instanceof JetFunction) {
return ((JetFunction) declaration).getValueParameterList();
}
@@ -119,6 +119,16 @@ public class QuickFixUtil {
return null;
}
@Nullable
public static PsiElement safeGetDeclaration(@NotNull BindingContext context, @NotNull ResolvedCall<?> resolvedCall) {
List<PsiElement> declarations = BindingContextUtils.descriptorToDeclarations(context, resolvedCall.getResultingDescriptor());
//do not create fix if descriptor has more than one overridden declaration
if (declarations.size() == 1) {
return declarations.iterator().next();
}
return null;
}
@Nullable
public static JetParameter getParameterCorrespondingToFunctionLiteralPassedOutsideArgumentList(@NotNull JetFunctionLiteralExpression functionLiteralExpression) {
if (!(functionLiteralExpression.getParent() instanceof JetCallExpression)) {
@@ -0,0 +1,13 @@
// "class org.jetbrains.jet.plugin.quickfix.ChangeParameterTypeFix" "false"
// ERROR: <html>Type mismatch.<table><tr><td>Required:</td><td>kotlin.Int</td></tr><tr><td>Found:</td><td>kotlin.String</td></tr></table></html>
trait A {
fun f(i: Int): Boolean
}
open class AA {
fun f(i: Int) = true
}
class AAA: AA(), A
val c = AAA().f("<caret>")
@@ -2263,6 +2263,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest("idea/testData/quickfix/typeMismatch/parameterTypeMismatch/beforeChangePrimaryConstructorParameterType.kt");
}
@TestMetadata("beforeMultiFakeOverride.kt")
public void testMultiFakeOverride() throws Exception {
doTest("idea/testData/quickfix/typeMismatch/parameterTypeMismatch/beforeMultiFakeOverride.kt");
}
}
@TestMetadata("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression")