Create actual: add delegation to secondary constructor, if needed

#KT-26518 Fixed
This commit is contained in:
Mikhail Glukhikh
2018-11-14 19:28:41 +03:00
parent 90a2f70fd1
commit 46a5d76254
9 changed files with 39 additions and 2 deletions
@@ -187,6 +187,7 @@ interface DescriptorRendererOptions {
var normalizedVisibilities: Boolean
var renderDefaultVisibility: Boolean
var renderDefaultModality: Boolean
var renderConstructorDelegation: Boolean
var renderActualAnnotationPropertiesInPrimaryConstructor: Boolean
var uninferredTypeParameterAsName: Boolean
var overrideRenderingPolicy: OverrideRenderingPolicy
@@ -725,8 +725,8 @@ internal class DescriptorRendererImpl(
if (renderConstructorKeyword) {
builder.append(renderKeyword("constructor"))
}
val classDescriptor = constructor.containingDeclaration
if (secondaryConstructorsAsPrimary) {
val classDescriptor = constructor.containingDeclaration
if (renderConstructorKeyword) {
builder.append(" ")
}
@@ -736,6 +736,19 @@ internal class DescriptorRendererImpl(
renderValueParameters(constructor.valueParameters, constructor.hasSynthesizedParameterNames(), builder)
if (renderConstructorDelegation && !constructor.isPrimary && classDescriptor is ClassDescriptor) {
val primaryConstructor = classDescriptor.unsubstitutedPrimaryConstructor
if (primaryConstructor != null) {
val parametersWithoutDefault = primaryConstructor.valueParameters.filter {
!it.declaresDefaultValue() && it.varargElementType == null
}
if (parametersWithoutDefault.isNotEmpty()) {
builder.append(" : ").append(renderKeyword("this"))
builder.append(parametersWithoutDefault.joinToString(prefix = "(", postfix = ")", separator = ", ") { "" })
}
}
}
if (secondaryConstructorsAsPrimary) {
renderWhereSuffix(constructor.typeParameters, builder)
}
@@ -18,7 +18,6 @@ package org.jetbrains.kotlin.renderer
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationWithTarget
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.types.KotlinType
import java.lang.IllegalStateException
@@ -82,6 +81,7 @@ internal class DescriptorRendererOptionsImpl : DescriptorRendererOptions {
override var normalizedVisibilities by property(false)
override var renderDefaultVisibility by property(true)
override var renderDefaultModality by property(true)
override var renderConstructorDelegation by property(false)
override var renderActualAnnotationPropertiesInPrimaryConstructor by property(false)
override var uninferredTypeParameterAsName by property(false)
override var includePropertyConstant by property(false)