From 69c26685301dd1a77bfd5ad68aa5904b54f2f4ce Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Mon, 14 Dec 2015 18:45:17 +0300 Subject: [PATCH] Don't allow to use own members when resolve super constructor call in (companion) object --- .../ClassResolutionScopesSupport.kt | 2 +- ...ompanionObjectSuperConstructorArguments.kt | 19 +++++++ ...mpanionObjectSuperConstructorArguments.txt | 50 +++++++++++++++++++ .../objectSuperConstructorArguments.kt | 8 +++ .../objectSuperConstructorArguments.txt | 24 +++++++++ .../checkers/DiagnosticsTestGenerated.java | 12 +++++ 6 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/diagnostics/tests/scopes/classHeader/companionObjectSuperConstructorArguments.kt create mode 100644 compiler/testData/diagnostics/tests/scopes/classHeader/companionObjectSuperConstructorArguments.txt create mode 100644 compiler/testData/diagnostics/tests/scopes/classHeader/objectSuperConstructorArguments.kt create mode 100644 compiler/testData/diagnostics/tests/scopes/classHeader/objectSuperConstructorArguments.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/ClassResolutionScopesSupport.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/ClassResolutionScopesSupport.kt index 903215fa42b..db504d8fe79 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/ClassResolutionScopesSupport.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/ClassResolutionScopesSupport.kt @@ -45,7 +45,7 @@ class ClassResolutionScopesSupport( } public val scopeForConstructorHeaderResolution: () -> LexicalScope = storageManager.createLazyValue { - scopeWithGenerics(scopeForStaticMemberDeclarationResolution()) + scopeWithGenerics(if (classDescriptor.kind.isSingleton) scopeForCompanionObjectHeaderResolution() else scopeForStaticMemberDeclarationResolution()) } private val inheritanceScopeWithoutMe: () -> LexicalScope = storageManager.createLazyValue(onRecursion = createThrowingLexicalScope) { diff --git a/compiler/testData/diagnostics/tests/scopes/classHeader/companionObjectSuperConstructorArguments.kt b/compiler/testData/diagnostics/tests/scopes/classHeader/companionObjectSuperConstructorArguments.kt new file mode 100644 index 00000000000..2eb3300447f --- /dev/null +++ b/compiler/testData/diagnostics/tests/scopes/classHeader/companionObjectSuperConstructorArguments.kt @@ -0,0 +1,19 @@ +open class S(val a: Any, val b: Any, val c: Any) {} + +interface A { + companion object : S(prop1, prop2, func()) { + val prop1 = 1 + val prop2: Int + get() = 1 + fun func() {} + } +} + +class B { + companion object : S(prop1, prop2, func()) { + val prop1 = 1 + val prop2: Int + get() = 1 + fun func() {} + } +} diff --git a/compiler/testData/diagnostics/tests/scopes/classHeader/companionObjectSuperConstructorArguments.txt b/compiler/testData/diagnostics/tests/scopes/classHeader/companionObjectSuperConstructorArguments.txt new file mode 100644 index 00000000000..8d6e6cffa9f --- /dev/null +++ b/compiler/testData/diagnostics/tests/scopes/classHeader/companionObjectSuperConstructorArguments.txt @@ -0,0 +1,50 @@ +package + +public interface 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 companion object Companion : S { + private constructor Companion() + public final override /*1*/ /*fake_override*/ val a: kotlin.Any + public final override /*1*/ /*fake_override*/ val b: kotlin.Any + public final override /*1*/ /*fake_override*/ val c: kotlin.Any + public final val prop1: kotlin.Int = 1 + public final val prop2: kotlin.Int + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public final fun func(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } +} + +public final class B { + 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 companion object Companion : S { + private constructor Companion() + public final override /*1*/ /*fake_override*/ val a: kotlin.Any + public final override /*1*/ /*fake_override*/ val b: kotlin.Any + public final override /*1*/ /*fake_override*/ val c: kotlin.Any + public final val prop1: kotlin.Int = 1 + public final val prop2: kotlin.Int + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public final fun func(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } +} + +public open class S { + public constructor S(/*0*/ a: kotlin.Any, /*1*/ b: kotlin.Any, /*2*/ c: kotlin.Any) + public final val a: kotlin.Any + public final val b: kotlin.Any + public final val c: kotlin.Any + 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 +} diff --git a/compiler/testData/diagnostics/tests/scopes/classHeader/objectSuperConstructorArguments.kt b/compiler/testData/diagnostics/tests/scopes/classHeader/objectSuperConstructorArguments.kt new file mode 100644 index 00000000000..4304fc3db6f --- /dev/null +++ b/compiler/testData/diagnostics/tests/scopes/classHeader/objectSuperConstructorArguments.kt @@ -0,0 +1,8 @@ +open class S(val a: Any, val b: Any, val c: Any) {} + +object A : S(prop1, prop2, func()) { + val prop1 = 1 + val prop2: Int + get() = 1 + fun func() {} +} diff --git a/compiler/testData/diagnostics/tests/scopes/classHeader/objectSuperConstructorArguments.txt b/compiler/testData/diagnostics/tests/scopes/classHeader/objectSuperConstructorArguments.txt new file mode 100644 index 00000000000..022c1fba0e8 --- /dev/null +++ b/compiler/testData/diagnostics/tests/scopes/classHeader/objectSuperConstructorArguments.txt @@ -0,0 +1,24 @@ +package + +public object A : S { + private constructor A() + public final override /*1*/ /*fake_override*/ val a: kotlin.Any + public final override /*1*/ /*fake_override*/ val b: kotlin.Any + public final override /*1*/ /*fake_override*/ val c: kotlin.Any + public final val prop1: kotlin.Int = 1 + public final val prop2: kotlin.Int + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public final fun func(): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public open class S { + public constructor S(/*0*/ a: kotlin.Any, /*1*/ b: kotlin.Any, /*2*/ c: kotlin.Any) + public final val a: kotlin.Any + public final val b: kotlin.Any + public final val c: kotlin.Any + 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 +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 70a8f43d485..0901fc6eadc 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -14018,6 +14018,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("companionObjectSuperConstructorArguments.kt") + public void testCompanionObjectSuperConstructorArguments() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/scopes/classHeader/companionObjectSuperConstructorArguments.kt"); + doTest(fileName); + } + @TestMetadata("constructors.kt") public void testConstructors() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/scopes/classHeader/constructors.kt"); @@ -14036,6 +14042,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("objectSuperConstructorArguments.kt") + public void testObjectSuperConstructorArguments() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/scopes/classHeader/objectSuperConstructorArguments.kt"); + doTest(fileName); + } + @TestMetadata("simpleDelegation.kt") public void testSimpleDelegation() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/scopes/classHeader/simpleDelegation.kt");