Take into account data flow info changes for special call (if/when/elvis/!!) arguments #KT-10824 Fixed
Smart casts on complex expressions look as no more possible
This commit is contained in:
+1
-1
@@ -187,7 +187,7 @@ public class ControlStructureTypingUtils {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateInfo(@NotNull ValueArgument valueArgument, @NotNull DataFlowInfo dataFlowInfo) {
|
public void updateInfo(@NotNull ValueArgument valueArgument, @NotNull DataFlowInfo dataFlowInfo) {
|
||||||
//todo
|
dataFlowInfoForArgumentsMap.put(valueArgument, dataFlowInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
class A(val s: String = "FAIL")
|
||||||
|
|
||||||
|
private fun foo(a: A?, aOther: A?): A {
|
||||||
|
return if (a == null) {
|
||||||
|
A()
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (aOther == null) {
|
||||||
|
return A()
|
||||||
|
}
|
||||||
|
aOther
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun box() = foo(A("???"), A("OK")).s
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
// See KT-10824: Smart cast depending on control flow does not work inside `if`
|
||||||
|
class A
|
||||||
|
fun foo(a: A?, aOther: A?): A {
|
||||||
|
return if (a == null) {
|
||||||
|
A()
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var newA = aOther
|
||||||
|
if (newA == null) {
|
||||||
|
newA = A()
|
||||||
|
}
|
||||||
|
<!DEBUG_INFO_SMARTCAST!>newA<!>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fun bar(a: A?, aOther: A?): A {
|
||||||
|
return if (a == null) {
|
||||||
|
A()
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (aOther == null) {
|
||||||
|
return A()
|
||||||
|
}
|
||||||
|
|
||||||
|
<!DEBUG_INFO_SMARTCAST!>aOther<!>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fun foo1(a: A?, aOther: A?): A {
|
||||||
|
val result = if (a == null) {
|
||||||
|
A()
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var newA = aOther
|
||||||
|
if (newA == null) {
|
||||||
|
newA = A()
|
||||||
|
}
|
||||||
|
<!DEBUG_INFO_SMARTCAST!>newA<!>
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
fun bar1(a: A?, aOther: A?): A {
|
||||||
|
val result = if (a == null) {
|
||||||
|
A()
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (aOther == null) {
|
||||||
|
return A()
|
||||||
|
}
|
||||||
|
|
||||||
|
<!DEBUG_INFO_SMARTCAST!>aOther<!>
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package
|
||||||
|
|
||||||
|
public fun bar(/*0*/ a: A?, /*1*/ aOther: A?): A
|
||||||
|
public fun bar1(/*0*/ a: A?, /*1*/ aOther: A?): A
|
||||||
|
public fun foo(/*0*/ a: A?, /*1*/ aOther: A?): A
|
||||||
|
public fun foo1(/*0*/ a: A?, /*1*/ aOther: A?): A
|
||||||
|
|
||||||
|
public final class A {
|
||||||
|
public constructor A()
|
||||||
|
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
|
||||||
|
}
|
||||||
@@ -15,10 +15,10 @@ fun bar(s: Any?): String {
|
|||||||
else {
|
else {
|
||||||
val u: Any? = null
|
val u: Any? = null
|
||||||
if (u !is String) return ""
|
if (u !is String) return ""
|
||||||
u
|
<!DEBUG_INFO_SMARTCAST!>u<!>
|
||||||
}) ?: "xyz"
|
}) ?: "xyz"
|
||||||
// Ideally we should have smart cast to String here
|
// Ideally we should have smart cast to String here
|
||||||
return <!TYPE_MISMATCH!>t<!>
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
fun baz(s: String?, r: String?): String {
|
fun baz(s: String?, r: String?): String {
|
||||||
|
|||||||
@@ -5,10 +5,10 @@ fun baz(s: String?): String {
|
|||||||
else if (s == "") {
|
else if (s == "") {
|
||||||
val u: String? = null
|
val u: String? = null
|
||||||
if (u == null) return ""
|
if (u == null) return ""
|
||||||
u
|
<!DEBUG_INFO_SMARTCAST!>u<!>
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
s
|
<!DEBUG_INFO_SMARTCAST!>s<!>
|
||||||
}
|
}
|
||||||
return <!DEBUG_INFO_SMARTCAST!>t<!>
|
return t
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
fun baz(s: String?, b: Boolean?): String {
|
fun baz(s: String?, b: Boolean?): String {
|
||||||
val t = if (if (b == null) return "" else <!DEBUG_INFO_SMARTCAST!>b<!>) {
|
val t = if (if (b == null) return "" else <!DEBUG_INFO_SMARTCAST!>b<!>) {
|
||||||
if (s == null) return ""
|
if (s == null) return ""
|
||||||
s
|
<!DEBUG_INFO_SMARTCAST!>s<!>
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (s != null) return <!DEBUG_INFO_SMARTCAST!>s<!>
|
if (s != null) return <!DEBUG_INFO_SMARTCAST!>s<!>
|
||||||
""
|
""
|
||||||
}
|
}
|
||||||
return <!DEBUG_INFO_SMARTCAST!>t<!>
|
return t
|
||||||
}
|
}
|
||||||
+4
-4
@@ -1,11 +1,11 @@
|
|||||||
fun baz(s: String?, u: String?): String {
|
fun baz(s: String?, u: String?): String {
|
||||||
val t = when(if (u == null) return "" else <!DEBUG_INFO_SMARTCAST!>u<!>) {
|
val t = when(if (u == null) return "" else <!DEBUG_INFO_SMARTCAST!>u<!>) {
|
||||||
"abc" -> u
|
"abc" -> <!DEBUG_INFO_SMARTCAST!>u<!>
|
||||||
"" -> {
|
"" -> {
|
||||||
if (s == null) return ""
|
if (s == null) return ""
|
||||||
s
|
<!DEBUG_INFO_SMARTCAST!>s<!>
|
||||||
}
|
}
|
||||||
else -> u
|
else -> <!DEBUG_INFO_SMARTCAST!>u<!>
|
||||||
}
|
}
|
||||||
return <!DEBUG_INFO_SMARTCAST!>t<!>
|
return t
|
||||||
}
|
}
|
||||||
@@ -7,9 +7,9 @@ fun baz(s: String?): String {
|
|||||||
val u: String? = null
|
val u: String? = null
|
||||||
if (u == null) return ""
|
if (u == null) return ""
|
||||||
// !! is detected as unnecessary here
|
// !! is detected as unnecessary here
|
||||||
u
|
<!DEBUG_INFO_SMARTCAST!>u<!>
|
||||||
}
|
}
|
||||||
return <!DEBUG_INFO_SMARTCAST!>t<!>
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
fun foo(s: String?): String {
|
fun foo(s: String?): String {
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
fun baz(s: String?): String {
|
fun baz(s: String?): String {
|
||||||
val t = if (s != null) s
|
val t = if (s != null) <!DEBUG_INFO_SMARTCAST!>s<!>
|
||||||
else {
|
else {
|
||||||
val u: String? = null
|
val u: String? = null
|
||||||
if (u == null) return ""
|
if (u == null) return ""
|
||||||
u
|
<!DEBUG_INFO_SMARTCAST!>u<!>
|
||||||
}
|
}
|
||||||
return <!DEBUG_INFO_SMARTCAST!>t<!>
|
return t
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
fun baz(s: String?): Int {
|
fun baz(s: String?): Int {
|
||||||
return <!DEBUG_INFO_SMARTCAST!>if (s == null) {
|
return if (s == null) {
|
||||||
""
|
""
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
val u: String? = null
|
val u: String? = null
|
||||||
if (u == null) return 0
|
if (u == null) return 0
|
||||||
u
|
<!DEBUG_INFO_SMARTCAST!>u<!>
|
||||||
}<!>.length
|
}.length
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,12 +2,12 @@ fun baz(s: String?, u: String?): String {
|
|||||||
val t = when(s) {
|
val t = when(s) {
|
||||||
is String -> {
|
is String -> {
|
||||||
if (u == null) return <!DEBUG_INFO_SMARTCAST!>s<!>
|
if (u == null) return <!DEBUG_INFO_SMARTCAST!>s<!>
|
||||||
u
|
<!DEBUG_INFO_SMARTCAST!>u<!>
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
if (u == null) return ""
|
if (u == null) return ""
|
||||||
u
|
<!DEBUG_INFO_SMARTCAST!>u<!>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return <!DEBUG_INFO_SMARTCAST!>t<!>
|
return t
|
||||||
}
|
}
|
||||||
@@ -13374,6 +13374,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("kt10824.kt")
|
||||||
|
public void testKt10824() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/regressions/kt10824.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("kt127.kt")
|
@TestMetadata("kt127.kt")
|
||||||
public void testKt127() throws Exception {
|
public void testKt127() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/regressions/kt127.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/regressions/kt127.kt");
|
||||||
|
|||||||
+6
@@ -7363,6 +7363,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("smartCastInsideIf.kt")
|
||||||
|
public void testSmartCastInsideIf() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/smartCasts/smartCastInsideIf.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("whenSmartCast.kt")
|
@TestMetadata("whenSmartCast.kt")
|
||||||
public void testWhenSmartCast() throws Exception {
|
public void testWhenSmartCast() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/smartCasts/whenSmartCast.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/smartCasts/whenSmartCast.kt");
|
||||||
|
|||||||
+2
-2
@@ -1,10 +1,10 @@
|
|||||||
fun baz(s: String?): Int {
|
fun baz(s: String?): Int {
|
||||||
return <info descr="Smart cast to kotlin.String">if</info> (s == null) {
|
return if (s == null) {
|
||||||
""
|
""
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
val u: String? = null
|
val u: String? = null
|
||||||
if (u == null) return 0
|
if (u == null) return 0
|
||||||
u
|
<info descr="Smart cast to kotlin.String">u</info>
|
||||||
}.length
|
}.length
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user