Allow Native @Throws on override when the same list is specified
This commit is contained in:
+4
-1
@@ -12,7 +12,10 @@ import org.jetbrains.kotlin.diagnostics.rendering.Renderers
|
||||
private val DIAGNOSTIC_FACTORY_TO_RENDERER by lazy {
|
||||
DiagnosticFactoryToRendererMap("Native").apply {
|
||||
put(ErrorsNative.THROWS_LIST_EMPTY, "@Throws must have non-empty class list")
|
||||
put(ErrorsNative.THROWS_ON_OVERRIDE, "@Throws is prohibited for overridden members")
|
||||
put(
|
||||
ErrorsNative.INCOMPATIBLE_THROWS_OVERRIDE, "Member overrides different @Throws filter from {0}",
|
||||
Renderers.NAME
|
||||
)
|
||||
put(
|
||||
ErrorsNative.INCOMPATIBLE_THROWS_INHERITED, "Member inherits different @Throws filters from {0}",
|
||||
Renderers.commaSeparated(Renderers.NAME)
|
||||
|
||||
@@ -17,7 +17,7 @@ object ErrorsNative {
|
||||
@JvmField
|
||||
val THROWS_LIST_EMPTY = DiagnosticFactory0.create<KtElement>(Severity.ERROR)
|
||||
@JvmField
|
||||
val THROWS_ON_OVERRIDE = DiagnosticFactory0.create<KtElement>(Severity.ERROR)
|
||||
val INCOMPATIBLE_THROWS_OVERRIDE = DiagnosticFactory1.create<KtElement, DeclarationDescriptor>(Severity.ERROR)
|
||||
@JvmField
|
||||
val INCOMPATIBLE_THROWS_INHERITED = DiagnosticFactory1.create<KtDeclaration, Collection<DeclarationDescriptor>>(Severity.ERROR)
|
||||
|
||||
|
||||
+58
-20
@@ -11,48 +11,81 @@ import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||
import org.jetbrains.kotlin.psi.KtElement
|
||||
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
|
||||
import org.jetbrains.kotlin.resolve.checkers.DeclarationChecker
|
||||
import org.jetbrains.kotlin.resolve.checkers.DeclarationCheckerContext
|
||||
import org.jetbrains.kotlin.resolve.constants.ArrayValue
|
||||
import org.jetbrains.kotlin.resolve.constants.ConstantValue
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.firstArgument
|
||||
import org.jetbrains.kotlin.resolve.findOriginalTopMostOverriddenDescriptors
|
||||
import org.jetbrains.kotlin.utils.DFS
|
||||
|
||||
object NativeThrowsChecker : DeclarationChecker {
|
||||
private val throwsFqName = FqName("kotlin.native.Throws")
|
||||
|
||||
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
|
||||
checkForIncompatibleMultipleInheritance(declaration, descriptor, context)
|
||||
val throwsAnnotation = descriptor.annotations.findAnnotation(throwsFqName)
|
||||
val reportLocation = throwsAnnotation?.let { DescriptorToSourceUtils.getSourceFromAnnotation(it) } ?: declaration
|
||||
|
||||
val throwsAnnotation = descriptor.annotations.findAnnotation(throwsFqName) ?: return
|
||||
val element = DescriptorToSourceUtils.getSourceFromAnnotation(throwsAnnotation) ?: declaration
|
||||
if (!checkInheritance(declaration, descriptor, context, throwsAnnotation, reportLocation)) return
|
||||
|
||||
if (descriptor is CallableMemberDescriptor && descriptor.overriddenDescriptors.isNotEmpty()) {
|
||||
context.trace.report(ErrorsNative.THROWS_ON_OVERRIDE.on(element))
|
||||
return
|
||||
}
|
||||
|
||||
if (throwsAnnotation.getVariadicArguments().isEmpty()) {
|
||||
context.trace.report(ErrorsNative.THROWS_LIST_EMPTY.on(element))
|
||||
if (throwsAnnotation != null && throwsAnnotation.getVariadicArguments().isEmpty()) {
|
||||
context.trace.report(ErrorsNative.THROWS_LIST_EMPTY.on(reportLocation))
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkForIncompatibleMultipleInheritance(
|
||||
private fun checkInheritance(
|
||||
declaration: KtDeclaration,
|
||||
descriptor: DeclarationDescriptor,
|
||||
context: DeclarationCheckerContext
|
||||
) {
|
||||
if (descriptor !is CallableMemberDescriptor) return
|
||||
context: DeclarationCheckerContext,
|
||||
throwsAnnotation: AnnotationDescriptor?,
|
||||
reportLocation: KtElement
|
||||
): Boolean {
|
||||
if (descriptor !is CallableMemberDescriptor || descriptor.overriddenDescriptors.isEmpty()) return true
|
||||
|
||||
if (descriptor.overriddenDescriptors.size < 2) return // No multiple inheritance here.
|
||||
val inherited = findInheritedThrows(descriptor).entries.distinctBy { it.value }
|
||||
|
||||
val incompatible = descriptor.findOriginalTopMostOverriddenDescriptors()
|
||||
.distinctBy { it.annotations.findAnnotation(throwsFqName)?.getVariadicArguments()?.toSet() }
|
||||
if (inherited.size >= 2) {
|
||||
context.trace.report(ErrorsNative.INCOMPATIBLE_THROWS_INHERITED.on(declaration, inherited.map { it.key.containingDeclaration }))
|
||||
return false
|
||||
}
|
||||
|
||||
if (incompatible.size < 2) return
|
||||
if (throwsAnnotation == null) return true
|
||||
|
||||
context.trace.report(ErrorsNative.INCOMPATIBLE_THROWS_INHERITED.on(declaration, incompatible.map { it.containingDeclaration }))
|
||||
val (overriddenMember, overriddenThrows) = inherited.firstOrNull()
|
||||
?: return true // Should not happen though.
|
||||
|
||||
if (decodeThrowsFilter(throwsAnnotation) != overriddenThrows) {
|
||||
context.trace.report(ErrorsNative.INCOMPATIBLE_THROWS_OVERRIDE.on(reportLocation, overriddenMember.containingDeclaration))
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
private fun findInheritedThrows(descriptor: CallableMemberDescriptor): Map<CallableMemberDescriptor, ThrowsFilter> {
|
||||
val result = mutableMapOf<CallableMemberDescriptor, ThrowsFilter>()
|
||||
|
||||
DFS.dfs(
|
||||
descriptor.overriddenDescriptors,
|
||||
{ current -> current.overriddenDescriptors },
|
||||
object : DFS.AbstractNodeHandler<CallableMemberDescriptor, Unit>() {
|
||||
override fun beforeChildren(current: CallableMemberDescriptor): Boolean {
|
||||
val throwsAnnotation = current.annotations.findAnnotation(throwsFqName).takeIf { current.kind.isReal }
|
||||
return if (throwsAnnotation == null && current.overriddenDescriptors.isNotEmpty()) {
|
||||
// Visit overridden members:
|
||||
true
|
||||
} else {
|
||||
// Take current and ignore overridden:
|
||||
result[current.original] = decodeThrowsFilter(throwsAnnotation)
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
override fun result() {}
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
private fun AnnotationDescriptor.getVariadicArguments(): List<ConstantValue<*>> {
|
||||
@@ -60,4 +93,9 @@ object NativeThrowsChecker : DeclarationChecker {
|
||||
return argument.value
|
||||
}
|
||||
|
||||
private fun decodeThrowsFilter(throwsAnnotation: AnnotationDescriptor?) =
|
||||
ThrowsFilter(throwsAnnotation?.getVariadicArguments()?.toSet())
|
||||
|
||||
private data class ThrowsFilter(val classes: Set<ConstantValue<*>>?)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user