diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/OverloadResolver.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/OverloadResolver.java index 42a35e348ec..f128bad08e3 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/OverloadResolver.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/OverloadResolver.java @@ -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> redeclarations = findRedeclarations(functions); + reportRedeclarations(functionContainer, redeclarations); + } + + @NotNull + private Set> findRedeclarations(@NotNull Collection functions) { + Set> 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> redeclarations) { + for (Pair 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)); + } + } + } } diff --git a/compiler/testData/diagnostics/tests/redeclarations/ConflictingExtensionProperties.kt b/compiler/testData/diagnostics/tests/redeclarations/ConflictingExtensionProperties.kt new file mode 100644 index 00000000000..97428be2fa6 --- /dev/null +++ b/compiler/testData/diagnostics/tests/redeclarations/ConflictingExtensionProperties.kt @@ -0,0 +1,4 @@ +package foo + +val Int.foo = 2 +val Int.foo = 3 diff --git a/compiler/testData/diagnostics/tests/redeclarations/PropertyAndFunInClass.kt b/compiler/testData/diagnostics/tests/redeclarations/PropertyAndFunInClass.kt new file mode 100644 index 00000000000..651d4ac0235 --- /dev/null +++ b/compiler/testData/diagnostics/tests/redeclarations/PropertyAndFunInClass.kt @@ -0,0 +1,9 @@ +class A() { + val a: Int = 1 + fun a(): Int = 2 +} + +class B() { + fun b(): Int = 2 + val b: Int = 1 +} diff --git a/compiler/testData/diagnostics/tests/redeclarations/kt470.kt b/compiler/testData/diagnostics/tests/redeclarations/kt470.kt new file mode 100644 index 00000000000..6e0d1c45b1e --- /dev/null +++ b/compiler/testData/diagnostics/tests/redeclarations/kt470.kt @@ -0,0 +1,37 @@ +val a : Int = 1 +val a : Int = 1 +val a : Int = 1 + +val b : Int = 1 +val b : Int = 1 +val b : Int = 1 +val b : Int = 1 + +fun foo() {} // and here too +fun foo() {} // and here +fun foo() {} // and here +fun foo() {} // and here + +fun bar() {} // and here +fun bar() {} // and here +fun bar() {} // and here + +class A { + val a : Int = 1 + val a : Int = 1 + val a : Int = 1 + + val b : Int = 1 + val b : Int = 1 + val b : Int = 1 + val b : Int = 1 + + fun foo() {} // and here too + fun foo() {} // and here + fun foo() {} // and here + fun foo() {} // and here + + fun bar() {} // and here + fun bar() {} // and here + fun bar() {} // and here +} diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index 1c8f7d59ecd..25121a79a5f 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -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");