From e9b16f3e1bb6eb9494cfab3e2cb33326a96f1756 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Tue, 30 Sep 2014 13:53:11 +0400 Subject: [PATCH] Create Function From Usage: Use Any if error type is inferred --- .../createFromUsage/callableBuilder/CallableInfo.kt | 4 ++++ .../createFunction/call/afterInLambda.kt | 11 +++++++++++ .../createFunction/call/afterUnresolvedSupertype.kt | 13 +++++++++++++ .../createFunction/call/beforeInLambda.kt | 7 +++++++ .../call/beforeUnresolvedSupertype.kt | 10 ++++++++++ .../jet/plugin/quickfix/QuickFixTestGenerated.java | 12 ++++++++++++ 6 files changed, 57 insertions(+) create mode 100644 idea/testData/quickfix/createFromUsage/createFunction/call/afterInLambda.kt create mode 100644 idea/testData/quickfix/createFromUsage/createFunction/call/afterUnresolvedSupertype.kt create mode 100644 idea/testData/quickfix/createFromUsage/createFunction/call/beforeInLambda.kt create mode 100644 idea/testData/quickfix/createFromUsage/createFunction/call/beforeUnresolvedSupertype.kt diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/callableBuilder/CallableInfo.kt b/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/callableBuilder/CallableInfo.kt index 1c396eee270..2f1b2500ff4 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/callableBuilder/CallableInfo.kt +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/createFromUsage/callableBuilder/CallableInfo.kt @@ -10,6 +10,9 @@ import org.jetbrains.jet.plugin.refactoring.JetNameSuggester import org.jetbrains.jet.plugin.refactoring.EmptyValidator import org.jetbrains.jet.lang.resolve.BindingContext import org.jetbrains.jet.plugin.util.supertypes +import org.jetbrains.jet.lang.types.TypeUtils +import org.jetbrains.jet.lang.types.ErrorUtils +import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns /** * Represents a concrete type or a set of types yet to be inferred from an expression. @@ -42,6 +45,7 @@ abstract class TypeInfo(val variance: Variance) { abstract fun getPossibleTypes(builder: CallableBuilder): List protected fun JetType.getPossibleSupertypes(variance: Variance): List { + if (ErrorUtils.containsErrorType(this)) return Collections.singletonList(KotlinBuiltIns.getInstance().getAnyType()) val single = Collections.singletonList(this) return when (variance) { Variance.IN_VARIANCE -> single + supertypes() diff --git a/idea/testData/quickfix/createFromUsage/createFunction/call/afterInLambda.kt b/idea/testData/quickfix/createFromUsage/createFunction/call/afterInLambda.kt new file mode 100644 index 00000000000..ae9c0ee5a21 --- /dev/null +++ b/idea/testData/quickfix/createFromUsage/createFunction/call/afterInLambda.kt @@ -0,0 +1,11 @@ +// "Create function 'foo' from usage" "true" + +fun run(f: () -> T) = f() + +fun test() { + run { foo() } +} + +fun foo(): Any { + throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates. +} \ No newline at end of file diff --git a/idea/testData/quickfix/createFromUsage/createFunction/call/afterUnresolvedSupertype.kt b/idea/testData/quickfix/createFromUsage/createFunction/call/afterUnresolvedSupertype.kt new file mode 100644 index 00000000000..6996a1c5579 --- /dev/null +++ b/idea/testData/quickfix/createFromUsage/createFunction/call/afterUnresolvedSupertype.kt @@ -0,0 +1,13 @@ +// "Create function 'foo' from usage" "true" +// ERROR: Unresolved reference: B + +class A: B { + fun foo(): Any { + throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates. + } + +} + +fun test() { + A().foo() +} \ No newline at end of file diff --git a/idea/testData/quickfix/createFromUsage/createFunction/call/beforeInLambda.kt b/idea/testData/quickfix/createFromUsage/createFunction/call/beforeInLambda.kt new file mode 100644 index 00000000000..1a34c8f2d7d --- /dev/null +++ b/idea/testData/quickfix/createFromUsage/createFunction/call/beforeInLambda.kt @@ -0,0 +1,7 @@ +// "Create function 'foo' from usage" "true" + +fun run(f: () -> T) = f() + +fun test() { + run { foo() } +} \ No newline at end of file diff --git a/idea/testData/quickfix/createFromUsage/createFunction/call/beforeUnresolvedSupertype.kt b/idea/testData/quickfix/createFromUsage/createFunction/call/beforeUnresolvedSupertype.kt new file mode 100644 index 00000000000..5abd5e42a5f --- /dev/null +++ b/idea/testData/quickfix/createFromUsage/createFunction/call/beforeUnresolvedSupertype.kt @@ -0,0 +1,10 @@ +// "Create function 'foo' from usage" "true" +// ERROR: Unresolved reference: B + +class A: B { + +} + +fun test() { + A().foo() +} \ 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 ef94dc28b42..60cce54b248 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -802,6 +802,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest(fileName); } + @TestMetadata("beforeInLambda.kt") + public void testInLambda() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createFunction/call/beforeInLambda.kt"); + doTest(fileName); + } + @TestMetadata("beforeInconsistentTypes.kt") public void testInconsistentTypes() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createFunction/call/beforeInconsistentTypes.kt"); @@ -868,6 +874,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest(fileName); } + @TestMetadata("beforeUnresolvedSupertype.kt") + public void testUnresolvedSupertype() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createFunction/call/beforeUnresolvedSupertype.kt"); + doTest(fileName); + } + } @TestMetadata("idea/testData/quickfix/createFromUsage/createFunction/component")