Support parenthesized left expression for ExpectedTypeFromCast

This commit is contained in:
Stanislav Erokhin
2017-10-16 13:03:18 +03:00
parent 539e655802
commit b9fa8d4d96
5 changed files with 75 additions and 5 deletions

View File

@@ -180,12 +180,25 @@ class GenericCandidateResolver(
private fun getBinaryWithTypeParent(calleeExpression: KtExpression?): KtBinaryExpressionWithTypeRHS? {
val callExpression = calleeExpression?.parent.safeAs<KtCallExpression>() ?: return null
val parent = callExpression.parent
return when (parent) {
is KtBinaryExpressionWithTypeRHS -> parent
is KtQualifiedExpression -> parent.parent.safeAs<KtBinaryExpressionWithTypeRHS>().takeIf { parent.selectorExpression == callExpression }
else -> null
val possibleQualifiedExpression = callExpression.parent
val targetExpression = if (possibleQualifiedExpression is KtQualifiedExpression) {
if (possibleQualifiedExpression.selectorExpression != callExpression) return null
possibleQualifiedExpression
}
else {
callExpression
}
return targetExpression.topParenthesizedParentOrMe().parent.safeAs<KtBinaryExpressionWithTypeRHS>()
}
private fun KtExpression.topParenthesizedParentOrMe(): KtExpression {
var result: KtExpression = this
while (KtPsiUtil.deparenthesizeOnce(result.parent.safeAs()) == result) {
result = result.parent.safeAs() ?: break
}
return result
}
private fun addValidityConstraintsForConstituentTypes(builder: ConstraintSystem.Builder, type: KotlinType) {

View File

@@ -0,0 +1,21 @@
// !LANGUAGE: +ExpectedTypeFromCast
@Target(AnnotationTarget.EXPRESSION)
annotation class bar
fun <T> foo(): T = TODO()
fun <V> id(value: V) = value
val par1 = (foo()) as String
val par2 = ((foo())) as String
val par3 = (dd@ (foo())) as String
val par4 = ( @bar() (foo())) as String
object X {
fun <T> foo(): T = TODO()
}
val par5 = ( @bar() X.foo()) as String

View File

@@ -0,0 +1,24 @@
package
public val par1: kotlin.String
public val par2: kotlin.String
public val par3: kotlin.String
public val par4: kotlin.String
public val par5: kotlin.String
public fun </*0*/ T> foo(): T
public fun </*0*/ V> id(/*0*/ value: V): V
public object X {
private constructor X()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final fun </*0*/ T> foo(): T
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@kotlin.annotation.Target(allowedTargets = {AnnotationTarget.EXPRESSION}) public final annotation class bar : kotlin.Annotation {
public constructor bar()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}

View File

@@ -10240,6 +10240,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("expectedTypeFromCastParenthesized.kt")
public void testExpectedTypeFromCastParenthesized() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/expectedTypeFromCastParenthesized.kt");
doTest(fileName);
}
@TestMetadata("expectedTypeWithGenerics.kt")
public void testExpectedTypeWithGenerics() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/expectedTypeWithGenerics.kt");

View File

@@ -10240,6 +10240,12 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
doTest(fileName);
}
@TestMetadata("expectedTypeFromCastParenthesized.kt")
public void testExpectedTypeFromCastParenthesized() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/expectedTypeFromCastParenthesized.kt");
doTest(fileName);
}
@TestMetadata("expectedTypeWithGenerics.kt")
public void testExpectedTypeWithGenerics() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/inference/expectedTypeWithGenerics.kt");