From 8738458c34971319703345b896517bf539c60e27 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Fri, 27 May 2016 17:04:34 +0300 Subject: [PATCH] KT-2752: fix name clash diagnostic for case of extension property. Add more tests for the diagnostic --- .../name/extensionPropertyAndMethod.kt | 8 +++++++ .../name/extensionPropertyAndMethod.txt | 13 +++++++++++ .../extensionPropertyAndMethodDoNotClash.kt | 8 +++++++ .../extensionPropertyAndMethodDoNotClash.txt | 13 +++++++++++ .../testsWithJsStdLib/name/methodAndMethod.kt | 5 +++++ .../name/methodAndMethod.txt | 6 +++++ .../DiagnosticsTestWithJsStdLibGenerated.java | 18 +++++++++++++++ .../kotlin/js/naming/FQNGenerator.kt | 7 +++++- .../js/resolve/diagnostics/JsNameChecker.kt | 22 ++++++++++++------- 9 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 compiler/testData/diagnostics/testsWithJsStdLib/name/extensionPropertyAndMethod.kt create mode 100644 compiler/testData/diagnostics/testsWithJsStdLib/name/extensionPropertyAndMethod.txt create mode 100644 compiler/testData/diagnostics/testsWithJsStdLib/name/extensionPropertyAndMethodDoNotClash.kt create mode 100644 compiler/testData/diagnostics/testsWithJsStdLib/name/extensionPropertyAndMethodDoNotClash.txt create mode 100644 compiler/testData/diagnostics/testsWithJsStdLib/name/methodAndMethod.kt create mode 100644 compiler/testData/diagnostics/testsWithJsStdLib/name/methodAndMethod.txt diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/name/extensionPropertyAndMethod.kt b/compiler/testData/diagnostics/testsWithJsStdLib/name/extensionPropertyAndMethod.kt new file mode 100644 index 00000000000..7225f197ca3 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithJsStdLib/name/extensionPropertyAndMethod.kt @@ -0,0 +1,8 @@ +package foo + +class A + +fun A.get_bar() = 23 + +val A.bar: Int + get() = 42 \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/name/extensionPropertyAndMethod.txt b/compiler/testData/diagnostics/testsWithJsStdLib/name/extensionPropertyAndMethod.txt new file mode 100644 index 00000000000..c412fa6c1f1 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithJsStdLib/name/extensionPropertyAndMethod.txt @@ -0,0 +1,13 @@ +package + +package foo { + public val foo.A.bar: kotlin.Int + public fun foo.A.get_bar(): kotlin.Int + + public final class A { + 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 + } +} diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/name/extensionPropertyAndMethodDoNotClash.kt b/compiler/testData/diagnostics/testsWithJsStdLib/name/extensionPropertyAndMethodDoNotClash.kt new file mode 100644 index 00000000000..4819f4ad2c6 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithJsStdLib/name/extensionPropertyAndMethodDoNotClash.kt @@ -0,0 +1,8 @@ +package foo + +class A + +fun A.bar() = 32 + +val A.bar: Int + get() = 32 diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/name/extensionPropertyAndMethodDoNotClash.txt b/compiler/testData/diagnostics/testsWithJsStdLib/name/extensionPropertyAndMethodDoNotClash.txt new file mode 100644 index 00000000000..8132618654f --- /dev/null +++ b/compiler/testData/diagnostics/testsWithJsStdLib/name/extensionPropertyAndMethodDoNotClash.txt @@ -0,0 +1,13 @@ +package + +package foo { + public val foo.A.bar: kotlin.Int + public fun foo.A.bar(): kotlin.Int + + public final class A { + 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 + } +} diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/name/methodAndMethod.kt b/compiler/testData/diagnostics/testsWithJsStdLib/name/methodAndMethod.kt new file mode 100644 index 00000000000..c43a8179715 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithJsStdLib/name/methodAndMethod.kt @@ -0,0 +1,5 @@ +package foo + +fun bar(x: Int) = x + +fun `bar_za3lpa$`() = 42 \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/name/methodAndMethod.txt b/compiler/testData/diagnostics/testsWithJsStdLib/name/methodAndMethod.txt new file mode 100644 index 00000000000..79302cd2c94 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithJsStdLib/name/methodAndMethod.txt @@ -0,0 +1,6 @@ +package + +package foo { + public fun bar(/*0*/ x: kotlin.Int): kotlin.Int + public fun `bar_za3lpa$`(): kotlin.Int +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibGenerated.java index 8dcfb226bc5..082e3f1d0ea 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibGenerated.java @@ -307,6 +307,24 @@ public class DiagnosticsTestWithJsStdLibGenerated extends AbstractDiagnosticsTes doTest(fileName); } + @TestMetadata("extensionPropertyAndMethod.kt") + public void testExtensionPropertyAndMethod() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/name/extensionPropertyAndMethod.kt"); + doTest(fileName); + } + + @TestMetadata("extensionPropertyAndMethodDoNotClash.kt") + public void testExtensionPropertyAndMethodDoNotClash() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/name/extensionPropertyAndMethodDoNotClash.kt"); + doTest(fileName); + } + + @TestMetadata("methodAndMethod.kt") + public void testMethodAndMethod() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/name/methodAndMethod.kt"); + doTest(fileName); + } + @TestMetadata("methodAndPrivatePropertyDoNotClash.kt") public void testMethodAndPrivatePropertyDoNotClash() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/name/methodAndPrivatePropertyDoNotClash.kt"); diff --git a/js/js.frontend/src/org/jetbrains/kotlin/js/naming/FQNGenerator.kt b/js/js.frontend/src/org/jetbrains/kotlin/js/naming/FQNGenerator.kt index 2e34d471da7..2ce0d8297c9 100644 --- a/js/js.frontend/src/org/jetbrains/kotlin/js/naming/FQNGenerator.kt +++ b/js/js.frontend/src/org/jetbrains/kotlin/js/naming/FQNGenerator.kt @@ -61,7 +61,12 @@ class FQNGenerator { } val (localName, shared, parent) = getLocalName(descriptor) - return FQNPart(listOf(localName), shared, descriptor, parent) + return FQNPart(listOf(localName), shared, descriptor, fixParent(parent)) + } + + private fun fixParent(parent: DeclarationDescriptor) = when (parent) { + is PropertyDescriptor -> parent.containingDeclaration + else -> parent } private fun getLocalName(descriptor: DeclarationDescriptor): LocalName { diff --git a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsNameChecker.kt b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsNameChecker.kt index 46ccf5af505..853bdef1baf 100644 --- a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsNameChecker.kt +++ b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsNameChecker.kt @@ -22,9 +22,11 @@ import org.jetbrains.kotlin.diagnostics.DiagnosticSink import org.jetbrains.kotlin.js.naming.FQNGenerator import org.jetbrains.kotlin.js.translate.utils.AnnotationsUtils import org.jetbrains.kotlin.psi.KtDeclaration +import org.jetbrains.kotlin.psi.KtProperty import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.DeclarationChecker import org.jetbrains.kotlin.resolve.DescriptorUtils +import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension import org.jetbrains.kotlin.resolve.scopes.MemberScope import org.jetbrains.kotlin.resolve.source.getPsi @@ -39,7 +41,9 @@ class JsNameChecker : DeclarationChecker { diagnosticHolder: DiagnosticSink, bindingContext: BindingContext ) { - checkDescriptor(descriptor, declaration, diagnosticHolder) + if (declaration !is KtProperty || !descriptor.isExtension) { + checkDescriptor(descriptor, declaration, diagnosticHolder) + } } private fun checkDescriptor(descriptor: DeclarationDescriptor, declaration: KtDeclaration, diagnosticHolder: DiagnosticSink) { @@ -87,6 +91,15 @@ class JsNameChecker : DeclarationChecker { private fun collect(scope: MemberScope, target: MutableMap) { for (descriptor in scope.getContributedDescriptors()) { + collect(descriptor, target) + } + } + + private fun collect(descriptor: DeclarationDescriptor, target: MutableMap) { + if (descriptor is PropertyDescriptor && descriptor.isExtension) { + descriptor.accessors.forEach { collect(it, target) } + } + else { val fqn = fqnGenerator.generate(descriptor) if (fqn.shared && isOpaque(fqn.descriptor)) { target[fqn.names.last()] = fqn.descriptor @@ -94,13 +107,6 @@ class JsNameChecker : DeclarationChecker { } } - private fun collect(descriptor: DeclarationDescriptor, target: MutableMap) { - val fqn = fqnGenerator.generate(descriptor) - if (fqn.shared && isOpaque(fqn.descriptor)) { - target[fqn.names.last()] = fqn.descriptor - } - } - private fun isOpaque(descriptor: DeclarationDescriptor) = !AnnotationsUtils.isNativeObject(descriptor) && !AnnotationsUtils.isLibraryObject(descriptor) } \ No newline at end of file