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:
@@ -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) {
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER
|
||||
|
||||
class <!CONFLICTING_OVERLOADS!>A(x: String = "", y: String = "")<!> {
|
||||
<!CONFLICTING_OVERLOADS!>constructor(x: String, y: String)<!>: <!OVERLOAD_RESOLUTION_AMBIGUITY!>this<!>(x, y) {}
|
||||
<!CONFLICTING_OVERLOADS!>constructor()<!>: <!OVERLOAD_RESOLUTION_AMBIGUITY!>this<!>("", "") {}
|
||||
<!CONFLICTING_OVERLOADS!>constructor()<!>: <!OVERLOAD_RESOLUTION_AMBIGUITY!>this<!>("", "") {}
|
||||
}
|
||||
|
||||
class B {
|
||||
<!CONFLICTING_OVERLOADS!>constructor(x: Int)<!> {}
|
||||
}
|
||||
|
||||
<!CONFLICTING_OVERLOADS!>fun B(x: Int)<!> {}
|
||||
|
||||
class Outer {
|
||||
class <!CONFLICTING_OVERLOADS!>A(x: String = "", y: String = "")<!> {
|
||||
<!CONFLICTING_OVERLOADS!>constructor(x: String, y: String)<!>: <!OVERLOAD_RESOLUTION_AMBIGUITY!>this<!>(x, y) {}
|
||||
<!CONFLICTING_OVERLOADS!>constructor()<!>: <!OVERLOAD_RESOLUTION_AMBIGUITY!>this<!>("", "") {}
|
||||
<!CONFLICTING_OVERLOADS!>constructor()<!>: <!OVERLOAD_RESOLUTION_AMBIGUITY!>this<!>("", "") {}
|
||||
}
|
||||
|
||||
class B {
|
||||
<!CONFLICTING_OVERLOADS!>constructor(x: Int)<!> {
|
||||
}
|
||||
}
|
||||
|
||||
<!CONFLICTING_OVERLOADS!>fun B(x: Int)<!> {}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package
|
||||
|
||||
internal fun B(/*0*/ x: kotlin.Int): kotlin.Unit
|
||||
|
||||
internal final class A {
|
||||
public constructor A()
|
||||
public constructor A()
|
||||
public constructor A(/*0*/ x: kotlin.String = ..., /*1*/ y: kotlin.String = ...)
|
||||
public constructor A(/*0*/ x: kotlin.String, /*1*/ y: kotlin.String)
|
||||
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
|
||||
}
|
||||
|
||||
internal final class B {
|
||||
public constructor B(/*0*/ x: 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
|
||||
}
|
||||
|
||||
internal final class Outer {
|
||||
public constructor Outer()
|
||||
internal final fun B(/*0*/ x: kotlin.Int): kotlin.Unit
|
||||
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
|
||||
|
||||
internal final class A {
|
||||
public constructor A()
|
||||
public constructor A()
|
||||
public constructor A(/*0*/ x: kotlin.String = ..., /*1*/ y: kotlin.String = ...)
|
||||
public constructor A(/*0*/ x: kotlin.String, /*1*/ y: kotlin.String)
|
||||
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
|
||||
}
|
||||
|
||||
internal final class B {
|
||||
public constructor B(/*0*/ x: 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
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
class <!REDECLARATION!>A<!>
|
||||
class <!REDECLARATION!>A<!> {
|
||||
constructor() {}
|
||||
}
|
||||
|
||||
class B
|
||||
class Outer {
|
||||
class B {
|
||||
constructor() {}
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package
|
||||
|
||||
internal 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
|
||||
}
|
||||
|
||||
internal 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
|
||||
}
|
||||
|
||||
internal final class B {
|
||||
public constructor B()
|
||||
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
|
||||
}
|
||||
|
||||
internal final class Outer {
|
||||
public constructor Outer()
|
||||
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
|
||||
|
||||
internal final class B {
|
||||
public constructor B()
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -10547,6 +10547,18 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("redeclarations.kt")
|
||||
public void testRedeclarations() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/secondaryConstructors/redeclarations.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("redeclarationsOfConstructorsIgnored.kt")
|
||||
public void testRedeclarationsOfConstructorsIgnored() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/secondaryConstructors/redeclarationsOfConstructorsIgnored.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("resolvePropertyInitializerWithoutPrimary.kt")
|
||||
public void testResolvePropertyInitializerWithoutPrimary() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/secondaryConstructors/resolvePropertyInitializerWithoutPrimary.kt");
|
||||
|
||||
Reference in New Issue
Block a user