[FIR] Fix processing constructors of sealed classes

- Allow declaring protected constructors in sealed classes
- Make default visibility of sealed class constructor `protected`

KT-44861
KT-44865
This commit is contained in:
Dmitriy Novozhilov
2021-02-11 13:20:03 +03:00
committed by TeamCityServer
parent 7c61ddc72b
commit 2d5b685535
47 changed files with 98 additions and 106 deletions
@@ -38,7 +38,7 @@ final class TopLevelPrivate /* pkg.TopLevelPrivate*/ {
}
public abstract class Season /* pkg.Season*/ {
private Season();// .ctor()
protected Season();// .ctor()
class Nested ...
@@ -53,8 +53,8 @@ public static final class Nested /* pkg.Season.Nested*/ extends pkg.Season {
public abstract class SealedWithArgs /* pkg.SealedWithArgs*/ {
private final int a;
private SealedWithArgs(int);// .ctor(int)
protected SealedWithArgs(int);// .ctor(int)
public final int getA();// getA()
}
}
@@ -37,4 +37,4 @@ package bar
import foo.Base
class E : <!HIDDEN, SEALED_SUPERTYPE!>Base<!>()
class E : <!SEALED_SUPERTYPE!>Base<!>()
@@ -1,7 +1,7 @@
sealed class Sealed <!NON_PRIVATE_CONSTRUCTOR_IN_SEALED!>protected constructor(val x: Int)<!> {
sealed class Sealed protected constructor(val x: Int) {
object FIRST : Sealed()
<!NON_PRIVATE_CONSTRUCTOR_IN_SEALED!>public constructor(): this(42)<!>
<!NON_PRIVATE_OR_PROTECTED_CONSTRUCTOR_IN_SEALED!>public constructor(): this(42)<!>
constructor(y: Int, z: Int): this(y + z)
}
@@ -14,4 +14,4 @@ class A : Base()
package a
class B : <!HIDDEN!>Base<!>()
class B : Base()
@@ -1,17 +0,0 @@
// ISSUE: KT-44861
// DIAGNOSTICS: -UNUSED_VARIABLE
sealed class Foo() {
class A : Foo()
class B : Foo()
}
fun Foo(kind: String = "A"): Foo = when (kind) {
"A" -> Foo.A()
"B" -> Foo.B()
else -> throw Exception()
}
fun main() {
val foo = <!SEALED_CLASS_CONSTRUCTOR_CALL!>Foo<!>()
}
+1
View File
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// ISSUE: KT-44861
// DIAGNOSTICS: -UNUSED_VARIABLE
@@ -2,21 +2,21 @@
// DIAGNOSTICS: -UNUSED_PARAMETER
sealed class Case1(val x: Int) {
<!NON_PRIVATE_CONSTRUCTOR_IN_SEALED!>protected constructor(s: String) : this(s.length)<!>
protected constructor(s: String) : this(s.length)
class Inheritor1 : Case1(10)
class Inheritor2 : Case1("Hello")
}
sealed class Case2 <!NON_PRIVATE_CONSTRUCTOR_IN_SEALED!>protected constructor(val x: Int)<!> {
<!NON_PRIVATE_CONSTRUCTOR_IN_SEALED!>protected constructor(s: String) : this(s.length)<!>
sealed class Case2 protected constructor(val x: Int) {
protected constructor(s: String) : this(s.length)
class Inheritor1 : Case2(10)
class Inheritor2 : Case2("Hello")
}
sealed class Case3 private constructor(val x: Int) {
<!NON_PRIVATE_CONSTRUCTOR_IN_SEALED!>protected constructor(s: String) : this(s.length)<!>
protected constructor(s: String) : this(s.length)
class Inheritor1 : Case3(10) // should OK
class Inheritor2 : Case3("Hello")
@@ -25,8 +25,8 @@ sealed class Case3 private constructor(val x: Int) {
class Case3Inheritor3 : <!INAPPLICABLE_CANDIDATE!>Case3<!>(20) // should be an error in 1.6 (?)
sealed class Case4 {
<!NON_PRIVATE_CONSTRUCTOR_IN_SEALED!>protected constructor(x: Int)<!>
<!NON_PRIVATE_CONSTRUCTOR_IN_SEALED!>protected constructor(s: String) : this(s.length)<!>
protected constructor(x: Int)
protected constructor(s: String) : this(s.length)
class Inheritor1 : Case4(10)
class Inheritor2 : Case4("Hello")
@@ -34,8 +34,8 @@ sealed class Case4 {
sealed class Case5() {
private constructor(x: Int) : this()
<!NON_PRIVATE_CONSTRUCTOR_IN_SEALED!>protected constructor(x: Byte) : this()<!>
<!NON_PRIVATE_CONSTRUCTOR_IN_SEALED!>internal constructor(x: Short) : this()<!>
<!NON_PRIVATE_CONSTRUCTOR_IN_SEALED!>public constructor(x: Long) : this()<!>
protected constructor(x: Byte) : this()
<!NON_PRIVATE_OR_PROTECTED_CONSTRUCTOR_IN_SEALED!>internal constructor(x: Short) : this()<!>
<!NON_PRIVATE_OR_PROTECTED_CONSTRUCTOR_IN_SEALED!>public constructor(x: Long) : this()<!>
constructor(x: Double) : this()
}
@@ -2,21 +2,21 @@
// DIAGNOSTICS: -UNUSED_PARAMETER
sealed class Case1(val x: Int) {
<!NON_PRIVATE_CONSTRUCTOR_IN_SEALED!>protected constructor(s: String) : this(s.length)<!>
protected constructor(s: String) : this(s.length)
class Inheritor1 : Case1(10)
class Inheritor2 : Case1("Hello")
}
sealed class Case2 <!NON_PRIVATE_CONSTRUCTOR_IN_SEALED!>protected constructor(val x: Int)<!> { // should be REDUNDANT_MODIFIER
<!NON_PRIVATE_CONSTRUCTOR_IN_SEALED!>protected constructor(s: String) : this(s.length)<!>
sealed class Case2 protected constructor(val x: Int) { // should be REDUNDANT_MODIFIER
protected constructor(s: String) : this(s.length)
class Inheritor1 : Case2(10)
class Inheritor2 : Case2("Hello")
}
sealed class Case3 private constructor(val x: Int) {
<!NON_PRIVATE_CONSTRUCTOR_IN_SEALED!>protected constructor(s: String) : this(s.length)<!>
protected constructor(s: String) : this(s.length)
class Inheritor1 : Case3(10) // should OK
class Inheritor2 : Case3("Hello")
@@ -25,8 +25,8 @@ sealed class Case3 private constructor(val x: Int) {
class Case3Inheritor3 : <!INAPPLICABLE_CANDIDATE!>Case3<!>(20) // should be an error in 1.6 (?)
sealed class Case4 {
<!NON_PRIVATE_CONSTRUCTOR_IN_SEALED!>protected constructor(x: Int)<!>
<!NON_PRIVATE_CONSTRUCTOR_IN_SEALED!>protected constructor(s: String) : this(s.length)<!>
protected constructor(x: Int)
protected constructor(s: String) : this(s.length)
class Inheritor1 : Case4(10)
class Inheritor2 : Case4("Hello")
@@ -34,8 +34,8 @@ sealed class Case4 {
sealed class Case5() {
private constructor(x: Int) : this()
<!NON_PRIVATE_CONSTRUCTOR_IN_SEALED!>protected constructor(x: Byte) : this()<!>
<!NON_PRIVATE_CONSTRUCTOR_IN_SEALED!>internal constructor(x: Short) : this()<!>
<!NON_PRIVATE_CONSTRUCTOR_IN_SEALED!>public constructor(x: Long) : this()<!>
protected constructor(x: Byte) : this()
<!NON_PRIVATE_OR_PROTECTED_CONSTRUCTOR_IN_SEALED!>internal constructor(x: Short) : this()<!>
<!NON_PRIVATE_OR_PROTECTED_CONSTRUCTOR_IN_SEALED!>public constructor(x: Long) : this()<!>
constructor(x: Double) : this()
}
@@ -17,8 +17,8 @@ val test3a = <!HIDDEN!>EnumClass<!>()
sealed class SealedClass
typealias Test4 = SealedClass
val test4 = <!SEALED_CLASS_CONSTRUCTOR_CALL!>Test4<!>()
val test4a = <!SEALED_CLASS_CONSTRUCTOR_CALL!>SealedClass<!>()
val test4 = <!HIDDEN!>Test4<!>()
val test4a = <!HIDDEN!>SealedClass<!>()
class Outer {
inner class Inner
@@ -1,5 +1,5 @@
sealed class Expr {
private constructor() /* primary */ {
protected constructor() /* primary */ {
super/*Any*/()
/* <init>() */
+4 -4
View File
@@ -1,7 +1,7 @@
FILE fqName:<root> fileName:/sealedClasses.kt
CLASS CLASS name:Expr modality:SEALED visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Expr
CONSTRUCTOR visibility:private <> () returnType:<root>.Expr [primary]
CONSTRUCTOR visibility:protected <> () returnType:<root>.Expr [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Expr modality:SEALED visibility:public superTypes:[kotlin.Any]'
@@ -10,7 +10,7 @@ FILE fqName:<root> fileName:/sealedClasses.kt
CONSTRUCTOR visibility:public <> (number:kotlin.Double) returnType:<root>.Expr.Const [primary]
VALUE_PARAMETER name:number index:0 type:kotlin.Double
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'private constructor <init> () [primary] declared in <root>.Expr'
DELEGATING_CONSTRUCTOR_CALL 'protected constructor <init> () [primary] declared in <root>.Expr'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Const modality:FINAL visibility:public superTypes:[<root>.Expr]'
PROPERTY name:number visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:number type:kotlin.Double visibility:private [final]
@@ -42,7 +42,7 @@ FILE fqName:<root> fileName:/sealedClasses.kt
VALUE_PARAMETER name:e1 index:0 type:<root>.Expr
VALUE_PARAMETER name:e2 index:1 type:<root>.Expr
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'private constructor <init> () [primary] declared in <root>.Expr'
DELEGATING_CONSTRUCTOR_CALL 'protected constructor <init> () [primary] declared in <root>.Expr'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Sum modality:FINAL visibility:public superTypes:[<root>.Expr]'
PROPERTY name:e1 visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:e1 type:<root>.Expr visibility:private [final]
@@ -83,7 +83,7 @@ FILE fqName:<root> fileName:/sealedClasses.kt
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Expr.NotANumber
CONSTRUCTOR visibility:private <> () returnType:<root>.Expr.NotANumber [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'private constructor <init> () [primary] declared in <root>.Expr'
DELEGATING_CONSTRUCTOR_CALL 'protected constructor <init> () [primary] declared in <root>.Expr'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS OBJECT name:NotANumber modality:FINAL visibility:public superTypes:[<root>.Expr]'
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
@@ -1,5 +1,5 @@
expect sealed class Ops {
private expect constructor() /* primary */
protected expect constructor() /* primary */
}
@@ -9,7 +9,7 @@ expect class Add : Ops {
}
sealed class Ops {
private constructor() /* primary */ {
protected constructor() /* primary */ {
super/*Any*/()
/* <init>() */
@@ -1,7 +1,7 @@
FILE fqName:<root> fileName:/expectedSealedClass.kt
CLASS CLASS name:Ops modality:SEALED visibility:public [expect] superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Ops
CONSTRUCTOR visibility:private <> () returnType:<root>.Ops [primary,expect]
CONSTRUCTOR visibility:protected <> () returnType:<root>.Ops [primary,expect]
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [expect,fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] declared in kotlin.Any
@@ -33,7 +33,7 @@ FILE fqName:<root> fileName:/expectedSealedClass.kt
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
CLASS CLASS name:Ops modality:SEALED visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Ops
CONSTRUCTOR visibility:private <> () returnType:<root>.Ops [primary]
CONSTRUCTOR visibility:protected <> () returnType:<root>.Ops [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Ops modality:SEALED visibility:public superTypes:[kotlin.Any]'
@@ -54,7 +54,7 @@ FILE fqName:<root> fileName:/expectedSealedClass.kt
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Add
CONSTRUCTOR visibility:public <> () returnType:<root>.Add [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'private constructor <init> () [primary] declared in <root>.Ops'
DELEGATING_CONSTRUCTOR_CALL 'protected constructor <init> () [primary] declared in <root>.Ops'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Add modality:FINAL visibility:public superTypes:[<root>.Ops]'
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden: