FIR: Prohibit calling singletons' constructors

This commit is contained in:
Denis Zharkov
2020-04-17 10:56:36 +03:00
parent b62400124a
commit 7612f37c85
6 changed files with 118 additions and 4 deletions
@@ -0,0 +1,31 @@
class A {
companion object Comp {}
fun foo() {
<!INAPPLICABLE_CANDIDATE!>Comp<!>()
}
}
object B {
private val x = <!INAPPLICABLE_CANDIDATE!>B<!>()
}
class D {
companion object Comp2 {
operator fun invoke() {}
}
fun foo() {
Comp2()
}
}
enum class E {
X {
};
fun foo() {
<!UNRESOLVED_REFERENCE!>X<!>()
}
}
@@ -0,0 +1,70 @@
FILE: singletonConstructors.kt
public final class A : R|kotlin/Any| {
public constructor(): R|A| {
super<R|kotlin/Any|>()
}
public final companion object Comp : R|kotlin/Any| {
private constructor(): R|A.Comp| {
super<R|kotlin/Any|>()
}
}
public final fun foo(): R|kotlin/Unit| {
<Inapplicable(HIDDEN): [/A.Comp.Comp]>#()
}
}
public final object B : R|kotlin/Any| {
private constructor(): R|B| {
super<R|kotlin/Any|>()
}
private final val x: <ERROR TYPE REF: Inapplicable(HIDDEN): [/B.B]> = <Inapplicable(HIDDEN): [/B.B]>#()
private get(): <ERROR TYPE REF: Inapplicable(HIDDEN): [/B.B]>
}
public final class D : R|kotlin/Any| {
public constructor(): R|D| {
super<R|kotlin/Any|>()
}
public final companion object Comp2 : R|kotlin/Any| {
private constructor(): R|D.Comp2| {
super<R|kotlin/Any|>()
}
public final operator fun invoke(): R|kotlin/Unit| {
}
}
public final fun foo(): R|kotlin/Unit| {
Q|D.Comp2|.R|/D.Comp2.invoke|()
}
}
public final enum class E : R|kotlin/Enum<E>| {
private constructor(): R|E| {
super<R|kotlin/Enum<E>|>()
}
public final static enum entry X: R|E| = object : R|E| {
private constructor(): R|anonymous| {
super<R|E|>()
}
}
public final fun foo(): R|kotlin/Unit| {
<Unresolved name: X>#()
}
public final static fun values(): R|kotlin/Array<E>| {
}
public final static fun valueOf(value: R|kotlin/String|): R|E| {
}
}
@@ -2172,5 +2172,10 @@ public class FirDiagnosticsTestGenerated extends AbstractFirDiagnosticsTest {
public void testProtectedInCompanion() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/visibility/protectedInCompanion.kt");
}
@TestMetadata("singletonConstructors.kt")
public void testSingletonConstructors() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/visibility/singletonConstructors.kt");
}
}
}
@@ -2172,5 +2172,10 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos
public void testProtectedInCompanion() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/visibility/protectedInCompanion.kt");
}
@TestMetadata("singletonConstructors.kt")
public void testSingletonConstructors() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/visibility/singletonConstructors.kt");
}
}
}
@@ -476,6 +476,9 @@ internal object CheckVisibility : CheckerStage() {
val classSymbol = provider.getClassLikeSymbolByFqName(ownerClassId)
if (classSymbol is FirRegularClassSymbol) {
if (classSymbol.fir.classKind.isSingleton) {
sink.yieldApplicability(CandidateApplicability.HIDDEN)
}
checkVisibility(classSymbol.fir, classSymbol, sink, callInfo)
}
}
@@ -1,11 +1,11 @@
object A1<!CONSTRUCTOR_IN_OBJECT!>()<!> {
<!CONSTRUCTOR_IN_OBJECT!>constructor(x: Int = "", y: Int)<!> : this() {
<!CONSTRUCTOR_IN_OBJECT!>constructor(x: Int = "", y: Int)<!> : <!INAPPLICABLE_CANDIDATE!>this<!>() {
x + y
}
}
object A2 public <!CONSTRUCTOR_IN_OBJECT!>constructor(private val prop: Int)<!> {
<!CONSTRUCTOR_IN_OBJECT!>constructor(x: Int = "", y: Int)<!> : this(x * y) {
<!CONSTRUCTOR_IN_OBJECT!>constructor(x: Int = "", y: Int)<!> : <!INAPPLICABLE_CANDIDATE!>this<!>(x * y) {
x + y
}
}
@@ -19,6 +19,6 @@ val x = object <!CONSTRUCTOR_IN_OBJECT!>(val prop: Int)<!> {
class A3 {
companion object B<!CONSTRUCTOR_IN_OBJECT!>(val prop: Int)<!> {
public <!CONSTRUCTOR_IN_OBJECT!>constructor()<!> : this(2)
public <!CONSTRUCTOR_IN_OBJECT!>constructor()<!> : <!INAPPLICABLE_CANDIDATE!>this<!>(2)
}
}
}