Merge pull request #244 from lopekpl/type_mismatch3

QuickFix for EXPECTED_PARAMETER/RETURN_TYPE_MISMATCH
This commit is contained in:
Zalim Bashorov
2013-04-17 07:35:08 -07:00
10 changed files with 137 additions and 4 deletions
@@ -215,7 +215,7 @@ public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
if (typeReference != null) {
type = context.expressionTypingServices.getTypeResolver().resolveType(context.scope, typeReference, context.trace, true);
if (expectedType != null) {
if (!JetTypeChecker.INSTANCE.isSubtypeOf(type, expectedType)) {
if (!JetTypeChecker.INSTANCE.isSubtypeOf(expectedType, type)) {
context.trace.report(EXPECTED_PARAMETER_TYPE_MISMATCH.on(declaredParameter, expectedType));
}
}
@@ -280,7 +280,7 @@ public class ClosureExpressionsTypingVisitor extends ExpressionTypingVisitor {
context.expressionTypingServices.checkFunctionReturnType(expression.getFunctionLiteral(), context.replaceScope(functionInnerScope).
replaceExpectedType(returnType).replaceBindingTrace(temporaryTrace), temporaryTrace);
if (expectedReturnType != null) {
if (!JetTypeChecker.INSTANCE.isSubtypeOf(expectedReturnType, returnType)) {
if (!JetTypeChecker.INSTANCE.isSubtypeOf(returnType, expectedReturnType)) {
temporaryTrace.report(EXPECTED_RETURN_TYPE_MISMATCH.on(returnTypeRef, expectedReturnType));
}
}
@@ -8,12 +8,12 @@ fun foo(f: (Trait) -> Trait) = f
fun test(s: Sub) {
foo {
(<!EXPECTED_PARAMETER_TYPE_MISMATCH!>t: Super<!>): <!EXPECTED_RETURN_TYPE_MISMATCH!>Sub<!> -> s
(t: Super): Sub -> s
}
foo {
(t: Trait): Trait -> s
}
foo {
(t: Sub): Super -> s
(<!EXPECTED_PARAMETER_TYPE_MISMATCH!>t: Sub<!>): <!EXPECTED_RETURN_TYPE_MISMATCH!>Super<!> -> s
}
}
@@ -60,6 +60,7 @@ add.semicolon.family=Add Semicolon
change.function.return.type=Change ''{0}'' function return type to ''{1}''
remove.function.return.type=Remove explicitly specified return type in ''{0}'' function
change.element.type=Change ''{0}'' type to ''{1}''
change.type=Change type from ''{0}'' to ''{1}''
change.type.family=Change Type
add.kotlin.signature.action.family.name=Specify Custom Kotlin Signature
@@ -0,0 +1,89 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.plugin.quickfix;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.diagnostics.DiagnosticWithParameters1;
import org.jetbrains.jet.lang.diagnostics.Errors;
import org.jetbrains.jet.lang.psi.JetParameter;
import org.jetbrains.jet.lang.psi.JetPsiFactory;
import org.jetbrains.jet.lang.psi.JetTypeReference;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.plugin.JetBundle;
public class ChangeTypeFix extends JetIntentionAction<JetTypeReference> {
private final JetType type;
public ChangeTypeFix(@NotNull JetTypeReference element, JetType type) {
super(element);
this.type = type;
}
@NotNull
@Override
public String getText() {
return JetBundle.message("change.type", element.getText(), type);
}
@NotNull
@Override
public String getFamilyName() {
return JetBundle.message("change.type.family");
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
element.replace(JetPsiFactory.createType(project, type.toString()));
}
@NotNull
public static JetIntentionActionFactory createFactoryForExpectedParameterTypeMismatch() {
return new JetIntentionActionFactory() {
@Nullable
@Override
public IntentionAction createAction(Diagnostic diagnostic) {
assert diagnostic.getFactory() == Errors.EXPECTED_PARAMETER_TYPE_MISMATCH;
@SuppressWarnings("unchecked")
DiagnosticWithParameters1<JetParameter, JetType> diagnosticWithParameters = (DiagnosticWithParameters1<JetParameter, JetType>) diagnostic;
JetTypeReference typeReference = diagnosticWithParameters.getPsiElement().getTypeReference();
assert typeReference != null : "EXPECTED_PARAMETER_TYPE_MISMATCH reported on parameter without explicitly declared type";
return new ChangeTypeFix(typeReference, diagnosticWithParameters.getA());
}
};
}
@NotNull
public static JetIntentionActionFactory createFactoryForExpectedReturnTypeMismatch() {
return new JetIntentionActionFactory() {
@Nullable
@Override
public IntentionAction createAction(Diagnostic diagnostic) {
assert diagnostic.getFactory() == Errors.EXPECTED_RETURN_TYPE_MISMATCH;
@SuppressWarnings("unchecked")
DiagnosticWithParameters1<JetTypeReference, JetType> diagnosticWithParameters = (DiagnosticWithParameters1<JetTypeReference, JetType>) diagnostic;
return new ChangeTypeFix(diagnosticWithParameters.getPsiElement(), diagnosticWithParameters.getA());
}
};
}
}
@@ -220,6 +220,9 @@ public class QuickFixes {
factories.put(HAS_NEXT_FUNCTION_TYPE_MISMATCH, ChangeFunctionReturnTypeFix.createFactoryForHasNextFunctionTypeMismatch());
factories.put(COMPARE_TO_TYPE_MISMATCH, ChangeFunctionReturnTypeFix.createFactoryForCompareToTypeMismatch());
factories.put(EXPECTED_PARAMETER_TYPE_MISMATCH, ChangeTypeFix.createFactoryForExpectedParameterTypeMismatch());
factories.put(EXPECTED_RETURN_TYPE_MISMATCH, ChangeTypeFix.createFactoryForExpectedReturnTypeMismatch());
factories.put(PLATFORM_CLASS_MAPPED_TO_KOTLIN, MapPlatformClassToKotlinFix.createFactory());
factories.put(MANY_CLASSES_IN_SUPERTYPE_LIST, RemoveSupertypeFix.createFactory());
@@ -0,0 +1,6 @@
// "Change type from 'String' to 'Int'" "true"
fun foo(f: (Int) -> String) {
foo {
(x: Int) -> ""
}
}
@@ -0,0 +1,9 @@
// "Change type from 'A' to 'B'" "true"
trait A {}
trait B : A {}
fun foo(f: (B) -> B) {
foo {
(b: B): B -> b
}
}
@@ -0,0 +1,6 @@
// "Change type from 'String' to 'Int'" "true"
fun foo(f: (Int) -> String) {
foo {
(x: String<caret>) -> ""
}
}
@@ -0,0 +1,9 @@
// "Change type from 'A' to 'B'" "true"
trait A {}
trait B : A {}
fun foo(f: (B) -> B) {
foo {
(b: B): A<caret> -> b
}
}
@@ -1033,6 +1033,16 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest("idea/testData/quickfix/typeMismatch/beforeComponentFunctionReturnTypeMismatch5.kt");
}
@TestMetadata("beforeExpectedParameterTypeMismatch.kt")
public void testExpectedParameterTypeMismatch() throws Exception {
doTest("idea/testData/quickfix/typeMismatch/beforeExpectedParameterTypeMismatch.kt");
}
@TestMetadata("beforeExpectedReturnTypeMismatch.kt")
public void testExpectedReturnTypeMismatch() throws Exception {
doTest("idea/testData/quickfix/typeMismatch/beforeExpectedReturnTypeMismatch.kt");
}
@TestMetadata("beforeHasNextFunctionReturnTypeMismatch.kt")
public void testHasNextFunctionReturnTypeMismatch() throws Exception {
doTest("idea/testData/quickfix/typeMismatch/beforeHasNextFunctionReturnTypeMismatch.kt");