Refine smart cast check for type parameters
This commit is contained in:
@@ -226,6 +226,22 @@ public class DataFlowAnalyzer {
|
||||
}
|
||||
}
|
||||
|
||||
// For cases like:
|
||||
// fun bar(x: Any) {}
|
||||
// fun <T : Any?> foo(x: T) {
|
||||
// if (x != null) {
|
||||
// bar(x) // Should be allowed with smart cast
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// It doesn't handled by upper code with getPossibleTypes because smart cast of T after `x != null` is still has same type T.
|
||||
// But at the same time we're sure that `x` can't be null and just check for such cases manually
|
||||
if (!c.dataFlowInfo.getNullability(dataFlowValue).canBeNull()
|
||||
&& JetTypeChecker.DEFAULT.isSubtypeOf(expressionType, TypeUtils.makeNullable(c.expectedType))) {
|
||||
smartCastManager.recordCastOrError(expression, expressionType, c.trace, dataFlowValue.isPredictable(), false);
|
||||
return expressionType;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
// !DIAGNOSTICS: -UNUSED_EXPRESSION,-UNUSED_VARIABLE,-UNUSED_PARAMETER,-ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE,-UNUSED_VALUE
|
||||
|
||||
fun <T : CharSequence> bar1(x: T) {}
|
||||
fun bar2(x: CharSequence) {}
|
||||
fun bar3(x: String) {}
|
||||
|
||||
fun <T : CharSequence?> foo(x: T) {
|
||||
var y1: CharSequence = ""
|
||||
var y2: String = ""
|
||||
if (x != null) {
|
||||
if (<!SENSELESS_COMPARISON!>x != null<!>) {}
|
||||
|
||||
y1 = <!DEBUG_INFO_SMARTCAST!>x<!>
|
||||
y2 = <!TYPE_MISMATCH!>x<!>
|
||||
|
||||
<!TYPE_INFERENCE_UPPER_BOUND_VIOLATED!>bar1<!>(x)
|
||||
bar2(<!DEBUG_INFO_SMARTCAST!>x<!>)
|
||||
bar3(<!TYPE_MISMATCH!>x<!>)
|
||||
}
|
||||
|
||||
if (x is String) {
|
||||
y1 = <!DEBUG_INFO_SMARTCAST!>x<!>
|
||||
y2 = <!DEBUG_INFO_SMARTCAST!>x<!>
|
||||
|
||||
bar1(<!DEBUG_INFO_SMARTCAST!>x<!>)
|
||||
bar2(<!DEBUG_INFO_SMARTCAST!>x<!>)
|
||||
bar3(<!DEBUG_INFO_SMARTCAST!>x<!>)
|
||||
}
|
||||
|
||||
if (x is CharSequence) {
|
||||
y1 = <!DEBUG_INFO_SMARTCAST!>x<!>
|
||||
y2 = <!TYPE_MISMATCH!>x<!>
|
||||
|
||||
bar1(<!DEBUG_INFO_SMARTCAST!>x<!>)
|
||||
bar2(<!DEBUG_INFO_SMARTCAST!>x<!>)
|
||||
bar3(<!TYPE_MISMATCH!>x<!>)
|
||||
}
|
||||
|
||||
if (1 == 1) {
|
||||
val y = x!!
|
||||
<!TYPE_INFERENCE_UPPER_BOUND_VIOLATED!>bar1<!>(x)
|
||||
bar2(<!DEBUG_INFO_SMARTCAST!>x<!>)
|
||||
bar3(<!TYPE_MISMATCH!>x<!>)
|
||||
|
||||
<!TYPE_INFERENCE_UPPER_BOUND_VIOLATED!>bar1<!>(y)
|
||||
bar2(<!DEBUG_INFO_SMARTCAST!>y<!>)
|
||||
bar3(<!TYPE_MISMATCH!>y<!>)
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package
|
||||
|
||||
internal fun </*0*/ T : kotlin.CharSequence> bar1(/*0*/ x: T): kotlin.Unit
|
||||
internal fun bar2(/*0*/ x: kotlin.CharSequence): kotlin.Unit
|
||||
internal fun bar3(/*0*/ x: kotlin.String): kotlin.Unit
|
||||
internal fun </*0*/ T : kotlin.CharSequence?> foo(/*0*/ x: T): kotlin.Unit
|
||||
@@ -28,9 +28,8 @@ class A<T : CharSequence?, E1 : T, E2: T?> {
|
||||
|
||||
t = <!TYPE_MISMATCH!>y<!>
|
||||
|
||||
// Could be smart-cast
|
||||
if (y != null) {
|
||||
t = <!TYPE_MISMATCH!>y<!>
|
||||
t = <!DEBUG_INFO_SMARTCAST!>y<!>
|
||||
}
|
||||
|
||||
if (tN != null) {
|
||||
|
||||
@@ -6167,6 +6167,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("smartCastsValueArgument.kt")
|
||||
public void testSmartCastsValueArgument() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/nullability/smartCastsValueArgument.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("tpBoundsViolation.kt")
|
||||
public void testTpBoundsViolation() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/nullability/tpBoundsViolation.kt");
|
||||
|
||||
Reference in New Issue
Block a user