Skip to content

Commit

Permalink
Improve error messaging for error types used as annotation arguments (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ZacSweers authored Oct 24, 2024
1 parent 07bc75e commit 3e46507
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
**Unreleased**
--------------

- **Enhancement**: Improve error messaging for error types used as annotation arguments.

0.3.3
-----

Expand Down
1 change: 1 addition & 0 deletions compiler-utils/api/compiler-utils.api
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,17 @@ public fun KSAnnotation.classNameArgumentAt(
public inline fun <reified T> KSAnnotation.argumentOfTypeAt(
name: String,
): T? {
return argumentOfTypeWithMapperAt<T, T>(name) { _, value ->
return argumentOfTypeWithMapperAt<T, T>(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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 3e46507

Please sign in to comment.