From 6ca5ba3615c5059c74dcb2d479357d35e285dbee Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Fri, 16 Dec 2016 16:28:40 +0300 Subject: [PATCH] Don't call getClassDescriptor for local declarations (KT-15259) #KT-15259 Open --- .../src/org/jetbrains/kotlin/psi/KtPsiUtil.java | 1 - .../idea/caches/resolve/CodeFragmentAnalyzer.kt | 13 +++++++++++-- .../checker/codeFragments/anonymousObject.kt | 5 +++++ .../codeFragments/anonymousObject.kt.fragment | 1 + .../codeFragments/primaryConstructorLocal.kt | 16 ++++++++++++++++ .../primaryConstructorLocal.kt.fragment | 1 + .../CodeFragmentHighlightingTestGenerated.java | 12 ++++++++++++ 7 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 idea/testData/checker/codeFragments/anonymousObject.kt create mode 100644 idea/testData/checker/codeFragments/anonymousObject.kt.fragment create mode 100644 idea/testData/checker/codeFragments/primaryConstructorLocal.kt create mode 100644 idea/testData/checker/codeFragments/primaryConstructorLocal.kt.fragment diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/KtPsiUtil.java b/compiler/frontend/src/org/jetbrains/kotlin/psi/KtPsiUtil.java index 6a470f73a7b..dd54c87bed3 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/KtPsiUtil.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/KtPsiUtil.java @@ -747,7 +747,6 @@ public class KtPsiUtil { if (((KtParameter) declaration).hasValOrVar() && parent != null && parent.getParent() instanceof KtPrimaryConstructor) { return getEnclosingElementForLocalDeclaration(((KtPrimaryConstructor) parent.getParent()).getContainingClassOrObject(), skipParameters); } - else if (skipParameters && parent != null && parent.getParent() instanceof KtNamedFunction) { declaration = (KtNamedFunction) parent.getParent(); } diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/CodeFragmentAnalyzer.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/CodeFragmentAnalyzer.kt index d7d4de99847..f1b27e3f1e5 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/CodeFragmentAnalyzer.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/CodeFragmentAnalyzer.kt @@ -16,6 +16,7 @@ package org.jetbrains.kotlin.idea.caches.resolve +import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.ClassDescriptorWithResolutionScopes import org.jetbrains.kotlin.idea.project.ResolveElementCache import org.jetbrains.kotlin.incremental.components.NoLookupLocation @@ -91,9 +92,17 @@ class CodeFragmentAnalyzer( val scopeForContextElement: LexicalScope? val dataFlowInfo: DataFlowInfo + fun getClassDescriptor(classOrObject: KtClassOrObject): ClassDescriptor? { + if (!KtPsiUtil.isLocal(classOrObject)) { + return resolveSession.getClassDescriptor(classOrObject, NoLookupLocation.FROM_IDE) + } + + return resolveToElement(classOrObject)[BindingContext.DECLARATION_TO_DESCRIPTOR, classOrObject] as ClassDescriptor? + } + when (context) { is KtPrimaryConstructor -> { - val descriptor = resolveSession.getClassDescriptor(context.getContainingClassOrObject(), NoLookupLocation.FROM_IDE) as ClassDescriptorWithResolutionScopes + val descriptor = (getClassDescriptor(context.getContainingClassOrObject()) as? ClassDescriptorWithResolutionScopes) ?: return null scopeForContextElement = descriptor.scopeForInitializerResolution dataFlowInfo = DataFlowInfo.EMPTY @@ -107,7 +116,7 @@ class CodeFragmentAnalyzer( dataFlowInfo = DataFlowInfo.EMPTY } is KtClassOrObject -> { - val descriptor = resolveSession.getClassDescriptor(context, NoLookupLocation.FROM_IDE) as ClassDescriptorWithResolutionScopes + val descriptor = (getClassDescriptor(context) as? ClassDescriptorWithResolutionScopes) ?: return null scopeForContextElement = descriptor.scopeForMemberDeclarationResolution dataFlowInfo = DataFlowInfo.EMPTY diff --git a/idea/testData/checker/codeFragments/anonymousObject.kt b/idea/testData/checker/codeFragments/anonymousObject.kt new file mode 100644 index 00000000000..359571355df --- /dev/null +++ b/idea/testData/checker/codeFragments/anonymousObject.kt @@ -0,0 +1,5 @@ +val global = 2 + +fun makeFace() = object : ObjectFace { + val inObject = 1 +} \ No newline at end of file diff --git a/idea/testData/checker/codeFragments/anonymousObject.kt.fragment b/idea/testData/checker/codeFragments/anonymousObject.kt.fragment new file mode 100644 index 00000000000..7470cc366cc --- /dev/null +++ b/idea/testData/checker/codeFragments/anonymousObject.kt.fragment @@ -0,0 +1 @@ +global + inObject \ No newline at end of file diff --git a/idea/testData/checker/codeFragments/primaryConstructorLocal.kt b/idea/testData/checker/codeFragments/primaryConstructorLocal.kt new file mode 100644 index 00000000000..c2a0a9932d6 --- /dev/null +++ b/idea/testData/checker/codeFragments/primaryConstructorLocal.kt @@ -0,0 +1,16 @@ +fun test() { + val local = 12 + + class A( + a: Int + ) : Base(1) { + val c = 1 + + init { + val d = 1 + val e = 1 + } + } +} + +open class Base(i: Int) \ No newline at end of file diff --git a/idea/testData/checker/codeFragments/primaryConstructorLocal.kt.fragment b/idea/testData/checker/codeFragments/primaryConstructorLocal.kt.fragment new file mode 100644 index 00000000000..58474a52168 --- /dev/null +++ b/idea/testData/checker/codeFragments/primaryConstructorLocal.kt.fragment @@ -0,0 +1 @@ +local + a + c + d + e \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/debugger/evaluate/CodeFragmentHighlightingTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/debugger/evaluate/CodeFragmentHighlightingTestGenerated.java index 0919284d0b0..d2cecfba472 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/debugger/evaluate/CodeFragmentHighlightingTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/debugger/evaluate/CodeFragmentHighlightingTestGenerated.java @@ -38,6 +38,12 @@ public class CodeFragmentHighlightingTestGenerated extends AbstractCodeFragmentH KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/checker/codeFragments"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, false); } + @TestMetadata("anonymousObject.kt") + public void testAnonymousObject() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/checker/codeFragments/anonymousObject.kt"); + doTest(fileName); + } + @TestMetadata("binaryExpression.kt") public void testBinaryExpression() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/checker/codeFragments/binaryExpression.kt"); @@ -104,6 +110,12 @@ public class CodeFragmentHighlightingTestGenerated extends AbstractCodeFragmentH doTest(fileName); } + @TestMetadata("primaryConstructorLocal.kt") + public void testPrimaryConstructorLocal() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/checker/codeFragments/primaryConstructorLocal.kt"); + doTest(fileName); + } + @TestMetadata("privateFunArgumentsResolve.kt") public void testPrivateFunArgumentsResolve() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/checker/codeFragments/privateFunArgumentsResolve.kt");