Partial fix for KT-470: Remove duplicating errors on the same element.

Redeclarations for top-level properties still get two REDECLARATION diagnostics. (In overload resolver and declaration resolver)
Also added test for redeclaring extension properties.
Also remove useless null check.
This commit is contained in:
Pavel V. Talanov
2012-07-26 13:09:49 +04:00
parent 1691298539
commit fa1c471d92
5 changed files with 92 additions and 14 deletions
@@ -16,6 +16,7 @@
package org.jetbrains.jet.lang.resolve;
import com.google.common.collect.Sets;
import com.intellij.openapi.util.Pair;
import com.intellij.util.containers.MultiMap;
import org.jetbrains.annotations.NotNull;
@@ -30,6 +31,7 @@ import org.jetbrains.jet.lang.resolve.name.Name;
import javax.inject.Inject;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
/**
* @author Stepan Koltsov
@@ -141,7 +143,7 @@ public class OverloadResolver {
checkOverloadsWithSameName(e.getKey().getFunctionName(), e.getValue(), e.getKey().getNamespace());
}
}
private String nameForErrorMessage(ClassDescriptor classDescriptor, JetClassOrObject jetClass) {
String name = jetClass.getName();
if (name != null) {
@@ -183,7 +185,13 @@ public class OverloadResolver {
// microoptimization
return;
}
Set<Pair<JetDeclaration, CallableMemberDescriptor>> redeclarations = findRedeclarations(functions);
reportRedeclarations(functionContainer, redeclarations);
}
@NotNull
private Set<Pair<JetDeclaration, CallableMemberDescriptor>> findRedeclarations(@NotNull Collection<CallableMemberDescriptor> functions) {
Set<Pair<JetDeclaration, CallableMemberDescriptor>> redeclarations = Sets.newHashSet();
for (CallableMemberDescriptor member : functions) {
for (CallableMemberDescriptor member2 : functions) {
if (member == member2) {
@@ -192,21 +200,26 @@ public class OverloadResolver {
OverloadUtil.OverloadCompatibilityInfo overloadable = OverloadUtil.isOverloadable(member, member2);
if (!overloadable.isSuccess()) {
JetDeclaration jetDeclaration = (JetDeclaration) BindingContextUtils.descriptorToDeclaration(trace.getBindingContext(), member);
if (jetDeclaration == null) {
assert member.getKind() == CallableMemberDescriptor.Kind.DELEGATION;
return;
}
if (member instanceof PropertyDescriptor) {
trace.report(Errors.REDECLARATION.on(BindingContextUtils.descriptorToDeclaration(trace.getBindingContext(), member), member.getName().getName()));
}
else {
trace.report(Errors.CONFLICTING_OVERLOADS.on(jetDeclaration, member, functionContainer));
}
JetDeclaration jetDeclaration = (JetDeclaration) BindingContextUtils
.descriptorToDeclaration(trace.getBindingContext(), member);
redeclarations.add(Pair.create(jetDeclaration, member));
}
}
}
return redeclarations;
}
private void reportRedeclarations(@NotNull String functionContainer,
@NotNull Set<Pair<JetDeclaration, CallableMemberDescriptor>> redeclarations) {
for (Pair<JetDeclaration, CallableMemberDescriptor> redeclaration : redeclarations) {
CallableMemberDescriptor memberDescriptor = redeclaration.getSecond();
JetDeclaration jetDeclaration = redeclaration.getFirst();
if (memberDescriptor instanceof PropertyDescriptor) {
trace.report(Errors.REDECLARATION.on(jetDeclaration, memberDescriptor.getName().getName()));
}
else {
trace.report(Errors.CONFLICTING_OVERLOADS.on(jetDeclaration, memberDescriptor, functionContainer));
}
}
}
}
@@ -0,0 +1,4 @@
package foo
val Int.<!REDECLARATION!>foo<!> = 2
val Int.<!REDECLARATION!>foo<!> = 3
@@ -0,0 +1,9 @@
class A() {
val a: Int = 1
fun a(): Int = 2
}
class B() {
fun b(): Int = 2
val b: Int = 1
}
@@ -0,0 +1,37 @@
val <!REDECLARATION, REDECLARATION!>a<!> : Int = 1
val <!REDECLARATION, REDECLARATION!>a<!> : Int = 1
val <!REDECLARATION, REDECLARATION!>a<!> : Int = 1
val <!REDECLARATION, REDECLARATION!>b<!> : Int = 1
val <!REDECLARATION, REDECLARATION!>b<!> : Int = 1
val <!REDECLARATION, REDECLARATION!>b<!> : Int = 1
val <!REDECLARATION, REDECLARATION!>b<!> : Int = 1
<!CONFLICTING_OVERLOADS!>fun foo()<!> {} // and here too
<!CONFLICTING_OVERLOADS!>fun foo()<!> {} // and here
<!CONFLICTING_OVERLOADS!>fun foo()<!> {} // and here
<!CONFLICTING_OVERLOADS!>fun foo()<!> {} // and here
<!CONFLICTING_OVERLOADS!>fun bar()<!> {} // and here
<!CONFLICTING_OVERLOADS!>fun bar()<!> {} // and here
<!CONFLICTING_OVERLOADS!>fun bar()<!> {} // and here
class A {
val <!REDECLARATION!>a<!> : Int = 1
val <!REDECLARATION!>a<!> : Int = 1
val <!REDECLARATION!>a<!> : Int = 1
val <!REDECLARATION!>b<!> : Int = 1
val <!REDECLARATION!>b<!> : Int = 1
val <!REDECLARATION!>b<!> : Int = 1
val <!REDECLARATION!>b<!> : Int = 1
<!CONFLICTING_OVERLOADS!>fun foo()<!> {} // and here too
<!CONFLICTING_OVERLOADS!>fun foo()<!> {} // and here
<!CONFLICTING_OVERLOADS!>fun foo()<!> {} // and here
<!CONFLICTING_OVERLOADS!>fun foo()<!> {} // and here
<!CONFLICTING_OVERLOADS!>fun bar()<!> {} // and here
<!CONFLICTING_OVERLOADS!>fun bar()<!> {} // and here
<!CONFLICTING_OVERLOADS!>fun bar()<!> {} // and here
}
@@ -1905,6 +1905,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.checkers.AbstractDiagnosticsTestWithEagerResolve", new File("compiler/testData/diagnostics/tests/redeclarations"), "kt", false);
}
@TestMetadata("ConflictingExtensionProperties.kt")
public void testConflictingExtensionProperties() throws Exception {
doTest("compiler/testData/diagnostics/tests/redeclarations/ConflictingExtensionProperties.kt");
}
@TestMetadata("kt2247.kt")
public void testKt2247() throws Exception {
doTest("compiler/testData/diagnostics/tests/redeclarations/kt2247.kt");
@@ -1915,11 +1920,21 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage
doTest("compiler/testData/diagnostics/tests/redeclarations/kt2438.kt");
}
@TestMetadata("kt470.kt")
public void testKt470() throws Exception {
doTest("compiler/testData/diagnostics/tests/redeclarations/kt470.kt");
}
@TestMetadata("MultiFilePackageRedeclaration.kt")
public void testMultiFilePackageRedeclaration() throws Exception {
doTest("compiler/testData/diagnostics/tests/redeclarations/MultiFilePackageRedeclaration.kt");
}
@TestMetadata("PropertyAndFunInClass.kt")
public void testPropertyAndFunInClass() throws Exception {
doTest("compiler/testData/diagnostics/tests/redeclarations/PropertyAndFunInClass.kt");
}
@TestMetadata("Redeclarations.kt")
public void testRedeclarations() throws Exception {
doTest("compiler/testData/diagnostics/tests/redeclarations/Redeclarations.kt");