From 0498bd7739a5d26f4738e82652f64ee7025f52dc Mon Sep 17 00:00:00 2001 From: Evgeny Gerashchenko Date: Wed, 13 May 2015 16:21:13 +0300 Subject: [PATCH] KT-7587 Overloads are not generated during partial compilation of package #KT-7587 fixed --- .../DefaultParameterValueSubstitutor.kt | 2 +- .../kotlin/codegen/FunctionCodegen.java | 24 +++++++++++++------ .../kotlin/codegen/PackageCodegen.java | 2 ++ .../nullabilityAnnotations/JvmOverloads.java | 3 +++ .../build/IncrementalJpsTestGenerated.java | 12 ++++++++++ .../pureKotlin/optionalParameter/build.log | 7 ++++++ .../pureKotlin/optionalParameter/fun.kt | 6 +++++ .../pureKotlin/optionalParameter/other.kt | 5 ++++ .../pureKotlin/optionalParameter/other.kt.new | 5 ++++ .../addOptionalParameter/JavaUsage.java | 5 ++++ .../addOptionalParameter/build.log | 18 ++++++++++++++ .../addOptionalParameter/fun.kt | 5 ++++ .../addOptionalParameter/fun.kt.new | 6 +++++ .../addOptionalParameter/other.kt | 5 ++++ .../src/kotlin/jvm/JvmPlatformAnnotations.kt | 2 +- 15 files changed, 98 insertions(+), 9 deletions(-) create mode 100644 jps-plugin/testData/incremental/pureKotlin/optionalParameter/build.log create mode 100644 jps-plugin/testData/incremental/pureKotlin/optionalParameter/fun.kt create mode 100644 jps-plugin/testData/incremental/pureKotlin/optionalParameter/other.kt create mode 100644 jps-plugin/testData/incremental/pureKotlin/optionalParameter/other.kt.new create mode 100644 jps-plugin/testData/incremental/withJava/kotlinUsedInJava/addOptionalParameter/JavaUsage.java create mode 100644 jps-plugin/testData/incremental/withJava/kotlinUsedInJava/addOptionalParameter/build.log create mode 100644 jps-plugin/testData/incremental/withJava/kotlinUsedInJava/addOptionalParameter/fun.kt create mode 100644 jps-plugin/testData/incremental/withJava/kotlinUsedInJava/addOptionalParameter/fun.kt.new create mode 100644 jps-plugin/testData/incremental/withJava/kotlinUsedInJava/addOptionalParameter/other.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/DefaultParameterValueSubstitutor.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/DefaultParameterValueSubstitutor.kt index a139c96dbc2..d19a7778055 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/DefaultParameterValueSubstitutor.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/DefaultParameterValueSubstitutor.kt @@ -70,7 +70,7 @@ public class DefaultParameterValueSubstitutor(val state: GenerationState) { * implementation in the companion object class) * @return true if the overloads annotation was found on the element, false otherwise */ - fun generateOverloadsIfNeeded(methodElement: JetElement, + fun generateOverloadsIfNeeded(methodElement: JetElement?, functionDescriptor: FunctionDescriptor, delegateFunctionDescriptor: FunctionDescriptor, owner: CodegenContext<*>, diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionCodegen.java index 3b994d6b0fe..cd37b9d7475 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/FunctionCodegen.java @@ -118,8 +118,11 @@ public class FunctionCodegen { generateOverloadsWithDefaultValues(function, functionDescriptor, functionDescriptor); } - public void generateOverloadsWithDefaultValues(@NotNull JetNamedFunction function, - FunctionDescriptor functionDescriptor, FunctionDescriptor delegateFunctionDescriptor) { + public void generateOverloadsWithDefaultValues( + @Nullable JetNamedFunction function, + @NotNull FunctionDescriptor functionDescriptor, + @NotNull FunctionDescriptor delegateFunctionDescriptor + ) { new DefaultParameterValueSubstitutor(state).generateOverloadsIfNeeded(function, functionDescriptor, delegateFunctionDescriptor, @@ -303,7 +306,11 @@ public class FunctionCodegen { } @Nullable - private static Type getThisTypeForFunction(@NotNull FunctionDescriptor functionDescriptor, @NotNull MethodContext context, @NotNull JetTypeMapper typeMapper) { + private static Type getThisTypeForFunction( + @NotNull FunctionDescriptor functionDescriptor, + @NotNull MethodContext context, + @NotNull JetTypeMapper typeMapper + ) { ReceiverParameterDescriptor dispatchReceiver = functionDescriptor.getDispatchReceiverParameter(); if (functionDescriptor instanceof ConstructorDescriptor) { return typeMapper.mapType(functionDescriptor); @@ -382,14 +389,15 @@ public class FunctionCodegen { //add this if (thisType != null) { mv.visitLocalVariable("this", thisType.getDescriptor(), null, methodBegin, methodEnd, shift); - } else { + } + else { //TODO: provide thisType for callable reference } shift++; } for (int i = 0; i < params.size(); i++) { - JvmMethodParameterSignature param = params.get(i); + JvmMethodParameterSignature param = params.get(i); JvmMethodParameterKind kind = param.getKind(); String parameterName; @@ -749,7 +757,8 @@ public class FunctionCodegen { ) { int flags = ACC_PUBLIC | ACC_BRIDGE | ACC_SYNTHETIC; // TODO. - MethodVisitor mv = v.newMethod(DiagnosticsPackage.Bridge(descriptor, origin), flags, delegateTo.getName(), bridge.getDescriptor(), null, null); + MethodVisitor mv = + v.newMethod(DiagnosticsPackage.Bridge(descriptor, origin), flags, delegateTo.getName(), bridge.getDescriptor(), null, null); if (state.getClassBuilderMode() != ClassBuilderMode.FULL) return; mv.visitCode(); @@ -776,7 +785,8 @@ public class FunctionCodegen { } public void genDelegate(@NotNull FunctionDescriptor functionDescriptor, FunctionDescriptor overriddenDescriptor, StackValue field) { - genDelegate(functionDescriptor, overriddenDescriptor.getOriginal(), (ClassDescriptor) overriddenDescriptor.getContainingDeclaration(), field); + genDelegate(functionDescriptor, overriddenDescriptor.getOriginal(), + (ClassDescriptor) overriddenDescriptor.getContainingDeclaration(), field); } public void genDelegate( diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/PackageCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/PackageCodegen.java index 4047017504e..460411b90f4 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/PackageCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/PackageCodegen.java @@ -190,6 +190,8 @@ public class PackageCodegen { memberCodegen.functionCodegen.generateDefaultIfNeeded( context.intoFunction(function), function, OwnerKind.PACKAGE, DefaultParameterValueLoader.DEFAULT, null ); + + memberCodegen.functionCodegen.generateOverloadsWithDefaultValues(null, function, function); } else if (member instanceof DeserializedPropertyDescriptor) { memberCodegen.propertyCodegen.generateInPackageFacade((DeserializedPropertyDescriptor) member); diff --git a/compiler/testData/asJava/lightClasses/nullabilityAnnotations/JvmOverloads.java b/compiler/testData/asJava/lightClasses/nullabilityAnnotations/JvmOverloads.java index 595d9c8dbe4..513098d2c44 100644 --- a/compiler/testData/asJava/lightClasses/nullabilityAnnotations/JvmOverloads.java +++ b/compiler/testData/asJava/lightClasses/nullabilityAnnotations/JvmOverloads.java @@ -1,10 +1,13 @@ public final class C { + @kotlin.jvm.overloads @org.jetbrains.annotations.NotNull public final java.lang.String foo(@org.jetbrains.annotations.NotNull java.lang.String o, @org.jetbrains.annotations.NotNull java.lang.String s1, @org.jetbrains.annotations.NotNull java.lang.String k, @org.jetbrains.annotations.Nullable java.lang.String s2) { /* compiled code */ } + @kotlin.jvm.overloads @org.jetbrains.annotations.NotNull public java.lang.String foo(@org.jetbrains.annotations.NotNull java.lang.String p, @org.jetbrains.annotations.NotNull java.lang.String p1, @org.jetbrains.annotations.Nullable java.lang.String p2) { /* compiled code */ } + @kotlin.jvm.overloads @org.jetbrains.annotations.NotNull public java.lang.String foo(@org.jetbrains.annotations.NotNull java.lang.String p, @org.jetbrains.annotations.Nullable java.lang.String p1) { /* compiled code */ } diff --git a/jps-plugin/test/org/jetbrains/kotlin/jps/build/IncrementalJpsTestGenerated.java b/jps-plugin/test/org/jetbrains/kotlin/jps/build/IncrementalJpsTestGenerated.java index 9d89e84f6b2..5b7d3b434ea 100644 --- a/jps-plugin/test/org/jetbrains/kotlin/jps/build/IncrementalJpsTestGenerated.java +++ b/jps-plugin/test/org/jetbrains/kotlin/jps/build/IncrementalJpsTestGenerated.java @@ -299,6 +299,12 @@ public class IncrementalJpsTestGenerated extends AbstractIncrementalJpsTest { doTest(fileName); } + @TestMetadata("optionalParameter") + public void testOptionalParameter() throws Exception { + String fileName = JetTestUtils.navigationMetadata("jps-plugin/testData/incremental/pureKotlin/optionalParameter/"); + doTest(fileName); + } + @TestMetadata("ourClassReferenced") public void testOurClassReferenced() throws Exception { String fileName = JetTestUtils.navigationMetadata("jps-plugin/testData/incremental/pureKotlin/ourClassReferenced/"); @@ -552,6 +558,12 @@ public class IncrementalJpsTestGenerated extends AbstractIncrementalJpsTest { @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) public static class KotlinUsedInJava extends AbstractIncrementalJpsTest { + @TestMetadata("addOptionalParameter") + public void testAddOptionalParameter() throws Exception { + String fileName = JetTestUtils.navigationMetadata("jps-plugin/testData/incremental/withJava/kotlinUsedInJava/addOptionalParameter/"); + doTest(fileName); + } + public void testAllFilesPresentInKotlinUsedInJava() throws Exception { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("jps-plugin/testData/incremental/withJava/kotlinUsedInJava"), Pattern.compile("^([^\\.]+)$"), true); } diff --git a/jps-plugin/testData/incremental/pureKotlin/optionalParameter/build.log b/jps-plugin/testData/incremental/pureKotlin/optionalParameter/build.log new file mode 100644 index 00000000000..830fd9d2ae7 --- /dev/null +++ b/jps-plugin/testData/incremental/pureKotlin/optionalParameter/build.log @@ -0,0 +1,7 @@ +Cleaning output files: +out/production/module/test/TestPackage$other$*.class +out/production/module/test/TestPackage.class +End of files +Compiling files: +src/other.kt +End of files diff --git a/jps-plugin/testData/incremental/pureKotlin/optionalParameter/fun.kt b/jps-plugin/testData/incremental/pureKotlin/optionalParameter/fun.kt new file mode 100644 index 00000000000..1b8d4e731e4 --- /dev/null +++ b/jps-plugin/testData/incremental/pureKotlin/optionalParameter/fun.kt @@ -0,0 +1,6 @@ +package test + +kotlin.jvm.overloads +fun f(a: String, b: Int = 5) { + +} \ No newline at end of file diff --git a/jps-plugin/testData/incremental/pureKotlin/optionalParameter/other.kt b/jps-plugin/testData/incremental/pureKotlin/optionalParameter/other.kt new file mode 100644 index 00000000000..d2639425252 --- /dev/null +++ b/jps-plugin/testData/incremental/pureKotlin/optionalParameter/other.kt @@ -0,0 +1,5 @@ +package test + +fun other() { + +} \ No newline at end of file diff --git a/jps-plugin/testData/incremental/pureKotlin/optionalParameter/other.kt.new b/jps-plugin/testData/incremental/pureKotlin/optionalParameter/other.kt.new new file mode 100644 index 00000000000..d2639425252 --- /dev/null +++ b/jps-plugin/testData/incremental/pureKotlin/optionalParameter/other.kt.new @@ -0,0 +1,5 @@ +package test + +fun other() { + +} \ No newline at end of file diff --git a/jps-plugin/testData/incremental/withJava/kotlinUsedInJava/addOptionalParameter/JavaUsage.java b/jps-plugin/testData/incremental/withJava/kotlinUsedInJava/addOptionalParameter/JavaUsage.java new file mode 100644 index 00000000000..da2f9b63512 --- /dev/null +++ b/jps-plugin/testData/incremental/withJava/kotlinUsedInJava/addOptionalParameter/JavaUsage.java @@ -0,0 +1,5 @@ +class JavaUsage { + public static void main(String[] args) { + test.TestPackage.f("x"); + } +} \ No newline at end of file diff --git a/jps-plugin/testData/incremental/withJava/kotlinUsedInJava/addOptionalParameter/build.log b/jps-plugin/testData/incremental/withJava/kotlinUsedInJava/addOptionalParameter/build.log new file mode 100644 index 00000000000..2a0d68e2bd0 --- /dev/null +++ b/jps-plugin/testData/incremental/withJava/kotlinUsedInJava/addOptionalParameter/build.log @@ -0,0 +1,18 @@ +Cleaning output files: +out/production/module/test/TestPackage$fun$*.class +out/production/module/test/TestPackage.class +End of files +Compiling files: +src/fun.kt +End of files +Cleaning output files: +out/production/module/JavaUsage.class +out/production/module/test/TestPackage$other$*.class +out/production/module/test/TestPackage.class +End of files +Compiling files: +src/other.kt +End of files +Compiling files: +src/JavaUsage.java +End of files \ No newline at end of file diff --git a/jps-plugin/testData/incremental/withJava/kotlinUsedInJava/addOptionalParameter/fun.kt b/jps-plugin/testData/incremental/withJava/kotlinUsedInJava/addOptionalParameter/fun.kt new file mode 100644 index 00000000000..15105c825ed --- /dev/null +++ b/jps-plugin/testData/incremental/withJava/kotlinUsedInJava/addOptionalParameter/fun.kt @@ -0,0 +1,5 @@ +package test + +fun f(a: String) { + +} \ No newline at end of file diff --git a/jps-plugin/testData/incremental/withJava/kotlinUsedInJava/addOptionalParameter/fun.kt.new b/jps-plugin/testData/incremental/withJava/kotlinUsedInJava/addOptionalParameter/fun.kt.new new file mode 100644 index 00000000000..1b8d4e731e4 --- /dev/null +++ b/jps-plugin/testData/incremental/withJava/kotlinUsedInJava/addOptionalParameter/fun.kt.new @@ -0,0 +1,6 @@ +package test + +kotlin.jvm.overloads +fun f(a: String, b: Int = 5) { + +} \ No newline at end of file diff --git a/jps-plugin/testData/incremental/withJava/kotlinUsedInJava/addOptionalParameter/other.kt b/jps-plugin/testData/incremental/withJava/kotlinUsedInJava/addOptionalParameter/other.kt new file mode 100644 index 00000000000..d2639425252 --- /dev/null +++ b/jps-plugin/testData/incremental/withJava/kotlinUsedInJava/addOptionalParameter/other.kt @@ -0,0 +1,5 @@ +package test + +fun other() { + +} \ No newline at end of file diff --git a/libraries/stdlib/src/kotlin/jvm/JvmPlatformAnnotations.kt b/libraries/stdlib/src/kotlin/jvm/JvmPlatformAnnotations.kt index 055795f6598..a37428e74af 100644 --- a/libraries/stdlib/src/kotlin/jvm/JvmPlatformAnnotations.kt +++ b/libraries/stdlib/src/kotlin/jvm/JvmPlatformAnnotations.kt @@ -25,5 +25,5 @@ import java.lang.annotation.RetentionPolicy * If a method has N parameters and M of which have default values, M overloads are generated: the first one * takes N-1 parameters (all but the last one that takes a default value), the second takes N-2 parameters, and so on. */ -Retention(RetentionPolicy.SOURCE) +Retention(RetentionPolicy.CLASS) public annotation class overloads