From b218a3a7a2362bd777b1a95288872ce2166c4b3b Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Mon, 21 May 2018 11:06:55 +0300 Subject: [PATCH] Add 'disableDesignatedInitializerChecks' .def file flag When enabled for an Objective-C library, compiler would permit to call a constructor of a class from this library as super-constructor even if it is not marked as designated. Fix #1539 --- .../org/jetbrains/kotlin/native/interop/gen/ObjCStubs.kt | 3 ++- .../kotlin/native/interop/gen/jvm/InteropConfiguration.kt | 3 ++- .../org/jetbrains/kotlin/native/interop/gen/jvm/main.kt | 3 ++- OBJC_INTEROP.md | 6 ++++++ .../main/kotlin/org/jetbrains/kotlin/konan/util/DefFile.kt | 4 ++++ 5 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/ObjCStubs.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/ObjCStubs.kt index 994689d1642..7345ec3eaf3 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/ObjCStubs.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/ObjCStubs.kt @@ -85,7 +85,8 @@ class ObjCMethodStub(private val stubGenerator: StubGenerator, when (container) { is ObjCClass -> { // TODO: consider generating non-designated initializers as factories. - val designated = method.isDesginatedInitializer + val designated = method.isDesginatedInitializer || + stubGenerator.configuration.disableDesignatedInitializerChecks result.add("") result.add("@ObjCConstructor(${method.selector.quoteAsKotlinLiteral()}, $designated)") diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/InteropConfiguration.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/InteropConfiguration.kt index 9be9ed2a704..a2eae34b030 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/InteropConfiguration.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/InteropConfiguration.kt @@ -28,5 +28,6 @@ class InteropConfiguration( val strictEnums: Set, val nonStrictEnums: Set, val noStringConversion: Set, - val exportForwardDeclarations: List + val exportForwardDeclarations: List, + val disableDesignatedInitializerChecks: Boolean ) \ No newline at end of file diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/main.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/main.kt index e2f1287fdef..5dcad0933f8 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/main.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/main.kt @@ -276,7 +276,8 @@ private fun processCLib(args: Array): Array? { strictEnums = def.config.strictEnums.toSet(), nonStrictEnums = def.config.nonStrictEnums.toSet(), noStringConversion = def.config.noStringConversion.toSet(), - exportForwardDeclarations = def.config.exportForwardDeclarations + exportForwardDeclarations = def.config.exportForwardDeclarations, + disableDesignatedInitializerChecks = def.config.disableDesignatedInitializerChecks ) val nativeIndex = buildNativeIndex(library) diff --git a/OBJC_INTEROP.md b/OBJC_INTEROP.md index b09886ff09c..d408f2fdcd6 100644 --- a/OBJC_INTEROP.md +++ b/OBJC_INTEROP.md @@ -216,6 +216,12 @@ The overriding constructor must have the same parameter names and types as the o To override different methods with clashing Kotlin signatures, one can add `@Suppress("CONFLICTING_OVERLOADS")` annotation to the class. +By default Kotlin/Native compiler doesn't allow to call non-designated +Objective-C initializer as `super(...)` constructor. This behaviour can be +inconvenient if designated initializers aren't marked properly in the Objective-C +library. Adding `disableDesignatedInitializerChecks = true` to `.def` file for +this library would disable these compiler checks. + ## C features See [INTEROP.md](INTEROP.md) for the case when library uses some plain C features diff --git a/shared/src/main/kotlin/org/jetbrains/kotlin/konan/util/DefFile.kt b/shared/src/main/kotlin/org/jetbrains/kotlin/konan/util/DefFile.kt index 3d64e73ee72..f472da07331 100644 --- a/shared/src/main/kotlin/org/jetbrains/kotlin/konan/util/DefFile.kt +++ b/shared/src/main/kotlin/org/jetbrains/kotlin/konan/util/DefFile.kt @@ -99,6 +99,10 @@ class DefFile(val file:File?, val config:DefFileConfig, val manifestAddendProper val exportForwardDeclarations by lazy { properties.getSpaceSeparated("exportForwardDeclarations") } + + val disableDesignatedInitializerChecks by lazy { + properties.getProperty("disableDesignatedInitializerChecks")?.toBoolean() ?: false + } } }