[K2/N] Fix property accessors modality in K2 metadata

^KT-56603
This commit is contained in:
Pavel Kunyavskiy
2023-03-16 15:48:44 +01:00
committed by Space Team
parent aa3c189d83
commit b01cc1f88c
8 changed files with 63 additions and 7 deletions
@@ -929,7 +929,8 @@ class FirElementSerializer private constructor(
return Flags.getAccessorFlags(
nonSourceAnnotations.isNotEmpty(),
ProtoEnumFlags.visibility(normalizeVisibility(accessor)),
ProtoEnumFlags.modality(accessor.modality!!),
// non-default accessor modality is always final, so we check property.modality instead
ProtoEnumFlags.modality(property.modality!!),
!isDefault,
accessor.isExternal,
accessor.isInline
@@ -79,18 +79,22 @@ class DeclarationPrinter(
override fun visitPropertyDescriptor(descriptor: PropertyDescriptor, data: Unit) {
printer.printPlain(header = headerRenderer.render(descriptor), signature = signatureRenderer.render(descriptor))
descriptor.getter?.takeIf { !it.annotations.isEmpty() }?.let { getter ->
descriptor.getter?.takeUnless { canSkipAccessor(it, descriptor) }?.let { getter ->
printer.pushIndent()
printer.printPlain(header = headerRenderer.render(getter), signature = signatureRenderer.render(getter))
printer.popIndent()
}
descriptor.setter?.takeIf { !it.annotations.isEmpty() || it.visibility != descriptor.visibility }?.let { setter ->
descriptor.setter?.takeUnless { canSkipAccessor(it, descriptor) }?.let { setter ->
printer.pushIndent()
printer.printPlain(header = headerRenderer.render(setter), signature = signatureRenderer.render(setter))
printer.popIndent()
}
}
private fun canSkipAccessor(accessor: PropertyAccessorDescriptor, property: PropertyDescriptor) : Boolean {
return accessor.annotations.isEmpty() && accessor.visibility == property.visibility && accessor.modality == property.modality
}
override fun visitConstructorDescriptor(descriptor: ConstructorDescriptor, data: Unit) {
printer.printPlain(header = headerRenderer.render(descriptor), signature = signatureRenderer.render(descriptor))
}
@@ -48,6 +48,9 @@ object DefaultDeclarationHeaderRenderer : DeclarationHeaderRenderer {
if (descriptor.visibility != DescriptorVisibilities.DEFAULT_VISIBILITY) {
append(descriptor.visibility.internalDisplayName).append(" ")
}
if (descriptor.modality != descriptor.correspondingProperty.modality) {
append(descriptor.modality.name.lowercase()).append(" ")
}
when (descriptor) {
is PropertyGetterDescriptor -> append("get")
is PropertySetterDescriptor -> append("set")
@@ -0,0 +1,23 @@
package test;
abstract class A {
abstract val a: Int
abstract var b: Int
protected set
val c: Int = TODO()
val d: Int
get() = TODO()
var e: Int
get() = TODO()
set(value) = TODO()
var f: Int = TODO()
private set
open val g: Int = TODO()
open val h: Int
get() = TODO()
open var k: Int
get() = TODO()
set(value) = TODO()
open var l: Int = TODO()
protected set
}
@@ -0,0 +1,15 @@
abstract class A constructor() {
abstract val a: Int
abstract var b: Int
protected set
val c: Int
val d: Int
var e: Int
var f: Int
private set
open val g: Int
open val h: Int
open var k: Int
open var l: Int
protected set
}
@@ -53,6 +53,12 @@ public class FirNativeKLibContentsTestGenerated extends AbstractNativeKlibConten
runTest("native/native.tests/testData/klibContents/kt56018_value_parameters_annotations.kt");
}
@Test
@TestMetadata("property_accessors.kt")
public void testProperty_accessors() throws Exception {
runTest("native/native.tests/testData/klibContents/property_accessors.kt");
}
@Test
@TestMetadata("type_annotations.kt")
public void testType_annotations() throws Exception {
@@ -50,6 +50,12 @@ public class K1NativeKLibContentsTestGenerated extends AbstractNativeKlibContent
runTest("native/native.tests/testData/klibContents/kt56018_value_parameters_annotations.kt");
}
@Test
@TestMetadata("property_accessors.kt")
public void testProperty_accessors() throws Exception {
runTest("native/native.tests/testData/klibContents/property_accessors.kt");
}
@Test
@TestMetadata("type_annotations.kt")
public void testType_annotations() throws Exception {
@@ -15,6 +15,7 @@ import org.jetbrains.kotlin.konan.blackboxtest.support.runner.*
import org.jetbrains.kotlin.konan.blackboxtest.support.settings.*
import org.jetbrains.kotlin.konan.blackboxtest.support.util.*
import org.jetbrains.kotlin.test.services.JUnit5Assertions.assertEquals
import org.jetbrains.kotlin.test.services.JUnit5Assertions.assertEqualsToFile
import org.junit.jupiter.api.Tag
import java.io.File
@@ -31,10 +32,7 @@ abstract class AbstractNativeKlibContentsTest : AbstractNativeSimpleTest() {
val kotlinNativeClassLoader = testRunSettings.get<KotlinNativeClassLoader>()
val klibContents = testCompilationResult.assertSuccess().resultingArtifact.getContents(kotlinNativeClassLoader.classLoader)
val klibContentsFiltered = filterContentsOutput(klibContents, linestoExclude = listOf("package test {", "}", ""))
val expectedContents = File("${testPathFull.canonicalPath.substringBeforeLast(".")}.txt").readText()
assertEquals(StringUtilRt.convertLineSeparators(expectedContents), StringUtilRt.convertLineSeparators(klibContentsFiltered)) {
"Test failed. Compilation result was: $testCompilationResult"
}
assertEqualsToFile(File("${testPathFull.canonicalPath.substringBeforeLast(".")}.txt"), StringUtilRt.convertLineSeparators(klibContentsFiltered))
}
private fun generateTestCaseWithSingleSource(source: File, extraArgs: List<String>): TestCase {