Skip to content

Commit

Permalink
Fixed @rpc annotation diagnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr3zee committed Jan 15, 2025
1 parent d55adbd commit 8b68a62
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.rpc.codegen
Expand All @@ -12,6 +12,10 @@ object FirRpcPredicates {
annotated(RpcClassId.rpcAnnotation.asSingleFqName()) // @Rpc
}

internal val rpcMeta = DeclarationPredicate.create {
metaAnnotated(RpcClassId.rpcAnnotation.asSingleFqName(), includeItself = true)
}

internal val checkedAnnotationMeta = DeclarationPredicate.create {
metaAnnotated(RpcClassId.checkedTypeAnnotation.asSingleFqName(), includeItself = false)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.rpc.codegen.checkers
Expand All @@ -9,7 +9,9 @@ import kotlinx.rpc.codegen.FirRpcPredicates
import kotlinx.rpc.codegen.checkers.diagnostics.FirRpcDiagnostics
import kotlinx.rpc.codegen.isRemoteService
import kotlinx.rpc.codegen.remoteServiceSupertypeSource
import kotlinx.rpc.codegen.rpcAnnotation
import kotlinx.rpc.codegen.rpcAnnotationSource
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.diagnostics.DiagnosticReporter
import org.jetbrains.kotlin.diagnostics.reportOn
import org.jetbrains.kotlin.fir.analysis.checkers.MppCheckerKind
Expand All @@ -18,6 +20,7 @@ import org.jetbrains.kotlin.fir.analysis.checkers.declaration.FirRegularClassChe
import org.jetbrains.kotlin.fir.declarations.FirRegularClass
import org.jetbrains.kotlin.fir.declarations.utils.isInterface
import org.jetbrains.kotlin.fir.extensions.predicateBasedProvider
import org.jetbrains.kotlin.fir.types.resolvedType

class FirRpcAnnotationChecker(private val ctx: FirCheckersContext) : FirRegularClassChecker(MppCheckerKind.Common) {
override fun check(
Expand All @@ -26,12 +29,15 @@ class FirRpcAnnotationChecker(private val ctx: FirCheckersContext) : FirRegularC
reporter: DiagnosticReporter,
) {
val rpcAnnotated = context.session.predicateBasedProvider.matches(FirRpcPredicates.rpc, declaration)
val rpcMetaAnnotated = context.session.predicateBasedProvider.matches(FirRpcPredicates.rpcMeta, declaration)

if (!declaration.isInterface && rpcAnnotated) {
if (!declaration.isInterface && declaration.classKind != ClassKind.ANNOTATION_CLASS && rpcMetaAnnotated) {
reporter.reportOn(
source = declaration.symbol.rpcAnnotationSource(context.session),
factory = FirRpcDiagnostics.WRONG_RPC_ANNOTATION_TARGET,
context = context,
a = declaration.symbol.rpcAnnotation(context.session)?.resolvedType
?: error("Unexpected unresolved annotation type for declaration: ${declaration.symbol.classId.asSingleFqName()}"),
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.rpc.codegen.checkers.diagnostics
Expand All @@ -19,7 +19,7 @@ import org.jetbrains.kotlin.psi.KtAnnotationEntry
object FirRpcDiagnostics {
val MISSING_RPC_ANNOTATION by error0<KtAnnotationEntry>()
val MISSING_SERIALIZATION_MODULE by warning0<KtAnnotationEntry>()
val WRONG_RPC_ANNOTATION_TARGET by error0<KtAnnotationEntry>()
val WRONG_RPC_ANNOTATION_TARGET by error1<KtAnnotationEntry, ConeKotlinType>()
val CHECKED_ANNOTATION_VIOLATION by error1<KtAnnotationEntry, ConeKotlinType>()
val NON_SUSPENDING_REQUEST_WITHOUT_STREAMING_RETURN_TYPE by error0<PsiElement>()
val AD_HOC_POLYMORPHISM_IN_RPC_SERVICE by error2<PsiElement, Int, Name>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.rpc.codegen.checkers.diagnostics
Expand Down Expand Up @@ -29,7 +29,8 @@ object RpcDiagnosticRendererFactory : BaseDiagnosticRendererFactory() {

put(
factory = FirRpcDiagnostics.WRONG_RPC_ANNOTATION_TARGET,
message = "@Rpc annotation is only applicable to interfaces.",
message = "@{0} annotation is only applicable to interfaces and annotation classes.",
rendererA = FirDiagnosticRenderers.RENDER_TYPE,
)

put(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.jetbrains.kotlin.test.TargetBackend;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.Ignore;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.io.File;
Expand All @@ -20,7 +21,7 @@
@SuppressWarnings("all")
@TestMetadata("src/testData/box")
@TestDataPath("$PROJECT_ROOT")
@Ignore
@Disabled("KRPC-137")
public class BoxTestGenerated extends AbstractBoxTest {
@Test
public void testAllFilesPresentInBox() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.util.KtTestUtil;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.Ignore;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.regex.Pattern;

/** This class is generated by {@link kotlinx.rpc.codegen.test.GenerateTestsKt}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("src/testData/diagnostics")
@TestDataPath("$PROJECT_ROOT")
@Ignore
@Disabled("KRPC-137")
public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
@Test
public void testAllFilesPresentInDiagnostics() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

@file:OptIn(ExperimentalRpcApi::class)
Expand Down Expand Up @@ -61,3 +61,15 @@ inline suspend fun <reified T : Any> fail(client: RpcClient, server: RpcServer,
serviceDescriptorOf<<!CHECKED_ANNOTATION_VIOLATION!>NotAService<!>>()
serviceDescriptorOf<<!CHECKED_ANNOTATION_VIOLATION!>T<!>>()
}

@Rpc
annotation class Grpc

@Grpc
interface MyGrpcService

@Grpc
class WrongGrpcTarget

@Rpc
class WrongRpcTarget

0 comments on commit 8b68a62

Please sign in to comment.