Use JvmWildcard on return type of getContributedVariables/getContributedFunctions

This commit is contained in:
Alexander Udalov
2018-07-07 12:38:48 +02:00
parent 9dc53c38f3
commit 23c210e9f2
15 changed files with 53 additions and 63 deletions
@@ -349,7 +349,7 @@ public abstract class AnnotationCodegen {
private String getAnnotationArgumentJvmName(@Nullable ClassDescriptor annotationClass, @NotNull Name parameterName) {
if (annotationClass == null) return parameterName.asString();
Collection<PropertyDescriptor> variables =
Collection<? extends PropertyDescriptor> variables =
annotationClass.getUnsubstitutedMemberScope().getContributedVariables(parameterName, NoLookupLocation.FROM_BACKEND);
if (variables.size() != 1) return parameterName.asString();
@@ -467,7 +467,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
return;
}
Collection<SimpleFunctionDescriptor> functions = descriptor.getDefaultType().getMemberScope().getContributedFunctions(
Collection<? extends SimpleFunctionDescriptor> functions = descriptor.getDefaultType().getMemberScope().getContributedFunctions(
Name.identifier("toArray"), NoLookupLocation.FROM_BACKEND
);
boolean hasGenericToArray = false;
@@ -203,7 +203,7 @@ public class SignaturesPropagationData {
Method autoSignature = null;
boolean autoMethodContainsVararg = SignaturePropagationUtilKt.containsVarargs(autoMethodDescriptor);
for (KotlinType supertype : containingClass.getTypeConstructor().getSupertypes()) {
Collection<SimpleFunctionDescriptor> superFunctionCandidates =
Collection<? extends SimpleFunctionDescriptor> superFunctionCandidates =
supertype.getMemberScope().getContributedFunctions(name, NoLookupLocation.WHEN_GET_SUPER_MEMBERS);
if (!autoMethodContainsVararg && !SignaturePropagationUtilKt.containsAnyNotTrivialSignature(superFunctionCandidates)) continue;
@@ -1187,8 +1187,9 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
KotlinType refinedType = KotlinBuiltIns.isNothingOrNullableNothing(receiver.getType()) ?
components.builtIns.getNullableAnyType() :
receiver.getType();
Collection<SimpleFunctionDescriptor> equalsMembers = refinedType.getMemberScope().getContributedFunctions(
OperatorNameConventions.EQUALS, new KotlinLookupLocation(expression.getOperationReference()));
Collection<? extends SimpleFunctionDescriptor> equalsMembers = refinedType.getMemberScope().getContributedFunctions(
OperatorNameConventions.EQUALS, new KotlinLookupLocation(expression.getOperationReference())
);
return CollectionsKt.filter(equalsMembers, descriptor -> {
if (ErrorUtils.isError(descriptor)) return true;
@@ -99,8 +99,9 @@ public class DataFlowAnalyzer {
}
private boolean typeHasOverriddenEquals(@NotNull KotlinType type, @NotNull KtElement lookupElement) {
Collection<SimpleFunctionDescriptor> members = type.getMemberScope().getContributedFunctions(
OperatorNameConventions.EQUALS, new KotlinLookupLocation(lookupElement));
Collection<? extends SimpleFunctionDescriptor> members = type.getMemberScope().getContributedFunctions(
OperatorNameConventions.EQUALS, new KotlinLookupLocation(lookupElement)
);
for (FunctionDescriptor member : members) {
KotlinType returnType = member.getReturnType();
if (returnType == null || !KotlinBuiltIns.isBoolean(returnType)) continue;
@@ -169,7 +169,8 @@ public abstract class AbstractAnnotationDescriptorResolveTest extends KotlinTest
protected static FunctionDescriptor getFunctionDescriptor(@NotNull PackageFragmentDescriptor packageView, @NotNull String name) {
Name functionName = Name.identifier(name);
MemberScope memberScope = packageView.getMemberScope();
Collection<SimpleFunctionDescriptor> functions = memberScope.getContributedFunctions(functionName, NoLookupLocation.FROM_TEST);
Collection<? extends SimpleFunctionDescriptor> functions =
memberScope.getContributedFunctions(functionName, NoLookupLocation.FROM_TEST);
assert functions.size() == 1 : "Failed to find function " + functionName + " in class" + "." + packageView.getName();
return functions.iterator().next();
}
@@ -178,7 +179,8 @@ public abstract class AbstractAnnotationDescriptorResolveTest extends KotlinTest
private static FunctionDescriptor getFunctionDescriptor(@NotNull ClassDescriptor classDescriptor, @NotNull String name) {
Name functionName = Name.identifier(name);
MemberScope memberScope = classDescriptor.getMemberScope(Collections.emptyList());
Collection<SimpleFunctionDescriptor> functions = memberScope.getContributedFunctions(functionName, NoLookupLocation.FROM_TEST);
Collection<? extends SimpleFunctionDescriptor> functions =
memberScope.getContributedFunctions(functionName, NoLookupLocation.FROM_TEST);
assert functions.size() == 1 : "Failed to find function " + functionName + " in class" + "." + classDescriptor.getName();
return functions.iterator().next();
}
@@ -187,12 +189,13 @@ public abstract class AbstractAnnotationDescriptorResolveTest extends KotlinTest
protected static PropertyDescriptor getPropertyDescriptor(@NotNull PackageFragmentDescriptor packageView, @NotNull String name, boolean failOnMissing) {
Name propertyName = Name.identifier(name);
MemberScope memberScope = packageView.getMemberScope();
Collection<PropertyDescriptor> properties = memberScope.getContributedVariables(propertyName, NoLookupLocation.FROM_TEST);
Collection<? extends PropertyDescriptor> properties = memberScope.getContributedVariables(propertyName, NoLookupLocation.FROM_TEST);
if (properties.isEmpty()) {
for (DeclarationDescriptor descriptor : DescriptorUtils.getAllDescriptors(memberScope)) {
if (descriptor instanceof ClassDescriptor) {
Collection<PropertyDescriptor> classProperties = ((ClassDescriptor) descriptor).getMemberScope(Collections.emptyList())
.getContributedVariables(propertyName, NoLookupLocation.FROM_TEST);
Collection<? extends PropertyDescriptor> classProperties =
((ClassDescriptor) descriptor).getMemberScope(Collections.emptyList())
.getContributedVariables(propertyName, NoLookupLocation.FROM_TEST);
if (!classProperties.isEmpty()) {
properties = classProperties;
break;
@@ -213,7 +216,7 @@ public abstract class AbstractAnnotationDescriptorResolveTest extends KotlinTest
private static PropertyDescriptor getPropertyDescriptor(@NotNull ClassDescriptor classDescriptor, @NotNull String name) {
Name propertyName = Name.identifier(name);
MemberScope memberScope = classDescriptor.getMemberScope(Collections.emptyList());
Collection<PropertyDescriptor> properties = memberScope.getContributedVariables(propertyName, NoLookupLocation.FROM_TEST);
Collection<? extends PropertyDescriptor> properties = memberScope.getContributedVariables(propertyName, NoLookupLocation.FROM_TEST);
assert properties.size() == 1 : "Failed to find property " + propertyName + " in class " + classDescriptor.getName();
return properties.iterator().next();
}
@@ -72,7 +72,7 @@ public class BoundsSubstitutorTest extends KotlinTestWithEnvironment {
private void doTest(String text, String expected) {
KtFile ktFile = KtPsiFactoryKt.KtPsiFactory(getProject()).createFile("fun.kt", text);
ModuleDescriptor module = JvmResolveUtil.analyze(ktFile, getEnvironment()).getModuleDescriptor();
Collection<SimpleFunctionDescriptor> functions =
Collection<? extends SimpleFunctionDescriptor> functions =
module.getPackage(FqName.ROOT).getMemberScope().getContributedFunctions(Name.identifier("f"), NoLookupLocation.FROM_TEST);
FunctionDescriptor substituted = BoundsSubstitutor.substituteBounds(CollectionsKt.single(functions));
String actual = DescriptorRenderer.COMPACT.render(substituted);
@@ -182,21 +182,21 @@ public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
}
private class EnumEntryScope extends MemberScopeImpl {
private final MemoizedFunctionToNotNull<Name, Collection<SimpleFunctionDescriptor>> functions;
private final MemoizedFunctionToNotNull<Name, Collection<PropertyDescriptor>> properties;
private final MemoizedFunctionToNotNull<Name, Collection<? extends SimpleFunctionDescriptor>> functions;
private final MemoizedFunctionToNotNull<Name, Collection<? extends PropertyDescriptor>> properties;
private final NotNullLazyValue<Collection<DeclarationDescriptor>> allDescriptors;
public EnumEntryScope(@NotNull StorageManager storageManager) {
this.functions = storageManager.createMemoizedFunction(new Function1<Name, Collection<SimpleFunctionDescriptor>>() {
this.functions = storageManager.createMemoizedFunction(new Function1<Name, Collection<? extends SimpleFunctionDescriptor>>() {
@Override
public Collection<SimpleFunctionDescriptor> invoke(Name name) {
public Collection<? extends SimpleFunctionDescriptor> invoke(Name name) {
return computeFunctions(name);
}
});
this.properties = storageManager.createMemoizedFunction(new Function1<Name, Collection<PropertyDescriptor>>() {
this.properties = storageManager.createMemoizedFunction(new Function1<Name, Collection<? extends PropertyDescriptor>>() {
@Override
public Collection<PropertyDescriptor> invoke(Name name) {
public Collection<? extends PropertyDescriptor> invoke(Name name) {
return computeProperties(name);
}
});
@@ -210,32 +210,23 @@ public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
@NotNull
@Override
// TODO: Convert to Kotlin or add @JvmWildcard to MemberScope declarations
// method is covariantly overridden in Kotlin, but collections in Java are invariant
@SuppressWarnings({"unchecked"})
public Collection getContributedVariables(@NotNull Name name, @NotNull LookupLocation location) {
public Collection<? extends PropertyDescriptor> getContributedVariables(@NotNull Name name, @NotNull LookupLocation location) {
return properties.invoke(name);
}
@NotNull
// TODO: Convert to Kotlin or add @JvmWildcard to MemberScope declarations
// method is covariantly overridden in Kotlin, but collections in Java are invariant
@SuppressWarnings({"unchecked"})
private Collection<PropertyDescriptor> computeProperties(@NotNull Name name) {
return resolveFakeOverrides(name, (Collection) getSupertypeScope().getContributedVariables(name, NoLookupLocation.FOR_NON_TRACKED_SCOPE));
private Collection<? extends PropertyDescriptor> computeProperties(@NotNull Name name) {
return resolveFakeOverrides(name, getSupertypeScope().getContributedVariables(name, NoLookupLocation.FOR_NON_TRACKED_SCOPE));
}
@NotNull
@Override
// TODO: Convert to Kotlin or add @JvmWildcard to MemberScope declarations
// method is covariantly overridden in Kotlin, but collections in Java are invariant
@SuppressWarnings({"unchecked"})
public Collection getContributedFunctions(@NotNull Name name, @NotNull LookupLocation location) {
public Collection<? extends SimpleFunctionDescriptor> getContributedFunctions(@NotNull Name name, @NotNull LookupLocation location) {
return functions.invoke(name);
}
@NotNull
private Collection<SimpleFunctionDescriptor> computeFunctions(@NotNull Name name) {
private Collection<? extends SimpleFunctionDescriptor> computeFunctions(@NotNull Name name) {
return resolveFakeOverrides(name, getSupertypeScope().getContributedFunctions(name, NoLookupLocation.FOR_NON_TRACKED_SCOPE));
}
@@ -247,9 +238,9 @@ public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
}
@NotNull
private <D extends CallableMemberDescriptor> Collection<D> resolveFakeOverrides(
private <D extends CallableMemberDescriptor> Collection<? extends D> resolveFakeOverrides(
@NotNull Name name,
@NotNull Collection<D> fromSupertypes
@NotNull Collection<? extends D> fromSupertypes
) {
final Set<D> result = new LinkedHashSet<D>();
@@ -587,8 +587,7 @@ public class DescriptorUtils {
@Nullable
public static FunctionDescriptor getFunctionByNameOrNull(@NotNull MemberScope scope, @NotNull Name name) {
Collection<SimpleFunctionDescriptor> functions = scope.getContributedFunctions(name, NoLookupLocation.FROM_BACKEND);
for (SimpleFunctionDescriptor d : functions) {
for (SimpleFunctionDescriptor d : scope.getContributedFunctions(name, NoLookupLocation.FROM_BACKEND)) {
if (name.equals(d.getOriginal().getName())) {
return d;
}
@@ -599,8 +598,7 @@ public class DescriptorUtils {
@NotNull
public static PropertyDescriptor getPropertyByName(@NotNull MemberScope scope, @NotNull Name name) {
Collection<PropertyDescriptor> properties = scope.getContributedVariables(name, NoLookupLocation.FROM_BACKEND);
for (PropertyDescriptor d : properties) {
for (PropertyDescriptor d : scope.getContributedVariables(name, NoLookupLocation.FROM_BACKEND)) {
if (name.equals(d.getOriginal().getName())) {
return d;
}
@@ -25,8 +25,9 @@ import java.lang.reflect.Modifier
interface MemberScope : ResolutionScope {
override fun getContributedVariables(name: Name, location: LookupLocation): Collection<PropertyDescriptor>
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor>
override fun getContributedVariables(name: Name, location: LookupLocation): Collection<@JvmWildcard PropertyDescriptor>
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<@JvmWildcard SimpleFunctionDescriptor>
/**
* These methods may return a superset of an actual names' set
@@ -25,9 +25,11 @@ import org.jetbrains.kotlin.utils.alwaysTrue
abstract class MemberScopeImpl : MemberScope {
override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? = null
override fun getContributedVariables(name: Name, location: LookupLocation): Collection<PropertyDescriptor> = emptyList()
override fun getContributedVariables(name: Name, location: LookupLocation): Collection<@JvmWildcard PropertyDescriptor> =
emptyList()
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor> = emptyList()
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<@JvmWildcard SimpleFunctionDescriptor> =
emptyList()
override fun getContributedDescriptors(kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> Boolean): Collection<DeclarationDescriptor> = emptyList()
@@ -39,9 +39,9 @@ interface ResolutionScope {
fun getContributedClassifierIncludeDeprecated(name: Name, location: LookupLocation): DescriptorWithDeprecation<ClassifierDescriptor>? =
getContributedClassifier(name, location)?.let { DescriptorWithDeprecation.createNonDeprecated(it) }
fun getContributedVariables(name: Name, location: LookupLocation): Collection<VariableDescriptor>
fun getContributedVariables(name: Name, location: LookupLocation): Collection<@JvmWildcard VariableDescriptor>
fun getContributedFunctions(name: Name, location: LookupLocation): Collection<FunctionDescriptor>
fun getContributedFunctions(name: Name, location: LookupLocation): Collection<@JvmWildcard FunctionDescriptor>
/**
* All visible descriptors from current scope possibly filtered by the given name and kind filters
@@ -198,19 +198,13 @@ public class ErrorUtils {
@NotNull
@Override
// TODO: Convert to Kotlin or add @JvmWildcard to MemberScope declarations
// method is covariantly overridden in Kotlin, but collections in Java are invariant
@SuppressWarnings({"unchecked"})
public Set getContributedVariables(@NotNull Name name, @NotNull LookupLocation location) {
public Set<? extends PropertyDescriptor> getContributedVariables(@NotNull Name name, @NotNull LookupLocation location) {
return ERROR_PROPERTY_GROUP;
}
@NotNull
@Override
// TODO: Convert to Kotlin or add @JvmWildcard to MemberScope declarations
// method is covariantly overridden in Kotlin, but collections in Java are invariant
@SuppressWarnings({"unchecked"})
public Set getContributedFunctions(@NotNull Name name, @NotNull LookupLocation location) {
public Set<? extends SimpleFunctionDescriptor> getContributedFunctions(@NotNull Name name, @NotNull LookupLocation location) {
return Collections.singleton(createErrorFunction(this));
}
@@ -284,17 +278,15 @@ public class ErrorUtils {
@NotNull
@Override
@SuppressWarnings({"unchecked"}) // KT-9898 Impossible implement kotlin interface from java
public Collection getContributedVariables(@NotNull Name name, @NotNull LookupLocation location) {
public Collection<? extends PropertyDescriptor> getContributedVariables(@NotNull Name name, @NotNull LookupLocation location) {
throw new IllegalStateException(debugMessage+", required name: " + name);
}
@NotNull
@Override
// TODO: Convert to Kotlin or add @JvmWildcard to MemberScope declarations
// method is covariantly overridden in Kotlin, but collections in Java are invariant
@SuppressWarnings({"unchecked"})
public Collection getContributedFunctions(@NotNull Name name, @NotNull LookupLocation location) {
public Collection<? extends SimpleFunctionDescriptor> getContributedFunctions(
@NotNull Name name, @NotNull LookupLocation location
) {
throw new IllegalStateException(debugMessage+", required name: " + name);
}
@@ -216,8 +216,9 @@ public final class Namer {
// TODO: get rid of this function
@NotNull
private static String getStableMangledNameForDescriptor(@NotNull ClassDescriptor descriptor, @NotNull String functionName) {
Collection<SimpleFunctionDescriptor> functions = descriptor.getDefaultType().getMemberScope().getContributedFunctions(
Name.identifier(functionName), NoLookupLocation.FROM_BACKEND);
Collection<? extends SimpleFunctionDescriptor> functions = descriptor.getDefaultType().getMemberScope().getContributedFunctions(
Name.identifier(functionName), NoLookupLocation.FROM_BACKEND
);
assert functions.size() == 1 : "Can't select a single function: " + functionName + " in " + descriptor;
SuggestedName suggested = new NameSuggestion().suggest(functions.iterator().next());
assert suggested != null : "Suggested name for class members is always non-null: " + functions.iterator().next();
@@ -152,7 +152,7 @@ public final class CallExpressionTranslator extends AbstractCallExpressionTransl
@Nullable
private static VariableDescriptor getVariableByName(@NotNull LexicalScope scope, @NotNull Name name) {
while (true) {
Collection<VariableDescriptor> variables = scope.getContributedVariables(name, NoLookupLocation.FROM_BACKEND);
Collection<? extends VariableDescriptor> variables = scope.getContributedVariables(name, NoLookupLocation.FROM_BACKEND);
if (!variables.isEmpty()) {
return variables.size() == 1 ? variables.iterator().next() : null;
}