From 7bd35a87a9b26bd0579cbeba04ee90997bddf4f4 Mon Sep 17 00:00:00 2001 From: Zac Sweers Date: Wed, 22 May 2024 16:11:40 -0400 Subject: [PATCH] Update to K2 (#510) * Updates * Update KCT * Cleanups * Update CI * Add .kotlin to .gitignore * Misc opt-ins and beta3 fixes * Update KCT to alpha02 * Update KCT + add KSP2 testing * Add exclusions * Spotless * Oops one too many * concurrency * Remove JDK from matrix * Typo * Spotless * Enable CC * Enable caching and COD * Fixes * Disable CC in publishing * Mark validateProguardRules as not CC compatible * Beta4 * Update KSP2 and KCT bits * Update to beta5 * kct 0.5.0-alpha06 * Try raising * Use correct rename * Update CI * KSP 1.0.20 * RC1 * Migrate to kotlin-metadata * RC2 * KSP RC2 * RC3 * KSP RC3 * 2.0.0 final * KSP final + lint alpha --- .github/workflows/ci.yml | 27 ++- .gitignore | 1 + build.gradle.kts | 21 +- gradle.properties | 4 +- gradle/libs.versions.toml | 15 +- .../moshi-compiler-plugin/lint-baseline.xml | 217 ++++++++---------- .../moshix/ir/compiler/BaseSymbols.kt | 10 +- .../ir/compiler/MoshiIrGenerationExtension.kt | 2 + .../moshix/ir/compiler/MoshiIrUtil.kt | 3 + .../moshix/ir/compiler/MoshiSymbols.kt | 3 + .../moshix/ir/compiler/TargetTypes.kt | 3 + .../moshix/ir/compiler/api/DelegateKey.kt | 4 + .../ir/compiler/api/MoshiAdapterGenerator.kt | 10 +- .../moshix/ir/compiler/api/MoshiOrigin.kt | 4 +- .../ir/compiler/sealed/MoshiSealedOrigin.kt | 4 +- .../ir/compiler/sealed/MoshiSealedSymbols.kt | 2 + .../compiler/sealed/SealedAdapterGenerator.kt | 6 + .../ir/compiler/util/IrSourcePrinter.kt | 5 +- .../zacsweers/moshix/ir/compiler/util/ir.kt | 6 + .../moshix/ir/compiler/MoshiIrVisitorTest.kt | 1 - moshi-ir/moshi-kotlin-tests/build.gradle.kts | 1 + .../moshix/reflect/JvmDescriptors.kt | 2 +- .../dev/zacsweers/moshix/reflect/KtTypes.kt | 28 +-- .../MetadataKotlinJsonAdapterFactory.kt | 45 ++-- .../moshix/reflect/JvmDescriptorsTest.kt | 2 +- moshi-proguard-rule-gen/build.gradle.kts | 16 +- .../MoshiProguardGenSymbolProcessorTest.kt | 72 ++++-- moshi-sealed/codegen/build.gradle.kts | 22 +- .../ksp/MoshiSealedSymbolProcessorTest.kt | 23 +- .../MetadataMoshiSealedJsonAdapterFactory.kt | 16 +- publish.sh | 4 +- 31 files changed, 316 insertions(+), 263 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7c4b625b..3d3c1fa1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,23 +1,30 @@ name: CI -on: [ push, pull_request ] +on: + # Only run push on main + push: + branches: + - main + paths-ignore: + - '**/*.md' + # Always run on PRs + pull_request: + branches: [ main ] + +concurrency: + group: 'ci-${{ github.event.merge_group.head_ref || github.head_ref }}-${{ github.workflow }}' + cancel-in-progress: true jobs: build: - name: 'JDK ${{ matrix.java }} - KSP ${{ matrix.ksp_enabled }} - Kotlin ${{ matrix.kotlin }} - K2 ${{ matrix.k2_enabled }}' + name: 'KSP ${{ matrix.ksp_enabled }} / K2 ${{ matrix.k2_enabled }}' runs-on: ubuntu-latest strategy: matrix: - java: [ '21' ] - kotlin: [ '1.9.24' ] ksp_enabled: [ true, false ] k2_enabled: [ true, false ] fail-fast: false - env: - DEP_OVERRIDE_kotlin: ${{ matrix.kotlin }} - DEP_OVERRIDE_java: ${{ matrix.java }} - steps: - name: Checkout uses: actions/checkout@v4 @@ -29,10 +36,10 @@ jobs: uses: actions/setup-java@v4 with: distribution: 'zulu' - java-version: ${{ matrix.java }} + java-version: '21' - name: Build project - run: ./gradlew build check -Pmoshix.useKsp=${{ matrix.ksp_enabled }} -Pkotlin.experimental.tryK2=${{ matrix.k2_enabled }} --stacktrace + run: ./gradlew build check -Pmoshix.useKsp=${{ matrix.ksp_enabled }} -Pkotlin.experimental.tryK2=${{ matrix.k2_enabled }} --quiet publish-snapshot: needs: 'build' diff --git a/.gitignore b/.gitignore index fc1317f4..5a917282 100644 --- a/.gitignore +++ b/.gitignore @@ -44,6 +44,7 @@ gen/ .gradle/ .gradletasknamecache build/ +.kotlin/ # Local configuration file (sdk path, etc) local.properties diff --git a/build.gradle.kts b/build.gradle.kts index 65e00c16..7a81c693 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,6 +14,7 @@ * limitations under the License. */ import com.android.build.api.dsl.Lint +import com.google.devtools.ksp.gradle.KspTask import com.vanniktech.maven.publish.MavenPublishBaseExtension import java.net.URI import org.jetbrains.dokka.gradle.DokkaTask @@ -85,15 +86,19 @@ subprojects { project.tasks.withType().configureEach { options.release.set(jvmTargetProvider) } } pluginManager.withPlugin("org.jetbrains.kotlin.jvm") { - tasks.withType().configureEach { - compilerOptions { - jvmTarget.set(libs.versions.jvmTarget.map(JvmTarget::fromTarget)) - freeCompilerArgs.addAll("-Xjsr305=strict", "-progressive") - // TODO disabled because Gradle's Kotlin handling is silly - // https://github.com/gradle/gradle/issues/16779 - // allWarningsAsErrors = true + tasks + .withType() + .matching { it !is KspTask } + .configureEach { + compilerOptions { + jvmTarget.set(libs.versions.jvmTarget.map(JvmTarget::fromTarget)) + freeCompilerArgs.addAll("-Xjsr305=strict") + progressiveMode.set(true) + // TODO disabled because Gradle's Kotlin handling is silly + // https://github.com/gradle/gradle/issues/16779 + // allWarningsAsErrors = true + } } - } if ( project.name != "sample" && !project.path.contains("sample") && !project.path.contains("test") ) { diff --git a/gradle.properties b/gradle.properties index a870f25e..b3838ca2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -32,7 +32,9 @@ POM_DEVELOPER_NAME=Zac Sweers SONATYPE_STAGING_PROFILE=dev.zacsweers RELEASE_SIGNING_ENABLED=true -org.gradle.configureondemand=false +org.gradle.caching=true +org.gradle.configureondemand=true +org.gradle.configuration-cache=true kapt.include.compile.classpath=false diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 744a5acb..64b71e86 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -19,10 +19,10 @@ gjf = "1.17.0" incap = "1.0.0" jdk = "21" jvmTarget = "11" -kotlin = "1.9.24" -kotlinCompileTesting = "0.4.1" +kotlin = "2.0.0" +kotlinCompileTesting = "0.5.0-alpha07" kotlinpoet = "1.16.0" -ksp = "1.9.24-1.0.20" +ksp = "2.0.0-1.0.21" ktfmt = "0.49" moshi = "1.15.1" okhttp = "4.12.0" @@ -33,7 +33,7 @@ dokka = { id = "org.jetbrains.dokka", version = "1.9.20" } kotlinBinaryCompatibilityValidator = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version = "0.14.0" } kotlinJvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" } -lint = { id = "com.android.lint", version = "8.5.0-beta01" } +lint = { id = "com.android.lint", version = "8.6.0-alpha02" } mavenPublish = { id = "com.vanniktech.maven.publish", version = "0.28.0" } mavenShadow = { id = "com.github.johnrengelman.shadow", version = "8.1.1" } moshix = { id = "dev.zacsweers.moshix", version = "0.26.0" } # Always replaced by the local plugin @@ -56,7 +56,7 @@ incap = { module = "net.ltgt.gradle.incap:incap", version.ref = "incap" } incap-processor = { module = "net.ltgt.gradle.incap:incap-processor", version.ref = "incap" } kotlin-compilerEmbeddable = { module = "org.jetbrains.kotlin:kotlin-compiler-embeddable", version.ref = "kotlin" } -kotlin-metadata = { module = "org.jetbrains.kotlinx:kotlinx-metadata-jvm", version = "0.9.0" } +kotlin-metadata = { module = "org.jetbrains.kotlin:kotlin-metadata-jvm", version.ref = "kotlin" } kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" } kotlin-gradlePlugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" } kotlin-gradlePlugin-api = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin-api", version.ref = "kotlin" } @@ -68,8 +68,11 @@ kotlinpoet-metadata = { module = "com.squareup:kotlinpoet-metadata", version.ref kotlinpoet-ksp = { module = "com.squareup:kotlinpoet-ksp", version.ref = "kotlinpoet" } ksp = { module = "com.google.devtools.ksp:symbol-processing", version.ref = "ksp" } -ksp-api = { module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "ksp" } ksp-gradlePlugin = { module = "com.google.devtools.ksp:symbol-processing-gradle-plugin", version.ref = "ksp" } +ksp-api = { module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "ksp" } +ksp-cli = { module = "com.google.devtools.ksp:symbol-processing-cmdline", version.ref = "ksp" } +ksp-aa-embeddable = { module = "com.google.devtools.ksp:symbol-processing-aa-embeddable", version.ref = "ksp" } +ksp-commonDeps = { module = "com.google.devtools.ksp:symbol-processing-common-deps", version.ref = "ksp" } # Only present to trigger automatic renovate updates ktfmt = { module = "com.facebook:ktfmt", version.ref = "ktfmt" } diff --git a/moshi-ir/moshi-compiler-plugin/lint-baseline.xml b/moshi-ir/moshi-compiler-plugin/lint-baseline.xml index 21468ef3..c9a512d0 100644 --- a/moshi-ir/moshi-compiler-plugin/lint-baseline.xml +++ b/moshi-ir/moshi-compiler-plugin/lint-baseline.xml @@ -1,5 +1,5 @@ - + + errorLine1=" createEmptyExternalPackageFragment(moduleFragment.descriptor, FqName(packageName))" + errorLine2=" ~~~~~~~~~~"> + line="142" + column="55"/> @@ -85,7 +85,7 @@ errorLine2=" ~~~~~~~~"> @@ -96,7 +96,7 @@ errorLine2=" ~~~~~~~~"> @@ -107,7 +107,7 @@ errorLine2=" ~~~~~~~~"> @@ -118,7 +118,7 @@ errorLine2=" ~~~~~~~~"> @@ -129,7 +129,7 @@ errorLine2=" ~~~~~~~~"> @@ -140,7 +140,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~"> @@ -151,7 +151,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -162,7 +162,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~"> @@ -173,7 +173,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -184,7 +184,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -195,21 +195,10 @@ errorLine2=" ~~~~~~~~~~~~~~~~"> - - - - @@ -228,21 +217,10 @@ errorLine2=" ~~~~"> - - - - @@ -261,7 +239,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -272,7 +250,7 @@ errorLine2=" ~~~~"> @@ -283,7 +261,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -294,7 +272,7 @@ errorLine2=" ~~~~"> @@ -305,7 +283,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~"> @@ -316,7 +294,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -327,7 +305,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~"> @@ -338,7 +316,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -349,7 +327,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -360,7 +338,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~"> @@ -371,7 +349,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -382,7 +360,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~"> @@ -393,7 +371,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -404,7 +382,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -415,21 +393,10 @@ errorLine2=" ~~~~~~~~~~~~~~~~"> - - - - @@ -448,7 +415,7 @@ errorLine2=" ~~~~"> @@ -459,7 +426,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -470,7 +437,7 @@ errorLine2=" ~~~~"> @@ -481,7 +448,7 @@ errorLine2=" ~~~~~~~~"> @@ -492,7 +459,7 @@ errorLine2=" ~~~~~~~~"> @@ -503,7 +470,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -514,7 +481,7 @@ errorLine2=" ~~~~~~~~~"> @@ -525,7 +492,7 @@ errorLine2=" ~~~~~~~~"> @@ -536,7 +503,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~"> @@ -547,7 +514,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~"> @@ -558,7 +525,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -569,7 +536,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~"> @@ -580,7 +547,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -591,7 +558,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~"> @@ -602,7 +569,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -613,7 +580,7 @@ errorLine2=" ~~~~~~~~~"> @@ -624,7 +591,7 @@ errorLine2=" ~~~~~~~~"> @@ -635,7 +602,7 @@ errorLine2=" ~~~~~~~~"> @@ -646,7 +613,7 @@ errorLine2=" ~~~~~~~~~"> @@ -657,7 +624,7 @@ errorLine2=" ~~~~~~~~~"> @@ -668,7 +635,7 @@ errorLine2=" ~~~~~~~~~"> @@ -679,7 +646,7 @@ errorLine2=" ~~~~~~~~~"> @@ -690,7 +657,7 @@ errorLine2=" ~~~~~~~~~"> @@ -701,7 +668,7 @@ errorLine2=" ~~~~~~~~~"> @@ -712,7 +679,7 @@ errorLine2=" ~~~~~~~~~"> @@ -723,7 +690,7 @@ errorLine2=" ~~~~~~~~~"> @@ -734,7 +701,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -745,7 +712,7 @@ errorLine2=" ~~~~"> @@ -756,7 +723,7 @@ errorLine2=" ~~~~~~~~~"> @@ -767,7 +734,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -778,7 +745,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~"> @@ -789,7 +756,7 @@ errorLine2=" ~~~~"> @@ -877,7 +844,7 @@ errorLine2=" ~~~~"> @@ -888,7 +855,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~"> @@ -899,7 +866,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -910,7 +877,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~"> @@ -921,7 +888,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -932,7 +899,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~"> @@ -943,7 +910,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -954,7 +921,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~"> @@ -965,7 +932,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -976,7 +943,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -987,7 +954,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~"> @@ -998,7 +965,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -1009,7 +976,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -1020,7 +987,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~"> @@ -1031,7 +998,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -1042,7 +1009,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~"> @@ -1053,7 +1020,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -1064,7 +1031,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~"> @@ -1075,7 +1042,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -1086,7 +1053,7 @@ errorLine2=" ~~~~~~~~~~"> @@ -1097,7 +1064,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~"> @@ -1108,7 +1075,7 @@ errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~"> @@ -1119,7 +1086,7 @@ errorLine2=" ~~~~"> diff --git a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/BaseSymbols.kt b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/BaseSymbols.kt index e942e5c7..bcd8707e 100644 --- a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/BaseSymbols.kt +++ b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/BaseSymbols.kt @@ -1,3 +1,5 @@ +@file:OptIn(UnsafeDuringIrConstructionAPI::class) + package dev.zacsweers.moshix.ir.compiler import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext @@ -17,12 +19,13 @@ import org.jetbrains.kotlin.ir.declarations.IrModuleFragment import org.jetbrains.kotlin.ir.declarations.IrPackageFragment import org.jetbrains.kotlin.ir.declarations.IrProperty import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction -import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl +import org.jetbrains.kotlin.ir.declarations.createEmptyExternalPackageFragment import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.expressions.impl.IrClassReferenceImpl import org.jetbrains.kotlin.ir.symbols.IrClassSymbol import org.jetbrains.kotlin.ir.symbols.IrPropertySymbol import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol +import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.classFqName import org.jetbrains.kotlin.ir.types.classifierOrNull @@ -136,10 +139,7 @@ internal open class BaseSymbols( .symbol protected fun createPackage(packageName: String): IrPackageFragment = - IrExternalPackageFragmentImpl.createEmptyExternalPackageFragment( - moduleFragment.descriptor, - FqName(packageName), - ) + createEmptyExternalPackageFragment(moduleFragment.descriptor, FqName(packageName)) protected fun createClass( irParent: IrDeclarationParent, diff --git a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/MoshiIrGenerationExtension.kt b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/MoshiIrGenerationExtension.kt index f605ad9a..8079b928 100644 --- a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/MoshiIrGenerationExtension.kt +++ b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/MoshiIrGenerationExtension.kt @@ -20,6 +20,7 @@ import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext import org.jetbrains.kotlin.cli.common.messages.MessageCollector import org.jetbrains.kotlin.ir.declarations.IrModuleFragment +import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI import org.jetbrains.kotlin.name.ClassId internal class MoshiIrGenerationExtension( @@ -29,6 +30,7 @@ internal class MoshiIrGenerationExtension( private val debug: Boolean, ) : IrGenerationExtension { + @OptIn(UnsafeDuringIrConstructionAPI::class) override fun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext) { val generatedAnnotation = generatedAnnotationName?.let { name -> diff --git a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/MoshiIrUtil.kt b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/MoshiIrUtil.kt index 198059f8..57b9fe2f 100644 --- a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/MoshiIrUtil.kt +++ b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/MoshiIrUtil.kt @@ -29,6 +29,7 @@ import org.jetbrains.kotlin.ir.declarations.IrProperty import org.jetbrains.kotlin.ir.expressions.IrConst import org.jetbrains.kotlin.ir.expressions.IrConstructorCall import org.jetbrains.kotlin.ir.expressions.IrGetEnumValue +import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI import org.jetbrains.kotlin.ir.types.classOrNull import org.jetbrains.kotlin.ir.util.file import org.jetbrains.kotlin.ir.util.getAnnotation @@ -38,6 +39,7 @@ import org.jetbrains.kotlin.name.FqName internal val JSON_ANNOTATION = FqName("com.squareup.moshi.Json") internal val JSON_QUALIFIER_ANNOTATION = FqName("com.squareup.moshi.JsonQualifier") +@OptIn(UnsafeDuringIrConstructionAPI::class) internal fun IrAnnotationContainer?.jsonQualifiers(): Set { if (this == null) return emptySet() return annotations.filterTo(LinkedHashSet()) { @@ -81,6 +83,7 @@ private val TargetProperty.isVisible: Boolean * Returns a generator for this property, or null if either there is an error and this property * cannot be used with code gen, or if no codegen is necessary for this property. */ +@OptIn(UnsafeDuringIrConstructionAPI::class) internal fun TargetProperty.generator( originalType: IrClass, errors: MutableList<(logger: MessageCollector) -> Unit>, diff --git a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/MoshiSymbols.kt b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/MoshiSymbols.kt index af5c0170..5cb7b900 100644 --- a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/MoshiSymbols.kt +++ b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/MoshiSymbols.kt @@ -13,6 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +@file:OptIn(UnsafeDuringIrConstructionAPI::class) + package dev.zacsweers.moshix.ir.compiler import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext @@ -28,6 +30,7 @@ import org.jetbrains.kotlin.ir.declarations.IrModuleFragment import org.jetbrains.kotlin.ir.declarations.IrPackageFragment import org.jetbrains.kotlin.ir.symbols.IrClassSymbol import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol +import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI import org.jetbrains.kotlin.ir.types.defaultType import org.jetbrains.kotlin.ir.types.makeNotNull import org.jetbrains.kotlin.ir.types.makeNullable diff --git a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/TargetTypes.kt b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/TargetTypes.kt index b19898d6..3b9588e4 100644 --- a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/TargetTypes.kt +++ b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/TargetTypes.kt @@ -29,6 +29,7 @@ import org.jetbrains.kotlin.descriptors.DescriptorVisibilities import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.ir.declarations.IrClass import org.jetbrains.kotlin.ir.declarations.IrDeclaration +import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI import org.jetbrains.kotlin.ir.util.defaultType import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable import org.jetbrains.kotlin.ir.util.hasDefaultValue @@ -163,6 +164,7 @@ internal fun targetType( ) } +@OptIn(UnsafeDuringIrConstructionAPI::class) internal fun primaryConstructor(targetType: IrClass): TargetConstructor? { val primaryConstructor = targetType.primaryConstructor ?: return null @@ -185,6 +187,7 @@ internal fun primaryConstructor(targetType: IrClass): TargetConstructor? { return TargetConstructor(primaryConstructor, parameters, primaryConstructor.visibility) } +@OptIn(UnsafeDuringIrConstructionAPI::class) private fun declaredProperties( constructor: TargetConstructor, classDecl: IrClass, diff --git a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/api/DelegateKey.kt b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/api/DelegateKey.kt index 3b032102..008768a2 100644 --- a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/api/DelegateKey.kt +++ b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/api/DelegateKey.kt @@ -41,6 +41,7 @@ import org.jetbrains.kotlin.ir.expressions.IrExpressionBody import org.jetbrains.kotlin.ir.symbols.IrClassSymbol import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol +import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI import org.jetbrains.kotlin.ir.types.IrSimpleType import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.IrTypeProjection @@ -66,6 +67,7 @@ internal data class DelegateKey( get() = delegateType.isNullable() /** Returns an adapter to use when encoding and decoding this property. */ + @OptIn(UnsafeDuringIrConstructionAPI::class) internal fun generateProperty( pluginContext: IrPluginContext, moshiSymbols: MoshiSymbols, @@ -203,6 +205,7 @@ private fun IrBuilderWithScope.addAnnotationsParam( private fun List.toVariableNames() = joinToString("") { it.toVariableName() } /** Returns a suggested variable name derived from a type name, like nullableListOfString. */ +@OptIn(UnsafeDuringIrConstructionAPI::class) private fun IrType.toVariableName(): String { val base = when (val classifier = classifierOrNull) { @@ -234,6 +237,7 @@ private fun IrType.toVariableName(): String { * are too late to properly use it hear, and also avoids some possible conflicts with * kotlin-reflect. */ +@OptIn(UnsafeDuringIrConstructionAPI::class) private fun IrBuilderWithScope.renderType( moshiSymbols: MoshiSymbols, delegateType: IrType, diff --git a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/api/MoshiAdapterGenerator.kt b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/api/MoshiAdapterGenerator.kt index 82789614..00ff2ee1 100644 --- a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/api/MoshiAdapterGenerator.kt +++ b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/api/MoshiAdapterGenerator.kt @@ -80,11 +80,11 @@ import org.jetbrains.kotlin.ir.declarations.IrField import org.jetbrains.kotlin.ir.declarations.IrFunction import org.jetbrains.kotlin.ir.declarations.IrValueParameter import org.jetbrains.kotlin.ir.declarations.IrVariable -import org.jetbrains.kotlin.ir.deepCopyWithVariables import org.jetbrains.kotlin.ir.expressions.IrDelegatingConstructorCall import org.jetbrains.kotlin.ir.expressions.addArgument import org.jetbrains.kotlin.ir.expressions.impl.IrDelegatingConstructorCallImpl import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol +import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.IrTypeArgument import org.jetbrains.kotlin.ir.types.classifierOrFail @@ -93,6 +93,7 @@ import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl import org.jetbrains.kotlin.ir.types.makeNotNull import org.jetbrains.kotlin.ir.types.typeWith import org.jetbrains.kotlin.ir.util.constructors +import org.jetbrains.kotlin.ir.util.deepCopyWithoutPatchingParents import org.jetbrains.kotlin.ir.util.defaultType import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable import org.jetbrains.kotlin.ir.util.getPropertyGetter @@ -306,6 +307,7 @@ internal class MoshiAdapterGenerator( } } + @OptIn(UnsafeDuringIrConstructionAPI::class) private fun IrClass.generateToJsonFun(adapterProperties: Map): IrFunction { return addOverride( FqName("com.squareup.moshi.JsonAdapter"), @@ -384,6 +386,7 @@ internal class MoshiAdapterGenerator( } } + @OptIn(UnsafeDuringIrConstructionAPI::class) private fun IrClass.generateFromJsonFun( adapterProperties: Map, optionsField: IrField, @@ -533,7 +536,7 @@ internal class MoshiAdapterGenerator( // one to compile against val defaultsConstructor = target.constructor.irConstructor - .deepCopyWithVariables() + .deepCopyWithoutPatchingParents() .apply { parent = target.irClass repeat(maskCount) { @@ -639,6 +642,7 @@ internal class MoshiAdapterGenerator( } } + @OptIn(UnsafeDuringIrConstructionAPI::class) private fun IrBuilderWithScope.buildWhileHasNextLoop( fieldsHolder: IrValueParameter, optionsField: IrField, @@ -801,6 +805,7 @@ internal class MoshiAdapterGenerator( // TODO currently this creates an exception and just steals its message, // would be nice if we had a separate utility? Not a big deal regardless // since we're gonna error at the end anyway + @OptIn(UnsafeDuringIrConstructionAPI::class) private fun IrBuilderWithScope.addError( errors: IrVariable, property: PropertyGenerator, @@ -872,6 +877,7 @@ internal class MoshiAdapterGenerator( } } + @OptIn(UnsafeDuringIrConstructionAPI::class) private fun IrBuilderWithScope.generateJsonAdapterSuperConstructorCall(): IrDelegatingConstructorCall { return IrDelegatingConstructorCallImpl.fromSymbolOwner( diff --git a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/api/MoshiOrigin.kt b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/api/MoshiOrigin.kt index c5f7750f..49daddbc 100644 --- a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/api/MoshiOrigin.kt +++ b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/api/MoshiOrigin.kt @@ -15,6 +15,8 @@ */ package dev.zacsweers.moshix.ir.compiler.api +import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin import org.jetbrains.kotlin.ir.declarations.IrDeclarationOriginImpl -internal object MoshiOrigin : IrDeclarationOriginImpl("GENERATED_MOSHI_ADAPTER_CLASS") +internal val MoshiOrigin: IrDeclarationOrigin = + IrDeclarationOriginImpl("GENERATED_MOSHI_ADAPTER_CLASS") diff --git a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/sealed/MoshiSealedOrigin.kt b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/sealed/MoshiSealedOrigin.kt index f4864294..b1ca4185 100644 --- a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/sealed/MoshiSealedOrigin.kt +++ b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/sealed/MoshiSealedOrigin.kt @@ -15,6 +15,8 @@ */ package dev.zacsweers.moshix.ir.compiler.sealed +import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin import org.jetbrains.kotlin.ir.declarations.IrDeclarationOriginImpl -internal object MoshiSealedOrigin : IrDeclarationOriginImpl("GENERATED_MOSHI_SEALED_ADAPTER_CLASS") +internal val MoshiSealedOrigin: IrDeclarationOrigin = + IrDeclarationOriginImpl("GENERATED_MOSHI_SEALED_ADAPTER_CLASS") diff --git a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/sealed/MoshiSealedSymbols.kt b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/sealed/MoshiSealedSymbols.kt index 69fb38cd..a1a128c0 100644 --- a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/sealed/MoshiSealedSymbols.kt +++ b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/sealed/MoshiSealedSymbols.kt @@ -23,6 +23,7 @@ import org.jetbrains.kotlin.ir.builders.declarations.addFunction import org.jetbrains.kotlin.ir.builders.declarations.addTypeParameter import org.jetbrains.kotlin.ir.builders.declarations.addValueParameter import org.jetbrains.kotlin.ir.declarations.IrPackageFragment +import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI import org.jetbrains.kotlin.ir.types.defaultType import org.jetbrains.kotlin.ir.types.makeNullable import org.jetbrains.kotlin.ir.types.typeWith @@ -34,6 +35,7 @@ import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.types.Variance +@OptIn(UnsafeDuringIrConstructionAPI::class) internal class MoshiSealedSymbols(private val moshiSymbols: MoshiSymbols) : BaseSymbols(moshiSymbols) { diff --git a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/sealed/SealedAdapterGenerator.kt b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/sealed/SealedAdapterGenerator.kt index adc3d02b..4657ef1f 100644 --- a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/sealed/SealedAdapterGenerator.kt +++ b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/sealed/SealedAdapterGenerator.kt @@ -55,6 +55,7 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrDelegatingConstructorCallImpl import org.jetbrains.kotlin.ir.interpreter.hasAnnotation import org.jetbrains.kotlin.ir.symbols.IrClassSymbol import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol +import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.classifierOrFail import org.jetbrains.kotlin.ir.types.createType @@ -84,6 +85,7 @@ private constructor( private val target: IrClass, private val labelKey: String, ) : AdapterGenerator { + @OptIn(UnsafeDuringIrConstructionAPI::class) override fun prepare(): PreparedAdapter? { // If this is a nested sealed type of a moshi-sealed parent, defer to the parent val sealedParent = @@ -222,6 +224,7 @@ private constructor( } } + @OptIn(UnsafeDuringIrConstructionAPI::class) private fun walkTypeLabels( rootType: IrClass, subtype: IrClass, @@ -512,6 +515,7 @@ private constructor( return PreparedAdapter(adapterCls) } + @OptIn(UnsafeDuringIrConstructionAPI::class) private fun IrClass.generateToJsonFun(delegateField: IrField): IrFunction { return addOverride( FqName("com.squareup.moshi.JsonAdapter"), @@ -544,6 +548,7 @@ private constructor( } } + @OptIn(UnsafeDuringIrConstructionAPI::class) private fun IrClass.generateFromJsonFun(delegateField: IrField): IrFunction { return addOverride( FqName("com.squareup.moshi.JsonAdapter"), @@ -594,6 +599,7 @@ private constructor( return ctor } + @OptIn(UnsafeDuringIrConstructionAPI::class) private fun IrBuilderWithScope.generateJsonAdapterSuperConstructorCall(): IrDelegatingConstructorCall { return IrDelegatingConstructorCallImpl.fromSymbolOwner( diff --git a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/util/IrSourcePrinter.kt b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/util/IrSourcePrinter.kt index fdd42edb..ef65a499 100644 --- a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/util/IrSourcePrinter.kt +++ b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/util/IrSourcePrinter.kt @@ -13,6 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +@file:OptIn(UnsafeDuringIrConstructionAPI::class) + package dev.zacsweers.moshix.ir.compiler.util import java.util.Locale @@ -87,6 +89,7 @@ import org.jetbrains.kotlin.ir.expressions.IrWhen import org.jetbrains.kotlin.ir.expressions.IrWhileLoop import org.jetbrains.kotlin.ir.expressions.impl.IrIfThenElseImpl import org.jetbrains.kotlin.ir.symbols.IrTypeAliasSymbol +import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI import org.jetbrains.kotlin.ir.types.IrDynamicType import org.jetbrains.kotlin.ir.types.IrErrorType import org.jetbrains.kotlin.ir.types.IrSimpleType @@ -450,7 +453,7 @@ internal class IrSourcePrinterVisitor(out: Appendable, indentUnit: String = " " print(".") } - val prop = (function as? IrSimpleFunction)?.correspondingPropertySymbol?.owner + val prop = function.correspondingPropertySymbol?.owner if (prop != null) { val propName = prop.name.asString() diff --git a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/util/ir.kt b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/util/ir.kt index cffc1466..3f16a695 100644 --- a/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/util/ir.kt +++ b/moshi-ir/moshi-compiler-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/compiler/util/ir.kt @@ -59,6 +59,7 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrInstanceInitializerCallImpl import org.jetbrains.kotlin.ir.symbols.IrClassSymbol import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol import org.jetbrains.kotlin.ir.symbols.IrSymbol +import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl import org.jetbrains.kotlin.ir.types.IrSimpleType import org.jetbrains.kotlin.ir.types.IrType @@ -158,6 +159,7 @@ internal fun IrSimpleFunction.overridesFunctionIn(fqName: FqName): Boolean = parentClassOrNull?.fqNameWhenAvailable == fqName || allOverridden().any { it.parentClassOrNull?.fqNameWhenAvailable == fqName } +@OptIn(UnsafeDuringIrConstructionAPI::class) internal fun IrPluginContext.createIrBuilder(symbol: IrSymbol): DeclarationIrBuilder { return DeclarationIrBuilder(this, symbol, symbol.owner.startOffset, symbol.owner.endOffset) } @@ -212,6 +214,7 @@ internal fun IrType.rawType(): IrClass { } /** Returns the raw [IrClass] of this [IrType] or null. */ +@OptIn(UnsafeDuringIrConstructionAPI::class) internal fun IrType.rawTypeOrNull(): IrClass? { return when (val classifier = classifierOrNull) { is IrClassSymbol -> classifier.owner @@ -219,6 +222,7 @@ internal fun IrType.rawTypeOrNull(): IrClass? { } } +@OptIn(UnsafeDuringIrConstructionAPI::class) internal fun IrClass.addOverride( baseFqName: FqName, name: String, @@ -246,6 +250,7 @@ internal fun IrClass.addOverride( } } +@OptIn(UnsafeDuringIrConstructionAPI::class) internal fun IrBuilderWithScope.irBinOp( pluginContext: IrPluginContext, name: Name, @@ -258,6 +263,7 @@ internal fun IrBuilderWithScope.irBinOp( return irInvoke(lhs, symbol, rhs) } +@OptIn(UnsafeDuringIrConstructionAPI::class) internal fun IrBuilderWithScope.irInvoke( dispatchReceiver: IrExpression? = null, callee: IrFunctionSymbol, diff --git a/moshi-ir/moshi-compiler-plugin/src/test/kotlin/dev/zacsweers/moshix/ir/compiler/MoshiIrVisitorTest.kt b/moshi-ir/moshi-compiler-plugin/src/test/kotlin/dev/zacsweers/moshix/ir/compiler/MoshiIrVisitorTest.kt index 36e303f5..131fff95 100644 --- a/moshi-ir/moshi-compiler-plugin/src/test/kotlin/dev/zacsweers/moshix/ir/compiler/MoshiIrVisitorTest.kt +++ b/moshi-ir/moshi-compiler-plugin/src/test/kotlin/dev/zacsweers/moshix/ir/compiler/MoshiIrVisitorTest.kt @@ -1052,7 +1052,6 @@ class MoshiIrVisitorTest(private val useK2: Boolean) { sources = sourceFiles.asList() verbose = false jvmTarget = JvmTarget.fromString(System.getProperty("moshix.jvmTarget"))!!.description - supportsK2 = true // Necessary for K2 testing, even if useK2 itself isn't part of this test! kotlincArguments += listOf("-Xskip-prerelease-check", "-Xallow-unstable-dependencies") if (this@MoshiIrVisitorTest.useK2) { diff --git a/moshi-ir/moshi-kotlin-tests/build.gradle.kts b/moshi-ir/moshi-kotlin-tests/build.gradle.kts index 7f767c88..cc5c99fe 100644 --- a/moshi-ir/moshi-kotlin-tests/build.gradle.kts +++ b/moshi-ir/moshi-kotlin-tests/build.gradle.kts @@ -27,6 +27,7 @@ moshi { enableSealed.set(true) } val proguardRuleValidator = tasks.register("validateProguardRules") { doNotTrackState("This is a validation task that should always run") + notCompatibleWithConfigurationCache("This task always runs") doLast { logger.lifecycle("Validating proguard rules") val proguardRulesDir = project.file("build/generated/ksp/test/resources/META-INF/proguard") diff --git a/moshi-metadata-reflect/src/main/kotlin/dev/zacsweers/moshix/reflect/JvmDescriptors.kt b/moshi-metadata-reflect/src/main/kotlin/dev/zacsweers/moshix/reflect/JvmDescriptors.kt index 22a1d3df..44291a1f 100644 --- a/moshi-metadata-reflect/src/main/kotlin/dev/zacsweers/moshix/reflect/JvmDescriptors.kt +++ b/moshi-metadata-reflect/src/main/kotlin/dev/zacsweers/moshix/reflect/JvmDescriptors.kt @@ -16,7 +16,7 @@ package dev.zacsweers.moshix.reflect import java.lang.reflect.Constructor -import kotlinx.metadata.jvm.JvmMethodSignature +import kotlin.metadata.jvm.JvmMethodSignature private val PRIMITIVE_CLASS_TO_DESC = mapOf( diff --git a/moshi-metadata-reflect/src/main/kotlin/dev/zacsweers/moshix/reflect/KtTypes.kt b/moshi-metadata-reflect/src/main/kotlin/dev/zacsweers/moshix/reflect/KtTypes.kt index 474f46ba..f9236618 100644 --- a/moshi-metadata-reflect/src/main/kotlin/dev/zacsweers/moshix/reflect/KtTypes.kt +++ b/moshi-metadata-reflect/src/main/kotlin/dev/zacsweers/moshix/reflect/KtTypes.kt @@ -20,19 +20,19 @@ import java.lang.reflect.Constructor import java.lang.reflect.Field import java.lang.reflect.Method import java.lang.reflect.Type -import kotlinx.metadata.KmClass -import kotlinx.metadata.KmClassifier -import kotlinx.metadata.KmClassifier.TypeAlias -import kotlinx.metadata.KmClassifier.TypeParameter -import kotlinx.metadata.KmConstructor -import kotlinx.metadata.KmProperty -import kotlinx.metadata.KmType -import kotlinx.metadata.KmValueParameter -import kotlinx.metadata.declaresDefaultValue -import kotlinx.metadata.isLocalClassName -import kotlinx.metadata.isNullable -import kotlinx.metadata.isSecondary -import kotlinx.metadata.jvm.signature +import kotlin.metadata.KmClass +import kotlin.metadata.KmClassifier +import kotlin.metadata.KmClassifier.TypeAlias +import kotlin.metadata.KmClassifier.TypeParameter +import kotlin.metadata.KmConstructor +import kotlin.metadata.KmProperty +import kotlin.metadata.KmType +import kotlin.metadata.KmValueParameter +import kotlin.metadata.declaresDefaultValue +import kotlin.metadata.isLocalClassName +import kotlin.metadata.isNullable +import kotlin.metadata.isSecondary +import kotlin.metadata.jvm.signature private fun defaultPrimitiveValue(type: Type): Any? = if (type is Class<*> && type.isPrimitive) { @@ -75,7 +75,7 @@ internal val KmType.canonicalName: String } /** - * Creates a canonical class name as represented in Metadata's [kotlinx.metadata.ClassName], where + * Creates a canonical class name as represented in Metadata's [kotlin.metadata.ClassName], where * package names in this name are separated by '/' and class names are separated by '.'. * * Example ClassName that we want to canonicalize: `"java/util/Map.Entry"`. diff --git a/moshi-metadata-reflect/src/main/kotlin/dev/zacsweers/moshix/reflect/MetadataKotlinJsonAdapterFactory.kt b/moshi-metadata-reflect/src/main/kotlin/dev/zacsweers/moshix/reflect/MetadataKotlinJsonAdapterFactory.kt index 99965a08..724f74f9 100644 --- a/moshi-metadata-reflect/src/main/kotlin/dev/zacsweers/moshix/reflect/MetadataKotlinJsonAdapterFactory.kt +++ b/moshi-metadata-reflect/src/main/kotlin/dev/zacsweers/moshix/reflect/MetadataKotlinJsonAdapterFactory.kt @@ -29,28 +29,28 @@ import java.lang.reflect.Field import java.lang.reflect.Method import java.lang.reflect.Modifier import java.lang.reflect.Type -import kotlinx.metadata.ClassKind -import kotlinx.metadata.KmClass -import kotlinx.metadata.KmFlexibleTypeUpperBound -import kotlinx.metadata.KmProperty -import kotlinx.metadata.KmType -import kotlinx.metadata.KmTypeProjection -import kotlinx.metadata.Modality -import kotlinx.metadata.Visibility -import kotlinx.metadata.isInner -import kotlinx.metadata.isNullable -import kotlinx.metadata.isVar -import kotlinx.metadata.jvm.JvmFieldSignature -import kotlinx.metadata.jvm.JvmMethodSignature -import kotlinx.metadata.jvm.KotlinClassMetadata -import kotlinx.metadata.jvm.Metadata -import kotlinx.metadata.jvm.fieldSignature -import kotlinx.metadata.jvm.getterSignature -import kotlinx.metadata.jvm.setterSignature -import kotlinx.metadata.jvm.syntheticMethodForAnnotations -import kotlinx.metadata.kind -import kotlinx.metadata.modality -import kotlinx.metadata.visibility +import kotlin.metadata.ClassKind +import kotlin.metadata.KmClass +import kotlin.metadata.KmFlexibleTypeUpperBound +import kotlin.metadata.KmProperty +import kotlin.metadata.KmType +import kotlin.metadata.KmTypeProjection +import kotlin.metadata.Modality +import kotlin.metadata.Visibility +import kotlin.metadata.isInner +import kotlin.metadata.isNullable +import kotlin.metadata.isVar +import kotlin.metadata.jvm.JvmFieldSignature +import kotlin.metadata.jvm.JvmMethodSignature +import kotlin.metadata.jvm.KotlinClassMetadata +import kotlin.metadata.jvm.Metadata +import kotlin.metadata.jvm.fieldSignature +import kotlin.metadata.jvm.getterSignature +import kotlin.metadata.jvm.setterSignature +import kotlin.metadata.jvm.syntheticMethodForAnnotations +import kotlin.metadata.kind +import kotlin.metadata.modality +import kotlin.metadata.visibility /** Classes annotated with this are eligible for this adapter. */ private val KOTLIN_METADATA = Metadata::class.java @@ -338,7 +338,6 @@ public class MetadataKotlinJsonAdapterFactory : JsonAdapter.Factory { this != null && other != null -> { // Note we don't check abbreviatedType because typealiases and their backing types are equal // for our purposes. - @Suppress("DEPRECATION") arguments valueEquals other.arguments && classifier == other.classifier && isNullable == other.isNullable && diff --git a/moshi-metadata-reflect/src/test/kotlin/dev/zacsweers/moshix/reflect/JvmDescriptorsTest.kt b/moshi-metadata-reflect/src/test/kotlin/dev/zacsweers/moshix/reflect/JvmDescriptorsTest.kt index 38f12d87..82701675 100644 --- a/moshi-metadata-reflect/src/test/kotlin/dev/zacsweers/moshix/reflect/JvmDescriptorsTest.kt +++ b/moshi-metadata-reflect/src/test/kotlin/dev/zacsweers/moshix/reflect/JvmDescriptorsTest.kt @@ -16,7 +16,7 @@ package dev.zacsweers.moshix.reflect import com.google.common.truth.Truth.assertThat -import kotlinx.metadata.jvm.JvmMethodSignature +import kotlin.metadata.jvm.JvmMethodSignature import org.junit.Test class JvmDescriptorsTest { diff --git a/moshi-proguard-rule-gen/build.gradle.kts b/moshi-proguard-rule-gen/build.gradle.kts index 4eef2fca..de3fb6cf 100644 --- a/moshi-proguard-rule-gen/build.gradle.kts +++ b/moshi-proguard-rule-gen/build.gradle.kts @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { alias(libs.plugins.kotlinJvm) @@ -22,19 +21,17 @@ plugins { alias(libs.plugins.ksp) } -tasks.withType().configureEach { - compilerOptions { - freeCompilerArgs.addAll("-opt-in=org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi") - } -} +kotlin { compilerOptions.optIn.add("org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi") } -tasks.withType().configureEach { +tasks.test { systemProperty("moshix.jvmTarget", libs.versions.jvmTarget.get()) + // KSP2 needs more memory to run + minHeapSize = "1024m" + maxHeapSize = "1024m" } dependencies { compileOnly(libs.kotlin.compilerEmbeddable) - compileOnly(libs.ksp) compileOnly(libs.ksp.api) implementation(libs.autoService) @@ -45,12 +42,9 @@ dependencies { ksp(libs.autoService.ksp) - testImplementation(libs.ksp.api) - testImplementation(libs.ksp) testImplementation(libs.truth) testImplementation(libs.junit) testImplementation(libs.kotlinCompileTesting) testImplementation(libs.kotlinCompileTesting.ksp) - testImplementation(libs.kotlin.compilerEmbeddable) testImplementation(project(":moshi-sealed:runtime")) } diff --git a/moshi-proguard-rule-gen/src/test/kotlin/dev/zacsweers/moshix/proguardgen/MoshiProguardGenSymbolProcessorTest.kt b/moshi-proguard-rule-gen/src/test/kotlin/dev/zacsweers/moshix/proguardgen/MoshiProguardGenSymbolProcessorTest.kt index e7f9091f..7ad818f1 100644 --- a/moshi-proguard-rule-gen/src/test/kotlin/dev/zacsweers/moshix/proguardgen/MoshiProguardGenSymbolProcessorTest.kt +++ b/moshi-proguard-rule-gen/src/test/kotlin/dev/zacsweers/moshix/proguardgen/MoshiProguardGenSymbolProcessorTest.kt @@ -20,15 +20,29 @@ import com.tschuchort.compiletesting.KotlinCompilation import com.tschuchort.compiletesting.KotlinCompilation.ExitCode import com.tschuchort.compiletesting.SourceFile import com.tschuchort.compiletesting.SourceFile.Companion.kotlin -import com.tschuchort.compiletesting.kspArgs +import com.tschuchort.compiletesting.configureKsp +import com.tschuchort.compiletesting.kspProcessorOptions import com.tschuchort.compiletesting.kspSourcesDir -import com.tschuchort.compiletesting.symbolProcessorProviders +import com.tschuchort.compiletesting.useKsp2 import dev.zacsweers.moshix.proguardgen.MoshiProguardGenSymbolProcessor.Companion.OPTION_GENERATE_MOSHI_CORE_PROGUARD_RULES import dev.zacsweers.moshix.proguardgen.MoshiProguardGenSymbolProcessor.Companion.OPTION_GENERATE_PROGUARD_RULES import java.io.File +import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.Parameterized -class MoshiSealedSymbolProcessorProviderTest { +@OptIn(ExperimentalCompilerApi::class) +@RunWith(Parameterized::class) +class MoshiProguardGenSymbolProcessorTest(private val useKSP2: Boolean) { + + companion object { + @JvmStatic + @Parameterized.Parameters(name = "useKSP2={0}") + fun data(): Collection> { + return listOf(arrayOf(true), arrayOf(false)) + } + } @Test fun `standard test with only sealed enabled`() { @@ -103,7 +117,9 @@ class MoshiSealedSymbolProcessorProviderTest { ) val compilation = - prepareCompilation(source) { kspArgs[OPTION_GENERATE_PROGUARD_RULES] = "false" } + prepareCompilation(source) { + kspProcessorOptions += mapOf(OPTION_GENERATE_PROGUARD_RULES to "false") + } val result = compilation.compile() assertThat(result.exitCode).isEqualTo(ExitCode.OK) assertThat(result.generatedFiles.filter { it.extension == "pro" }).isEmpty() @@ -114,29 +130,30 @@ class MoshiSealedSymbolProcessorProviderTest { val source = kotlin( "BaseType.kt", - """ - package test - import com.squareup.moshi.JsonClass - import dev.zacsweers.moshix.sealed.annotations.TypeLabel - import dev.zacsweers.moshix.sealed.annotations.NestedSealed + """package test - @JsonClass(generateAdapter = true, generator = "sealed:type") - sealed class BaseType { - @TypeLabel("a", ["aa"]) - class TypeA : BaseType() - @TypeLabel("b") - class TypeB : BaseType() - @NestedSealed - sealed class TypeC : BaseType() { - @TypeLabel("c") - class TypeCImpl : TypeC() - } - } - """, +import com.squareup.moshi.JsonClass +import dev.zacsweers.moshix.sealed.annotations.NestedSealed +import dev.zacsweers.moshix.sealed.annotations.TypeLabel + +@JsonClass(generateAdapter = true, generator = "sealed:type") +sealed class BaseType { + @TypeLabel("a", ["aa"]) class TypeA : BaseType() + + @TypeLabel("b") class TypeB : BaseType() + + @NestedSealed + sealed class TypeC : BaseType() { + @TypeLabel("c") class TypeCImpl : TypeC() + } +} +""", ) val compilation = - prepareCompilation(source) { kspArgs[OPTION_GENERATE_MOSHI_CORE_PROGUARD_RULES] = "true" } + prepareCompilation(source) { + kspProcessorOptions += mapOf(OPTION_GENERATE_MOSHI_CORE_PROGUARD_RULES to "true") + } val result = compilation.compile() assertThat(result.exitCode).isEqualTo(ExitCode.OK) val generatedSourcesDir = compilation.kspSourcesDir @@ -176,7 +193,14 @@ class MoshiSealedSymbolProcessorProviderTest { KotlinCompilation().apply { sources = sourceFiles.toList() inheritClassPath = true - symbolProcessorProviders = listOf(MoshiProguardGenSymbolProcessor.Provider()) + if (useKSP2) { + useKsp2() + } else { + languageVersion = "1.9" + } + configureKsp(useKSP2) { + symbolProcessorProviders += MoshiProguardGenSymbolProcessor.Provider() + } block() } diff --git a/moshi-sealed/codegen/build.gradle.kts b/moshi-sealed/codegen/build.gradle.kts index ff4be18f..9008cc46 100644 --- a/moshi-sealed/codegen/build.gradle.kts +++ b/moshi-sealed/codegen/build.gradle.kts @@ -1,5 +1,3 @@ -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile - /* * Copyright (C) 2020 Zac Sweers * @@ -22,8 +20,13 @@ plugins { alias(libs.plugins.mavenPublish) } -// --add-opens for kapt to work. KGP covers this for us but local JVMs in tests do not -tasks.withType().configureEach { +kotlin { compilerOptions.optIn.add("org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi") } + +tasks.test { + // KSP2 needs more memory to run + minHeapSize = "2096m" + maxHeapSize = "2096m" + // --add-opens for kapt to work. KGP covers this for us but local JVMs in tests do not jvmArgs( "--add-opens=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED", "--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED", @@ -38,18 +41,10 @@ tasks.withType().configureEach { ) } -tasks.withType().configureEach { - compilerOptions { - freeCompilerArgs.addAll("-opt-in=org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi") - } -} - dependencies { implementation(libs.autoService) implementation(project(":moshi-proguard-rule-gen")) ksp(libs.autoService.ksp) - // For access to MessageCollectorBasedKSPLogger - compileOnly(libs.ksp) compileOnly(libs.ksp.api) compileOnly(libs.kotlin.compilerEmbeddable) @@ -61,11 +56,8 @@ dependencies { implementation(libs.moshi) implementation(project(":moshi-sealed:runtime")) - testImplementation(libs.ksp.api) - testImplementation(libs.ksp) testImplementation(libs.truth) testImplementation(libs.junit) testImplementation(libs.kotlinCompileTesting) testImplementation(libs.kotlinCompileTesting.ksp) - testImplementation(libs.kotlin.compilerEmbeddable) } diff --git a/moshi-sealed/codegen/src/test/kotlin/dev/zacsweers/moshix/sealed/codegen/ksp/MoshiSealedSymbolProcessorTest.kt b/moshi-sealed/codegen/src/test/kotlin/dev/zacsweers/moshix/sealed/codegen/ksp/MoshiSealedSymbolProcessorTest.kt index 87ad27f4..a859954d 100644 --- a/moshi-sealed/codegen/src/test/kotlin/dev/zacsweers/moshix/sealed/codegen/ksp/MoshiSealedSymbolProcessorTest.kt +++ b/moshi-sealed/codegen/src/test/kotlin/dev/zacsweers/moshix/sealed/codegen/ksp/MoshiSealedSymbolProcessorTest.kt @@ -21,12 +21,24 @@ import com.tschuchort.compiletesting.KotlinCompilation import com.tschuchort.compiletesting.KotlinCompilation.ExitCode import com.tschuchort.compiletesting.SourceFile import com.tschuchort.compiletesting.SourceFile.Companion.kotlin +import com.tschuchort.compiletesting.configureKsp import com.tschuchort.compiletesting.kspSourcesDir -import com.tschuchort.compiletesting.symbolProcessorProviders +import com.tschuchort.compiletesting.useKsp2 import java.io.File import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.Parameterized -class MoshiSealedSymbolProcessorProviderTest { +@RunWith(Parameterized::class) +class MoshiSealedSymbolProcessorProviderTest(private val useKSP2: Boolean) { + + companion object { + @JvmStatic + @Parameterized.Parameters(name = "useKSP2={0}") + fun data(): Collection> { + return listOf(arrayOf(true), arrayOf(false)) + } + } @Test fun smokeTest() { @@ -509,7 +521,12 @@ class MoshiSealedSymbolProcessorProviderTest { KotlinCompilation().apply { sources = sourceFiles.toList() inheritClassPath = true - symbolProcessorProviders = listOf(MoshiSealedSymbolProcessorProvider()) + if (useKSP2) { + useKsp2() + } else { + languageVersion = "1.9" + } + configureKsp(useKSP2) { symbolProcessorProviders += MoshiSealedSymbolProcessorProvider() } } private fun compile(vararg sourceFiles: SourceFile): CompilationResult { diff --git a/moshi-sealed/metadata-reflect/src/main/kotlin/dev/zacsweers/moshix/sealed/reflect/MetadataMoshiSealedJsonAdapterFactory.kt b/moshi-sealed/metadata-reflect/src/main/kotlin/dev/zacsweers/moshix/sealed/reflect/MetadataMoshiSealedJsonAdapterFactory.kt index 4664f2a0..8ad24441 100644 --- a/moshi-sealed/metadata-reflect/src/main/kotlin/dev/zacsweers/moshix/sealed/reflect/MetadataMoshiSealedJsonAdapterFactory.kt +++ b/moshi-sealed/metadata-reflect/src/main/kotlin/dev/zacsweers/moshix/sealed/reflect/MetadataMoshiSealedJsonAdapterFactory.kt @@ -28,14 +28,14 @@ import dev.zacsweers.moshix.sealed.annotations.TypeLabel import dev.zacsweers.moshix.sealed.runtime.internal.ObjectJsonAdapter import dev.zacsweers.moshix.sealed.runtime.internal.Util.fallbackAdapter import java.lang.reflect.Type -import kotlinx.metadata.ClassKind -import kotlinx.metadata.ClassName -import kotlinx.metadata.KmClass -import kotlinx.metadata.Modality -import kotlinx.metadata.jvm.KotlinClassMetadata -import kotlinx.metadata.jvm.Metadata -import kotlinx.metadata.kind -import kotlinx.metadata.modality +import kotlin.metadata.ClassKind +import kotlin.metadata.ClassName +import kotlin.metadata.KmClass +import kotlin.metadata.Modality +import kotlin.metadata.jvm.KotlinClassMetadata +import kotlin.metadata.jvm.Metadata +import kotlin.metadata.kind +import kotlin.metadata.modality /** Classes annotated with this are eligible for this adapter. */ private val KOTLIN_METADATA = Metadata::class.java diff --git a/publish.sh b/publish.sh index 2af4dd18..c6bd7aa7 100755 --- a/publish.sh +++ b/publish.sh @@ -3,8 +3,8 @@ if [[ "$1" = "--local" ]]; then local=true; fi if ! [[ ${local} ]]; then - ./gradlew -p moshi-ir/moshi-gradle-plugin publish -x dokkaHtml - ./gradlew publish -x dokkaHtml + ./gradlew -p moshi-ir/moshi-gradle-plugin publish -x dokkaHtml --no-configuration-cache + ./gradlew publish -x dokkaHtml --no-configuration-cache else ./gradlew -p moshi-ir/moshi-gradle-plugin publishToMavenLocal -x dokkaHtml ./gradlew publishToMavenLocal -x dokkaHtml