diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionLiteralReturnTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionLiteralReturnTypeFix.java index b257b99cfdc..c7ac17cd164 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionLiteralReturnTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionLiteralReturnTypeFix.java @@ -85,7 +85,7 @@ public class ChangeFunctionLiteralReturnTypeFix extends JetIntentionAction typeWhiteSpaceAndColon = JetPsiFactory.createTypeWhiteSpaceAndColon(project, renderedType); element.addRangeAfter(typeWhiteSpaceAndColon.first, typeWhiteSpaceAndColon.second, nameIdentifier); + + if (element instanceof JetProperty) { + JetPropertyAccessor getter = ((JetProperty) element).getGetter(); + JetTypeReference getterReturnTypeRef = getter == null ? null : getter.getReturnTypeReference(); + if (getterReturnTypeRef != null) { + getterReturnTypeRef.replace(JetPsiFactory.createType(project, renderedType)); + } + + JetPropertyAccessor setter = ((JetProperty) element).getSetter(); + JetParameter setterParameter = setter == null ? null : setter.getParameter(); + JetTypeReference setterParameterTypeRef = setterParameter == null ? null : setterParameter.getTypeReference(); + if (setterParameterTypeRef != null) { + setterParameterTypeRef.replace(JetPsiFactory.createType(project, renderedType)); + } + } } @NotNull diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java index 70c1c6632b0..ec304583cb9 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java @@ -56,13 +56,17 @@ public class QuickFixFactoryForTypeMismatchError implements JetIntentionActionsF // Property initializer type mismatch property type: JetProperty property = PsiTreeUtil.getParentOfType(expression, JetProperty.class); - if (property != null && QuickFixUtil.canEvaluateTo(property.getInitializer(), expression)) { - actions.add(new ChangeVariableTypeFix(property, expressionType)); + if (property != null) { + JetPropertyAccessor getter = property.getGetter(); + if (QuickFixUtil.canEvaluateTo(property.getInitializer(), expression) || + (getter != null && QuickFixUtil.canFunctionOrGetterReturnExpression(property.getGetter(), expression))) { + actions.add(new ChangeVariableTypeFix(property, expressionType)); + } } // Mismatch in returned expression: JetFunction function = PsiTreeUtil.getParentOfType(expression, JetFunction.class, true); - if (function != null && QuickFixUtil.canFunctionReturnExpression(function, expression)) { + if (function != null && QuickFixUtil.canFunctionOrGetterReturnExpression(function, expression)) { actions.add(new ChangeFunctionReturnTypeFix(function, expressionType)); } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java index a7217ba7006..c7d003be348 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java @@ -175,14 +175,14 @@ public class QuickFixUtil { return true; } - public static boolean canFunctionReturnExpression(@NotNull JetFunction function, @NotNull JetExpression expression) { - if (function instanceof JetFunctionLiteral) { - JetBlockExpression functionLiteralBody = ((JetFunctionLiteral) function).getBodyExpression(); + public static boolean canFunctionOrGetterReturnExpression(@NotNull JetDeclaration functionOrGetter, @NotNull JetExpression expression) { + if (functionOrGetter instanceof JetFunctionLiteral) { + JetBlockExpression functionLiteralBody = ((JetFunctionLiteral) functionOrGetter).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)) { + if (functionOrGetter instanceof JetWithExpressionInitializer && canEvaluateTo(((JetWithExpressionInitializer) functionOrGetter).getInitializer(), expression)) { return true; } JetReturnExpression returnExpression = PsiTreeUtil.getParentOfType(expression, JetReturnExpression.class); diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterPropertyGetterInitializerTypeMismatch.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterPropertyGetterInitializerTypeMismatch.kt new file mode 100644 index 00000000000..e22fc7027ef --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterPropertyGetterInitializerTypeMismatch.kt @@ -0,0 +1,6 @@ +// "Change 'A.x' type to '() -> Int'" "true" +class A { + var x: () -> Int + get(): () -> Int = if (true) { {42} } else { {24} } + set(i: () -> Int) {} +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforePropertyGetterInitializerTypeMismatch.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforePropertyGetterInitializerTypeMismatch.kt new file mode 100644 index 00000000000..c9196877276 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforePropertyGetterInitializerTypeMismatch.kt @@ -0,0 +1,6 @@ +// "Change 'A.x' type to '() -> Int'" "true" +class A { + var x: Int + get(): Int = if (true) { {42} } else { {24} } + set(i: Int) {} +} \ 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 8e766d00b3a..66801905864 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -1514,6 +1514,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeExpectedTypeMismatch.kt"); } + @TestMetadata("beforePropertyGetterInitializerTypeMismatch.kt") + public void testPropertyGetterInitializerTypeMismatch() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforePropertyGetterInitializerTypeMismatch.kt"); + } + @TestMetadata("beforeReturnedExpresionCantEvaluateToExpresionThatTypeMismatch.kt") public void testReturnedExpresionCantEvaluateToExpresionThatTypeMismatch() throws Exception { doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeReturnedExpresionCantEvaluateToExpresionThatTypeMismatch.kt");