Report conflicting overloads for constructors

- It's worked almost fine but diagnostics were filtered out by
  positioning strategy.

- Also a couple of "put" calls to Multimap are replaced by "putValues"
  within OverloadResolver, that is more semantically correct.

- Note that constructors of top-level classes are handled when processing
  package, it helps to figure out if there are clashes with top-level
  functions that have the same name.

- But constructors of different classes are not reported as
  overloads because containing classes has the same name and will be
  reported as redeclaration.
This commit is contained in:
Denis Zharkov
2015-03-04 19:02:09 +03:00
parent 9d2d3c0c7b
commit 5eb4dcd913
7 changed files with 153 additions and 7 deletions
@@ -31,7 +31,9 @@ import org.jetbrains.kotlin.psi.psiUtil.*
public object PositioningStrategies {
private open class DeclarationHeader<T : JetDeclaration> : PositioningStrategy<T>() {
override fun isValid(element: T): Boolean {
if (element is JetNamedDeclaration && element !is JetObjectDeclaration) {
if (element is JetNamedDeclaration &&
(element !is JetObjectDeclaration && element !is JetSecondaryConstructor)
) {
if (element.getNameIdentifier() == null) {
return false
}
@@ -137,6 +139,9 @@ public object PositioningStrategies {
is JetObjectDeclaration -> {
return DECLARATION_NAME.mark(element)
}
is JetSecondaryConstructor -> {
return markRange(element.getConstructorKeyword(), element.getValueParameterList() ?: element.getConstructorKeyword())
}
}
return super.mark(element)
}
@@ -71,10 +71,10 @@ public class OverloadResolver {
DeclarationDescriptor containingDeclaration = klass.getContainingDeclaration();
if (containingDeclaration instanceof ClassDescriptor) {
ClassDescriptor classDescriptor = (ClassDescriptor) containingDeclaration;
inClasses.put(classDescriptor, klass.getConstructors());
inClasses.putValues(classDescriptor, klass.getConstructors());
}
else if (containingDeclaration instanceof PackageFragmentDescriptor) {
inPackages.put(getFqName(klass), klass.getConstructors());
inPackages.putValues(getFqName(klass), klass.getConstructors());
}
else if (containingDeclaration instanceof ScriptDescriptor) {
// TODO: check overload conflicts of functions with constructors in scripts
@@ -146,9 +146,6 @@ public class OverloadResolver {
for (Map.Entry<Name, Collection<CallableMemberDescriptor>> e : functionsByName.entrySet()) {
checkOverloadsWithSameName(e.getValue(), nameForErrorMessage(classDescriptor, klass));
}
// Kotlin has no secondary constructors at this time
}
private void checkOverloadsWithSameName(
@@ -167,7 +164,7 @@ public class OverloadResolver {
Set<Pair<JetDeclaration, CallableMemberDescriptor>> redeclarations = Sets.newLinkedHashSet();
for (CallableMemberDescriptor member : functions) {
for (CallableMemberDescriptor member2 : functions) {
if (member == member2) {
if (member == member2 || isConstructorsOfDifferentRedeclaredClasses(member, member2)) {
continue;
}
@@ -183,6 +180,18 @@ public class OverloadResolver {
return redeclarations;
}
private static boolean isConstructorsOfDifferentRedeclaredClasses(
@NotNull CallableMemberDescriptor member, @NotNull CallableMemberDescriptor member2
) {
if (!(member instanceof ConstructorDescriptor) || !(member2 instanceof ConstructorDescriptor)) return false;
// ignore conflicting overloads for constructors of different classes because their redeclarations will be reported
// but don't ignore if there's possibility that classes redeclarations will not be reported
// (e.g. they're declared in different packages)
assert member.getContainingDeclaration().getContainingDeclaration() != null : "Grandparent of constructor should not be null";
return member.getContainingDeclaration() != member2.getContainingDeclaration() &&
member.getContainingDeclaration().getContainingDeclaration().equals(member2.getContainingDeclaration().getContainingDeclaration());
}
private void reportRedeclarations(@NotNull String functionContainer,
@NotNull Set<Pair<JetDeclaration, CallableMemberDescriptor>> redeclarations) {
for (Pair<JetDeclaration, CallableMemberDescriptor> redeclaration : redeclarations) {