Skip to content

Commit 87179d8

Browse files
authored
Confusing compilation error when not specifying type argument to registerService #288 (#302)
1 parent 647fd34 commit 87179d8

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/FirCheckedAnnotationCheckers.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

55
package kotlinx.rpc.codegen.checkers
@@ -231,6 +231,7 @@ object FirCheckedAnnotationHelper {
231231
source = source,
232232
context = context,
233233
reporter = reporter,
234+
typeArgumentIndex = i,
234235
)
235236
}
236237

@@ -304,6 +305,7 @@ object FirCheckedAnnotationHelper {
304305
source: KtSourceElement?,
305306
context: CheckerContext,
306307
reporter: DiagnosticReporter,
308+
typeArgumentIndex: Int,
307309
) {
308310
for (annotationClass in annotations) {
309311
val hasCheckedAnnotation = hasCheckedAnnotation(
@@ -316,7 +318,9 @@ object FirCheckedAnnotationHelper {
316318
reporter.reportOn(
317319
source = source,
318320
factory = FirRpcDiagnostics.CHECKED_ANNOTATION_VIOLATION,
319-
a = annotationClass.defaultType(),
321+
a = typeArgumentIndex,
322+
b = annotationClass.defaultType(),
323+
c = classSymbol,
320324
context = context,
321325
)
322326
}

compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/diagnostics/FirRpcDiagnostics.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import org.jetbrains.kotlin.diagnostics.SourceElementPositioningStrategies
1111
import org.jetbrains.kotlin.diagnostics.error0
1212
import org.jetbrains.kotlin.diagnostics.error1
1313
import org.jetbrains.kotlin.diagnostics.error2
14+
import org.jetbrains.kotlin.diagnostics.error3
1415
import org.jetbrains.kotlin.diagnostics.rendering.RootDiagnosticRendererFactory
16+
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
1517
import org.jetbrains.kotlin.fir.types.ConeKotlinType
1618
import org.jetbrains.kotlin.name.Name
1719
import org.jetbrains.kotlin.psi.KtAnnotated
@@ -27,7 +29,7 @@ object FirRpcDiagnostics {
2729
val MISSING_RPC_ANNOTATION by error0<KtAnnotated>()
2830
val MISSING_SERIALIZATION_MODULE by error0<KtAnnotated>()
2931
val WRONG_RPC_ANNOTATION_TARGET by error1<KtAnnotated, ConeKotlinType>()
30-
val CHECKED_ANNOTATION_VIOLATION by error1<KtAnnotated, ConeKotlinType>()
32+
val CHECKED_ANNOTATION_VIOLATION by error3<KtAnnotated, Int, ConeKotlinType, FirBasedSymbol<*>>()
3133
val NON_SUSPENDING_REQUEST_WITHOUT_STREAMING_RETURN_TYPE by error0<KtElement>()
3234
val AD_HOC_POLYMORPHISM_IN_RPC_SERVICE by error2<KtElement, Int, Name>()
3335
val TYPE_PARAMETERS_IN_RPC_FUNCTION by error0<KtElement>(SourceElementPositioningStrategies.TYPE_PARAMETERS_LIST)

compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/diagnostics/RpcDiagnosticRendererFactory.kt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ object RpcDiagnosticRendererFactory : BaseDiagnosticRendererFactory() {
3535

3636
put(
3737
factory = FirRpcDiagnostics.CHECKED_ANNOTATION_VIOLATION,
38-
message = "Type argument marked with {0} annotation " +
39-
"must be annotated with {0} or an annotation annotated with {0}.",
40-
rendererA = FirDiagnosticRenderers.RENDER_TYPE,
38+
message = "{0} type argument is marked with @{1} annotation, but inferred type is {2}. " +
39+
"Provide a type that is marked with @{1} annotation explicitly " +
40+
"or remove the annotation from the type argument declaration.",
41+
rendererA = Renderer { it.indexPositionSpelled() },
42+
rendererB = FirDiagnosticRenderers.RENDER_TYPE,
43+
rendererC = FirDiagnosticRenderers.SYMBOL,
4144
)
4245

4346
put(
@@ -64,6 +67,18 @@ object RpcDiagnosticRendererFactory : BaseDiagnosticRendererFactory() {
6467
}
6568
}
6669

70+
private fun Int.indexPositionSpelled(): String {
71+
val padded = this + 1
72+
val suffix = when (padded % 10) {
73+
1 -> "st"
74+
2 -> "nd"
75+
3 -> "rd"
76+
else -> "th"
77+
}
78+
79+
return "$padded$suffix"
80+
}
81+
6782
class RpcStrictModeDiagnosticRendererFactory(
6883
private val diagnostics: FirRpcStrictModeDiagnostics,
6984
) : BaseDiagnosticRendererFactory() {

0 commit comments

Comments
 (0)