[FIR] Fix K2 behavior according to RULES1
The compiler should only report diagnostics for
comparisons over builtins and identity-less types,
other incompatibilities should be reported
via inspections.
It's ok that in `equalityChecksOnIntegerTypes`
instead of `EQUALITY_NOT_APPLICABLE_WARNING` we get
`EQUALITY_NOT_APPLICABLE`, because
`ProperEqualityChecksInBuilderInferenceCalls`
is already active by default.
This change also replaces the notion of a representative superclass
with the least upper bound.
This makes complex types like
intersection/flexible transparent to
RULES1-based compatibility checks.
One way to look at it is to think
that this is an automatic way of handling
type parameters: automatic picking of
"interesting" bounds, and checking them against one another.
Note that `TypeIntersector.intersectTypes`
for `Int` and `T` where `T` is a type parameter
may return both `{Int & T}` or `null`
depending on `T`-s bounds. At the same time,
for type parameters `T` and `K` it will
always return `{T & K}`.
`ConeTypeIntersector.intersectTypes`, on the
other hand, will always return `{Int & T}`
irrespectively of the bounds. Meaning, the two
intersectors differ in corner cases.
`lowerBoundIfFlexible` call in `isLiterallyTypeParameter` is backed by
the `equalityOfFlexibleTypeParameters` test.
^KT-35134 #fixed-in-k2
^KT-22499 #fixed-in-k2
^KT-46383 #fixed-in-k2
This commit is contained in:
committed by
Space Team
parent
06e687addd
commit
f0720c1d12
Vendored
+10
@@ -0,0 +1,10 @@
|
||||
// ISSUE: KT-54473
|
||||
|
||||
interface I
|
||||
class A : I
|
||||
class B : I
|
||||
|
||||
fun test(a: A, b: B) {
|
||||
a == b
|
||||
a == b as I
|
||||
}
|
||||
Vendored
+10
@@ -0,0 +1,10 @@
|
||||
// ISSUE: KT-54473
|
||||
|
||||
interface I
|
||||
class A : I
|
||||
class B : I
|
||||
|
||||
fun test(a: A, b: B) {
|
||||
<!EQUALITY_NOT_APPLICABLE!>a == b<!>
|
||||
a == b as I
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// ISSUE: KT-25808
|
||||
// WITH_STDLIB
|
||||
|
||||
// B.java
|
||||
public class B {
|
||||
}
|
||||
|
||||
// test.kt
|
||||
class A
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
(1 to A()) == A()
|
||||
(1 to B()) == B()
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// ISSUE: KT-25808
|
||||
// WITH_STDLIB
|
||||
|
||||
// B.java
|
||||
public class B {
|
||||
}
|
||||
|
||||
// test.kt
|
||||
class A
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
<!EQUALITY_NOT_APPLICABLE!>(1 to A()) == A()<!>
|
||||
<!EQUALITY_NOT_APPLICABLE!>(1 to B()) == B()<!>
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
// ISSUE: KT-35134
|
||||
|
||||
interface A
|
||||
|
||||
fun foo(a: Any) {
|
||||
if (a is A) {
|
||||
if (<!EQUALITY_NOT_APPLICABLE_WARNING!>a == (<!EQUALITY_NOT_APPLICABLE_WARNING!>a == 1<!>)<!>) {
|
||||
println(1)
|
||||
}
|
||||
|
||||
when (a) {
|
||||
<!CONFUSING_BRANCH_CONDITION_ERROR, EQUALITY_NOT_APPLICABLE_WARNING, INCOMPATIBLE_TYPES!>a == 1<!> -> print("1")
|
||||
}
|
||||
|
||||
if (<!EQUALITY_NOT_APPLICABLE!>(a <!USELESS_CAST!>as A<!>) == (<!EQUALITY_NOT_APPLICABLE_WARNING!>a == 1<!>)<!>) {
|
||||
println(1)
|
||||
}
|
||||
|
||||
when (a <!USELESS_CAST!>as A<!>) {
|
||||
<!CONFUSING_BRANCH_CONDITION_ERROR, EQUALITY_NOT_APPLICABLE_WARNING, INCOMPATIBLE_TYPES!>a == 1<!> -> print("1")
|
||||
}
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
// ISSUE: KT-35134
|
||||
|
||||
interface A
|
||||
|
||||
fun foo(a: Any) {
|
||||
if (a is A) {
|
||||
if (a == (a == 1)) {
|
||||
println(1)
|
||||
}
|
||||
|
||||
when (a) {
|
||||
<!CONFUSING_BRANCH_CONDITION_ERROR!>a == 1<!> -> print("1")
|
||||
}
|
||||
|
||||
if (<!EQUALITY_NOT_APPLICABLE!>(a <!USELESS_CAST!>as A<!>) == (a == 1)<!>) {
|
||||
println(1)
|
||||
}
|
||||
|
||||
when (a <!USELESS_CAST!>as A<!>) {
|
||||
<!CONFUSING_BRANCH_CONDITION_ERROR, INCOMPATIBLE_TYPES!>a == 1<!> -> print("1")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// ISSUE: KT-47979
|
||||
|
||||
enum class Foo { A, B }
|
||||
|
||||
fun test() {
|
||||
if (Triple(Foo.A, 1, 2) == Pair("a", "b")) println("Doesn't compile")
|
||||
if (Triple(0, 1, 2) == Pair(Foo.A, "a")) println("Doesn't compile")
|
||||
if (Triple(0, 1, 2) == Pair("a", "b")) println("Doesn't compile")
|
||||
if (Triple(Foo.A, 1, 2) == Pair(Foo.A, "a")) println("Compiles, but why?")
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// ISSUE: KT-47979
|
||||
|
||||
enum class Foo { A, B }
|
||||
|
||||
fun test() {
|
||||
if (<!EQUALITY_NOT_APPLICABLE!>Triple(Foo.A, 1, 2) == Pair("a", "b")<!>) println("Doesn't compile")
|
||||
if (<!EQUALITY_NOT_APPLICABLE!>Triple(0, 1, 2) == Pair(Foo.A, "a")<!>) println("Doesn't compile")
|
||||
if (<!EQUALITY_NOT_APPLICABLE!>Triple(0, 1, 2) == Pair("a", "b")<!>) println("Doesn't compile")
|
||||
if (Triple(Foo.A, 1, 2) == Pair(Foo.A, "a")) println("Compiles, but why?")
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
interface B
|
||||
|
||||
fun equalityNotApplicable(a: Int, b: B) {
|
||||
<!EQUALITY_NOT_APPLICABLE!>a == b<!>
|
||||
}
|
||||
|
||||
fun equalityNotApplicableSmartCast(a: Any?, b: Any?) {
|
||||
if (a is Int && b is B) {
|
||||
<!EQUALITY_NOT_APPLICABLE_WARNING!>a == b<!>
|
||||
}
|
||||
}
|
||||
|
||||
@JvmInline
|
||||
value class C(val int: Int)
|
||||
@JvmInline
|
||||
value class D(val bool: Boolean)
|
||||
|
||||
fun forbiddenIdentityEquals(c: C, d: D) {
|
||||
<!FORBIDDEN_IDENTITY_EQUALS!>c === d<!>
|
||||
}
|
||||
|
||||
fun forbiddenIdentityEqualsSmartCast(c: Any?, d: Any?) {
|
||||
if (c is C && d is D) {
|
||||
<!FORBIDDEN_IDENTITY_EQUALS_WARNING!>c === d<!>
|
||||
}
|
||||
}
|
||||
|
||||
fun implicitBoxingInIdentityEquals(i: Int, a: Any?) {
|
||||
<!IMPLICIT_BOXING_IN_IDENTITY_EQUALS!>i === a<!>
|
||||
}
|
||||
|
||||
fun implicitBoxingInIdentityEqualsSmartCast(i: Any?, a: Any?) {
|
||||
if (i is Int) {
|
||||
<!IMPLICIT_BOXING_IN_IDENTITY_EQUALS!>i === a<!>
|
||||
}
|
||||
}
|
||||
|
||||
fun deprecatedIdentityEquals(a: Int, b: Int) {
|
||||
<!DEPRECATED_IDENTITY_EQUALS!>a === b<!>
|
||||
}
|
||||
|
||||
fun deprecatedIdentityEqualsSmartCast(a: Any?, b: Any?) {
|
||||
if (a is Int && b is Int) {
|
||||
<!DEPRECATED_IDENTITY_EQUALS!>a === b<!>
|
||||
}
|
||||
}
|
||||
|
||||
fun incompatibleTypes(a: Int) = when(a) {
|
||||
<!INCOMPATIBLE_TYPES!>C(10)<!> -> 1
|
||||
else -> 2
|
||||
}
|
||||
|
||||
fun incompatibleTypesSmartCast(a: Any?) {
|
||||
if (a is Int) {
|
||||
when(a) {
|
||||
<!INCOMPATIBLE_TYPES!>C(10)<!> -> 1
|
||||
else -> 2
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum class E {
|
||||
A, B
|
||||
}
|
||||
|
||||
fun incompatibleEnumComparison(c: B, e: E) {
|
||||
<!INCOMPATIBLE_ENUM_COMPARISON_ERROR!>c == e<!>
|
||||
}
|
||||
|
||||
fun incompatibleEnumComparisonSmartCast(c: Any?, e: Any?) {
|
||||
if (c is B && e is E) {
|
||||
<!INCOMPATIBLE_ENUM_COMPARISON!>c == e<!>
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
interface B
|
||||
|
||||
fun equalityNotApplicable(a: Int, b: B) {
|
||||
<!EQUALITY_NOT_APPLICABLE!>a == b<!>
|
||||
}
|
||||
|
||||
fun equalityNotApplicableSmartCast(a: Any?, b: Any?) {
|
||||
if (a is Int && b is B) {
|
||||
a == b
|
||||
}
|
||||
}
|
||||
|
||||
@JvmInline
|
||||
value class C(val int: Int)
|
||||
@JvmInline
|
||||
value class D(val bool: Boolean)
|
||||
|
||||
fun forbiddenIdentityEquals(c: C, d: D) {
|
||||
<!EQUALITY_NOT_APPLICABLE, FORBIDDEN_IDENTITY_EQUALS!>c === d<!>
|
||||
}
|
||||
|
||||
fun forbiddenIdentityEqualsSmartCast(c: Any?, d: Any?) {
|
||||
if (c is C && d is D) {
|
||||
c === d
|
||||
}
|
||||
}
|
||||
|
||||
fun implicitBoxingInIdentityEquals(i: Int, a: Any?) {
|
||||
<!IMPLICIT_BOXING_IN_IDENTITY_EQUALS!>i === a<!>
|
||||
}
|
||||
|
||||
fun implicitBoxingInIdentityEqualsSmartCast(i: Any?, a: Any?) {
|
||||
if (i is Int) {
|
||||
i === a
|
||||
}
|
||||
}
|
||||
|
||||
fun deprecatedIdentityEquals(a: Int, b: Int) {
|
||||
<!DEPRECATED_IDENTITY_EQUALS!>a === b<!>
|
||||
}
|
||||
|
||||
fun deprecatedIdentityEqualsSmartCast(a: Any?, b: Any?) {
|
||||
if (a is Int && b is Int) {
|
||||
a === b
|
||||
}
|
||||
}
|
||||
|
||||
fun incompatibleTypes(a: Int) = when(a) {
|
||||
<!INCOMPATIBLE_TYPES!>C(10)<!> -> 1
|
||||
else -> 2
|
||||
}
|
||||
|
||||
fun incompatibleTypesSmartCast(a: Any?) {
|
||||
if (a is Int) {
|
||||
when(<!DEBUG_INFO_SMARTCAST!>a<!>) {
|
||||
C(10) -> 1
|
||||
else -> 2
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum class E {
|
||||
A, B
|
||||
}
|
||||
|
||||
fun incompatibleEnumComparison(c: B, e: E) {
|
||||
<!INCOMPATIBLE_ENUM_COMPARISON_ERROR!>c == e<!>
|
||||
}
|
||||
|
||||
fun incompatibleEnumComparisonSmartCast(c: Any?, e: Any?) {
|
||||
if (c is B && e is E) {
|
||||
c == e
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
// LANGUAGE: +ReportErrorsForComparisonOperators
|
||||
|
||||
fun nullableNothingIdentity(a: Int, b: Nothing?) {
|
||||
<!FORBIDDEN_IDENTITY_EQUALS, SENSELESS_COMPARISON!>a === b<!>
|
||||
}
|
||||
|
||||
fun samePrimitiveIdentity(a: Int, b: Int) {
|
||||
<!DEPRECATED_IDENTITY_EQUALS!>a === b<!>
|
||||
}
|
||||
|
||||
fun identityWithImplicitBoxing(a: Int, b: Any?) {
|
||||
<!IMPLICIT_BOXING_IN_IDENTITY_EQUALS!>a === b<!>
|
||||
}
|
||||
|
||||
enum class E1 { A, B }
|
||||
enum class E2 { C, D }
|
||||
|
||||
fun nullableEnums(a: E1?, b: E2?) {
|
||||
<!INCOMPATIBLE_ENUM_COMPARISON_ERROR!>a == b<!>
|
||||
}
|
||||
|
||||
fun <T> enumAsTypeParameterBound(a: T, b: Int) where T : Any, T : <!ONLY_ONE_CLASS_BOUND_ALLOWED!>E1<!> {
|
||||
<!EQUALITY_NOT_APPLICABLE!>a == b<!>
|
||||
}
|
||||
|
||||
fun <T, K> twoTypeParameters(a: T, b: K) where T : Number, K : <!FINAL_UPPER_BOUND!>String<!> {
|
||||
<!EQUALITY_NOT_APPLICABLE!>a == b<!>
|
||||
}
|
||||
|
||||
interface I1
|
||||
interface I2
|
||||
|
||||
enum class E3 : I1 { A, B }
|
||||
|
||||
fun <A> compareTypeParameterWithEnum(a: A) where A: I1, A: I2 {
|
||||
<!INCOMPATIBLE_ENUM_COMPARISON_ERROR!>a == E1.A<!>
|
||||
<!INCOMPATIBLE_ENUM_COMPARISON_ERROR!>a == E3.A<!>
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
// LANGUAGE: +ReportErrorsForComparisonOperators
|
||||
|
||||
fun nullableNothingIdentity(a: Int, b: Nothing?) {
|
||||
a === <!DEBUG_INFO_CONSTANT!>b<!>
|
||||
}
|
||||
|
||||
fun samePrimitiveIdentity(a: Int, b: Int) {
|
||||
<!DEPRECATED_IDENTITY_EQUALS!>a === b<!>
|
||||
}
|
||||
|
||||
fun identityWithImplicitBoxing(a: Int, b: Any?) {
|
||||
<!IMPLICIT_BOXING_IN_IDENTITY_EQUALS!>a === b<!>
|
||||
}
|
||||
|
||||
enum class E1 { A, B }
|
||||
enum class E2 { C, D }
|
||||
|
||||
fun nullableEnums(a: E1?, b: E2?) {
|
||||
a == b
|
||||
}
|
||||
|
||||
fun <T> enumAsTypeParameterBound(a: T, b: Int) where T : Any, T : <!ONLY_ONE_CLASS_BOUND_ALLOWED!>E1<!> {
|
||||
<!EQUALITY_NOT_APPLICABLE!>a == b<!>
|
||||
}
|
||||
|
||||
fun <T, K> twoTypeParameters(a: T, b: K) where T : Number, K : <!FINAL_UPPER_BOUND!>String<!> {
|
||||
a == b
|
||||
}
|
||||
|
||||
interface I1
|
||||
interface I2
|
||||
|
||||
enum class E3 : I1 { A, B }
|
||||
|
||||
fun <A> compareTypeParameterWithEnum(a: A) where A: I1, A: I2 {
|
||||
<!INCOMPATIBLE_ENUM_COMPARISON_ERROR!>a == E1.A<!>
|
||||
a == E3.A
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
// LANGUAGE: -ReportErrorsForComparisonOperators
|
||||
|
||||
fun nullableNothingIdentity(a: Int, b: Nothing?) {
|
||||
<!FORBIDDEN_IDENTITY_EQUALS_WARNING, SENSELESS_COMPARISON!>a === b<!>
|
||||
}
|
||||
|
||||
fun samePrimitiveIdentity(a: Int, b: Int) {
|
||||
<!DEPRECATED_IDENTITY_EQUALS!>a === b<!>
|
||||
}
|
||||
|
||||
fun identityWithImplicitBoxing(a: Int, b: Any?) {
|
||||
<!IMPLICIT_BOXING_IN_IDENTITY_EQUALS!>a === b<!>
|
||||
}
|
||||
|
||||
enum class E1 { A, B }
|
||||
enum class E2 { C, D }
|
||||
|
||||
fun nullableEnums(a: E1?, b: E2?) {
|
||||
<!INCOMPATIBLE_ENUM_COMPARISON!>a == b<!>
|
||||
}
|
||||
|
||||
fun <T> enumAsTypeParameterBound(a: T, b: Int) where T : Any, T : <!ONLY_ONE_CLASS_BOUND_ALLOWED!>E1<!> {
|
||||
<!EQUALITY_NOT_APPLICABLE!>a == b<!>
|
||||
}
|
||||
|
||||
fun <T, K> twoTypeParameters(a: T, b: K) where T : Number, K : <!FINAL_UPPER_BOUND!>String<!> {
|
||||
<!EQUALITY_NOT_APPLICABLE_WARNING!>a == b<!>
|
||||
}
|
||||
|
||||
interface I1
|
||||
interface I2
|
||||
|
||||
enum class E3 : I1 { A, B }
|
||||
|
||||
fun <A> compareTypeParameterWithEnum(a: A) where A: I1, A: I2 {
|
||||
<!INCOMPATIBLE_ENUM_COMPARISON!>a == E1.A<!>
|
||||
<!INCOMPATIBLE_ENUM_COMPARISON!>a == E3.A<!>
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
// LANGUAGE: -ReportErrorsForComparisonOperators
|
||||
|
||||
fun nullableNothingIdentity(a: Int, b: Nothing?) {
|
||||
a === <!DEBUG_INFO_CONSTANT!>b<!>
|
||||
}
|
||||
|
||||
fun samePrimitiveIdentity(a: Int, b: Int) {
|
||||
<!DEPRECATED_IDENTITY_EQUALS!>a === b<!>
|
||||
}
|
||||
|
||||
fun identityWithImplicitBoxing(a: Int, b: Any?) {
|
||||
<!IMPLICIT_BOXING_IN_IDENTITY_EQUALS!>a === b<!>
|
||||
}
|
||||
|
||||
enum class E1 { A, B }
|
||||
enum class E2 { C, D }
|
||||
|
||||
fun nullableEnums(a: E1?, b: E2?) {
|
||||
a == b
|
||||
}
|
||||
|
||||
fun <T> enumAsTypeParameterBound(a: T, b: Int) where T : Any, T : <!ONLY_ONE_CLASS_BOUND_ALLOWED!>E1<!> {
|
||||
<!EQUALITY_NOT_APPLICABLE!>a == b<!>
|
||||
}
|
||||
|
||||
fun <T, K> twoTypeParameters(a: T, b: K) where T : Number, K : <!FINAL_UPPER_BOUND!>String<!> {
|
||||
a == b
|
||||
}
|
||||
|
||||
interface I1
|
||||
interface I2
|
||||
|
||||
enum class E3 : I1 { A, B }
|
||||
|
||||
fun <A> compareTypeParameterWithEnum(a: A) where A: I1, A: I2 {
|
||||
<!INCOMPATIBLE_ENUM_COMPARISON_ERROR!>a == E1.A<!>
|
||||
a == E3.A
|
||||
}
|
||||
Reference in New Issue
Block a user