From bea5aa8a45ff70f90a88185c4ae65eaa88cde1fc Mon Sep 17 00:00:00 2001 From: Igor Chevdar Date: Thu, 14 Jun 2018 17:26:36 +0300 Subject: [PATCH] Fixed bugs with contracts DSL --- .../kotlin/backend/konan/KonanLower.kt | 2 ++ .../konan/lower/ContractsDslRemover.kt | 35 +++++++++++++++++++ .../konan/serialization/DescriptorTable.kt | 6 ++++ .../serialization/IrDescriptorDeserializer.kt | 2 +- 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ContractsDslRemover.kt diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLower.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLower.kt index 2d9ff2ae563..8210f3bed49 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLower.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLower.kt @@ -70,6 +70,8 @@ internal class KonanLower(val context: Context) { phaser.phase(KonanPhase.LOWER_AFTER_INLINE) { irModule.files.forEach(PostInlineLowering(context)::lower) + // TODO: Seems like this should be deleted in PsiToIR. + irModule.files.forEach(ContractsDslRemover(context)::lower) } phaser.phase(KonanPhase.LOWER_INTEROP_PART1) { diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ContractsDslRemover.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ContractsDslRemover.kt new file mode 100644 index 00000000000..a4e729c9b38 --- /dev/null +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ContractsDslRemover.kt @@ -0,0 +1,35 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.backend.konan.lower + +import org.jetbrains.kotlin.backend.common.DeclarationContainerLoweringPass +import org.jetbrains.kotlin.backend.konan.Context +import org.jetbrains.kotlin.contracts.parsing.ContractsDslNames +import org.jetbrains.kotlin.ir.declarations.IrClass +import org.jetbrains.kotlin.ir.declarations.IrDeclarationContainer +import org.jetbrains.kotlin.ir.util.transformFlat + +internal class ContractsDslRemover(val context: Context) : DeclarationContainerLoweringPass { + override fun lower(irDeclarationContainer: IrDeclarationContainer) { + irDeclarationContainer.declarations.transformFlat { + if (it is IrClass && it.descriptor.annotations.hasAnnotation(ContractsDslNames.CONTRACTS_DSL_ANNOTATION_FQN)) + null + else + listOf(it) + } + } +} \ No newline at end of file diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/DescriptorTable.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/DescriptorTable.kt index cc37fd2eab8..f65fa274ee5 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/DescriptorTable.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/DescriptorTable.kt @@ -24,6 +24,7 @@ import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.backend.konan.llvm.localHash import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.contracts.parsing.ContractsDslNames import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction import org.jetbrains.kotlin.name.FqName @@ -107,6 +108,11 @@ val IrBuiltIns.irBuiltInDescriptors internal tailrec fun DeclarationDescriptor.isExported(): Boolean { assert(!this.isExpectMember) { this } + if (this.annotations.findAnnotation(ContractsDslNames.CONTRACTS_DSL_ANNOTATION_FQN) != null) { + // TODO: Seems like this should be deleted in PsiToIR. + // Treat any `@ContractsDsl` declaration as exported. + return true + } if (this.annotations.findAnnotation(symbolNameAnnotation) != null) { // Treat any `@SymbolName` declaration as exported. return true diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/IrDescriptorDeserializer.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/IrDescriptorDeserializer.kt index 7ec0933b113..7ae3a5e4052 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/IrDescriptorDeserializer.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/IrDescriptorDeserializer.kt @@ -108,7 +108,7 @@ internal class IrDescriptorDeserializer(val context: Context, descriptorIndex[index]!! CLASS, CONSTRUCTOR, FUNCTION, ACCESSOR -> descriptorIndex[index] ?: - findInTheDescriptorTree(proto)!! // Note: can be null when using `@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE")` + findInTheDescriptorTree(proto) ?: error("${proto.name} is not found") // Note: can be null when using `@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE")` else -> TODO("Unexpected descriptor kind: $kind") }