mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-04-15 15:52:16 +00:00
Support parenthesized left expression for ExpectedTypeFromCast
This commit is contained in:
@@ -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) {
|
||||
|
||||
21
compiler/testData/diagnostics/tests/inference/expectedTypeFromCastParenthesized.kt
vendored
Normal file
21
compiler/testData/diagnostics/tests/inference/expectedTypeFromCastParenthesized.kt
vendored
Normal 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
|
||||
24
compiler/testData/diagnostics/tests/inference/expectedTypeFromCastParenthesized.txt
vendored
Normal file
24
compiler/testData/diagnostics/tests/inference/expectedTypeFromCastParenthesized.txt
vendored
Normal 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
|
||||
}
|
||||
@@ -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");
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user