From 3e46507ea730a850c982b8126d2604cc9d4845b3 Mon Sep 17 00:00:00 2001 From: Zac Sweers Date: Thu, 24 Oct 2024 15:24:00 -0400 Subject: [PATCH] Improve error messaging for error types used as annotation arguments (#80) --- CHANGELOG.md | 2 ++ compiler-utils/api/compiler-utils.api | 1 + .../compiler/internal/ksp/KSAnnotationExtensions.kt | 12 +++++++++++- .../squareup/anvil/compiler/internal/ksp/KspUtil.kt | 11 ++++++----- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed99a75f1..a60c8c15c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ **Unreleased** -------------- +- **Enhancement**: Improve error messaging for error types used as annotation arguments. + 0.3.3 ----- diff --git a/compiler-utils/api/compiler-utils.api b/compiler-utils/api/compiler-utils.api index cb02e60a6..89b8c17ca 100644 --- a/compiler-utils/api/compiler-utils.api +++ b/compiler-utils/api/compiler-utils.api @@ -112,6 +112,7 @@ public final class com/squareup/anvil/compiler/internal/ksp/KspUtilKt { public static final fun anySymbolsWithAnnotations (Lcom/google/devtools/ksp/processing/Resolver;Ljava/util/Collection;)Z public static final fun atLeastOneAnnotation (Lcom/google/devtools/ksp/symbol/KSClassDeclaration;Ljava/lang/String;Lcom/google/devtools/ksp/symbol/KSType;)Ljava/util/List; public static synthetic fun atLeastOneAnnotation$default (Lcom/google/devtools/ksp/symbol/KSClassDeclaration;Ljava/lang/String;Lcom/google/devtools/ksp/symbol/KSType;ILjava/lang/Object;)Ljava/util/List; + public static final fun checkErrorType (Lcom/google/devtools/ksp/symbol/KSType;Lcom/google/devtools/ksp/symbol/KSNode;)Lcom/google/devtools/ksp/symbol/KSType; public static final fun contextualToClassName (Lcom/google/devtools/ksp/symbol/KSType;Lcom/google/devtools/ksp/symbol/KSNode;)Lcom/squareup/kotlinpoet/ClassName; public static final fun contextualToClassName (Lcom/google/devtools/ksp/symbol/KSTypeReference;)Lcom/squareup/kotlinpoet/ClassName; public static final fun contextualToTypeName (Lcom/google/devtools/ksp/symbol/KSType;Lcom/google/devtools/ksp/symbol/KSNode;Lcom/squareup/kotlinpoet/ksp/TypeParameterResolver;)Lcom/squareup/kotlinpoet/TypeName; diff --git a/compiler-utils/src/main/java/com/squareup/anvil/compiler/internal/ksp/KSAnnotationExtensions.kt b/compiler-utils/src/main/java/com/squareup/anvil/compiler/internal/ksp/KSAnnotationExtensions.kt index 32711169c..162414f7c 100644 --- a/compiler-utils/src/main/java/com/squareup/anvil/compiler/internal/ksp/KSAnnotationExtensions.kt +++ b/compiler-utils/src/main/java/com/squareup/anvil/compiler/internal/ksp/KSAnnotationExtensions.kt @@ -166,7 +166,17 @@ public fun KSAnnotation.classNameArgumentAt( public inline fun KSAnnotation.argumentOfTypeAt( name: String, ): T? { - return argumentOfTypeWithMapperAt(name) { _, value -> + return argumentOfTypeWithMapperAt(name) { arg, value -> + when { + value is KSType -> { + value.checkErrorType(arg) + } + value is List<*> && value.firstOrNull() is KSType -> { + for (element in value) { + (element as KSType).checkErrorType(arg) + } + } + } value } } diff --git a/compiler-utils/src/main/java/com/squareup/anvil/compiler/internal/ksp/KspUtil.kt b/compiler-utils/src/main/java/com/squareup/anvil/compiler/internal/ksp/KspUtil.kt index 81906fabd..da51d3ce2 100644 --- a/compiler-utils/src/main/java/com/squareup/anvil/compiler/internal/ksp/KspUtil.kt +++ b/compiler-utils/src/main/java/com/squareup/anvil/compiler/internal/ksp/KspUtil.kt @@ -387,18 +387,19 @@ public fun KSType.contextualToClassName(origin: KSNode): ClassName { return toClassName() } -private fun KSType.checkErrorType(origin: KSNode) { +@PublishedApi +internal fun KSType.checkErrorType(origin: KSNode): KSType { val (type, node) = if (isError) { this to origin } else { arguments.asSequence() - .mapNotNull { - it.type?.let { - it.resolve() to it + .mapNotNull { arg -> + arg.type?.let { argType -> + argType.resolve() to argType } } .firstOrNull { it.first.isError } - } ?: return + } ?: return this val message = buildString { appendLine(