Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix error types handling #1533

Merged
merged 2 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.google.devtools.ksp.KSObjectCache
import com.google.devtools.ksp.getClassDeclarationByName
import com.google.devtools.ksp.impl.ResolverAAImpl
import com.google.devtools.ksp.impl.symbol.kotlin.KSErrorType
import com.google.devtools.ksp.impl.symbol.kotlin.KSTypeImpl
import com.google.devtools.ksp.impl.symbol.kotlin.KSTypeReferenceImpl
import com.google.devtools.ksp.impl.symbol.kotlin.KSValueArgumentImpl
import com.google.devtools.ksp.impl.symbol.kotlin.analyze
Expand Down Expand Up @@ -38,9 +37,12 @@ import com.intellij.psi.PsiReference
import com.intellij.psi.PsiType
import com.intellij.psi.impl.source.PsiAnnotationMethodImpl
import org.jetbrains.kotlin.analysis.api.annotations.KtNamedAnnotationValue
import org.jetbrains.kotlin.analysis.api.components.buildClassType
import org.jetbrains.kotlin.analysis.api.symbols.KtClassOrObjectSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtSymbolOrigin
import org.jetbrains.kotlin.analysis.api.types.KtType
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName

class KSAnnotationJavaImpl private constructor(private val psi: PsiAnnotation) : KSAnnotation {
companion object : KSObjectCache<PsiAnnotation, KSAnnotationJavaImpl>() {
Expand All @@ -49,8 +51,10 @@ class KSAnnotationJavaImpl private constructor(private val psi: PsiAnnotation) :
}

private val type: KtType by lazy {
(ResolverAAImpl.instance.getClassDeclarationByName(psi.qualifiedName!!)!!.asStarProjectedType() as KSTypeImpl)
.type
// TODO: local class annotations?
analyze {
buildClassType(ClassId.topLevel(FqName(psi.qualifiedName!!)))
}
}

override val annotationType: KSTypeReference by lazy {
Expand Down
44 changes: 44 additions & 0 deletions kotlin-analysis-api/testData/errorTypes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2022 Google LLC
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// WITH_RUNTIME
// TEST PROCESSOR: ErrorTypeProcessor
// EXPECTED:
// kotlin.collections.MutableMap
// kotlin.collections.Map
// kotlin.String
// ERROR TYPE
// errorInComponent is assignable from errorAtTop: true
// errorInComponent is assignable from class C: false
// Any is assignable from errorInComponent: true
// class C is assignable from errorInComponent: false
// Any is assignable from class C: true
// Cls's super type is Error type: true
// Cls's annotation is Error type: true
// END
// FILE: a.kt
class C {
val errorAtTop = mutableMapOf<String, NonExistType, Int>()
val errorInComponent: Map<String, NonExistType>
}

// FILE: Cls.java

@NonExistingAnnotation
public class Cls extends NonExistType {

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@ class ErrorTypeProcessor : AbstractTestProcessor() {
errorInComponent.type.resolve().arguments.forEach { result.add(it.type!!.resolve().print()) }
result.add(
"errorInComponent is assignable from errorAtTop: ${
errorAtTop.type.resolve().isAssignableFrom(errorAtTop.type.resolve())
errorInComponent.type.resolve().isAssignableFrom(errorAtTop.type.resolve())
}"
)
result.add(
"errorInComponent is assignable from class C: ${
errorAtTop.type.resolve().isAssignableFrom(classC.asStarProjectedType())
errorInComponent.type.resolve().isAssignableFrom(classC.asStarProjectedType())
}"
)
result.add(
"Any is assignable from errorInComponent: ${
resolver.builtIns.anyType.isAssignableFrom(errorAtTop.type.resolve())
resolver.builtIns.anyType.isAssignableFrom(errorInComponent.type.resolve())
}"
)
result.add(
"class C is assignable from errorInComponent: ${
classC.asStarProjectedType().isAssignableFrom(errorAtTop.type.resolve())
classC.asStarProjectedType().isAssignableFrom(errorInComponent.type.resolve())
}"
)
result.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,10 @@ class KSPAATest : AbstractKSPAATest() {
runTest("../test-utils/testData/api/equivalentJavaWildcards.kt")
}

@Disabled
@TestMetadata("errorTypes.kt")
@Test
fun testErrorTypes() {
runTest("../test-utils/testData/api/errorTypes.kt")
runTest("../kotlin-analysis-api/testData/errorTypes.kt")
}

@TestMetadata("functionTypeAlias.kt")
Expand Down
2 changes: 1 addition & 1 deletion test-utils/testData/api/errorTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
// ERROR TYPE
// errorInComponent is assignable from errorAtTop: false
// errorInComponent is assignable from class C: false
// Any is assignable from errorInComponent: false
// Any is assignable from errorInComponent: true
// class C is assignable from errorInComponent: false
// Any is assignable from class C: true
// Cls's super type is Error type: true
Expand Down
Loading