Added special error for incorrect KClass type in signatures.

This commit is contained in:
Stanislav Erokhin
2017-01-16 05:33:52 +03:00
parent 3d70d73964
commit 0ef1234f2e
11 changed files with 129 additions and 9 deletions
@@ -414,6 +414,11 @@ public interface Errors {
DiagnosticFactory1<KtDeclaration, Collection<KotlinType>> AMBIGUOUS_ANONYMOUS_TYPE_INFERRED =
DiagnosticFactory1.create(ERROR, DECLARATION_SIGNATURE);
DiagnosticFactory0<KtNamedDeclaration>
KCLASS_WITH_NULLABLE_ARGUMENT_IN_SIGNATURE = DiagnosticFactory0.create(ERROR, PositioningStrategies.DECLARATION_NAME);
DiagnosticFactory1<KtNamedDeclaration, TypeParameterDescriptor>
KCLASS_WITH_NULLABLE_TYPE_PARAMETER_IN_SIGNATURE = DiagnosticFactory1.create(ERROR, PositioningStrategies.DECLARATION_NAME);
// Property-specific
DiagnosticFactory2<KtNamedDeclaration, PropertyDescriptor, PropertyDescriptor> VAR_OVERRIDDEN_BY_VAL =
@@ -859,6 +859,9 @@ public class DefaultErrorMessages {
MAP.put(DATA_CLASS_NOT_PROPERTY_PARAMETER, "Data class primary constructor must have only property (val / var) parameters");
MAP.put(AMBIGUOUS_ANONYMOUS_TYPE_INFERRED, "Right-hand side has anonymous type. Please specify type explicitly", TO_STRING);
MAP.put(KCLASS_WITH_NULLABLE_ARGUMENT_IN_SIGNATURE, "Please specify type explicitly");
MAP.put(KCLASS_WITH_NULLABLE_TYPE_PARAMETER_IN_SIGNATURE,
"Please add upper bound Any for type parameter ''{0}'' or specify return type explicitly", NAME);
MAP.put(EXTENSION_IN_CLASS_REFERENCE_NOT_ALLOWED,
"''{0}'' is a member and an extension at the same time. References to such elements are not allowed", NAME);
@@ -74,7 +74,8 @@ private val DEFAULT_DECLARATION_CHECKERS = listOf(
SinceKotlinAnnotationValueChecker,
ReifiedTypeParameterAnnotationChecker(),
DynamicReceiverChecker,
DelegationChecker()
DelegationChecker(),
KClassWithIncorrectTypeArgumentChecker
)
private val DEFAULT_CALL_CHECKERS = listOf(
@@ -0,0 +1,70 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* 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.
*/
package org.jetbrains.kotlin.resolve.checkers
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.diagnostics.DiagnosticSink
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.psi.KtCallableDeclaration
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.types.UnwrappedType
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
import org.jetbrains.kotlin.types.typeUtil.builtIns
import org.jetbrains.kotlin.types.typeUtil.contains
object KClassWithIncorrectTypeArgumentChecker : SimpleDeclarationChecker {
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, diagnosticHolder: DiagnosticSink, bindingContext: BindingContext) {
if (descriptor !is CallableMemberDescriptor || descriptor.visibility == Visibilities.LOCAL) return
if (declaration !is KtCallableDeclaration || declaration.typeReference != null) return
// prevent duplicate reporting
if (descriptor is PropertyAccessorDescriptor) return
val returnType = descriptor.returnType ?: return
var typeParameterWithoutNotNullableUpperBound: TypeParameterDescriptor? = null
val thereIsBadKClassType = returnType.contains {
val kClassWithBadArgument = it.isKClassWithBadArgument()
if (kClassWithBadArgument) {
it.arguments.singleOrNull()?.type?.constructor?.declarationDescriptor?.let {
if (it is TypeParameterDescriptor && it.containingDeclaration == descriptor) {
typeParameterWithoutNotNullableUpperBound = it
}
}
}
kClassWithBadArgument
}
if (typeParameterWithoutNotNullableUpperBound != null) {
diagnosticHolder.report(Errors.KCLASS_WITH_NULLABLE_TYPE_PARAMETER_IN_SIGNATURE.on(declaration, typeParameterWithoutNotNullableUpperBound!!))
}
else if (thereIsBadKClassType) {
diagnosticHolder.report(Errors.KCLASS_WITH_NULLABLE_ARGUMENT_IN_SIGNATURE.on(declaration))
}
}
private fun UnwrappedType.isKClassWithBadArgument(): Boolean {
val argumentType = arguments.singleOrNull()?.let { if (it.isStarProjection) null else it.type.unwrap() } ?: return false
val klass = (constructor.declarationDescriptor as? ClassDescriptor) ?: return false
return KotlinBuiltIns.isKClass(klass) && !KotlinTypeChecker.DEFAULT.isSubtypeOf(argumentType, argumentType.builtIns.anyType)
}
}