From 9247f4f0962b314be0838ec4e0798072f184028d Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Mon, 30 Mar 2015 17:54:39 +0300 Subject: [PATCH] Quick-Fixes: Fix type checking scope for type mismatch in call argument --- .../kotlin/idea/quickfix/ChangeParameterTypeFix.java | 2 +- .../QuickFixFactoryForTypeMismatchError.java | 3 ++- .../typeMismatch/afterResolvableTypeParams.kt | 8 ++++++++ .../typeMismatch/afterUnresolvableTypeParams.kt | 8 ++++++++ .../typeMismatch/beforeResolvableTypeParams.kt | 8 ++++++++ .../typeMismatch/beforeUnresolvableTypeParams.kt | 8 ++++++++ .../kotlin/idea/quickfix/QuickFixTestGenerated.java | 12 ++++++++++++ 7 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 idea/testData/quickfix/typeMismatch/afterResolvableTypeParams.kt create mode 100644 idea/testData/quickfix/typeMismatch/afterUnresolvableTypeParams.kt create mode 100644 idea/testData/quickfix/typeMismatch/beforeResolvableTypeParams.kt create mode 100644 idea/testData/quickfix/typeMismatch/beforeUnresolvableTypeParams.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeParameterTypeFix.java b/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeParameterTypeFix.java index 82e2eed9f55..50dd091e2d4 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeParameterTypeFix.java +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeParameterTypeFix.java @@ -42,7 +42,7 @@ public class ChangeParameterTypeFix extends JetIntentionAction { JetNamedDeclaration declaration = PsiTreeUtil.getParentOfType(element, JetNamedDeclaration.class); isPrimaryConstructorParameter = declaration instanceof JetClass; FqName declarationFQName = declaration == null ? null : declaration.getFqName(); - containingDeclarationName = declarationFQName == null ? null : declarationFQName.asString(); + containingDeclarationName = declarationFQName == null ? declaration.getName() : declarationFQName.asString(); } @Override diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixFactoryForTypeMismatchError.java b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixFactoryForTypeMismatchError.java index ed71a936ded..41372139f3a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixFactoryForTypeMismatchError.java +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixFactoryForTypeMismatchError.java @@ -156,7 +156,8 @@ public class QuickFixFactoryForTypeMismatchError extends JetIntentionActionsFact ? expressionType : context.get(BindingContext.EXPRESSION_TYPE, valueArgument.getArgumentExpression()); if (correspondingParameter != null && valueArgumentType != null) { - JetScope scope = JetScopeUtils.getResolutionScope(valueArgument.getArgumentExpression(), context); + JetCallableDeclaration callable = PsiTreeUtil.getParentOfType(correspondingParameter, JetCallableDeclaration.class, true); + JetScope scope = callable != null ? JetScopeUtils.getResolutionScope(callable, context) : null; JetType typeToInsert = UtilPackage.approximateWithResolvableType(valueArgumentType, scope, true); actions.add(new ChangeParameterTypeFix(correspondingParameter, typeToInsert)); } diff --git a/idea/testData/quickfix/typeMismatch/afterResolvableTypeParams.kt b/idea/testData/quickfix/typeMismatch/afterResolvableTypeParams.kt new file mode 100644 index 00000000000..6a133cbca8b --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/afterResolvableTypeParams.kt @@ -0,0 +1,8 @@ +// "Change parameter 'n' type of function 'foo' to 'T'" "true" +fun bar(t: T) { + fun foo(n: T) { + + } + + foo(t) +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/afterUnresolvableTypeParams.kt b/idea/testData/quickfix/typeMismatch/afterUnresolvableTypeParams.kt new file mode 100644 index 00000000000..dd6e8a72fce --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/afterUnresolvableTypeParams.kt @@ -0,0 +1,8 @@ +// "Change parameter 'n' type of function 'foo' to 'Any?'" "true" +fun foo(n: Any?) { + +} + +fun bar(t: T) { + foo(t) +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/beforeResolvableTypeParams.kt b/idea/testData/quickfix/typeMismatch/beforeResolvableTypeParams.kt new file mode 100644 index 00000000000..7d13d0a2558 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/beforeResolvableTypeParams.kt @@ -0,0 +1,8 @@ +// "Change parameter 'n' type of function 'foo' to 'T'" "true" +fun bar(t: T) { + fun foo(n: Int) { + + } + + foo(t) +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/beforeUnresolvableTypeParams.kt b/idea/testData/quickfix/typeMismatch/beforeUnresolvableTypeParams.kt new file mode 100644 index 00000000000..228a661c210 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/beforeUnresolvableTypeParams.kt @@ -0,0 +1,8 @@ +// "Change parameter 'n' type of function 'foo' to 'Any?'" "true" +fun foo(n: Int) { + +} + +fun bar(t: T) { + foo(t) +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index 213a3c06da4..7160a0be7f1 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -4912,6 +4912,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest(fileName); } + @TestMetadata("beforeResolvableTypeParams.kt") + public void testResolvableTypeParams() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/beforeResolvableTypeParams.kt"); + doTest(fileName); + } + @TestMetadata("beforeReturnTypeMismatch.kt") public void testReturnTypeMismatch() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/beforeReturnTypeMismatch.kt"); @@ -4924,6 +4930,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest(fileName); } + @TestMetadata("beforeUnresolvableTypeParams.kt") + public void testUnresolvableTypeParams() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/beforeUnresolvableTypeParams.kt"); + doTest(fileName); + } + @TestMetadata("idea/testData/quickfix/typeMismatch/casts") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)