Files
kotlin-fork/compiler/testData/diagnostics/testsWithStdLib/equalityCompatibilityCommonCases.fir.kt
T
Nikolay Lunyak f0720c1d12 [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
2023-03-31 15:01:50 +00:00

75 lines
1.6 KiB
Kotlin
Vendored

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<!>
}
}