Update KSP to 20210111
This CL updates KSP to 20210111.
* Removed the fake checker impl since we don't need that anymore. KSP
skips fakes now.
* Re-enabled tests for java enums since that issue is fixed.
* Removed safeGetJvmName for properties because #200 is fixed.
Unfortunately, there is another bug for methods so we are still using
safeGet for them.
https://github.com/google/ksp/issues/240
* Removed workaround in findOverridee but unfortunately, now we hit
another bug there: https://github.com/google/ksp/issues/248
Bug: 160322705
Test: existing tests
Change-Id: Iddabe0a828821a13a20b8b23c7f9ad340893429b
diff --git a/buildSrc/build_dependencies.gradle b/buildSrc/build_dependencies.gradle
index 2d1fafb..115538f 100644
--- a/buildSrc/build_dependencies.gradle
+++ b/buildSrc/build_dependencies.gradle
@@ -24,7 +24,7 @@
build_versions.kotlin = "1.4.21"
build_versions.kotlin_coroutines = "1.4.1"
-build_versions.ksp = "1.4.20-dev-experimental-20201204"
+build_versions.ksp = "1.4.20-dev-experimental-20210111"
build_versions.hilt = "2.29.1-alpha"
@@ -65,7 +65,7 @@
build_libs.kotlin = [
stdlib: "org.jetbrains.kotlin:kotlin-stdlib:${build_versions.kotlin}",
gradle_plugin: "org.jetbrains.kotlin:kotlin-gradle-plugin:${build_versions.kotlin}",
- ksp_gradle_plugin: "com.google.devtools.ksp:symbol-processing:${build_versions.ksp}"
+ ksp_gradle_plugin: "com.google.devtools.ksp:symbol-processing-gradle-plugin:${build_versions.ksp}"
]
build_libs.dex_member_list = "com.jakewharton.dex:dex-member-list:4.1.1"
build_libs.dokka_gradle = [
diff --git a/buildSrc/src/main/kotlin/androidx/build/dependencies/Dependencies.kt b/buildSrc/src/main/kotlin/androidx/build/dependencies/Dependencies.kt
index 97635f2..4f725e8 100644
--- a/buildSrc/src/main/kotlin/androidx/build/dependencies/Dependencies.kt
+++ b/buildSrc/src/main/kotlin/androidx/build/dependencies/Dependencies.kt
@@ -76,8 +76,8 @@
const val KOTLINPOET_METADATA_SPECS = "com.squareup:kotlinpoet-metadata-specs:1.4.0"
const val KOTLINPOET_CLASSINSPECTOR_ELEMENTS =
"com.squareup:kotlinpoet-classinspector-elements:1.4.0"
-const val KOTLIN_COMPILE_TESTING = "com.github.tschuchortdev:kotlin-compile-testing:1.3.1"
-const val KOTLIN_COMPILE_TESTING_KSP = "com.github.tschuchortdev:kotlin-compile-testing-ksp:1.3.1"
+const val KOTLIN_COMPILE_TESTING = "com.github.tschuchortdev:kotlin-compile-testing:1.3.4"
+const val KOTLIN_COMPILE_TESTING_KSP = "com.github.tschuchortdev:kotlin-compile-testing-ksp:1.3.4"
/**
* KSP is used both as a plugin and runtime dependency, hence its version is declared in the
@@ -88,7 +88,6 @@
val KSP_VERSION get() = kspVersion
val KOTLIN_KSP_API get() = "com.google.devtools.ksp:symbol-processing-api:$KSP_VERSION"
val KOTLIN_KSP get() = "com.google.devtools.ksp:symbol-processing:$KSP_VERSION"
-val KOTLIN_KSP_GRADLE_PLUGIN get() = "com.google.devtools.ksp:symbol-processing:$KSP_VERSION"
const val KOTLIN_GRADLE_PLUGIN = "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.21"
const val KOTLIN_METADATA = "me.eugeniomarletti.kotlin.metadata:kotlin-metadata:1.4.0"
diff --git a/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KSAsMemberOf.kt b/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KSAsMemberOf.kt
index 2c9b601..7e84d7c 100644
--- a/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KSAsMemberOf.kt
+++ b/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KSAsMemberOf.kt
@@ -84,17 +84,9 @@
// object
resolved
}
- else -> {
- // workaround for https://github.com/google/ksp/issues/200
- val resolvedReturnType = resolver.asMemberOf(
- function = this,
- containing = ksType
- ).returnType
- if (resolvedReturnType == null || resolvedReturnType.isError) {
- resolved
- } else {
- resolvedReturnType
- }
- }
+ else -> resolver.asMemberOf(
+ function = this,
+ containing = ksType
+ ).returnType
} ?: error("cannot find return type for $this")
}
diff --git a/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspFiler.kt b/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspFiler.kt
index de20737..3b2cb0c 100644
--- a/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspFiler.kt
+++ b/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspFiler.kt
@@ -19,12 +19,15 @@
import androidx.room.compiler.processing.XFiler
import com.squareup.javapoet.JavaFile
import com.google.devtools.ksp.processing.CodeGenerator
+import com.google.devtools.ksp.processing.Dependencies
internal class KspFiler(
private val delegate: CodeGenerator
) : XFiler {
override fun write(javaFile: JavaFile) {
delegate.createNewFile(
+ // TODO: track originating files: b/176453350
+ dependencies = Dependencies.ALL_FILES,
packageName = javaFile.packageName,
fileName = javaFile.typeSpec.name,
extensionName = "java"
diff --git a/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspProcessingEnv.kt b/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspProcessingEnv.kt
index ba8ae4e..7583d75 100644
--- a/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspProcessingEnv.kt
+++ b/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspProcessingEnv.kt
@@ -27,15 +27,12 @@
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.KSClassDeclaration
-import com.google.devtools.ksp.symbol.KSFunctionDeclaration
import com.google.devtools.ksp.symbol.KSType
import com.google.devtools.ksp.symbol.KSTypeArgument
import com.google.devtools.ksp.symbol.KSTypeParameter
import com.google.devtools.ksp.symbol.KSTypeReference
import com.google.devtools.ksp.symbol.Nullability
-import com.google.devtools.ksp.symbol.Origin
import com.google.devtools.ksp.symbol.Variance
-import java.lang.reflect.Method
internal class KspProcessingEnv(
override val options: Map<String, String>,
@@ -205,13 +202,6 @@
return typeElementStore[declaration]
}
- // workaround until KSP stops returning fake overrides from methods.
- private val fakeChecker = FakeOverrideChecker()
-
- fun isFakeOverride(ksFunctionDeclaration: KSFunctionDeclaration): Boolean {
- return fakeChecker.isFakeOverride(ksFunctionDeclaration)
- }
-
class CommonTypes(resolver: Resolver) {
val nullableInt by lazy {
resolver.builtIns.intType.makeNullable()
@@ -223,58 +213,4 @@
resolver.builtIns.byteType.makeNullable()
}
}
-
- /**
- * Current version of KSP returns fake methods which kotlin generates for overrides. This
- * create exceptions later on when we try to resolve them, and we don't want them.
- * This class is a temporary band aid to filter them out.
- * KSP already fixed this issue in 1.4.20-dev-experimental-20201222 but we cannot update to
- * that version yet due to https://github.com/google/ksp/issues/216
- * https://github.com/google/ksp/commit/93675bef1dc20be096ada7afa4baa46e04acdb12
- */
- @Suppress("BanUncheckedReflection")
- private class FakeOverrideChecker {
- private lateinit var kindMethod: Method
- private lateinit var fakeOverrideKind: Any
-
- private fun initializeIfNecessary(ref: Any) {
- if (this::kindMethod.isInitialized) {
- return
- }
- val callableDescriptorClass = ref.javaClass.classLoader
- .loadClass(CALLABLE_MEMBER_DESCRIPTOR_CLASS_NAME)
- kindMethod = callableDescriptorClass.getDeclaredMethod("getKind")
- check(kindMethod.returnType.isEnum) {
- "expected getKind method to return an enum"
- }
- fakeOverrideKind = kindMethod.returnType.enumConstants.firstOrNull {
- it.toString() == FAKE_OVERRIDE_ENUM_VALUE
- } ?: error("cannot find FAKE_OVERRIDE enum constant in ${kindMethod.returnType}")
- }
-
- /**
- * Return true if this is a FAKE_OVERRIDE which kotlin generates for overrides when there
- * is no real override and method is inherited.
- */
- fun isFakeOverride(declaration: KSFunctionDeclaration): Boolean {
- if (declaration.origin != Origin.CLASS) return false
-
- val descriptorField = try {
- declaration::class.java.getDeclaredField("descriptor").also {
- it.trySetAccessible()
- }
- } catch (ignored: Throwable) {
- null
- }
- val descriptor = descriptorField?.get(declaration) ?: return false
- initializeIfNecessary(declaration)
- return fakeOverrideKind == kindMethod.invoke(descriptor)
- }
-
- companion object {
- private val CALLABLE_MEMBER_DESCRIPTOR_CLASS_NAME =
- "org.jetbrains.kotlin.descriptors.CallableMemberDescriptor"
- private val FAKE_OVERRIDE_ENUM_VALUE = "FAKE_OVERRIDE"
- }
- }
}
diff --git a/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspTypeElement.kt b/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspTypeElement.kt
index 437d9a3..b2ffecd 100644
--- a/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspTypeElement.kt
+++ b/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KspTypeElement.kt
@@ -249,14 +249,12 @@
private val _declaredMethods by lazy {
val instanceMethods = declaration.getDeclaredFunctions().asSequence()
- .filterNot(env::isFakeOverride)
val companionMethods = declaration.findCompanionObject()
?.getDeclaredFunctions()
?.asSequence()
?.filter {
it.isStatic()
- }?.filterNot(env::isFakeOverride)
- ?: emptySequence()
+ } ?: emptySequence()
val declaredMethods = (instanceMethods + companionMethods)
.filterNot {
// filter out constructors
diff --git a/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/ResolverExt.kt b/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/ResolverExt.kt
index 21ab8ac..48b1ad5 100644
--- a/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/ResolverExt.kt
+++ b/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/ResolverExt.kt
@@ -23,7 +23,6 @@
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.KSDeclaration
import com.google.devtools.ksp.symbol.KSFunctionDeclaration
-import com.google.devtools.ksp.symbol.KSPropertyAccessor
import com.google.devtools.ksp.symbol.KSPropertyDeclaration
import com.google.devtools.ksp.symbol.KSTypeParameter
import com.google.devtools.ksp.symbol.Nullability
@@ -82,8 +81,8 @@
private fun KSFunctionDeclaration.overrides(other: KSFunctionDeclaration): Boolean {
val overridee = try {
findOverridee()
- } catch (ignored: ClassCastException) {
- // workaround for https://github.com/google/ksp/issues/164
+ } catch (ignored: IllegalStateException) {
+ // workaround for https://github.com/google/ksp/issues/248
null
}
// before accepting this override, check if we have a primitive parameter that was a type
@@ -143,22 +142,7 @@
// workaround for https://github.com/google/ksp/issues/164
return declaration.simpleName.asString()
} catch (cannotFindDeclaration: IllegalStateException) {
- // workaround for https://github.com/google/ksp/issues/200
- // happens for setters getters as well as `values` method of Enum descriptor
+ // workaround for https://github.com/google/ksp/issues/240
return declaration.simpleName.asString()
}
}
-
-@OptIn(KspExperimental::class)
-internal fun Resolver.safeGetJvmName(
- accessor: KSPropertyAccessor,
- fallback: () -> String
-): String {
- return try {
- getJvmName(accessor)
- } catch (ignored: ClassCastException) {
- // TODO remove this catch once that issue is fixed.
- // workaround for https://github.com/google/ksp/issues/164
- return fallback()
- }
-}
diff --git a/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/synthetic/KspSyntheticPropertyMethodElement.kt b/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/synthetic/KspSyntheticPropertyMethodElement.kt
index 43f053d..4989024 100644
--- a/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/synthetic/KspSyntheticPropertyMethodElement.kt
+++ b/room/compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/synthetic/KspSyntheticPropertyMethodElement.kt
@@ -34,7 +34,6 @@
import androidx.room.compiler.processing.ksp.KspProcessingEnv
import androidx.room.compiler.processing.ksp.KspTypeElement
import androidx.room.compiler.processing.ksp.overrides
-import androidx.room.compiler.processing.ksp.safeGetJvmName
import com.google.devtools.ksp.KspExperimental
import com.google.devtools.ksp.symbol.KSPropertyAccessor
import java.util.Locale
@@ -121,9 +120,7 @@
@OptIn(KspExperimental::class)
override val name: String by lazy {
field.declaration.getter?.let {
- return@lazy env.resolver.safeGetJvmName(it) {
- computeGetterName(field.name)
- }
+ return@lazy env.resolver.getJvmName(it)
}
computeGetterName(field.name)
}
@@ -183,9 +180,7 @@
@OptIn(KspExperimental::class)
override val name: String by lazy {
field.declaration.setter?.let {
- return@lazy env.resolver.safeGetJvmName(it) {
- computeSetterName(field.name)
- }
+ return@lazy env.resolver.getJvmName(it)
}
computeSetterName(field.name)
}
diff --git a/room/compiler-processing/src/test/java/androidx/room/compiler/processing/XTypeElementTest.kt b/room/compiler-processing/src/test/java/androidx/room/compiler/processing/XTypeElementTest.kt
index 7e46099..c88d944 100644
--- a/room/compiler-processing/src/test/java/androidx/room/compiler/processing/XTypeElementTest.kt
+++ b/room/compiler-processing/src/test/java/androidx/room/compiler/processing/XTypeElementTest.kt
@@ -826,19 +826,15 @@
contains("enumMethod")
containsNoneOf("VAL1", "VAL2")
}
- if (qName != "app.JavaEnum" || !invocation.isKsp) {
- // KSP does not properly return enum constants for java sources yet
- // https://github.com/google/ksp/issues/234
- assertWithMessage("$qName can return enum constants")
- .that((typeElement as XEnumTypeElement).enumConstantNames)
- .containsExactly("VAL1", "VAL2")
- assertWithMessage("$qName does not report enum constants in fields")
- .that(typeElement.getAllFieldNames())
- .run {
- contains("x")
- containsNoneOf("VAL1", "VAL2")
- }
- }
+ assertWithMessage("$qName can return enum constants")
+ .that((typeElement as XEnumTypeElement).enumConstantNames)
+ .containsExactly("VAL1", "VAL2")
+ assertWithMessage("$qName does not report enum constants in fields")
+ .that(typeElement.getAllFieldNames())
+ .run {
+ contains("x")
+ containsNoneOf("VAL1", "VAL2")
+ }
}
}
}
diff --git a/room/compiler-processing/src/test/java/androidx/room/compiler/processing/XTypeTest.kt b/room/compiler-processing/src/test/java/androidx/room/compiler/processing/XTypeTest.kt
index 901d901..3aa2c26 100644
--- a/room/compiler-processing/src/test/java/androidx/room/compiler/processing/XTypeTest.kt
+++ b/room/compiler-processing/src/test/java/androidx/room/compiler/processing/XTypeTest.kt
@@ -367,8 +367,6 @@
"""
import java.lang.Void;
interface JavaInterface {
- // get void triggers a bug in KSP, which is why we keep it here.
- // https://github.com/google/ksp/issues/200
Void getVoid();
Void anotherVoid();
}
diff --git a/room/integration-tests/kotlintestapp/build.gradle b/room/integration-tests/kotlintestapp/build.gradle
index 2aeabf20..c29c2cc 100644
--- a/room/integration-tests/kotlintestapp/build.gradle
+++ b/room/integration-tests/kotlintestapp/build.gradle
@@ -33,7 +33,7 @@
// (b/153917176)
def useKsp = project.properties.getOrDefault("useKsp", "false").toBoolean()
if (useKsp) {
- apply plugin:"symbol-processing"
+ apply plugin: "com.google.devtools.ksp"
} else {
apply plugin: "kotlin-kapt"
}