From bd5dbb665e1af5b3ac2f63eaea49b36377bb8b1a Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Tue, 17 Mar 2015 19:34:47 +0300 Subject: [PATCH] Fix header scope for secondary constructors Add companion object's scope and nested classes #KT-6996 fixed --- .../lazy/descriptors/LazyClassDescriptor.java | 12 ++++--- .../accessToCompanion.kt | 21 +++++++++++ .../companionObjectScope.kt | 17 +++++++++ .../companionObjectScope.txt | 35 +++++++++++++++++++ .../checkers/JetDiagnosticsTestGenerated.java | 6 ++++ .../BlackBoxCodegenTestGenerated.java | 6 ++++ 6 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/codegen/box/secondaryConstructors/accessToCompanion.kt create mode 100644 compiler/testData/diagnostics/tests/secondaryConstructors/companionObjectScope.kt create mode 100644 compiler/testData/diagnostics/tests/secondaryConstructors/companionObjectScope.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyClassDescriptor.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyClassDescriptor.java index 051caf4b5a1..e8a947b69a0 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyClassDescriptor.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyClassDescriptor.java @@ -277,20 +277,22 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes thisScope.setImplicitReceiver(this.getThisAsReceiverParameter()); thisScope.changeLockLevel(WritableScope.LockLevel.READING); - ClassDescriptor companionObjectDescriptor = getCompanionObjectDescriptor(); - JetScope companionObjectAdapterScope = (companionObjectDescriptor != null) ? new CompanionObjectMixinScope(companionObjectDescriptor) : JetScope.Empty.INSTANCE$; - return new ChainedScope( this, "ScopeForMemberDeclarationResolution: " + getName(), thisScope, getScopeForMemberLookup(), getScopeForClassHeaderResolution(), - companionObjectAdapterScope, + getCompanionObjectScope(), getStaticScope() ); } + private JetScope getCompanionObjectScope() { + ClassDescriptor companionObjectDescriptor = getCompanionObjectDescriptor(); + return (companionObjectDescriptor != null) ? new CompanionObjectMixinScope(companionObjectDescriptor) : JetScope.Empty.INSTANCE$; + } + @Override @NotNull public JetScope getScopeForInitializerResolution() { @@ -346,6 +348,8 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes this, "ScopeForSecondaryConstructorHeaderResolution: " + getName(), getScopeForClassHeaderResolution(), + getCompanionObjectScope(), + DescriptorUtils.getStaticNestedClassesScope(this), getStaticScope() ); } diff --git a/compiler/testData/codegen/box/secondaryConstructors/accessToCompanion.kt b/compiler/testData/codegen/box/secondaryConstructors/accessToCompanion.kt new file mode 100644 index 00000000000..ec5e0bbb1e6 --- /dev/null +++ b/compiler/testData/codegen/box/secondaryConstructors/accessToCompanion.kt @@ -0,0 +1,21 @@ +class A(val result: Int) { + companion object { + fun foo(): Int = 1 + val prop = 2 + val C = 3 + } + object B { + fun bar(): Int = 4 + val prop = 5 + } + object C { + } + + constructor() : this(foo() + prop + B.bar() + B.prop + C) {} +} + +fun box(): String { + val result = A().result + if (result != 15) return "fail: $result" + return "OK" +} diff --git a/compiler/testData/diagnostics/tests/secondaryConstructors/companionObjectScope.kt b/compiler/testData/diagnostics/tests/secondaryConstructors/companionObjectScope.kt new file mode 100644 index 00000000000..43a40a6e941 --- /dev/null +++ b/compiler/testData/diagnostics/tests/secondaryConstructors/companionObjectScope.kt @@ -0,0 +1,17 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER +class A { + companion object { + fun foo(): Int = 1 + val prop = 2 + val C = 3 + } + object B { + fun bar(): Int = 4 + val prop = 5 + } + object C { + } + + constructor(x: Int) {} + constructor() : this(foo() + prop + B.bar() + B.prop + C) {} +} diff --git a/compiler/testData/diagnostics/tests/secondaryConstructors/companionObjectScope.txt b/compiler/testData/diagnostics/tests/secondaryConstructors/companionObjectScope.txt new file mode 100644 index 00000000000..ce9dc54bddc --- /dev/null +++ b/compiler/testData/diagnostics/tests/secondaryConstructors/companionObjectScope.txt @@ -0,0 +1,35 @@ +package + +internal final class A { + public constructor A() + public constructor A(/*0*/ x: kotlin.Int) + 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 + + internal object B { + private constructor B() + internal final val prop: kotlin.Int = 5 + internal final fun bar(): kotlin.Int + 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 + } + + internal object C { + private 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 + } + + internal companion object Companion { + private constructor Companion() + internal final val C: kotlin.Int = 3 + internal final val prop: kotlin.Int = 2 + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + internal final fun foo(): kotlin.Int + 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/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java index c8798054dfa..0e194f976a1 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java @@ -10587,6 +10587,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("companionObjectScope.kt") + public void testCompanionObjectScope() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/secondaryConstructors/companionObjectScope.kt"); + doTest(fileName); + } + @TestMetadata("constructorCallType.kt") public void testConstructorCallType() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/secondaryConstructors/constructorCallType.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java index cff58a07348..a6be7a9c335 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java @@ -6317,6 +6317,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) public static class SecondaryConstructors extends AbstractBlackBoxCodegenTest { + @TestMetadata("accessToCompanion.kt") + public void testAccessToCompanion() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/secondaryConstructors/accessToCompanion.kt"); + doTest(fileName); + } + public void testAllFilesPresentInSecondaryConstructors() throws Exception { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/secondaryConstructors"), Pattern.compile("^(.+)\\.kt$"), true); }