From 4bf7151e473cf3cf78c89379943b895b936703e9 Mon Sep 17 00:00:00 2001 From: Wojciech Lopata Date: Wed, 29 May 2013 13:52:27 +0200 Subject: [PATCH] Make "type mismatch" quickfixes not available in case of error types --- .../plugin/quickfix/ChangeFunctionReturnTypeFix.java | 7 +++++++ .../jet/plugin/quickfix/ChangeVariableTypeFix.java | 9 +++++++++ ...eforeDontChangeOverriddenPropertyTypeToErrorType.kt | 10 ++++++++++ .../beforeDontChangeFunctionReturnTypeToErrorType.kt | 6 ++++++ .../jet/plugin/quickfix/QuickFixTestGenerated.java | 10 ++++++++++ 5 files changed, 42 insertions(+) create mode 100644 idea/testData/quickfix/typeMismatch/beforeDontChangeOverriddenPropertyTypeToErrorType.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeDontChangeFunctionReturnTypeToErrorType.kt diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java index c9a974790aa..cf22d69bf9c 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java @@ -22,6 +22,7 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Pair; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiErrorElement; +import com.intellij.psi.PsiFile; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; @@ -38,6 +39,7 @@ import org.jetbrains.jet.lang.resolve.DescriptorResolver; import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall; import org.jetbrains.jet.lang.resolve.name.FqName; import org.jetbrains.jet.lang.resolve.name.Name; +import org.jetbrains.jet.lang.types.ErrorUtils; import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.checker.JetTypeChecker; import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; @@ -96,6 +98,11 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction return JetBundle.message("change.type.family"); } + @Override + public boolean isAvailable(@NotNull Project project, @Nullable Editor editor, @Nullable PsiFile file) { + return super.isAvailable(project, editor, file) && !ErrorUtils.containsErrorType(type); + } + @Override public void invoke(@NotNull Project project, @Nullable Editor editor, @Nullable JetFile file) throws IncorrectOperationException { if (changeFunctionLiteralReturnTypeFix != null) { diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java index c80f8d356dd..bcb408900ba 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java @@ -21,6 +21,7 @@ import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Pair; import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -32,6 +33,7 @@ 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.resolve.name.FqName; +import org.jetbrains.jet.lang.types.ErrorUtils; import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.checker.JetTypeChecker; import org.jetbrains.jet.plugin.JetBundle; @@ -45,10 +47,12 @@ import java.util.List; public class ChangeVariableTypeFix extends JetIntentionAction { private final String renderedType; + private final JetType type; public ChangeVariableTypeFix(@NotNull JetVariableDeclaration element, @NotNull JetType type) { super(element); renderedType = DescriptorRenderer.SHORT_NAMES_IN_TYPES.renderType(type); + this.type = type; } @NotNull @@ -67,6 +71,11 @@ public class ChangeVariableTypeFix extends JetIntentionAction [ERROR : Ay]'" "false" +// ACTION: Change 'A.x' type to '(Int) -> Int' +// ERROR: Return type is '(jet.Int) → jet.Int', which is not a subtype of overridden
internal abstract val x: (jet.String) → [ERROR : Ay] defined in A +// ERROR: Unresolved reference: Ay +trait A { + val x: (String) -> Ay +} +trait B : A { + override val x: (Int) -> Int +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeDontChangeFunctionReturnTypeToErrorType.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeDontChangeFunctionReturnTypeToErrorType.kt new file mode 100644 index 00000000000..ff1d6c75cf7 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeDontChangeFunctionReturnTypeToErrorType.kt @@ -0,0 +1,6 @@ +// "Change 'foo' function return type to '([ERROR : NoSuchType]) -> Int'" "false" +// ERROR: Type mismatch.
Required:jet.Int
Found:([ERROR : NoSuchType]) → jet.Int
+// ERROR: Unresolved reference: NoSuchType +fun foo(): Int { + return { (x: NoSuchType) -> 42 } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java index fe3b3c67a02..f4226698736 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -1306,6 +1306,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest("idea/testData/quickfix/typeMismatch/beforeCompareToTypeMismatch.kt"); } + @TestMetadata("beforeDontChangeOverriddenPropertyTypeToErrorType.kt") + public void testDontChangeOverriddenPropertyTypeToErrorType() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/beforeDontChangeOverriddenPropertyTypeToErrorType.kt"); + } + @TestMetadata("beforeExpectedParameterTypeMismatch.kt") public void testExpectedParameterTypeMismatch() throws Exception { doTest("idea/testData/quickfix/typeMismatch/beforeExpectedParameterTypeMismatch.kt"); @@ -1514,6 +1519,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionReturnTypeToMatchReturnTypeOfReturnedLiteral.kt"); } + @TestMetadata("beforeDontChangeFunctionReturnTypeToErrorType.kt") + public void testDontChangeFunctionReturnTypeToErrorType() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeDontChangeFunctionReturnTypeToErrorType.kt"); + } + @TestMetadata("beforeExpectedTypeMismatch.kt") public void testExpectedTypeMismatch() throws Exception { doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeExpectedTypeMismatch.kt");