Don't generate empty constructor if there is secondary one with empty parameter list
This commit is contained in:
@@ -39,8 +39,10 @@ import org.jetbrains.kotlin.descriptors.*;
|
|||||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor;
|
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor;
|
||||||
import org.jetbrains.kotlin.load.kotlin.nativeDeclarations.NativeDeclarationsPackage;
|
import org.jetbrains.kotlin.load.kotlin.nativeDeclarations.NativeDeclarationsPackage;
|
||||||
import org.jetbrains.kotlin.name.FqName;
|
import org.jetbrains.kotlin.name.FqName;
|
||||||
|
import org.jetbrains.kotlin.psi.JetClass;
|
||||||
import org.jetbrains.kotlin.psi.JetClassOrObject;
|
import org.jetbrains.kotlin.psi.JetClassOrObject;
|
||||||
import org.jetbrains.kotlin.psi.JetNamedFunction;
|
import org.jetbrains.kotlin.psi.JetNamedFunction;
|
||||||
|
import org.jetbrains.kotlin.psi.JetSecondaryConstructor;
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext;
|
import org.jetbrains.kotlin.resolve.BindingContext;
|
||||||
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils;
|
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils;
|
||||||
import org.jetbrains.kotlin.resolve.DescriptorUtils;
|
import org.jetbrains.kotlin.resolve.DescriptorUtils;
|
||||||
@@ -789,6 +791,7 @@ public class FunctionCodegen {
|
|||||||
Visibilities.isPrivate(constructorDescriptor.getVisibility())) return false;
|
Visibilities.isPrivate(constructorDescriptor.getVisibility())) return false;
|
||||||
|
|
||||||
if (constructorDescriptor.getValueParameters().isEmpty()) return false;
|
if (constructorDescriptor.getValueParameters().isEmpty()) return false;
|
||||||
|
if (classOrObject instanceof JetClass && hasSecondaryConstructorsWithNoParameters((JetClass) classOrObject)) return false;
|
||||||
|
|
||||||
for (ValueParameterDescriptor parameterDescriptor : constructorDescriptor.getValueParameters()) {
|
for (ValueParameterDescriptor parameterDescriptor : constructorDescriptor.getValueParameters()) {
|
||||||
if (!parameterDescriptor.declaresDefaultValue()) {
|
if (!parameterDescriptor.declaresDefaultValue()) {
|
||||||
@@ -798,6 +801,13 @@ public class FunctionCodegen {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean hasSecondaryConstructorsWithNoParameters(@NotNull JetClass klass) {
|
||||||
|
for (JetSecondaryConstructor constructor : klass.getSecondaryConstructors()) {
|
||||||
|
if (constructor.getValueParameters().isEmpty()) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private void generateBridge(
|
private void generateBridge(
|
||||||
@Nullable PsiElement origin,
|
@Nullable PsiElement origin,
|
||||||
@NotNull FunctionDescriptor descriptor,
|
@NotNull FunctionDescriptor descriptor,
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
// do not report generate empty synthetic constructor by primary as it leads to CONFLICTING_JVM_DECLARATIONS
|
||||||
|
class A(val x: Int = 1, val y: Int = 2) {
|
||||||
|
constructor(): this(0, 0) {}
|
||||||
|
}
|
||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
package
|
||||||
|
|
||||||
|
internal final class A {
|
||||||
|
public constructor A()
|
||||||
|
public constructor A(/*0*/ x: kotlin.Int = ..., /*1*/ y: kotlin.Int = ...)
|
||||||
|
internal final val x: kotlin.Int
|
||||||
|
internal final val y: 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
|
||||||
|
}
|
||||||
@@ -10523,6 +10523,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("noDefaultIfEmptySecondary.kt")
|
||||||
|
public void testNoDefaultIfEmptySecondary() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/secondaryConstructors/noDefaultIfEmptySecondary.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("noPrimaryConstructor.kt")
|
@TestMetadata("noPrimaryConstructor.kt")
|
||||||
public void testNoPrimaryConstructor() throws Exception {
|
public void testNoPrimaryConstructor() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/secondaryConstructors/noPrimaryConstructor.kt");
|
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/secondaryConstructors/noPrimaryConstructor.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user