[FE, Java resolve] Support inheritors of sealed Java type without permits clause

This fixes a false negative NO_ELSE_IN_WHEN in K2 and incidentally
also fixes a false positive NO_ELSE_IN_WHEN in K1 since the fix is in
the common code.

#KT-62491 Fixed
This commit is contained in:
Kirill Rakhman
2023-10-24 10:00:18 +02:00
committed by Space Team
parent 15d3bf5e25
commit ac203591e5
22 changed files with 291 additions and 21 deletions
@@ -10,11 +10,29 @@ public final class A extends Base {}
// FILE: B.java
public sealed class B extends Base permits B.C, B.D {
public static final class C implements B {}
public static final class C extends B {}
public static non-sealed class D extends B {}
}
// FILE: SameFile.java
public sealed class SameFile {
public static final class A extends SameFile {}
public static sealed class B extends SameFile {
public static final class C extends B {}
public static non-sealed class D extends B {}
}
}
// FILE: SameFileNonSealed.java
public class SameFileNonSealed {
public static final class A extends SameFileNonSealed {}
public static class B extends SameFileNonSealed {
public static final class C extends B {}
public static class D extends B {}
}
}
// FILE: main.kt
fun test_ok_1(base: Base) {
val x = when (base) {
@@ -31,6 +49,21 @@ fun test_ok_2(base: Base) {
}
}
fun test_ok_3(sameFile: SameFile) {
val x = when (sameFile) {
is SameFile.A -> 1
is SameFile.B -> 2
}
}
fun test_ok_4(sameFile: SameFile) {
val x = when (sameFile) {
is SameFile.A -> 1
is SameFile.B.C -> 2
is SameFile.B.D -> 3
}
}
fun test_error_1(base: Base) {
val x = <!NO_ELSE_IN_WHEN!>when<!> (base) {
is A -> 1
@@ -43,3 +76,31 @@ fun test_error_2(base: Base) {
is B.C -> 2
}
}
fun test_error_3(sameFile: SameFile) {
val x = <!NO_ELSE_IN_WHEN!>when<!> (sameFile) {
is SameFile.A -> 1
}
}
fun test_error_4(sameFile: SameFile) {
val x = <!NO_ELSE_IN_WHEN!>when<!> (sameFile) {
is SameFile.A -> 1
is SameFile.B.C -> 2
}
}
fun test_error_5(sameFile: SameFileNonSealed) {
val x = <!NO_ELSE_IN_WHEN!>when<!> (sameFile) {
is SameFileNonSealed.A -> 1
is SameFileNonSealed.B -> 2
}
}
fun test_error_6(sameFile: SameFileNonSealed) {
val x = <!NO_ELSE_IN_WHEN!>when<!> (sameFile) {
is SameFileNonSealed.A -> 1
is SameFileNonSealed.B.C -> 2
is SameFileNonSealed.B.D -> 2
}
}
@@ -2,8 +2,14 @@ package
public fun test_error_1(/*0*/ base: Base): kotlin.Unit
public fun test_error_2(/*0*/ base: Base): kotlin.Unit
public fun test_error_3(/*0*/ sameFile: SameFile): kotlin.Unit
public fun test_error_4(/*0*/ sameFile: SameFile): kotlin.Unit
public fun test_error_5(/*0*/ sameFile: SameFileNonSealed): kotlin.Unit
public fun test_error_6(/*0*/ sameFile: SameFileNonSealed): kotlin.Unit
public fun test_ok_1(/*0*/ base: Base): kotlin.Unit
public fun test_ok_2(/*0*/ base: Base): kotlin.Unit
public fun test_ok_3(/*0*/ sameFile: SameFile): kotlin.Unit
public fun test_ok_4(/*0*/ sameFile: SameFile): kotlin.Unit
public final class A : Base {
public constructor A()
@@ -39,3 +45,74 @@ public sealed class Base {
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public sealed class SameFile {
public constructor SameFile()
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 A : SameFile {
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
}
public sealed class B : SameFile {
public constructor B()
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 C : SameFile.B {
public constructor C()
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 D : SameFile.B {
public constructor D()
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 SameFileNonSealed {
public constructor SameFileNonSealed()
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 A : SameFileNonSealed {
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
}
public open class B : SameFileNonSealed {
public constructor B()
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 C : SameFileNonSealed.B {
public constructor C()
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 D : SameFileNonSealed.B {
public constructor D()
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
}
}
}
@@ -20,6 +20,12 @@ public enum E implements Base {
First, Second
}
// FILE: SameFile.java
public sealed interface SameFile {
public static final class A implements SameFile {}
public static non-sealed class B implements SameFile {}
}
// FILE: main.kt
fun test_ok_1(base: Base) {
val x = when (base) {
@@ -39,6 +45,13 @@ fun test_ok_2(base: Base) {
}
}
fun test_ok_3(sameFile: SameFile) {
val x = when (sameFile) {
is SameFile.A -> 1
is SameFile.B -> 2
}
}
fun test_error_1(base: Base) {
val x = <!NO_ELSE_IN_WHEN!>when<!> (base) {
is A -> 1
@@ -63,3 +76,9 @@ fun test_error_3(base: Base) {
E.Second -> 5
}
}
fun test_error_4(sameFile: SameFile) {
val x = <!NO_ELSE_IN_WHEN!>when<!> (sameFile) {
is SameFile.A -> 1
}
}
@@ -3,8 +3,10 @@ package
public fun test_error_1(/*0*/ base: Base): kotlin.Unit
public fun test_error_2(/*0*/ base: Base): kotlin.Unit
public fun test_error_3(/*0*/ base: Base): kotlin.Unit
public fun test_error_4(/*0*/ sameFile: SameFile): kotlin.Unit
public fun test_ok_1(/*0*/ base: Base): kotlin.Unit
public fun test_ok_2(/*0*/ base: Base): kotlin.Unit
public fun test_ok_3(/*0*/ sameFile: SameFile): kotlin.Unit
public interface A : Base {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
@@ -59,3 +61,24 @@ public final enum class E : kotlin.Enum<E!>, Base {
public final /*synthesized*/ fun valueOf(/*0*/ value: kotlin.String): E
public final /*synthesized*/ fun values(): kotlin.Array<E>
}
public sealed interface SameFile {
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 A : SameFile {
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
}
public open class B : SameFile {
public constructor B()
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
}
}