From ed6e734c14d2ecd2431da482cd801dd7aa9ec2d4 Mon Sep 17 00:00:00 2001 From: Wojciech Lopata Date: Thu, 9 May 2013 22:07:31 +0200 Subject: [PATCH] Single factory for TYPE_MISMATCH error --- .../plugin/quickfix/CastExpressionFix.java | 29 ------- .../ChangeFunctionParameterTypeFix.java | 39 --------- .../quickfix/ChangeFunctionReturnTypeFix.java | 20 ----- .../QuickFixFactoryForTypeMismatchError.java | 84 +++++++++++++++++++ .../jet/plugin/quickfix/QuickFixUtil.java | 15 ++++ .../jet/plugin/quickfix/QuickFixes.java | 4 +- ...ypeWithoutChangingFunctionParameterType.kt | 7 ++ ...nLiteralTypeWithoutChangingPropertyType.kt | 7 ++ ...nTypeToMatchReturnTypeOfReturnedLiteral.kt | 4 + ...essionTypeMismatchFunctionParameterType.kt | 6 ++ ...MismatchInIfStatementReturnedByFunction.kt | 7 ++ ...eMismatchInIfStatementReturnedByLiteral.kt | 12 +++ ...ypeWithoutChangingFunctionParameterType.kt | 7 ++ ...nLiteralTypeWithoutChangingPropertyType.kt | 7 ++ ...nTypeToMatchReturnTypeOfReturnedLiteral.kt | 4 + ...CantEvaluateToExpresionThatTypeMismatch.kt | 6 ++ ...essionTypeMismatchFunctionParameterType.kt | 6 ++ ...MismatchInIfStatementReturnedByFunction.kt | 7 ++ ...eMismatchInIfStatementReturnedByLiteral.kt | 12 +++ .../quickfix/QuickFixTestGenerated.java | 35 ++++++++ 20 files changed, 227 insertions(+), 91 deletions(-) create mode 100644 idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionLiteralTypeWithoutChangingFunctionParameterType.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionLiteralTypeWithoutChangingPropertyType.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionReturnTypeToMatchReturnTypeOfReturnedLiteral.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterReturnedExpressionTypeMismatchFunctionParameterType.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterTypeMismatchInIfStatementReturnedByFunction.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterTypeMismatchInIfStatementReturnedByLiteral.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionLiteralTypeWithoutChangingFunctionParameterType.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionLiteralTypeWithoutChangingPropertyType.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionReturnTypeToMatchReturnTypeOfReturnedLiteral.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeReturnedExpresionCantEvaluateToExpresionThatTypeMismatch.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeReturnedExpressionTypeMismatchFunctionParameterType.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInIfStatementReturnedByFunction.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInIfStatementReturnedByLiteral.kt diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/CastExpressionFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/CastExpressionFix.java index 6cbf68bf8a9..d58f92e6ebd 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/CastExpressionFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/CastExpressionFix.java @@ -20,7 +20,6 @@ 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.psi.util.PsiTreeUtil; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -94,32 +93,4 @@ public class CastExpressionFix extends JetIntentionAction { } }; } - - @NotNull - public static JetSingleIntentionActionFactory createFactoryForTypeMismatch() { - return new JetSingleIntentionActionFactory() { - @Nullable - @Override - public IntentionAction createAction(Diagnostic diagnostic) { - assert diagnostic.getFactory() == Errors.TYPE_MISMATCH; - @SuppressWarnings("unchecked") - DiagnosticWithParameters2 diagnosticWithParameters = - (DiagnosticWithParameters2) diagnostic; - JetExpression expression = diagnosticWithParameters.getPsiElement(); - - // we don't want to cast a cast: - if (expression instanceof JetBinaryExpressionWithTypeRHS) { - return null; - } - - // 'x: Int' - TYPE_MISMATCH might be reported on 'x', and we don't want this quickfix to be available: - JetBinaryExpressionWithTypeRHS parentExpressionWithTypeRHS = - PsiTreeUtil.getParentOfType(expression, JetBinaryExpressionWithTypeRHS.class, true); - if (parentExpressionWithTypeRHS != null && parentExpressionWithTypeRHS.getLeft() == expression) { - return null; - } - return new CastExpressionFix(expression, diagnosticWithParameters.getA()); - } - }; - } } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionParameterTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionParameterTypeFix.java index 3c7d2b2ba67..3375089e6f7 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionParameterTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionParameterTypeFix.java @@ -16,21 +16,16 @@ 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.psi.util.PsiTreeUtil; 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.psi.*; -import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.name.FqName; import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.plugin.JetBundle; -import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; import org.jetbrains.jet.renderer.DescriptorRenderer; public class ChangeFunctionParameterTypeFix extends JetIntentionAction { @@ -68,38 +63,4 @@ public class ChangeFunctionParameterTypeFix extends JetIntentionAction } }; } - - @NotNull - public static JetSingleIntentionActionFactory createFactoryForTypeMismatch() { - return new JetSingleIntentionActionFactory() { - @Nullable - @Override - public IntentionAction createAction(Diagnostic diagnostic) { - JetExpression expression = (JetExpression) diagnostic.getPsiElement(); - JetNamedFunction function = QuickFixUtil.getParentElementOfType(diagnostic, JetNamedFunction.class); - - if (function != null && (function.getInitializer() == expression || expression.getParent() instanceof JetReturnExpression)) { - BindingContext context = AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) diagnostic.getPsiFile()).getBindingContext(); - JetType type = context.get(BindingContext.EXPRESSION_TYPE, expression); - assert type != null : "Expression type mismatch, but expression has no type"; - return new ChangeFunctionReturnTypeFix(function, type); - } - return null; - } - }; - } } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java new file mode 100644 index 00000000000..1f0ae66d09b --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java @@ -0,0 +1,84 @@ +/* + * 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.psi.util.PsiTreeUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.diagnostics.Diagnostic; +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.types.JetType; +import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; + +import java.util.LinkedList; +import java.util.List; + +public class QuickFixFactoryForTypeMismatchError implements JetIntentionActionsFactory { + @NotNull + @Override + public List createActions(Diagnostic diagnostic) { + List actions = new LinkedList(); + + assert diagnostic.getFactory() == Errors.TYPE_MISMATCH; + @SuppressWarnings("unchecked") + DiagnosticWithParameters2 diagnosticWithParameters = + (DiagnosticWithParameters2) diagnostic; + JetExpression expression = diagnosticWithParameters.getPsiElement(); + JetType expectedType = diagnosticWithParameters.getA(); + JetType expressionType = diagnosticWithParameters.getB(); + BindingContext context = AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) diagnostic.getPsiFile()).getBindingContext(); + + // We don't want to cast a cast or type-asserted expression: + if (!(expression instanceof JetBinaryExpressionWithTypeRHS) && !(expression.getParent() instanceof JetBinaryExpressionWithTypeRHS)) { + actions.add(new CastExpressionFix(expression, expectedType)); + } + + // Mismatch in returned expression: + JetFunction function = PsiTreeUtil.getParentOfType(expression, JetFunction.class, true); + if (function != null && QuickFixUtil.canFunctionReturnExpression(function, expression)) { + actions.add(new ChangeFunctionReturnTypeFix(function, expressionType)); + } + + // Change type of a function parameter in case TYPE_MISMATCH is reported on expression passed as value argument of call. + // 1) When an argument is a dangling function literal: + JetFunctionLiteralExpression functionLiteralExpression = + QuickFixUtil.getParentElementOfType(diagnostic, JetFunctionLiteralExpression.class); + if (functionLiteralExpression != null && functionLiteralExpression.getBodyExpression() == expression) { + JetParameter correspondingParameter = + QuickFixUtil.getFunctionParameterCorrespondingToFunctionLiteralPassedOutsideArgumentList(functionLiteralExpression); + JetType functionLiteralExpressionType = context.get(BindingContext.EXPRESSION_TYPE, functionLiteralExpression); + if (correspondingParameter != null && functionLiteralExpressionType != null) { + actions.add(new ChangeFunctionParameterTypeFix(correspondingParameter, functionLiteralExpressionType)); + } + } + // 2) When an argument is passed inside value argument list: + else { + JetValueArgument valueArgument = QuickFixUtil.getParentElementOfType(diagnostic, JetValueArgument.class); + if (valueArgument != null && valueArgument.getArgumentExpression() == expression) { + JetParameter correspondingParameter = QuickFixUtil.getFunctionParameterCorrespondingToValueArgumentPassedInCall(valueArgument); + JetType valueArgumentType = context.get(BindingContext.EXPRESSION_TYPE, valueArgument.getArgumentExpression()); + if (correspondingParameter != null && valueArgumentType != null) { + actions.add(new ChangeFunctionParameterTypeFix(correspondingParameter, valueArgumentType)); + } + } + } + return actions; + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java index beb270a8724..a7217ba7006 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java @@ -174,4 +174,19 @@ public class QuickFixUtil { } return true; } + + public static boolean canFunctionReturnExpression(@NotNull JetFunction function, @NotNull JetExpression expression) { + if (function instanceof JetFunctionLiteral) { + JetBlockExpression functionLiteralBody = ((JetFunctionLiteral) function).getBodyExpression(); + PsiElement returnedElement = functionLiteralBody == null ? null : functionLiteralBody.getLastChild(); + return returnedElement instanceof JetExpression && canEvaluateTo((JetExpression) returnedElement, expression); + } + else { + if (function instanceof JetWithExpressionInitializer && canEvaluateTo(((JetWithExpressionInitializer) function).getInitializer(), expression)) { + return true; + } + JetReturnExpression returnExpression = PsiTreeUtil.getParentOfType(expression, JetReturnExpression.class); + return returnExpression != null && canEvaluateTo(returnExpression.getReturnedExpression(), expression); + } + } } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java index 7a4ce23f253..ad2eb561f5a 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java @@ -226,7 +226,6 @@ public class QuickFixes { factories.put(COMPONENT_FUNCTION_RETURN_TYPE_MISMATCH, ChangeFunctionReturnTypeFix.createFactoryForComponentFunctionReturnTypeMismatch()); factories.put(HAS_NEXT_FUNCTION_TYPE_MISMATCH, ChangeFunctionReturnTypeFix.createFactoryForHasNextFunctionTypeMismatch()); factories.put(COMPARE_TO_TYPE_MISMATCH, ChangeFunctionReturnTypeFix.createFactoryForCompareToTypeMismatch()); - factories.put(TYPE_MISMATCH, ChangeFunctionReturnTypeFix.createFactoryForTypeMismatch()); factories.put(TOO_MANY_ARGUMENTS, ChangeFunctionSignatureFix.createFactory()); factories.put(NO_VALUE_FOR_PARAMETER, ChangeFunctionSignatureFix.createFactory()); @@ -240,10 +239,9 @@ public class QuickFixes { factories.put(EXPECTED_TYPE_MISMATCH, changeFunctionLiteralReturnTypeFix); factories.put(ASSIGNMENT_TYPE_MISMATCH, changeFunctionLiteralReturnTypeFix); - factories.put(TYPE_MISMATCH, ChangeFunctionParameterTypeFix.createFactory()); + factories.put(TYPE_MISMATCH, new QuickFixFactoryForTypeMismatchError()); factories.put(AUTOCAST_IMPOSSIBLE, CastExpressionFix.createFactoryForAutoCastImpossible()); - factories.put(TYPE_MISMATCH, CastExpressionFix.createFactoryForTypeMismatch()); factories.put(PLATFORM_CLASS_MAPPED_TO_KOTLIN, MapPlatformClassToKotlinFix.createFactory()); diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionLiteralTypeWithoutChangingFunctionParameterType.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionLiteralTypeWithoutChangingFunctionParameterType.kt new file mode 100644 index 00000000000..b2b6552068c --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionLiteralTypeWithoutChangingFunctionParameterType.kt @@ -0,0 +1,7 @@ +// "Change function literal return type to 'String'" "true" +fun foo(f: (String) -> Any) { + foo { + (s: String): String -> + s + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionLiteralTypeWithoutChangingPropertyType.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionLiteralTypeWithoutChangingPropertyType.kt new file mode 100644 index 00000000000..35be90c1bea --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionLiteralTypeWithoutChangingPropertyType.kt @@ -0,0 +1,7 @@ +// "Change function literal return type to 'String'" "true" +fun foo() { + val f: (String) -> String = { + (s: Any): String -> + "" + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionReturnTypeToMatchReturnTypeOfReturnedLiteral.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionReturnTypeToMatchReturnTypeOfReturnedLiteral.kt new file mode 100644 index 00000000000..5c7182093c5 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionReturnTypeToMatchReturnTypeOfReturnedLiteral.kt @@ -0,0 +1,4 @@ +// "Change 'foo' function return type to '() -> Any'" "true" +fun foo(x: Any): () -> Any { + return {x} +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterReturnedExpressionTypeMismatchFunctionParameterType.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterReturnedExpressionTypeMismatchFunctionParameterType.kt new file mode 100644 index 00000000000..16a4b270648 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterReturnedExpressionTypeMismatchFunctionParameterType.kt @@ -0,0 +1,6 @@ +// "Change parameter 'f' type of function 'foo' to '() -> String'" "true" +fun foo(f: () -> String) { + foo { + "" + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterTypeMismatchInIfStatementReturnedByFunction.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterTypeMismatchInIfStatementReturnedByFunction.kt new file mode 100644 index 00000000000..031dded208b --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterTypeMismatchInIfStatementReturnedByFunction.kt @@ -0,0 +1,7 @@ +// "Change 'boo' function return type to 'String'" "true" +fun boo(): String { + return ((if (true) { + val a = "" + a + } else "")) +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterTypeMismatchInIfStatementReturnedByLiteral.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterTypeMismatchInIfStatementReturnedByLiteral.kt new file mode 100644 index 00000000000..19f21d70c71 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterTypeMismatchInIfStatementReturnedByLiteral.kt @@ -0,0 +1,12 @@ +// "Change 'f' type to '(Int, Int) -> (String) -> Int'" "true" +fun foo() { + val f: (Int, Int) -> (String) -> Int = { + (a: Int, b: Int): (String) -> Int -> + val x = {(s: String) -> 42} + if (true) x + else if (true) x else { + var y = 42 + if (true) x else x + } + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionLiteralTypeWithoutChangingFunctionParameterType.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionLiteralTypeWithoutChangingFunctionParameterType.kt new file mode 100644 index 00000000000..0a04b9f473b --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionLiteralTypeWithoutChangingFunctionParameterType.kt @@ -0,0 +1,7 @@ +// "Change function literal return type to 'String'" "true" +fun foo(f: (String) -> Any) { + foo { + (s: String): Int -> + s + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionLiteralTypeWithoutChangingPropertyType.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionLiteralTypeWithoutChangingPropertyType.kt new file mode 100644 index 00000000000..dbec7994ac1 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionLiteralTypeWithoutChangingPropertyType.kt @@ -0,0 +1,7 @@ +// "Change function literal return type to 'String'" "true" +fun foo() { + val f: (String) -> String = { + (s: Any): Int -> + "" + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionReturnTypeToMatchReturnTypeOfReturnedLiteral.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionReturnTypeToMatchReturnTypeOfReturnedLiteral.kt new file mode 100644 index 00000000000..44c4c565685 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionReturnTypeToMatchReturnTypeOfReturnedLiteral.kt @@ -0,0 +1,4 @@ +// "Change 'foo' function return type to '() -> Any'" "true" +fun foo(x: Any): () -> Int { + return {x} +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeReturnedExpresionCantEvaluateToExpresionThatTypeMismatch.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeReturnedExpresionCantEvaluateToExpresionThatTypeMismatch.kt new file mode 100644 index 00000000000..8161c29c287 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeReturnedExpresionCantEvaluateToExpresionThatTypeMismatch.kt @@ -0,0 +1,6 @@ +// "Change function 'foo' return type to 'String'" "false" +// ERROR: Type mismatch.
Required:jet.Int
Found:jet.String
+// ACTION: Disable 'Replace 'if' with 'when'' +// ACTION: Edit intention settings +// ACTION: Replace 'if' with 'when' +fun foo(): Int = if (true) "": Int else 4 \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeReturnedExpressionTypeMismatchFunctionParameterType.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeReturnedExpressionTypeMismatchFunctionParameterType.kt new file mode 100644 index 00000000000..cb39787a072 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeReturnedExpressionTypeMismatchFunctionParameterType.kt @@ -0,0 +1,6 @@ +// "Change parameter 'f' type of function 'foo' to '() -> String'" "true" +fun foo(f: () -> Int) { + foo { + "" + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInIfStatementReturnedByFunction.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInIfStatementReturnedByFunction.kt new file mode 100644 index 00000000000..8a4869a7439 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInIfStatementReturnedByFunction.kt @@ -0,0 +1,7 @@ +// "Change 'boo' function return type to 'String'" "true" +fun boo(): Int { + return ((if (true) { + val a = "" + a + } else "")) +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInIfStatementReturnedByLiteral.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInIfStatementReturnedByLiteral.kt new file mode 100644 index 00000000000..ae1c5c6a142 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInIfStatementReturnedByLiteral.kt @@ -0,0 +1,12 @@ +// "Change 'f' type to '(Int, Int) -> (String) -> Int'" "true" +fun foo() { + val f: () -> Long = { + (a: Int, b: Int): Long -> + val x = {(s: String) -> 42} + if (true) x + else if (true) x else { + var y = 42 + if (true) x else x + } + } +} \ 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 810c38bc41d..7dbabee2862 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -1421,16 +1421,51 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeAssignmentTypeMismatch.kt"); } + @TestMetadata("beforeChangeFunctionLiteralTypeWithoutChangingFunctionParameterType.kt") + public void testChangeFunctionLiteralTypeWithoutChangingFunctionParameterType() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionLiteralTypeWithoutChangingFunctionParameterType.kt"); + } + + @TestMetadata("beforeChangeFunctionLiteralTypeWithoutChangingPropertyType.kt") + public void testChangeFunctionLiteralTypeWithoutChangingPropertyType() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionLiteralTypeWithoutChangingPropertyType.kt"); + } + @TestMetadata("beforeChangeFunctionReturnTypeToFunctionType.kt") public void testChangeFunctionReturnTypeToFunctionType() throws Exception { doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionReturnTypeToFunctionType.kt"); } + @TestMetadata("beforeChangeFunctionReturnTypeToMatchReturnTypeOfReturnedLiteral.kt") + public void testChangeFunctionReturnTypeToMatchReturnTypeOfReturnedLiteral() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionReturnTypeToMatchReturnTypeOfReturnedLiteral.kt"); + } + @TestMetadata("beforeExpectedTypeMismatch.kt") public void testExpectedTypeMismatch() throws Exception { doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeExpectedTypeMismatch.kt"); } + @TestMetadata("beforeReturnedExpresionCantEvaluateToExpresionThatTypeMismatch.kt") + public void testReturnedExpresionCantEvaluateToExpresionThatTypeMismatch() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeReturnedExpresionCantEvaluateToExpresionThatTypeMismatch.kt"); + } + + @TestMetadata("beforeReturnedExpressionTypeMismatchFunctionParameterType.kt") + public void testReturnedExpressionTypeMismatchFunctionParameterType() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeReturnedExpressionTypeMismatchFunctionParameterType.kt"); + } + + @TestMetadata("beforeTypeMismatchInIfStatementReturnedByFunction.kt") + public void testTypeMismatchInIfStatementReturnedByFunction() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInIfStatementReturnedByFunction.kt"); + } + + @TestMetadata("beforeTypeMismatchInIfStatementReturnedByLiteral.kt") + public void testTypeMismatchInIfStatementReturnedByLiteral() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInIfStatementReturnedByLiteral.kt"); + } + @TestMetadata("beforeTypeMismatchInInitializer.kt") public void testTypeMismatchInInitializer() throws Exception { doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInInitializer.kt");