From b5bbe26583128bc57b6080f9560c10f2677c64ee Mon Sep 17 00:00:00 2001 From: Wojciech Lopata Date: Wed, 24 Apr 2013 11:38:10 +0200 Subject: [PATCH 1/5] Remove unreachable code --- .../jetbrains/jet/lang/cfg/JetFlowInformationProvider.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/cfg/JetFlowInformationProvider.java b/compiler/frontend/src/org/jetbrains/jet/lang/cfg/JetFlowInformationProvider.java index 65587bff315..322e7667cf2 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/cfg/JetFlowInformationProvider.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/cfg/JetFlowInformationProvider.java @@ -133,13 +133,6 @@ public class JetFlowInformationProvider { List returnedExpressions = Lists.newArrayList(); collectReturnExpressions(returnedExpressions); - boolean nothingReturned = returnedExpressions.isEmpty(); - - returnedExpressions.remove(function); // This will be the only "expression" if the body is empty - - if (expectedReturnType != NO_EXPECTED_TYPE && !KotlinBuiltIns.getInstance().isUnit(expectedReturnType) && returnedExpressions.isEmpty() && !nothingReturned) { - trace.report(RETURN_TYPE_MISMATCH.on(bodyExpression, expectedReturnType)); - } final boolean blockBody = function.hasBlockBody(); final Set rootUnreachableElements = collectUnreachableCode(); From e49389bf0797e2159ade055c7075bbab49643351 Mon Sep 17 00:00:00 2001 From: Wojciech Lopata Date: Wed, 24 Apr 2013 11:21:04 +0200 Subject: [PATCH 2/5] Make clear ChangeFunctionReturnTypeFix can't hadle function literals --- .../quickfix/ChangeFunctionReturnTypeFix.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java index f6fc8ceec87..66fcfe8448a 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java @@ -44,10 +44,10 @@ import org.jetbrains.jet.plugin.caches.resolve.KotlinCacheManagerUtil; import org.jetbrains.jet.plugin.intentions.SpecifyTypeExplicitlyAction; import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; -public class ChangeFunctionReturnTypeFix extends JetIntentionAction { +public class ChangeFunctionReturnTypeFix extends JetIntentionAction { private final JetType type; - public ChangeFunctionReturnTypeFix(@NotNull JetFunction element, @NotNull JetType type) { + public ChangeFunctionReturnTypeFix(@NotNull JetNamedFunction element, @NotNull JetType type) { super(element); this.type = type; } @@ -109,7 +109,7 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction BindingContext context = AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) entry.getContainingFile().getContainingFile()).getBindingContext(); ResolvedCall resolvedCall = context.get(BindingContext.COMPONENT_RESOLVED_CALL, entry); if (resolvedCall == null) return null; - JetFunction componentFunction = (JetFunction) BindingContextUtils.descriptorToDeclaration(context, resolvedCall.getCandidateDescriptor()); + JetNamedFunction componentFunction = (JetNamedFunction) BindingContextUtils.descriptorToDeclaration(context, resolvedCall.getCandidateDescriptor()); JetType expectedType = context.get(BindingContext.TYPE, entry.getTypeRef()); if (componentFunction != null && expectedType != null) { return new ChangeFunctionReturnTypeFix(componentFunction, expectedType); @@ -130,7 +130,7 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction BindingContext context = AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) expression.getContainingFile()).getBindingContext(); ResolvedCall resolvedCall = context.get(BindingContext.LOOP_RANGE_HAS_NEXT_RESOLVED_CALL, expression); if (resolvedCall == null) return null; - JetFunction hasNextFunction = (JetFunction) BindingContextUtils.descriptorToDeclaration(context, resolvedCall.getCandidateDescriptor()); + JetNamedFunction hasNextFunction = (JetNamedFunction) BindingContextUtils.descriptorToDeclaration(context, resolvedCall.getCandidateDescriptor()); if (hasNextFunction != null) { return new ChangeFunctionReturnTypeFix(hasNextFunction, KotlinBuiltIns.getInstance().getBooleanType()); } @@ -151,8 +151,8 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction ResolvedCall resolvedCall = context.get(BindingContext.RESOLVED_CALL, expression.getOperationReference()); if (resolvedCall == null) return null; PsiElement compareTo = BindingContextUtils.descriptorToDeclaration(context, resolvedCall.getCandidateDescriptor()); - if (!(compareTo instanceof JetFunction)) return null; - return new ChangeFunctionReturnTypeFix((JetFunction) compareTo, KotlinBuiltIns.getInstance().getIntType()); + if (!(compareTo instanceof JetNamedFunction)) return null; + return new ChangeFunctionReturnTypeFix((JetNamedFunction) compareTo, KotlinBuiltIns.getInstance().getIntType()); } }; } @@ -163,7 +163,7 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction @Nullable @Override public IntentionAction createAction(Diagnostic diagnostic) { - JetFunction function = QuickFixUtil.getParentElementOfType(diagnostic, JetFunction.class); + JetNamedFunction function = QuickFixUtil.getParentElementOfType(diagnostic, JetNamedFunction.class); if (function == null) return null; BindingContext context = KotlinCacheManagerUtil.getDeclarationsBindingContext(function); JetType matchingReturnType = QuickFixUtil.findLowerBoundOfOverriddenCallablesReturnTypes(context, function); From dde4260d10c26015c665c580bff34f040f8ee780 Mon Sep 17 00:00:00 2001 From: Wojciech Lopata Date: Wed, 24 Apr 2013 12:17:20 +0200 Subject: [PATCH 3/5] Fix ChangeFunctionReturnTypeFix.getText() method --- .../jetbrains/jet/plugin/JetBundle.properties | 2 ++ .../quickfix/ChangeFunctionReturnTypeFix.java | 16 +++++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties index ec12e968fb3..ee84d2f38ec 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties +++ b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties @@ -58,7 +58,9 @@ rename.family=Rename add.semicolon.after.invocation=Add semicolon after invocation of ''{0}'' add.semicolon.family=Add Semicolon change.function.return.type=Change ''{0}'' function return type to ''{1}'' +change.no.name.function.return.type=Change function return type to ''{0}'' remove.function.return.type=Remove explicitly specified return type in ''{0}'' function +remove.no.name.function.return.type=Remove explicitly specified function return type change.element.type=Change ''{0}'' type to ''{1}'' change.type=Change type from ''{0}'' to ''{1}'' change.type.family=Change Type diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java index 66fcfe8448a..681b3cfdb2d 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java @@ -36,6 +36,7 @@ import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContextUtils; 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.JetType; import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; @@ -56,16 +57,17 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction Date: Wed, 24 Apr 2013 11:32:33 +0200 Subject: [PATCH 4/5] Quickfix for RETURN_TYPE_MISMATCH and NO_RETURN_IN_FUNCTION_WITH_BLOCK_BODY --- .../quickfix/ChangeFunctionReturnTypeFix.java | 12 ++++++++++++ .../jetbrains/jet/plugin/quickfix/QuickFixes.java | 3 +++ ...erChangeReturnTypeWhenFunctionNameIsMissing.kt | 4 ++++ .../afterNoReturnInFunctionWithBlockBody.kt | 3 +++ .../typeMismatch/afterReturnTypeMismatch.kt | 4 ++++ ...reChangeReturnTypeWhenFunctionNameIsMissing.kt | 4 ++++ .../beforeNoReturnInFunctionWithBlockBody.kt | 3 +++ .../typeMismatch/beforeReturnTypeMismatch.kt | 4 ++++ .../plugin/quickfix/QuickFixTestGenerated.java | 15 +++++++++++++++ 9 files changed, 52 insertions(+) create mode 100644 idea/testData/quickfix/typeMismatch/afterChangeReturnTypeWhenFunctionNameIsMissing.kt create mode 100644 idea/testData/quickfix/typeMismatch/afterNoReturnInFunctionWithBlockBody.kt create mode 100644 idea/testData/quickfix/typeMismatch/afterReturnTypeMismatch.kt create mode 100644 idea/testData/quickfix/typeMismatch/beforeChangeReturnTypeWhenFunctionNameIsMissing.kt create mode 100644 idea/testData/quickfix/typeMismatch/beforeNoReturnInFunctionWithBlockBody.kt create mode 100644 idea/testData/quickfix/typeMismatch/beforeReturnTypeMismatch.kt diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java index 681b3cfdb2d..ea34a1dbe19 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java @@ -173,4 +173,16 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/afterNoReturnInFunctionWithBlockBody.kt b/idea/testData/quickfix/typeMismatch/afterNoReturnInFunctionWithBlockBody.kt new file mode 100644 index 00000000000..e8876c8bdeb --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/afterNoReturnInFunctionWithBlockBody.kt @@ -0,0 +1,3 @@ +// "Remove explicitly specified return type in 'foo' function" "true" +fun foo() { +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/afterReturnTypeMismatch.kt b/idea/testData/quickfix/typeMismatch/afterReturnTypeMismatch.kt new file mode 100644 index 00000000000..d0cc2249cb8 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/afterReturnTypeMismatch.kt @@ -0,0 +1,4 @@ +// "Remove explicitly specified return type in 'foo' function" "true" +fun foo() { + return +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/beforeChangeReturnTypeWhenFunctionNameIsMissing.kt b/idea/testData/quickfix/typeMismatch/beforeChangeReturnTypeWhenFunctionNameIsMissing.kt new file mode 100644 index 00000000000..5a7e745cead --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/beforeChangeReturnTypeWhenFunctionNameIsMissing.kt @@ -0,0 +1,4 @@ +// "Remove explicitly specified function return type" "true" +fun (): Int { + return +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/beforeNoReturnInFunctionWithBlockBody.kt b/idea/testData/quickfix/typeMismatch/beforeNoReturnInFunctionWithBlockBody.kt new file mode 100644 index 00000000000..e254b4f61ed --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/beforeNoReturnInFunctionWithBlockBody.kt @@ -0,0 +1,3 @@ +// "Remove explicitly specified return type in 'foo' function" "true" +fun foo(): Int { +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/beforeReturnTypeMismatch.kt b/idea/testData/quickfix/typeMismatch/beforeReturnTypeMismatch.kt new file mode 100644 index 00000000000..7995f7a3e67 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/beforeReturnTypeMismatch.kt @@ -0,0 +1,4 @@ +// "Remove explicitly specified return type in 'foo' function" "true" +fun foo(): Int { + return +} \ 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 27ed4fe997f..05a974141e4 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -998,6 +998,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/typeMismatch"), Pattern.compile("^before(\\w+)\\.kt$"), true); } + @TestMetadata("beforeChangeReturnTypeWhenFunctionNameIsMissing.kt") + public void testChangeReturnTypeWhenFunctionNameIsMissing() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/beforeChangeReturnTypeWhenFunctionNameIsMissing.kt"); + } + @TestMetadata("beforeChangeReturnTypeWhenValueParameterListIsAbsent.kt") public void testChangeReturnTypeWhenValueParameterListIsAbsent() throws Exception { doTest("idea/testData/quickfix/typeMismatch/beforeChangeReturnTypeWhenValueParameterListIsAbsent.kt"); @@ -1048,6 +1053,16 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest("idea/testData/quickfix/typeMismatch/beforeHasNextFunctionReturnTypeMismatch.kt"); } + @TestMetadata("beforeNoReturnInFunctionWithBlockBody.kt") + public void testNoReturnInFunctionWithBlockBody() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/beforeNoReturnInFunctionWithBlockBody.kt"); + } + + @TestMetadata("beforeReturnTypeMismatch.kt") + public void testReturnTypeMismatch() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/beforeReturnTypeMismatch.kt"); + } + } @TestMetadata("idea/testData/quickfix/typeProjection") From 9b11db7a742665f31331eb01c96e5c84c14dce1c Mon Sep 17 00:00:00 2001 From: Wojciech Lopata Date: Wed, 24 Apr 2013 14:32:44 +0200 Subject: [PATCH 5/5] ChangeFunctionReturnTypeFix for TYPE_MISMATCH error --- .../quickfix/ChangeFunctionReturnTypeFix.java | 20 +++++++++++++++++++ .../jet/plugin/quickfix/QuickFixes.java | 1 + .../afterTypeMismatchInInitializer.kt | 2 ++ .../afterTypeMismatchInReturnStatement.kt | 4 ++++ .../beforeTypeMismatchInInitializer.kt | 2 ++ .../beforeTypeMismatchInReturnStatement.kt | 4 ++++ .../quickfix/QuickFixTestGenerated.java | 10 ++++++++++ 7 files changed, 43 insertions(+) create mode 100644 idea/testData/quickfix/typeMismatch/afterTypeMismatchInInitializer.kt create mode 100644 idea/testData/quickfix/typeMismatch/afterTypeMismatchInReturnStatement.kt create mode 100644 idea/testData/quickfix/typeMismatch/beforeTypeMismatchInInitializer.kt create mode 100644 idea/testData/quickfix/typeMismatch/beforeTypeMismatchInReturnStatement.kt diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java index ea34a1dbe19..246d3c322e5 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java @@ -185,4 +185,24 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction"" \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/afterTypeMismatchInReturnStatement.kt b/idea/testData/quickfix/typeMismatch/afterTypeMismatchInReturnStatement.kt new file mode 100644 index 00000000000..eb37598a97b --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/afterTypeMismatchInReturnStatement.kt @@ -0,0 +1,4 @@ +// "Change 'foo' function return type to 'String'" "true" +fun foo(): String { + return "" +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/beforeTypeMismatchInInitializer.kt b/idea/testData/quickfix/typeMismatch/beforeTypeMismatchInInitializer.kt new file mode 100644 index 00000000000..7fc2c617bd9 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/beforeTypeMismatchInInitializer.kt @@ -0,0 +1,2 @@ +// "Change 'foo' function return type to 'String'" "true" +fun foo(): Int = "" \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/beforeTypeMismatchInReturnStatement.kt b/idea/testData/quickfix/typeMismatch/beforeTypeMismatchInReturnStatement.kt new file mode 100644 index 00000000000..71a0a7aa945 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/beforeTypeMismatchInReturnStatement.kt @@ -0,0 +1,4 @@ +// "Change 'foo' function return type to 'String'" "true" +fun foo() { + return "" +} \ 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 05a974141e4..e26c3ac86b9 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -1063,6 +1063,16 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest("idea/testData/quickfix/typeMismatch/beforeReturnTypeMismatch.kt"); } + @TestMetadata("beforeTypeMismatchInInitializer.kt") + public void testTypeMismatchInInitializer() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/beforeTypeMismatchInInitializer.kt"); + } + + @TestMetadata("beforeTypeMismatchInReturnStatement.kt") + public void testTypeMismatchInReturnStatement() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/beforeTypeMismatchInReturnStatement.kt"); + } + } @TestMetadata("idea/testData/quickfix/typeProjection")