Fix annoying parameter name override warning

Some corner cases still remain: KotlinSignature, propagation, deserialized
delegates to Java interfaces

 #KT-1239 Obsolete
 #KT-1924 In Progress
 #KT-2081 Fixed
This commit is contained in:
Alexander Udalov
2014-03-12 01:30:42 +04:00
parent 80a374a6e4
commit 7fcd42f40c
16 changed files with 198 additions and 15 deletions
@@ -675,12 +675,15 @@ public class OverrideResolver {
}
}
if (!parameterFromSuperclass.getName().equals(parameterFromSubclass.getName())) {
DeclarationDescriptor superFunction = parameterFromSuperclass.getContainingDeclaration();
if (declared.hasStableParameterNames() &&
superFunction instanceof CallableDescriptor && ((CallableDescriptor) superFunction).hasStableParameterNames() &&
!parameterFromSuperclass.getName().equals(parameterFromSubclass.getName())) {
if (noDeclaration) {
trace.report(DIFFERENT_NAMES_FOR_THE_SAME_PARAMETER_IN_SUPERTYPES.on(classElement, declared.getOverriddenDescriptors(), parameterFromSuperclass.getIndex() + 1));
}
else {
trace.report(PARAMETER_NAME_CHANGED_ON_OVERRIDE.on(parameter, (ClassDescriptor) parameterFromSuperclass.getContainingDeclaration().getContainingDeclaration(), parameterFromSuperclass));
trace.report(PARAMETER_NAME_CHANGED_ON_OVERRIDE.on(parameter, (ClassDescriptor) superFunction.getContainingDeclaration(), parameterFromSuperclass));
}
}
}
@@ -0,0 +1,3 @@
interface JavaInterface {
void foo(int javaName);
}
@@ -0,0 +1,14 @@
// Simple inheritance. Checks that there's no PARAMETER_NAME_CHANGED_ON_OVERRIDE warning
class SimpleSubclass : JavaInterface {
override fun foo(kotlinName: Int) {}
}
// Class extends both Java and Kotlin traits. Checks that there's no DIFFERENT_NAMES_FOR_THE_SAME_PARAMETER_IN_SUPERTYPES warning
trait KotlinTrait {
fun foo(someOtherName: Int) {}
}
class BothTraitsSubclass : JavaInterface, KotlinTrait
@@ -7,8 +7,8 @@ trait D {
}
trait <!DIFFERENT_NAMES_FOR_THE_SAME_PARAMETER_IN_SUPERTYPES!>E<!> : C, D
trait F : C, D {
trait F : C, D {
override fun foo(<!PARAMETER_NAME_CHANGED_ON_OVERRIDE!>a<!> : Int) {
throw UnsupportedOperationException()
}
@@ -0,0 +1,16 @@
// FILE: Super.java
interface Super {
void foo(long superName);
}
// FILE: Sub.java
interface Sub extends Super {
}
// FILE: SubSub.kt
class SubSub : Sub {
override fun foo(subName: Long) {}
}
@@ -0,0 +1,13 @@
// FILE: JavaInterface.java
interface JavaInterface {
void foo(int javaName);
}
// FILE: kotlin.kt
trait KotlinTrait {
fun foo(someOtherName: Int) {}
}
class BothTraitsSubclass : JavaInterface, KotlinTrait
@@ -0,0 +1,18 @@
// FILE: JavaInterface.java
interface JavaInterface {
void foo(int javaName);
}
// FILE: kotlin.kt
class SimpleSubclass : JavaInterface {
override fun foo(kotlinName: Int) {}
}
trait SubtraitWithFakeOverride : JavaInterface
class Subclass : SubtraitWithFakeOverride {
override fun foo(otherKotlinName: Int) {}
}
@@ -5149,6 +5149,7 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
}
@TestMetadata("compiler/testData/diagnostics/tests/override")
@InnerTestClasses({Override.ParameterNames.class})
public static class Override extends AbstractJetDiagnosticsTest {
@TestMetadata("AbstractFunImplemented.kt")
public void testAbstractFunImplemented() throws Exception {
@@ -5204,16 +5205,6 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest("compiler/testData/diagnostics/tests/override/ConflictingFunctionSignatureFromSuperclass.kt");
}
@TestMetadata("ConflictingParameterNames.kt")
public void testConflictingParameterNames() throws Exception {
doTest("compiler/testData/diagnostics/tests/override/ConflictingParameterNames.kt");
}
@TestMetadata("ConflictingParameterNames-MultipleSupertypes.kt")
public void testConflictingParameterNames_MultipleSupertypes() throws Exception {
doTest("compiler/testData/diagnostics/tests/override/ConflictingParameterNames-MultipleSupertypes.kt");
}
@TestMetadata("ConflictingPropertySignatureFromSuperclass.kt")
public void testConflictingPropertySignatureFromSuperclass() throws Exception {
doTest("compiler/testData/diagnostics/tests/override/ConflictingPropertySignatureFromSuperclass.kt");
@@ -5344,6 +5335,45 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest("compiler/testData/diagnostics/tests/override/ToAbstractMembersFromSuper-kt1996.kt");
}
@TestMetadata("compiler/testData/diagnostics/tests/override/parameterNames")
public static class ParameterNames extends AbstractJetDiagnosticsTest {
public void testAllFilesPresentInParameterNames() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/diagnostics/tests/override/parameterNames"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("changeOnOverrideDiagnostic.kt")
public void testChangeOnOverrideDiagnostic() throws Exception {
doTest("compiler/testData/diagnostics/tests/override/parameterNames/changeOnOverrideDiagnostic.kt");
}
@TestMetadata("differentNamesInSupertypesDiagnostic.kt")
public void testDifferentNamesInSupertypesDiagnostic() throws Exception {
doTest("compiler/testData/diagnostics/tests/override/parameterNames/differentNamesInSupertypesDiagnostic.kt");
}
@TestMetadata("jjkHierarchy.kt")
public void testJjkHierarchy() throws Exception {
doTest("compiler/testData/diagnostics/tests/override/parameterNames/jjkHierarchy.kt");
}
@TestMetadata("kotlinInheritsBothJavaAndKotlin.kt")
public void testKotlinInheritsBothJavaAndKotlin() throws Exception {
doTest("compiler/testData/diagnostics/tests/override/parameterNames/kotlinInheritsBothJavaAndKotlin.kt");
}
@TestMetadata("kotlinInheritsJava.kt")
public void testKotlinInheritsJava() throws Exception {
doTest("compiler/testData/diagnostics/tests/override/parameterNames/kotlinInheritsJava.kt");
}
}
public static Test innerSuite() {
TestSuite suite = new TestSuite("Override");
suite.addTestSuite(Override.class);
suite.addTestSuite(ParameterNames.class);
return suite;
}
}
@TestMetadata("compiler/testData/diagnostics/tests/recovery")
@@ -6978,7 +7008,7 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
suite.addTestSuite(Objects.class);
suite.addTestSuite(OperatorsOverloading.class);
suite.addTestSuite(Overload.class);
suite.addTestSuite(Override.class);
suite.addTest(Override.innerSuite());
suite.addTestSuite(Recovery.class);
suite.addTestSuite(Redeclarations.class);
suite.addTestSuite(Regressions.class);
@@ -16,6 +16,7 @@
package org.jetbrains.jet.jvm.compiler;
import com.google.common.collect.Iterables;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull;
@@ -24,12 +25,15 @@ import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.MockLibraryUtil;
import org.jetbrains.jet.TestJdkKind;
import org.jetbrains.jet.analyzer.AnalyzeExhaust;
import org.jetbrains.jet.cli.common.messages.AnalyzerWithCompilerReport;
import org.jetbrains.jet.cli.common.messages.MessageCollectorPlainTextToStream;
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
import org.jetbrains.jet.config.CompilerConfiguration;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.PackageViewDescriptor;
import org.jetbrains.jet.lang.resolve.AnalyzerScriptParameter;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM;
import org.jetbrains.jet.test.TestCaseWithTmpdir;
import org.jetbrains.jet.test.util.DescriptorValidator;
@@ -160,4 +164,27 @@ public class CompileKotlinAgainstCustomBinariesTest extends TestCaseWithTmpdir {
public void testMissingEnumReferencedInAnnotationArgument() throws Exception {
doTestWithTxt(copyJarFileWithoutEntry(compileLibrary("library"), "test/E.class"));
}
public void testNoWarningsOnJavaKotlinInheritance() throws Exception {
// This test checks that there are no PARAMETER_NAME_CHANGED_ON_OVERRIDE or DIFFERENT_NAMES_FOR_THE_SAME_PARAMETER_IN_SUPERTYPES
// warnings when subclassing in Kotlin from Java binaries (in case when no parameter names are available for Java classes)
JetTestUtils.compileJavaFiles(
Collections.singletonList(getTestDataFileWithExtension("java")),
Arrays.asList("-d", tmpdir.getPath())
);
Project project = createEnvironment(Collections.singletonList(tmpdir)).getProject();
AnalyzeExhaust exhaust = AnalyzerFacadeForJVM.analyzeOneFileWithJavaIntegration(
JetTestUtils.loadJetFile(project, getTestDataFileWithExtension("kt")),
Collections.<AnalyzerScriptParameter>emptyList()
);
exhaust.throwIfError();
BindingContext bindingContext = exhaust.getBindingContext();
AnalyzerWithCompilerReport.reportDiagnostics(bindingContext, MessageCollectorPlainTextToStream.PLAIN_TEXT_TO_SYSTEM_ERR);
assertEquals("There should be no diagnostics", 0, Iterables.size(bindingContext.getDiagnostics()));
}
}
@@ -18,7 +18,9 @@ package org.jetbrains.jet.lang.resolve.java.descriptor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.SimpleFunctionDescriptor;
import org.jetbrains.jet.lang.descriptors.annotations.Annotations;
import org.jetbrains.jet.lang.descriptors.impl.FunctionDescriptorImpl;
import org.jetbrains.jet.lang.descriptors.impl.SimpleFunctionDescriptorImpl;
import org.jetbrains.jet.lang.resolve.name.Name;
@@ -28,6 +30,41 @@ public class JavaMethodDescriptor extends SimpleFunctionDescriptorImpl implement
@NotNull Annotations annotations,
@NotNull Name name
) {
super(containingDeclaration, annotations, name, Kind.DECLARATION);
this(containingDeclaration, annotations, name, Kind.DECLARATION);
}
private JavaMethodDescriptor(
@NotNull DeclarationDescriptor containingDeclaration,
@NotNull Annotations annotations,
@NotNull Name name,
@NotNull Kind kind
) {
super(containingDeclaration, annotations, name, kind);
}
private JavaMethodDescriptor(
@NotNull DeclarationDescriptor containingDeclaration,
@NotNull SimpleFunctionDescriptor original,
@NotNull Annotations annotations,
@NotNull Name name,
@NotNull Kind kind
) {
super(containingDeclaration, original, annotations, name, kind);
}
@Override
public boolean hasStableParameterNames() {
// TODO: propagated names should be stable
return false;
}
@Override
protected FunctionDescriptorImpl createSubstitutedCopy(DeclarationDescriptor newOwner, boolean preserveOriginal, Kind kind) {
if (preserveOriginal) {
return new JavaMethodDescriptor(newOwner, getOriginal(), getAnnotations(), getName(), kind);
}
else {
return new JavaMethodDescriptor(newOwner, getAnnotations(), getName(), kind);
}
}
}
@@ -50,6 +50,8 @@ public interface CallableDescriptor extends DeclarationDescriptorWithVisibility,
@NotNull
List<ValueParameterDescriptor> getValueParameters();
boolean hasStableParameterNames();
@NotNull
Set<? extends CallableDescriptor> getOverriddenDescriptors();
}
@@ -82,6 +82,11 @@ public abstract class AbstractReceiverParameterDescriptor extends DeclarationDes
return Collections.emptyList();
}
@Override
public boolean hasStableParameterNames() {
return false;
}
@NotNull
@Override
public Set<? extends CallableDescriptor> getOverriddenDescriptors() {
@@ -164,6 +164,11 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorNonRoo
return unsubstitutedValueParameters;
}
@Override
public boolean hasStableParameterNames() {
return true;
}
@Override
public JetType getReturnType() {
return unsubstitutedReturnType;
@@ -83,6 +83,11 @@ public abstract class PropertyAccessorDescriptorImpl extends DeclarationDescript
return Collections.emptyList();
}
@Override
public boolean hasStableParameterNames() {
return false;
}
@NotNull
@Override
public Modality getModality() {
@@ -89,6 +89,11 @@ public abstract class VariableDescriptorImpl extends DeclarationDescriptorNonRoo
return Collections.emptyList();
}
@Override
public boolean hasStableParameterNames() {
return false;
}
@NotNull
@Override
public Set<? extends CallableDescriptor> getOverriddenDescriptors() {