Fix InnerClassesScopeWrapper - remove properties and functions from this scope.

This commit is contained in:
Stanislav Erokhin
2015-08-26 22:13:41 +03:00
parent 49b294b8b4
commit 9a435e332f
11 changed files with 110 additions and 19 deletions
+2 -2
View File
@@ -32,7 +32,7 @@ class Test2<A, B, C>(foo: Any?, bar: Any?) {
class Test3<in A, B, C>(foo: Any?, bar: Any?) {
val foo = foo ?: this
private val bar = bar ?: this
private val bas = <!TYPECHECKER_HAS_RUN_INTO_RECURSIVE_PROBLEM!><!DEBUG_INFO_MISSING_UNRESOLVED!>bas<!>()<!>
private val bas = bas()
val bas2 = bas2()
private fun bas(): Int = null!!
@@ -47,7 +47,7 @@ class Test3<in A, B, C>(foo: Any?, bar: Any?) {
class Test4<A, out B, C>(foo: Any?, bar: Any?) {
val foo = foo ?: this
private val bar = bar ?: this
private val bas = <!TYPECHECKER_HAS_RUN_INTO_RECURSIVE_PROBLEM!><!DEBUG_INFO_MISSING_UNRESOLVED!>bas<!>()<!>
private val bas = bas()
val bas2 = bas2()
private fun bas(): Int = null!!
+2 -2
View File
@@ -37,7 +37,7 @@ internal final class Test2</*0*/ A, /*1*/ B, /*2*/ C> {
internal final class Test3</*0*/ in A, /*1*/ B, /*2*/ C> {
public constructor Test3</*0*/ in A, /*1*/ B, /*2*/ C>(/*0*/ foo: kotlin.Any?, /*1*/ bar: kotlin.Any?)
private final val bar: kotlin.Any
private final val bas: [ERROR : Type for bas()]
private final val bas: kotlin.Int
internal final val bas2: kotlin.Int
internal final val foo: kotlin.Any
internal final fun bar(): kotlin.Int
@@ -54,7 +54,7 @@ internal final class Test3</*0*/ in A, /*1*/ B, /*2*/ C> {
internal final class Test4</*0*/ A, /*1*/ out B, /*2*/ C> {
public constructor Test4</*0*/ A, /*1*/ out B, /*2*/ C>(/*0*/ foo: kotlin.Any?, /*1*/ bar: kotlin.Any?)
private final val bar: kotlin.Any
private final val bas: [ERROR : Type for bas()]
private final val bas: kotlin.Int
internal final val bas2: kotlin.Int
internal final val foo: kotlin.Any
internal final fun bar(): kotlin.Int
@@ -0,0 +1,11 @@
interface A {
val foo: Int
val bar: String
get() = ""
}
fun test(foo: Int, bar: Int) {
object : A {
override val foo: Int = foo + bar
}
}
@@ -0,0 +1,11 @@
package
internal fun test(/*0*/ foo: kotlin.Int, /*1*/ bar: kotlin.Int): kotlin.Unit
internal interface A {
internal open val bar: kotlin.String
internal abstract val foo: 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
}
@@ -0,0 +1,9 @@
interface A {
~from-interface~val foo: Int
}
fun test(~param~foo: Int) {
object : A {
~property~override val foo: Int = `param`foo
}
}
@@ -9465,6 +9465,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("objectInsideFun.kt")
public void testObjectInsideFun() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/objects/objectInsideFun.kt");
doTest(fileName);
}
@TestMetadata("objectLiteralExpressionTypeMismatch.kt")
public void testObjectLiteralExpressionTypeMismatch() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/objects/objectLiteralExpressionTypeMismatch.kt");
@@ -480,6 +480,12 @@ public class ResolveTestGenerated extends AbstractResolveTest {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/resolve/regressions/kt300.resolve");
doTest(fileName);
}
@TestMetadata("objectInsideFun.resolve")
public void testObjectInsideFun() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/resolve/regressions/objectInsideFun.resolve");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/resolve/varargs")
@@ -17,22 +17,27 @@
package org.jetbrains.kotlin.resolve.scopes
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ReceiverParameterDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.utils.Printer
public class InnerClassesScopeWrapper(val workerScope: JetScope) : JetScopeImpl() {
override fun getContainingDeclaration(): DeclarationDescriptor {
return workerScope.getContainingDeclaration()
}
public class InnerClassesScopeWrapper(override val workerScope: JetScope) : AbstractScopeAdapter() {
override fun getClassifier(name: Name, location: LookupLocation) = workerScope.getClassifier(name, location) as? ClassDescriptor
override fun getDeclarationsByLabel(labelName: Name) = workerScope.getDeclarationsByLabel(labelName).filterIsInstance<ClassDescriptor>()
override fun getDescriptors(kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> Boolean): List<ClassDescriptor> {
override fun getDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): List<ClassDescriptor> {
val restrictedFilter = kindFilter.restrictedToKindsOrNull(DescriptorKindFilter.CLASSIFIERS_MASK) ?: return listOf()
return workerScope.getDescriptors(restrictedFilter, nameFilter).filterIsInstance<ClassDescriptor>()
}
override fun getImplicitReceiversHierarchy(): List<ReceiverParameterDescriptor> = listOf()
override fun printScopeStructure(p: Printer) {
p.println("InnerClassesScopeWrapper for scope:")
workerScope.printScopeStructure(p)
}
override fun toString() = "Classes from " + workerScope
override fun toString() = "Classes from $workerScope"
}
@@ -0,0 +1,42 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.util
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.scopes.JetScope
public fun JetScope.getAllAccessibleVariables(name: Name): Collection<VariableDescriptor>
= getVariablesFromImplicitReceivers(name) + getProperties(name, NoLookupLocation.FROM_IDE) + listOfNotNull(getLocalVariable(name))
public fun JetScope.getAllAccessibleFunctions(name: Name): Collection<FunctionDescriptor>
= getImplicitReceiversWithInstance().flatMap { it.type.memberScope.getFunctions(name, NoLookupLocation.FROM_IDE) } +
getFunctions(name, NoLookupLocation.FROM_IDE)
public fun JetScope.getVariablesFromImplicitReceivers(name: Name): Collection<VariableDescriptor> = getImplicitReceiversWithInstance().flatMap {
it.type.memberScope.getProperties(name, NoLookupLocation.FROM_IDE)
}
public fun JetScope.getVariableFromImplicitReceivers(name: Name): VariableDescriptor? {
getImplicitReceiversWithInstance().forEach {
it.type.memberScope.getProperties(name, NoLookupLocation.FROM_IDE).singleOrNull()?.let { return it }
}
return null
}
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.idea.JetDescriptorIconProvider
import org.jetbrains.kotlin.idea.completion.*
import org.jetbrains.kotlin.idea.util.getVariableFromImplicitReceivers
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.psi.JetExpression
import org.jetbrains.kotlin.renderer.render
@@ -31,8 +32,7 @@ import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.hasDefaultValue
import org.jetbrains.kotlin.resolve.scopes.JetScope
import org.jetbrains.kotlin.types.checker.JetTypeChecker
import java.util.ArrayList
import java.util.HashSet
import java.util.*
class MultipleArgumentsItemProvider(val bindingContext: BindingContext,
val smartCastCalculator: SmartCastCalculator) {
@@ -90,7 +90,8 @@ class MultipleArgumentsItemProvider(val bindingContext: BindingContext,
private fun variableInScope(parameter: ValueParameterDescriptor, scope: JetScope): VariableDescriptor? {
val name = parameter.getName()
//TODO: there can be more than one property with such name in scope and we should be able to select one (but we need API for this)
val variable = scope.getLocalVariable(name) ?: scope.getProperties(name, NoLookupLocation.FROM_IDE).singleOrNull() ?: return null
val variable = scope.getLocalVariable(name) ?: scope.getProperties(name, NoLookupLocation.FROM_IDE).singleOrNull() ?:
scope.getVariableFromImplicitReceivers(name) ?: return null
return if (smartCastCalculator.types(variable).any { JetTypeChecker.DEFAULT.isSubtypeOf(it, parameter.getType()) })
variable
else
@@ -57,7 +57,7 @@ import org.jetbrains.kotlin.idea.references.JetSimpleNameReference;
import org.jetbrains.kotlin.idea.references.ReferencesPackage;
import org.jetbrains.kotlin.idea.search.usagesSearch.UsagesSearchPackage;
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers;
import org.jetbrains.kotlin.incremental.components.NoLookupLocation;
import org.jetbrains.kotlin.idea.util.UtilPackage;
import org.jetbrains.kotlin.kdoc.psi.impl.KDocName;
import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor;
import org.jetbrains.kotlin.name.Name;
@@ -619,8 +619,8 @@ public class JetChangeSignatureUsageProcessor implements ChangeSignatureUsagePro
if (!kind.getIsConstructor() && callableScope != null && !info.getNewName().isEmpty()) {
Name newName = Name.identifier(info.getNewName());
Collection<? extends CallableDescriptor> conflicts = oldDescriptor instanceof FunctionDescriptor
? callableScope.getFunctions(newName, NoLookupLocation.FROM_IDE)
: callableScope.getProperties(newName, NoLookupLocation.FROM_IDE);
? UtilPackage.getAllAccessibleFunctions(callableScope, newName)
: UtilPackage.getAllAccessibleVariables(callableScope, newName);
for (CallableDescriptor conflict : conflicts) {
if (conflict == oldDescriptor) continue;
@@ -643,7 +643,7 @@ public class JetChangeSignatureUsageProcessor implements ChangeSignatureUsagePro
}
if (parametersScope != null) {
if (kind == JetMethodDescriptor.Kind.PRIMARY_CONSTRUCTOR && valOrVar != JetValVar.None) {
for (VariableDescriptor property : parametersScope.getProperties(Name.identifier(parameterName), NoLookupLocation.FROM_IDE)) {
for (VariableDescriptor property : UtilPackage.getVariablesFromImplicitReceivers(parametersScope, Name.identifier(parameterName))) {
PsiElement propertyDeclaration = DescriptorToSourceUtils.descriptorToDeclaration(property);
if (propertyDeclaration != null && !(propertyDeclaration.getParent() instanceof JetParameterList)) {