Do not report KCLASS_WITH_NULLABLE_ARGUMENT_IN_SIGNATURE anymore

It was only reported in already erroneous cases
This commit is contained in:
Alexander Udalov
2017-01-16 14:49:27 +03:00
parent d02404b07b
commit 4f36376291
8 changed files with 17 additions and 24 deletions
@@ -421,8 +421,6 @@ 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);
@@ -865,9 +865,8 @@ 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);
"Declaration has an inconsistent return type. 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);
@@ -24,9 +24,9 @@ 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
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
object KClassWithIncorrectTypeArgumentChecker : SimpleDeclarationChecker {
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, diagnosticHolder: DiagnosticSink, bindingContext: BindingContext) {
@@ -40,31 +40,27 @@ object KClassWithIncorrectTypeArgumentChecker : SimpleDeclarationChecker {
val returnType = descriptor.returnType ?: return
var typeParameterWithoutNotNullableUpperBound: TypeParameterDescriptor? = null
val thereIsBadKClassType = returnType.contains {
val kClassWithBadArgument = it.isKClassWithBadArgument()
returnType.contains { type ->
val kClassWithBadArgument = type.isKClassWithBadArgument()
if (kClassWithBadArgument) {
it.arguments.singleOrNull()?.type?.constructor?.declarationDescriptor?.let {
type.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
val klass = constructor.declarationDescriptor as? ClassDescriptor ?: return false
return KotlinBuiltIns.isKClass(klass) && !KotlinTypeChecker.DEFAULT.isSubtypeOf(argumentType, argumentType.builtIns.anyType)
return KotlinBuiltIns.isKClass(klass) && !argumentType.isSubtypeOf(argumentType.builtIns.anyType)
}
}
}
@@ -23,8 +23,8 @@ class Test {
fun <T> List<T>.testCallable4(): () -> Unit = <!RESERVED_SYNTAX_IN_CALLABLE_REFERENCE_LHS!>b<T><!>?::<!UNSAFE_CALL!>foo<!>
fun <T> List<T>.testClassLiteral1() = <!RESERVED_SYNTAX_IN_CALLABLE_REFERENCE_LHS!>a<T><!>::class
fun <T> List<T>.<!KCLASS_WITH_NULLABLE_ARGUMENT_IN_SIGNATURE!>testClassLiteral2<!>() = <!RESERVED_SYNTAX_IN_CALLABLE_REFERENCE_LHS, EXPRESSION_OF_NULLABLE_TYPE_IN_CLASS_LITERAL_LHS!>b<!>?::class
fun <T> List<T>.<!KCLASS_WITH_NULLABLE_ARGUMENT_IN_SIGNATURE!>testClassLiteral3<!>() = <!RESERVED_SYNTAX_IN_CALLABLE_REFERENCE_LHS, EXPRESSION_OF_NULLABLE_TYPE_IN_CLASS_LITERAL_LHS!>b<T, Any><!>::class
fun <T> List<T>.testClassLiteral2() = <!RESERVED_SYNTAX_IN_CALLABLE_REFERENCE_LHS, EXPRESSION_OF_NULLABLE_TYPE_IN_CLASS_LITERAL_LHS!>b<!>?::class
fun <T> List<T>.testClassLiteral3() = <!RESERVED_SYNTAX_IN_CALLABLE_REFERENCE_LHS, EXPRESSION_OF_NULLABLE_TYPE_IN_CLASS_LITERAL_LHS!>b<T, Any><!>::class
fun <T> List<T>.testUnresolved1() = <!UNRESOLVED_REFERENCE!>unresolved<!><T>::<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>foo<!>
fun <T> List<T>.testUnresolved2() = <!RESERVED_SYNTAX_IN_CALLABLE_REFERENCE_LHS!>a<<!UNRESOLVED_REFERENCE!>unresolved<!>><!>::foo
@@ -2,11 +2,11 @@
class A
val <!KCLASS_WITH_NULLABLE_ARGUMENT_IN_SIGNATURE!>a1<!> = <!NULLABLE_TYPE_IN_CLASS_LITERAL_LHS!>A?::class<!>
val <!KCLASS_WITH_NULLABLE_ARGUMENT_IN_SIGNATURE!>a2<!> = <!NULLABLE_TYPE_IN_CLASS_LITERAL_LHS!>A??::class<!>
val a1 = <!NULLABLE_TYPE_IN_CLASS_LITERAL_LHS!>A?::class<!>
val a2 = <!NULLABLE_TYPE_IN_CLASS_LITERAL_LHS!>A??::class<!>
val <!KCLASS_WITH_NULLABLE_ARGUMENT_IN_SIGNATURE!>l1<!> = <!NULLABLE_TYPE_IN_CLASS_LITERAL_LHS!>List<String>?::class<!>
val <!KCLASS_WITH_NULLABLE_ARGUMENT_IN_SIGNATURE!>l2<!> = <!NULLABLE_TYPE_IN_CLASS_LITERAL_LHS!>List?::class<!>
val l1 = <!NULLABLE_TYPE_IN_CLASS_LITERAL_LHS!>List<String>?::class<!>
val l2 = <!NULLABLE_TYPE_IN_CLASS_LITERAL_LHS!>List?::class<!>
fun <T : Any> foo() {
val t1 = <!TYPE_PARAMETER_AS_REIFIED!>T::class<!>
@@ -2,7 +2,7 @@ typealias TString = String
fun f1() = TString::class
typealias TNullableString = String?
fun <!KCLASS_WITH_NULLABLE_ARGUMENT_IN_SIGNATURE!>f2<!>() = <!NULLABLE_TYPE_IN_CLASS_LITERAL_LHS!>TNullableString::class<!>
fun f2() = <!NULLABLE_TYPE_IN_CLASS_LITERAL_LHS!>TNullableString::class<!>
typealias TNullableTString = TString?
typealias TTNullableTString = TNullableTString
@@ -10,7 +10,7 @@ fun <T> <!KCLASS_WITH_NULLABLE_TYPE_PARAMETER_IN_SIGNATURE!>test5<!>() = listOf(
fun <T> test6(): kotlin.reflect.KClass<<!UPPER_BOUND_VIOLATED!>T<!>> = T::class
fun <T> test7(): kotlin.reflect.KClass<*> = T::class
fun <!KCLASS_WITH_NULLABLE_ARGUMENT_IN_SIGNATURE!>test8<!>() = <!NULLABLE_TYPE_IN_CLASS_LITERAL_LHS!>String?::class<!>
fun test8() = <!NULLABLE_TYPE_IN_CLASS_LITERAL_LHS!>String?::class<!>
fun <T> listOf(e: T): List<T> = null!!
@@ -9,6 +9,6 @@ val ok3 = O::class
val fail1 = <!UNSUPPORTED_FEATURE!>""<!>::class
val <!KCLASS_WITH_NULLABLE_ARGUMENT_IN_SIGNATURE!>fail2<!> = <!NULLABLE_TYPE_IN_CLASS_LITERAL_LHS!>String?::class<!>
val fail2 = <!NULLABLE_TYPE_IN_CLASS_LITERAL_LHS!>String?::class<!>
val fail3 = <!UNSUPPORTED_FEATURE!>(C)<!>::class
val fail4 = <!UNSUPPORTED_FEATURE!>(C.Companion)<!>::class