diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetTypeParameter.java b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetTypeParameter.java index 11909299aa9..5a8cbd5930b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetTypeParameter.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetTypeParameter.java @@ -17,6 +17,8 @@ package org.jetbrains.kotlin.psi; import com.intellij.lang.ASTNode; +import com.intellij.psi.PsiElement; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.psi.stubs.KotlinTypeParameterStub; @@ -56,6 +58,27 @@ public class JetTypeParameter extends JetNamedDeclarationStub(typeParameter) { + private val renderedUpperBound: String = IdeDescriptorRenderers.SOURCE_CODE.renderType(upperBound) + + override fun getText() = "Add '$renderedUpperBound' as upper bound for ${element.name}" + override fun getFamilyName() = "Add generic upper bound" + + override fun isAvailable(project: Project, editor: Editor?, file: PsiFile): Boolean { + if (!super.isAvailable(project, editor, file)) return false + // TODO: replacing existing upper bounds + return (element.name != null && element.extendsBound == null) + } + + override fun invoke(project: Project, editor: Editor?, file: JetFile) { + assert(element.extendsBound == null, "Don't know what to do with existing bounds") + + val typeReference = JetPsiFactory(project).createType(renderedUpperBound) + val insertedTypeReference = element.setExtendsBound(typeReference)!! + + ShortenReferences.DEFAULT.process(insertedTypeReference) + } + + companion object Factory : JetIntentionActionsFactory() { + override fun doCreateActions(diagnostic: Diagnostic): List { + return when (diagnostic.factory) { + Errors.UPPER_BOUND_VIOLATED -> { + val upperBoundViolated = Errors.UPPER_BOUND_VIOLATED.cast(diagnostic) + createAction(upperBoundViolated.b, upperBoundViolated.a).singletonOrEmptyList() + } + Errors.TYPE_INFERENCE_UPPER_BOUND_VIOLATED -> { + val inferenceData = Errors.TYPE_INFERENCE_UPPER_BOUND_VIOLATED.cast(diagnostic).a + createActionsByInferenceData(inferenceData) + } + else -> emptyList() + } + } + + private fun createActionsByInferenceData(inferenceData: InferenceErrorData): List { + val successfulConstraintSystem = (inferenceData.constraintSystem as? ConstraintSystemImpl) + ?.filterConstraintsOut(ConstraintPositionKind.TYPE_BOUND_POSITION) + ?: return emptyList() + + if (!successfulConstraintSystem.getStatus().isSuccessful()) return emptyList() + + val resultingSubstitutor = successfulConstraintSystem.getResultingSubstitutor() + + return inferenceData.descriptor.typeParameters.map factory@{ + typeParameterDescriptor -> + + if (ConstraintsUtil.checkUpperBoundIsSatisfied( + successfulConstraintSystem, typeParameterDescriptor, /* substituteOtherTypeParametersInBound */ true + )) return@factory null + + val upperBound = typeParameterDescriptor.upperBounds.singleOrNull() ?: return@factory null + val argument = resultingSubstitutor.substitute(typeParameterDescriptor.defaultType, Variance.INVARIANT) + ?: return@factory null + + createAction(argument, upperBound) + }.filterNotNull() + } + + private fun createAction(argument: JetType, upperBound: JetType): IntentionAction? { + if (!upperBound.constructor.isDenotable) return null + + val typeParameterDescriptor = (argument.constructor.declarationDescriptor as? TypeParameterDescriptor) ?: return null + val typeParameterDeclaration = + (DescriptorToSourceUtils.getSourceFromDescriptor(typeParameterDescriptor) as? JetTypeParameter) ?: return null + + return AddGenericUpperBoundFix(typeParameterDeclaration, upperBound) + } + } +} diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index 2acbc441b84..30ddcf9dbc9 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -318,5 +318,8 @@ public class QuickFixRegistrar : QuickFixContributor { NO_REFLECTION_IN_CLASS_PATH.registerFactory(AddReflectionQuickFix) ErrorsJvm.JAVA_TYPE_MISMATCH.registerFactory(CastExpressionFix.createFactoryForGenericVarianceConversion()) + + UPPER_BOUND_VIOLATED.registerFactory(AddGenericUpperBoundFix.Factory) + TYPE_INFERENCE_UPPER_BOUND_VIOLATED.registerFactory(AddGenericUpperBoundFix.Factory) } } diff --git a/idea/testData/quickfix/addGenericUpperBound/basic.kt b/idea/testData/quickfix/addGenericUpperBound/basic.kt new file mode 100644 index 00000000000..2175d79f764 --- /dev/null +++ b/idea/testData/quickfix/addGenericUpperBound/basic.kt @@ -0,0 +1,5 @@ +// "Add 'kotlin.Any' as upper bound for E" "true" + +fun foo() = 1 + +fun bar() = foo>() diff --git a/idea/testData/quickfix/addGenericUpperBound/basic.kt.after b/idea/testData/quickfix/addGenericUpperBound/basic.kt.after new file mode 100644 index 00000000000..cb40a6fa6a9 --- /dev/null +++ b/idea/testData/quickfix/addGenericUpperBound/basic.kt.after @@ -0,0 +1,5 @@ +// "Add 'kotlin.Any' as upper bound for E" "true" + +fun foo() = 1 + +fun bar() = foo() diff --git a/idea/testData/quickfix/addGenericUpperBound/boundAlreadyExists.kt b/idea/testData/quickfix/addGenericUpperBound/boundAlreadyExists.kt new file mode 100644 index 00000000000..42f1060232d --- /dev/null +++ b/idea/testData/quickfix/addGenericUpperBound/boundAlreadyExists.kt @@ -0,0 +1,6 @@ +// "class org.jetbrains.kotlin.idea.quickfix.AddGenericUpperBoundFix" "false" +// ERROR: Type argument is not within its bounds.
Expected:kotlin.Any
Found:E
+ +fun foo() = 1 + +fun bar() = foo>() diff --git a/idea/testData/quickfix/addGenericUpperBound/inferenceTwoParams.kt b/idea/testData/quickfix/addGenericUpperBound/inferenceTwoParams.kt new file mode 100644 index 00000000000..536314e583b --- /dev/null +++ b/idea/testData/quickfix/addGenericUpperBound/inferenceTwoParams.kt @@ -0,0 +1,6 @@ +// "Add 'kotlin.Any' as upper bound for E" "true" +// ERROR: Type parameter bound for U in
fun <T : kotlin.Any, U : kotlin.Any> foo(x: T,y: U): kotlin.Int
is not satisfied: inferred type F is not a subtype of kotlin.Any + +fun foo(x: T, y: U) = 1 + +fun bar(x: E, y: F) = foo(x, y) diff --git a/idea/testData/quickfix/addGenericUpperBound/inferenceTwoParams.kt.after b/idea/testData/quickfix/addGenericUpperBound/inferenceTwoParams.kt.after new file mode 100644 index 00000000000..c011a098a06 --- /dev/null +++ b/idea/testData/quickfix/addGenericUpperBound/inferenceTwoParams.kt.after @@ -0,0 +1,6 @@ +// "Add 'kotlin.Any' as upper bound for E" "true" +// ERROR: Type parameter bound for U in
fun <T : kotlin.Any, U : kotlin.Any> foo(x: T,y: U): kotlin.Int
is not satisfied: inferred type F is not a subtype of kotlin.Any + +fun foo(x: T, y: U) = 1 + +fun bar(x: E, y: F) = foo(x, y) diff --git a/idea/testData/quickfix/addGenericUpperBound/kClassRuntime.kt b/idea/testData/quickfix/addGenericUpperBound/kClassRuntime.kt new file mode 100644 index 00000000000..16fcc9096f3 --- /dev/null +++ b/idea/testData/quickfix/addGenericUpperBound/kClassRuntime.kt @@ -0,0 +1,3 @@ +// "Add 'kotlin.Any' as upper bound for E" "true" + +inline fun bar() = E::class.java diff --git a/idea/testData/quickfix/addGenericUpperBound/kClassRuntime.kt.after b/idea/testData/quickfix/addGenericUpperBound/kClassRuntime.kt.after new file mode 100644 index 00000000000..8de47854070 --- /dev/null +++ b/idea/testData/quickfix/addGenericUpperBound/kClassRuntime.kt.after @@ -0,0 +1,3 @@ +// "Add 'kotlin.Any' as upper bound for E" "true" + +inline fun bar() = E::class.java diff --git a/idea/testData/quickfix/addGenericUpperBound/paramAsBound.kt b/idea/testData/quickfix/addGenericUpperBound/paramAsBound.kt new file mode 100644 index 00000000000..257ef2adbcd --- /dev/null +++ b/idea/testData/quickfix/addGenericUpperBound/paramAsBound.kt @@ -0,0 +1,5 @@ +// "Add 'E' as upper bound for F" "true" + +fun foo() = 1 + +fun bar() = foo>() diff --git a/idea/testData/quickfix/addGenericUpperBound/paramAsBound.kt.after b/idea/testData/quickfix/addGenericUpperBound/paramAsBound.kt.after new file mode 100644 index 00000000000..5d2b2a4c286 --- /dev/null +++ b/idea/testData/quickfix/addGenericUpperBound/paramAsBound.kt.after @@ -0,0 +1,5 @@ +// "Add 'E' as upper bound for F" "true" + +fun foo() = 1 + +fun bar() = foo>() diff --git a/idea/testData/quickfix/addGenericUpperBound/withinDeclaration.kt b/idea/testData/quickfix/addGenericUpperBound/withinDeclaration.kt new file mode 100644 index 00000000000..b52d1b67990 --- /dev/null +++ b/idea/testData/quickfix/addGenericUpperBound/withinDeclaration.kt @@ -0,0 +1,4 @@ +// "Add 'kotlin.Any' as upper bound for E" "true" + +class A +fun bar(x: A>) {} diff --git a/idea/testData/quickfix/addGenericUpperBound/withinDeclaration.kt.after b/idea/testData/quickfix/addGenericUpperBound/withinDeclaration.kt.after new file mode 100644 index 00000000000..f76cd7de551 --- /dev/null +++ b/idea/testData/quickfix/addGenericUpperBound/withinDeclaration.kt.after @@ -0,0 +1,4 @@ +// "Add 'kotlin.Any' as upper bound for E" "true" + +class A +fun bar(x: A>) {} diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index e59f81e3eb8..34924b1ae30 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -176,6 +176,51 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { } } + @TestMetadata("idea/testData/quickfix/addGenericUpperBound") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class AddGenericUpperBound extends AbstractQuickFixTest { + public void testAllFilesPresentInAddGenericUpperBound() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/addGenericUpperBound"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); + } + + @TestMetadata("basic.kt") + public void testBasic() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/addGenericUpperBound/basic.kt"); + doTest(fileName); + } + + @TestMetadata("boundAlreadyExists.kt") + public void testBoundAlreadyExists() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/addGenericUpperBound/boundAlreadyExists.kt"); + doTest(fileName); + } + + @TestMetadata("inferenceTwoParams.kt") + public void testInferenceTwoParams() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/addGenericUpperBound/inferenceTwoParams.kt"); + doTest(fileName); + } + + @TestMetadata("kClassRuntime.kt") + public void testKClassRuntime() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/addGenericUpperBound/kClassRuntime.kt"); + doTest(fileName); + } + + @TestMetadata("paramAsBound.kt") + public void testParamAsBound() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/addGenericUpperBound/paramAsBound.kt"); + doTest(fileName); + } + + @TestMetadata("withinDeclaration.kt") + public void testWithinDeclaration() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/addGenericUpperBound/withinDeclaration.kt"); + doTest(fileName); + } + } + @TestMetadata("idea/testData/quickfix/addStarProjections") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)