From acd57beae787ec3bacfc9055307db03f5507ea54 Mon Sep 17 00:00:00 2001 From: Sergey Bogolepov Date: Wed, 31 Jul 2024 17:34:36 +0300 Subject: [PATCH] Upgrade to the most recent dev version: * Get rid of swift-export-experimental repo * Fix swift export API usages * Add a bit of tests on strings and chars --- build.gradle.kts | 1 - gradle.properties | 3 +- settings.gradle.kts | 1 + .../server/compiler/components/CliUtils.kt | 3 +- .../com/compiler/server/SwiftConverterTest.kt | 35 ++++++++++++------- swift-export-playground/build.gradle.kts | 29 ++++++++------- .../src/main/kotlin/PlaygroundSirSession.kt | 19 +++++++--- .../src/main/kotlin/Runner.kt | 16 ++++----- .../src/test/kotlin/Tests.kt | 33 +++++++++++++++-- 9 files changed, 94 insertions(+), 46 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 865e75582..4f0a81fd6 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -41,7 +41,6 @@ allprojects { maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap") maven("https://maven.pkg.jetbrains.space/kotlin/p/wasm/experimental") maven("https://maven.pkg.jetbrains.space/public/p/compose/dev") - maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/swift-export-experimental") } afterEvaluate { dependencies { diff --git a/gradle.properties b/gradle.properties index fa17d57f2..a4d843cb0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,7 +1,6 @@ -systemProp.kotlinVersion=2.0.0 +systemProp.kotlinVersion=2.1.0-dev-3023 systemProp.kotlinIdeVersion=1.9.20-506 systemProp.kotlinIdeVersionSuffix=IJ8109.175 -systemProp.swiftExportVersion=2.0.20-dev-3623 systemProp.policy=executor.policy systemProp.indexes=indexes.json systemProp.indexesJs=indexesJs.json diff --git a/settings.gradle.kts b/settings.gradle.kts index 3ab5e1358..47b657bcd 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -10,5 +10,6 @@ pluginManagement { gradlePluginPortal() mavenCentral() maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap") + maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev") } } \ No newline at end of file diff --git a/src/main/kotlin/com/compiler/server/compiler/components/CliUtils.kt b/src/main/kotlin/com/compiler/server/compiler/components/CliUtils.kt index fdd633b10..3e9d296c7 100644 --- a/src/main/kotlin/com/compiler/server/compiler/components/CliUtils.kt +++ b/src/main/kotlin/com/compiler/server/compiler/components/CliUtils.kt @@ -6,7 +6,6 @@ import com.compiler.server.model.ErrorDescriptor import com.compiler.server.model.ProjectSeveriry import com.compiler.server.model.TextInterval import org.jetbrains.kotlin.cli.common.CLICompiler -import org.jetbrains.kotlin.cli.common.CLITool import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.* import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSourceLocation @@ -56,7 +55,7 @@ fun CLICompiler<*>.tryCompilation(inputDirectory: Path, inputFiles: List Swift.Void { stub() } @@ -66,17 +65,15 @@ class SwiftConverterTest : BaseExecutorTest() { val myProperty: Int = 42 """.trimIndent(), expected = """ - public extension Playground.foo.bar { + @_exported import pkg + + public extension pkg.foo.bar { public static var myProperty: Swift.Int32 { get { stub() } } } - public enum foo { - public enum bar { - } - } """.trimIndent() ) @@ -96,4 +93,18 @@ class SwiftConverterTest : BaseExecutorTest() { } """.trimIndent() ) + + @Test + fun `unsupported type declaration`() = exactTest( + input = """ + interface Foo + + fun produceFoo(): Foo = TODO() + """.trimIndent(), + expected = """ + public func produceFoo() -> Swift.Never { + stub() + } + """.trimIndent() + ) } \ No newline at end of file diff --git a/swift-export-playground/build.gradle.kts b/swift-export-playground/build.gradle.kts index 134130e28..68dc171ba 100644 --- a/swift-export-playground/build.gradle.kts +++ b/swift-export-playground/build.gradle.kts @@ -6,30 +6,29 @@ repositories { mavenCentral() // For Analysis API components maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/kotlin-ide-plugin-dependencies") - maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/swift-export-experimental") + maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev") } val kotlinVersion = rootProject.properties["systemProp.kotlinVersion"] -val swiftExportVersion = rootProject.properties["systemProp.swiftExportVersion"] +val aaVersion = "2.1.0-dev-2515" dependencies { - implementation("org.jetbrains.kotlin:kotlin-compiler:$kotlinVersion") - // For K/N Distribution class - implementation("org.jetbrains.kotlin:kotlin-native-utils:$kotlinVersion") + implementation("org.jetbrains.kotlin:kotlin-compiler:$aaVersion") // Analysis API components which are required for the Swift export - implementation("org.jetbrains.kotlin:analysis-api-standalone-for-ide:$kotlinVersion") { isTransitive = false } - implementation("org.jetbrains.kotlin:high-level-api-for-ide:$kotlinVersion") { isTransitive = false } - implementation("org.jetbrains.kotlin:high-level-api-fir-for-ide:$kotlinVersion") { isTransitive = false } - implementation("org.jetbrains.kotlin:high-level-api-impl-base-for-ide:$kotlinVersion") { isTransitive = false } - implementation("org.jetbrains.kotlin:low-level-api-fir-for-ide:$kotlinVersion") { isTransitive = false } - implementation("org.jetbrains.kotlin:symbol-light-classes-for-ide:$kotlinVersion") { isTransitive = false } + implementation("org.jetbrains.kotlin:analysis-api-standalone-for-ide:$aaVersion") { isTransitive = false } + implementation("org.jetbrains.kotlin:high-level-api-for-ide:$aaVersion") { isTransitive = false } + implementation("org.jetbrains.kotlin:high-level-api-fir-for-ide:$aaVersion") { isTransitive = false } + implementation("org.jetbrains.kotlin:high-level-api-impl-base-for-ide:$aaVersion") { isTransitive = false } + implementation("org.jetbrains.kotlin:low-level-api-fir-for-ide:$aaVersion") { isTransitive = false } + implementation("org.jetbrains.kotlin:symbol-light-classes-for-ide:$aaVersion") { isTransitive = false } + implementation("org.jetbrains.kotlin:analysis-api-platform-interface-for-ide:$aaVersion") { isTransitive = false } // Swift export not-yet-published dependencies. - implementation("org.jetbrains.kotlin:sir:$swiftExportVersion") { isTransitive = false } - implementation("org.jetbrains.kotlin:sir-providers:$swiftExportVersion") { isTransitive = false } - implementation("org.jetbrains.kotlin:sir-light-classes:$swiftExportVersion") { isTransitive = false } - implementation("org.jetbrains.kotlin:sir-printer:$swiftExportVersion") { isTransitive = false } + implementation("org.jetbrains.kotlin:sir:$kotlinVersion") { isTransitive = false } + implementation("org.jetbrains.kotlin:sir-providers:$kotlinVersion") { isTransitive = false } + implementation("org.jetbrains.kotlin:sir-light-classes:$kotlinVersion") { isTransitive = false } + implementation("org.jetbrains.kotlin:sir-printer:$kotlinVersion") { isTransitive = false } testImplementation("junit:junit:4.13.2") testImplementation("org.jetbrains.kotlin:kotlin-test:$kotlinVersion") diff --git a/swift-export-playground/src/main/kotlin/PlaygroundSirSession.kt b/swift-export-playground/src/main/kotlin/PlaygroundSirSession.kt index cc603c349..d647dbfee 100644 --- a/swift-export-playground/src/main/kotlin/PlaygroundSirSession.kt +++ b/swift-export-playground/src/main/kotlin/PlaygroundSirSession.kt @@ -1,14 +1,21 @@ -import org.jetbrains.kotlin.analysis.project.structure.KtModule +import org.jetbrains.kotlin.analysis.api.projectStructure.KaModule +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.sir.SirModule + import org.jetbrains.kotlin.sir.providers.SirSession +import org.jetbrains.kotlin.sir.providers.SirTrampolineDeclarationsProvider import org.jetbrains.kotlin.sir.providers.SirTypeProvider import org.jetbrains.kotlin.sir.providers.impl.* +import org.jetbrains.kotlin.sir.providers.utils.UnsupportedDeclarationReporter import org.jetbrains.sir.lightclasses.SirDeclarationFromKtSymbolProvider internal class PlaygroundSirSession( - ktModule: KtModule, + ktModule: KaModule, + moduleForPackageEnums: SirModule, + unsupportedDeclarationReporter: UnsupportedDeclarationReporter, + targetPackageFqName: FqName?, ) : SirSession { override val declarationNamer = SirDeclarationNamerImpl() - override val enumGenerator = SirEnumGeneratorImpl() override val moduleProvider = SirSingleModuleProvider("Playground") override val declarationProvider = CachingSirDeclarationProvider( declarationsProvider = SirDeclarationFromKtSymbolProvider( @@ -16,16 +23,20 @@ internal class PlaygroundSirSession( sirSession = sirSession, ) ) + override val enumGenerator = SirEnumGeneratorImpl(moduleForPackageEnums) override val parentProvider = SirParentProviderImpl( sirSession = sirSession, + packageEnumGenerator = enumGenerator, ) override val typeProvider = SirTypeProviderImpl( errorTypeStrategy = SirTypeProvider.ErrorTypeStrategy.ErrorType, unsupportedTypeStrategy = SirTypeProvider.ErrorTypeStrategy.ErrorType, sirSession = sirSession, ) - override val visibilityChecker = SirVisibilityCheckerImpl() + override val visibilityChecker = SirVisibilityCheckerImpl(unsupportedDeclarationReporter) override val childrenProvider = SirDeclarationChildrenProviderImpl( sirSession = sirSession, ) + + override val trampolineDeclarationsProvider: SirTrampolineDeclarationsProvider = SirTrampolineDeclarationsProviderImpl(sirSession, targetPackageFqName) } \ No newline at end of file diff --git a/swift-export-playground/src/main/kotlin/Runner.kt b/swift-export-playground/src/main/kotlin/Runner.kt index 94cf70a07..353d746fb 100644 --- a/swift-export-playground/src/main/kotlin/Runner.kt +++ b/swift-export-playground/src/main/kotlin/Runner.kt @@ -1,7 +1,4 @@ -import org.jetbrains.kotlin.analysis.api.KtAnalysisApiInternals import org.jetbrains.kotlin.analysis.api.analyze -import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeTokenProvider -import org.jetbrains.kotlin.analysis.api.standalone.KtAlwaysAccessibleLifetimeTokenProvider import org.jetbrains.kotlin.analysis.api.standalone.buildStandaloneAnalysisAPISession import org.jetbrains.kotlin.analysis.project.structure.KtModule import org.jetbrains.kotlin.analysis.project.structure.builder.buildKtLibraryModule @@ -11,6 +8,8 @@ import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.sir.SirFunctionBody import org.jetbrains.kotlin.sir.SirModule import org.jetbrains.kotlin.sir.SirMutableDeclarationContainer +import org.jetbrains.kotlin.sir.builder.buildModule +import org.jetbrains.kotlin.sir.providers.utils.SimpleUnsupportedDeclarationReporter import org.jetbrains.kotlin.sir.util.addChild import org.jetbrains.sir.printer.SirAsSwiftSourcesPrinter import java.nio.file.Path @@ -26,11 +25,15 @@ fun runSwiftExport( val (ktModule, sources) = collectModuleAndSources(sourceFile, "Playground", stdlibPath) return analyze(ktModule) { - val sirSession = PlaygroundSirSession(ktModule) + val pkgModule = buildModule { + name = "pkg" + } + val unsupportedDeclarationReporter = SimpleUnsupportedDeclarationReporter() + val sirSession = PlaygroundSirSession(ktModule, pkgModule, unsupportedDeclarationReporter, targetPackageFqName = null) val sirModule: SirModule = with(sirSession) { ktModule.sirModule().also { sources.flatMap { file -> - file.getFileSymbol().getFileScope().extractDeclarations(analysisSession) + file.symbol.fileScope.extractDeclarations(useSiteSession) }.forEach { topLevelDeclaration -> val parent = topLevelDeclaration.parent as? SirMutableDeclarationContainer ?: error("top level declaration can contain only module or extension to package as a parent") @@ -49,15 +52,12 @@ fun runSwiftExport( } } -@OptIn(KtAnalysisApiInternals::class) private fun collectModuleAndSources( sourceRoot: Path, kotlinModuleName: String, stdlibPath: Path?, ): Pair> { val analysisAPISession = buildStandaloneAnalysisAPISession { - registerProjectService(KtLifetimeTokenProvider::class.java, KtAlwaysAccessibleLifetimeTokenProvider()) - buildKtModuleProvider { platform = NativePlatforms.unspecifiedNativePlatform diff --git a/swift-export-playground/src/test/kotlin/Tests.kt b/swift-export-playground/src/test/kotlin/Tests.kt index ccd6e9215..cd5cc4bb7 100644 --- a/swift-export-playground/src/test/kotlin/Tests.kt +++ b/swift-export-playground/src/test/kotlin/Tests.kt @@ -39,11 +39,16 @@ class SwiftExportTests { """.trimIndent(), """ import KotlinRuntime - + public class A : KotlinRuntime.KotlinBase { public override init() { stub() } + public override init( + __externalRCRef: Swift.UInt + ) { + stub() + } } """.trimIndent() ) @@ -55,7 +60,7 @@ class SwiftExportTests { """.trimIndent(), """ import KotlinRuntime - + public class O : KotlinRuntime.KotlinBase { public static var shared: Playground.O { get { @@ -65,6 +70,11 @@ class SwiftExportTests { private override init() { stub() } + public override init( + __externalRCRef: Swift.UInt + ) { + stub() + } } """.trimIndent() ) @@ -78,4 +88,23 @@ class SwiftExportTests { public typealias MyInt = Swift.Int32 """.trimIndent() ) + + @Test + fun `strings and chars`() = testSources( + """ + fun produceString(): String = "hello" + + fun firstChar(str: String): Char = str.first() + """.trimIndent(), + """ + public func firstChar( + str: Swift.String + ) -> Swift.Unicode.UTF16.CodeUnit { + stub() + } + public func produceString() -> Swift.String { + stub() + } + """.trimIndent() + ) } \ No newline at end of file