Skip to content

Commit 17778fc

Browse files
committed
Optimize exception messages to contain helpful debug information
1 parent 657931a commit 17778fc

17 files changed

+42
-28
lines changed

api/src/main/kotlin/com/google/devtools/ksp/utils.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ fun KSClassDeclaration.getAllSuperTypes(): Sequence<KSType> {
132132
is KSClassDeclaration -> sequenceOf(resolvedDeclaration)
133133
is KSTypeAlias -> sequenceOf(resolvedDeclaration.findActualType())
134134
is KSTypeParameter -> resolvedDeclaration.getTypesUpperBound()
135-
else -> throw IllegalStateException()
135+
else -> throw IllegalStateException("unhandled type parameter bound, $ExceptionMessage")
136136
}
137137
}
138138

@@ -148,7 +148,7 @@ fun KSClassDeclaration.getAllSuperTypes(): Sequence<KSType> {
148148
is KSClassDeclaration -> it.getAllSuperTypes()
149149
is KSTypeAlias -> it.findActualType().getAllSuperTypes()
150150
is KSTypeParameter -> it.getTypesUpperBound().flatMap { it.getAllSuperTypes() }
151-
else -> throw IllegalStateException()
151+
else -> throw IllegalStateException("unhandled super type kind, $ExceptionMessage")
152152
}
153153
}
154154
)
@@ -243,3 +243,5 @@ fun KSDeclaration.isVisibleFrom(other: KSDeclaration): Boolean {
243243
}
244244

245245
}
246+
247+
const val ExceptionMessage = "please file a bug at https://github.com/google/ksp/issues/new"

api/src/main/kotlin/com/google/devtools/ksp/visitor/KSValidateVisitor.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.google.devtools.ksp.visitor
22

3+
import com.google.devtools.ksp.ExceptionMessage
34
import com.google.devtools.ksp.symbol.*
45

56
class KSValidateVisitor(private val predicate: (KSNode, KSNode) -> Boolean) : KSDefaultVisitor<Unit, Boolean>() {
@@ -16,7 +17,7 @@ class KSValidateVisitor(private val predicate: (KSNode, KSNode) -> Boolean) : KS
1617
}
1718

1819
override fun defaultHandler(node: KSNode, data: Unit): Boolean {
19-
throw IllegalStateException("unhandled validation condition, please file a bug at https://github.com/google/ksp/issues/new")
20+
throw IllegalStateException("unhandled validation condition, $ExceptionMessage")
2021
}
2122

2223
override fun visitTypeReference(typeReference: KSTypeReference, data: Unit): Boolean {

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/processing/impl/CodeGeneratorImpl.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class CodeGeneratorImpl(
4646
val file = File(path)
4747
val parentFile = file.parentFile
4848
if (!parentFile.exists() && !parentFile.mkdirs()) {
49-
throw IllegalStateException()
49+
throw IllegalStateException("failed to make parent directories.")
5050
}
5151
file.writeText("")
5252
fileMap[path] = file

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/processing/impl/ResolverImpl.kt

+5-7
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818

1919
package com.google.devtools.ksp.processing.impl
2020

21-
import com.google.devtools.ksp.KspExperimental
22-
import com.google.devtools.ksp.closestClassDeclaration
23-
import com.google.devtools.ksp.isOpen
24-
import com.google.devtools.ksp.isVisibleFrom
21+
import com.google.devtools.ksp.*
2522
import com.intellij.openapi.project.Project
2623
import com.intellij.psi.*
2724
import com.intellij.psi.impl.source.PsiClassReferenceType
@@ -442,7 +439,7 @@ class ResolverImpl(
442439
return getKSTypeCached(resolveJavaType(type.psi), type.element.typeArguments, type.annotations)
443440
}
444441
}
445-
else -> throw IllegalStateException()
442+
else -> throw IllegalStateException("Unable to resolve type for $type, $ExceptionMessage")
446443
}
447444
}
448445

@@ -454,13 +451,14 @@ class ResolverImpl(
454451
is KtClassOrObject -> KSClassDeclarationImpl.getCached(psi)
455452
is PsiClass -> KSClassDeclarationJavaImpl.getCached(psi)
456453
is KtTypeAlias -> KSTypeAliasImpl.getCached(psi)
457-
else -> throw IllegalStateException()
454+
else -> throw IllegalStateException("Unexpected psi type: ${psi.javaClass}, $ExceptionMessage")
458455
}
459456
} else {
460457
when (descriptor) {
461458
is ClassDescriptor -> KSClassDeclarationDescriptorImpl.getCached(descriptor)
462459
is TypeParameterDescriptor -> KSTypeParameterDescriptorImpl.getCached(descriptor)
463-
else -> throw IllegalStateException()
460+
null -> throw IllegalStateException("Failed to resolve descriptor for $kotlinType")
461+
else -> throw IllegalStateException("Unexpected descriptor type: ${descriptor.javaClass}, $ExceptionMessage")
464462
}
465463
}
466464
}

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/binary/KSAnnotationDescriptorImpl.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package com.google.devtools.ksp.symbol.impl.binary
2020

21+
import com.google.devtools.ksp.ExceptionMessage
2122
import com.intellij.psi.JavaPsiFacade
2223
import com.intellij.psi.PsiAnnotationMethod
2324
import org.jetbrains.kotlin.descriptors.ClassConstructorDescriptor
@@ -122,6 +123,6 @@ fun ValueParameterDescriptor.getDefaultValue(): Any? {
122123
}
123124
is KtParameter -> ResolverImpl.instance.evaluateConstant(psi.defaultValue, this.type)?.value
124125
is PsiAnnotationMethod -> JavaPsiFacade.getInstance(psi.project).constantEvaluationHelper.computeConstantExpression((psi).defaultValue)
125-
else -> throw IllegalStateException()
126+
else -> throw IllegalStateException("Unexpected psi ${psi.javaClass}, $ExceptionMessage")
126127
}
127128
}

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/binary/KSClassDeclarationDescriptorImpl.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package com.google.devtools.ksp.symbol.impl.binary
2020

21+
import com.google.devtools.ksp.ExceptionMessage
2122
import org.jetbrains.kotlin.descriptors.*
2223
import com.google.devtools.ksp.symbol.*
2324
import com.google.devtools.ksp.symbol.impl.KSObjectCache
@@ -91,7 +92,7 @@ class KSClassDeclarationDescriptorImpl private constructor(val descriptor: Class
9192
is PropertyDescriptor -> KSPropertyDeclarationDescriptorImpl.getCached(it)
9293
is FunctionDescriptor -> KSFunctionDeclarationDescriptorImpl.getCached(it)
9394
is ClassDescriptor -> getCached(it)
94-
else -> throw IllegalStateException()
95+
else -> throw IllegalStateException("Unexpected descriptor type ${it.javaClass}, $ExceptionMessage")
9596
}
9697
}
9798
}

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/binary/KSFunctionDeclarationDescriptorImpl.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package com.google.devtools.ksp.symbol.impl.binary
2020

21+
import com.google.devtools.ksp.ExceptionMessage
2122
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
2223
import org.jetbrains.kotlin.descriptors.impl.AnonymousFunctionDescriptor
2324
import com.google.devtools.ksp.isOpen
@@ -31,6 +32,7 @@ import com.google.devtools.ksp.symbol.impl.toKSFunctionDeclaration
3132
import com.google.devtools.ksp.symbol.impl.toKSModifiers
3233
import org.jetbrains.kotlin.load.java.isFromJava
3334
import org.jetbrains.kotlin.resolve.OverridingUtil
35+
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
3436

3537
class KSFunctionDeclarationDescriptorImpl private constructor(val descriptor: FunctionDescriptor) : KSFunctionDeclaration,
3638
KSDeclarationDescriptorImpl(descriptor),
@@ -64,7 +66,7 @@ class KSFunctionDeclarationDescriptorImpl private constructor(val descriptor: Fu
6466
descriptor.dispatchReceiverParameter == null -> if (descriptor.isFromJava) FunctionKind.STATIC else FunctionKind.TOP_LEVEL
6567
!descriptor.name.isSpecial && !descriptor.name.asString().isEmpty() -> FunctionKind.MEMBER
6668
descriptor is AnonymousFunctionDescriptor -> FunctionKind.ANONYMOUS
67-
else -> throw IllegalStateException()
69+
else -> throw IllegalStateException("Unable to resolve FunctionKind for ${descriptor.fqNameSafe}, $ExceptionMessage")
6870
}
6971
}
7072

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/binary/KSPropertySetterDescriptorImpl.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class KSPropertySetterDescriptorImpl private constructor(descriptor: PropertySet
2929
}
3030

3131
override val parameter: KSValueParameter by lazy {
32-
descriptor.valueParameters.singleOrNull()?.let { KSValueParameterDescriptorImpl.getCached(it) } ?: throw IllegalStateException()
32+
descriptor.valueParameters.singleOrNull()?.let { KSValueParameterDescriptorImpl.getCached(it) }
33+
?: throw IllegalStateException("Failed to resolve property type")
3334
}
3435

3536
override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/binary/KSTypeParameterDescriptorImpl.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package com.google.devtools.ksp.symbol.impl.binary
2020

21+
import com.google.devtools.ksp.ExceptionMessage
2122
import org.jetbrains.kotlin.descriptors.ClassDescriptor
2223
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
2324
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
@@ -47,7 +48,7 @@ class KSTypeParameterDescriptorImpl private constructor(val descriptor: TypePara
4748
is ClassDescriptor -> KSClassDeclarationDescriptorImpl.getCached(parent)
4849
is FunctionDescriptor -> KSFunctionDeclarationDescriptorImpl.getCached(parent)
4950
is PropertyDescriptor -> KSPropertyDeclarationDescriptorImpl.getCached(parent)
50-
else -> throw IllegalStateException()
51+
else -> throw IllegalStateException("Unexpected containing declaration for ${descriptor.fqNameSafe}, $ExceptionMessage")
5152
}
5253
}
5354

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/binary/KSTypeReferenceDescriptorImpl.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package com.google.devtools.ksp.symbol.impl.binary
2020

21+
import com.google.devtools.ksp.ExceptionMessage
2122
import org.jetbrains.kotlin.builtins.isSuspendFunctionTypeOrSubtype
2223
import org.jetbrains.kotlin.descriptors.ClassDescriptor
2324
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
@@ -44,10 +45,10 @@ class KSTypeReferenceDescriptorImpl private constructor(val kotlinType: KotlinTy
4445
when (upperBound) {
4546
is FlexibleType -> KSClassifierReferenceDescriptorImpl.getCached(upperBound.upperBound)
4647
is SimpleType -> KSClassifierReferenceDescriptorImpl.getCached(upperBound)
47-
else -> throw IllegalStateException()
48+
else -> throw IllegalStateException("Unexpected upperbound type ${upperBound.javaClass}, $ExceptionMessage")
4849
}
4950
}
50-
else -> throw IllegalStateException()
51+
else -> throw IllegalStateException("Unexpected type: ${kotlinType.constructor.declarationDescriptor?.javaClass}, $ExceptionMessage")
5152
}
5253
}
5354

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/java/KSTypeReferenceJavaImpl.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package com.google.devtools.ksp.symbol.impl.java
2020

21+
import com.google.devtools.ksp.ExceptionMessage
2122
import com.intellij.psi.*
2223
import com.intellij.psi.impl.source.PsiClassReferenceType
2324
import com.google.devtools.ksp.processing.impl.ResolverImpl
@@ -62,7 +63,7 @@ class KSTypeReferenceJavaImpl private constructor(val psi: PsiType) : KSTypeRefe
6263
"char" -> ResolverImpl.instance.module.builtIns.charType
6364
"boolean" -> ResolverImpl.instance.module.builtIns.booleanType
6465
"void" -> ResolverImpl.instance.module.builtIns.unitType
65-
else -> throw IllegalStateException()
66+
else -> throw IllegalStateException("Unexpected primitive type ${this.name}, $ExceptionMessage")
6667
}
6768
}
6869

@@ -88,7 +89,7 @@ class KSTypeReferenceJavaImpl private constructor(val psi: PsiType) : KSTypeRefe
8889
}
8990
}
9091
null -> KSClassifierReferenceDescriptorImpl.getCached((ResolverImpl.instance.builtIns.anyType as KSTypeImpl).kotlinType.makeNullable())
91-
else -> throw IllegalStateException()
92+
else -> throw IllegalStateException("Unexpected psi type for ${type.javaClass}, $ExceptionMessage")
9293
}
9394
}
9495

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/kotlin/KSFunctionDeclarationImpl.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package com.google.devtools.ksp.symbol.impl.kotlin
2020

21+
import com.google.devtools.ksp.ExceptionMessage
2122
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
2223
import com.google.devtools.ksp.isOpen
2324
import com.google.devtools.ksp.isVisibleFrom
@@ -65,7 +66,7 @@ class KSFunctionDeclarationImpl private constructor(val ktFunction: KtFunction)
6566
when (ktFunction) {
6667
is KtNamedFunction, is KtPrimaryConstructor, is KtSecondaryConstructor -> FunctionKind.MEMBER
6768
is KtFunctionLiteral -> if (ktFunction.node.findChildByType(KtTokens.FUN_KEYWORD) != null) FunctionKind.ANONYMOUS else FunctionKind.LAMBDA
68-
else -> throw IllegalStateException()
69+
else -> throw IllegalStateException("Unexpected psi type ${ktFunction.javaClass}, $ExceptionMessage")
6970
}
7071
}
7172
}

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/kotlin/KSPropertySetterImpl.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class KSPropertySetterImpl private constructor(ktPropertySetter: KtPropertyAcces
3030

3131
override val parameter: KSValueParameter by lazy {
3232
ktPropertySetter.parameterList?.parameters?.singleOrNull()?.let { KSValueParameterImpl.getCached(it) }
33-
?: throw IllegalStateException()
33+
?: throw IllegalStateException("Failed to resolve property type")
3434
}
3535

3636
override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/kotlin/KSTypeParameterImpl.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package com.google.devtools.ksp.symbol.impl.kotlin
2020

21+
import com.google.devtools.ksp.ExceptionMessage
2122
import com.google.devtools.ksp.symbol.*
2223
import com.google.devtools.ksp.symbol.impl.KSObjectCache
2324
import com.google.devtools.ksp.symbol.impl.toKSModifiers
@@ -71,7 +72,7 @@ class KSTypeParameterImpl private constructor(val ktTypeParameter: KtTypeParamet
7172
is KtClassOrObject -> KSClassDeclarationImpl.getCached(owner)
7273
is KtFunction -> KSFunctionDeclarationImpl.getCached(owner)
7374
is KtProperty -> KSPropertyDeclarationImpl.getCached(owner)
74-
else -> throw IllegalStateException()
75+
else -> throw IllegalStateException("Unexpected containing declaration type ${owner.javaClass}, $ExceptionMessage")
7576
}
7677
}
7778

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/kotlin/KSTypeReferenceImpl.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package com.google.devtools.ksp.symbol.impl.kotlin
2020

21+
import com.google.devtools.ksp.ExceptionMessage
2122
import com.google.devtools.ksp.processing.impl.ResolverImpl
2223
import com.google.devtools.ksp.symbol.*
2324
import com.google.devtools.ksp.symbol.impl.KSObjectCache
@@ -53,7 +54,7 @@ class KSTypeReferenceImpl private constructor(val ktTypeReference: KtTypeReferen
5354
is KtFunctionType -> KSCallableReferenceImpl.getCached(typeElement)
5455
is KtUserType -> KSClassifierReferenceImpl.getCached(typeElement)
5556
is KtDynamicType -> KSDynamicReferenceImpl.getCached(Unit)
56-
else -> throw IllegalStateException()
57+
else -> throw IllegalStateException("Unexpected type element ${typeElement?.javaClass}, $ExceptionMessage")
5758
}
5859
}
5960

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/synthetic/KSPropertySetterSyntheticImpl.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class KSPropertySetterSyntheticImpl(val ksPropertyDeclaration: KSPropertyDeclara
3939
}
4040

4141
override val parameter: KSValueParameter by lazy {
42-
descriptor.valueParameters.singleOrNull()?.let { KSValueParameterDescriptorImpl.getCached(it) } ?: throw IllegalStateException()
42+
descriptor.valueParameters.singleOrNull()?.let { KSValueParameterDescriptorImpl.getCached(it) }
43+
?: throw IllegalStateException("Failed to resolve property type")
4344
}
4445

4546
override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/utils.kt

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package com.google.devtools.ksp.symbol.impl
2020

21+
import com.google.devtools.ksp.ExceptionMessage
2122
import com.intellij.lang.jvm.JvmModifier
2223
import com.intellij.psi.*
2324
import org.jetbrains.kotlin.descriptors.*
@@ -209,7 +210,7 @@ fun KtClassOrObject.getClassType(): ClassKind {
209210
this.isAnnotation() -> ClassKind.ANNOTATION_CLASS
210211
else -> ClassKind.CLASS
211212
}
212-
else -> throw IllegalStateException()
213+
else -> throw IllegalStateException("Unexpected psi type ${this.javaClass}, $ExceptionMessage")
213214
}
214215
}
215216

@@ -228,7 +229,7 @@ fun org.jetbrains.kotlin.types.Variance.toKSVariance(): Variance {
228229
org.jetbrains.kotlin.types.Variance.IN_VARIANCE -> Variance.CONTRAVARIANT
229230
org.jetbrains.kotlin.types.Variance.OUT_VARIANCE -> Variance.COVARIANT
230231
org.jetbrains.kotlin.types.Variance.INVARIANT -> Variance.INVARIANT
231-
else -> throw IllegalStateException()
232+
else -> throw IllegalStateException("Unexpected variance value $this, $ExceptionMessage")
232233
}
233234
}
234235

@@ -246,7 +247,7 @@ internal fun KotlinType.replaceTypeArguments(newArguments: List<KSTypeArgument>)
246247
val type = when (ksTypeArgument) {
247248
is KSTypeArgumentKtImpl, is KSTypeArgumentJavaImpl, is KSTypeArgumentLiteImpl -> ksTypeArgument.type!!
248249
is KSTypeArgumentDescriptorImpl -> return@mapIndexed ksTypeArgument.descriptor
249-
else -> throw IllegalStateException()
250+
else -> throw IllegalStateException("Unexpected psi for type argument: ${ksTypeArgument.javaClass}, $ExceptionMessage")
250251
}.toKotlinType()
251252

252253
TypeProjectionImpl(variance, type)

0 commit comments

Comments
 (0)