Add fine check of type inference when adding <> in completion

Add check if type inferred via type parameter of upper bound
 of extension receiver or value argument
 #KT-16161 fixed
This commit is contained in:
Simon Ogorodnik
2017-04-18 20:38:25 +03:00
parent c021af0fef
commit 02fec6e4a5
4 changed files with 35 additions and 2 deletions
@@ -26,8 +26,8 @@ import org.jetbrains.kotlin.idea.core.ExpectedInfo
import org.jetbrains.kotlin.idea.core.fuzzyType
import org.jetbrains.kotlin.idea.util.CallType
import org.jetbrains.kotlin.idea.util.fuzzyReturnType
import org.jetbrains.kotlin.resolve.descriptorUtil.hasDefaultValue
import org.jetbrains.kotlin.resolve.calls.util.getValueParametersCountFromFunctionType
import org.jetbrains.kotlin.resolve.descriptorUtil.hasDefaultValue
import org.jetbrains.kotlin.types.KotlinType
import java.util.*
@@ -94,8 +94,11 @@ class InsertHandlerProvider(
fun addPotentiallyInferred(type: KotlinType) {
val descriptor = type.constructor.declarationDescriptor as? TypeParameterDescriptor
if (descriptor != null && descriptor in typeParameters) {
if (descriptor != null && descriptor in typeParameters && descriptor !in potentiallyInferred) {
potentiallyInferred.add(descriptor)
// Add possible inferred by type-arguments of upper-bound of parameter
// e.g. <T, C: Iterable<T>>, so T inferred from C
descriptor.upperBounds.filter { it.arguments.isNotEmpty() }.forEach(::addPotentiallyInferred)
}
if (type.isFunctionType && getValueParametersCountFromFunctionType(type) <= 1) {
@@ -0,0 +1,12 @@
class Cn<T>
fun <T, C: Cn<T>> C.some(arg: (T) -> Unit): C {
return this
}
fun main(args: Array<String>) {
val x = Cn<String>()
x.<caret>
}
//ELEMENT: some
@@ -0,0 +1,12 @@
class Cn<T>
fun <T, C: Cn<T>> C.some(arg: (T) -> Unit): C {
return this
}
fun main(args: Array<String>) {
val x = Cn<String>()
x.some { }
}
//ELEMENT: some
@@ -216,6 +216,12 @@ public class BasicCompletionHandlerTestGenerated extends AbstractBasicCompletion
doTest(fileName);
}
@TestMetadata("TypeInferedFromWrapperType.kt")
public void testTypeInferedFromWrapperType() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/basic/TypeInferedFromWrapperType.kt");
doTest(fileName);
}
@TestMetadata("TypeParameter.kt")
public void testTypeParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/basic/TypeParameter.kt");