More precise type handling for equality #KT-2311 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
137a20e0ed
commit
5e9aa38950
+11
@@ -19,8 +19,10 @@ package org.jetbrains.kotlin.resolve.calls.smartcasts;
|
|||||||
import com.google.common.collect.*;
|
import com.google.common.collect.*;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
|
||||||
import org.jetbrains.kotlin.types.KotlinType;
|
import org.jetbrains.kotlin.types.KotlinType;
|
||||||
import org.jetbrains.kotlin.types.TypeUtils;
|
import org.jetbrains.kotlin.types.TypeUtils;
|
||||||
|
import org.jetbrains.kotlin.types.typeUtil.TypeUtilsKt;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
@@ -216,6 +218,15 @@ import static org.jetbrains.kotlin.resolve.calls.smartcasts.Nullability.NOT_NULL
|
|||||||
SetMultimap<DataFlowValue, KotlinType> newTypeInfo = newTypeInfo();
|
SetMultimap<DataFlowValue, KotlinType> newTypeInfo = newTypeInfo();
|
||||||
newTypeInfo.putAll(a, collectTypesFromMeAndParents(b));
|
newTypeInfo.putAll(a, collectTypesFromMeAndParents(b));
|
||||||
newTypeInfo.putAll(b, collectTypesFromMeAndParents(a));
|
newTypeInfo.putAll(b, collectTypesFromMeAndParents(a));
|
||||||
|
if (!a.getType().equals(b.getType())) {
|
||||||
|
// To avoid smart casts to Nothing or Nothing? and recording base types of own type
|
||||||
|
if (!KotlinBuiltIns.isNothingOrNullableNothing(b.getType()) && !TypeUtilsKt.isSubtypeOf(a.getType(), b.getType())) {
|
||||||
|
newTypeInfo.put(a, b.getType());
|
||||||
|
}
|
||||||
|
if (!KotlinBuiltIns.isNothingOrNullableNothing(a.getType()) && !TypeUtilsKt.isSubtypeOf(b.getType(), a.getType())) {
|
||||||
|
newTypeInfo.put(b, a.getType());
|
||||||
|
}
|
||||||
|
}
|
||||||
changed |= !newTypeInfo.isEmpty();
|
changed |= !newTypeInfo.isEmpty();
|
||||||
|
|
||||||
return !changed
|
return !changed
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
open class VeryBase
|
||||||
|
|
||||||
|
open class Base : VeryBase()
|
||||||
|
|
||||||
|
class Derived : Base() {
|
||||||
|
fun original(): VeryBase = this
|
||||||
|
}
|
||||||
|
|
||||||
|
class Another : Base()
|
||||||
|
|
||||||
|
fun foo(d: Derived, a: Another?): Base? {
|
||||||
|
// d is compared with d.original(): VeryBase but should retain its own type
|
||||||
|
if (d.original() != d) return null
|
||||||
|
// ad should be of type Base, not VeryBase
|
||||||
|
val ad = a ?: d
|
||||||
|
return ad
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package
|
||||||
|
|
||||||
|
public fun foo(/*0*/ d: Derived, /*1*/ a: Another?): Base?
|
||||||
|
|
||||||
|
public final class Another : Base {
|
||||||
|
public constructor Another()
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
public open class Base : VeryBase {
|
||||||
|
public constructor Base()
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
public final class Derived : Base {
|
||||||
|
public constructor Derived()
|
||||||
|
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 final fun original(): VeryBase
|
||||||
|
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||||
|
}
|
||||||
|
|
||||||
|
public open class VeryBase {
|
||||||
|
public constructor VeryBase()
|
||||||
|
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
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
fun foo(): Int {
|
||||||
|
val x: Any? = null
|
||||||
|
val y = 2
|
||||||
|
if (x == y) {
|
||||||
|
return <!DEBUG_INFO_SMARTCAST!>x<!> + y
|
||||||
|
}
|
||||||
|
return y
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package
|
||||||
|
|
||||||
|
public fun foo(): kotlin.Int
|
||||||
@@ -14643,6 +14643,18 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("typeDegradation.kt")
|
||||||
|
public void testTypeDegradation() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/typeDegradation.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("typeInComparison.kt")
|
||||||
|
public void testTypeInComparison() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/typeInComparison.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("varChangedInInitializer.kt")
|
@TestMetadata("varChangedInInitializer.kt")
|
||||||
public void testVarChangedInInitializer() throws Exception {
|
public void testVarChangedInInitializer() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/varChangedInInitializer.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/varChangedInInitializer.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user