Report error on expression of nullable type in class literal

#KT-15740 Fixed
This commit is contained in:
Alexander Udalov
2017-01-16 14:32:58 +03:00
parent 6a352a1da7
commit d02404b07b
10 changed files with 93 additions and 10 deletions
@@ -631,6 +631,7 @@ public interface Errors {
DiagnosticFactory0<KtExpression> CLASS_LITERAL_LHS_NOT_A_CLASS = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtExpression> ARRAY_CLASS_LITERAL_REQUIRES_ARGUMENT = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtExpression> NULLABLE_TYPE_IN_CLASS_LITERAL_LHS = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<KtExpression, KotlinType> EXPRESSION_OF_NULLABLE_TYPE_IN_CLASS_LITERAL_LHS = DiagnosticFactory1.create(ERROR);
// Destructuring-declarations
@@ -880,6 +880,7 @@ public class DefaultErrorMessages {
MAP.put(CLASS_LITERAL_LHS_NOT_A_CLASS, "Only classes are allowed on the left hand side of a class literal");
MAP.put(ARRAY_CLASS_LITERAL_REQUIRES_ARGUMENT, "Array class literal requires a type argument, please specify one in angle brackets");
MAP.put(NULLABLE_TYPE_IN_CLASS_LITERAL_LHS, "Type in a class literal must not be nullable");
MAP.put(EXPRESSION_OF_NULLABLE_TYPE_IN_CLASS_LITERAL_LHS, "Expression in a class literal has a nullable type ''{0}'', use !! to make the type non-nullable", RENDER_TYPE);
//Inline
MAP.put(NON_PUBLIC_CALL_FROM_PUBLIC_INLINE, "Public-API inline function cannot access non-public-API ''{0}''", SHORT_NAMES_IN_TYPES, SHORT_NAMES_IN_TYPES);
@@ -66,12 +66,10 @@ open class KotlinScriptDefinitionFromAnnotatedTemplate(
private val acceptedAnnotations: List<KClass<out Annotation>> by lazy {
val resolveMethod = ScriptDependenciesResolver::resolve
val resolverMethodAnnotations =
resolver::class.memberFunctions.find {
it.name == resolveMethod.name &&
sameSignature(it, resolveMethod)
}
?.annotations
?.filterIsInstance<AcceptedAnnotations>()
resolver?.let { it::class }?.memberFunctions?.find { function ->
function.name == resolveMethod.name &&
sameSignature(function, resolveMethod)
}?.annotations?.filterIsInstance<AcceptedAnnotations>()
resolverMethodAnnotations?.flatMap {
val v = it.supportedAnnotationClasses
v.toList() // TODO: inline after KT-9453 is resolved (now it fails with "java.lang.Class cannot be cast to kotlin.reflect.KClass")
@@ -50,6 +50,8 @@ import org.jetbrains.kotlin.resolve.source.toSourceElement
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.TypeUtils.NO_EXPECTED_TYPE
import org.jetbrains.kotlin.types.expressions.typeInfoFactory.createTypeInfo
import org.jetbrains.kotlin.types.typeUtil.builtIns
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
import org.jetbrains.kotlin.types.typeUtil.makeNullable
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import java.lang.UnsupportedOperationException
@@ -105,12 +107,19 @@ class DoubleColonExpressionResolver(
}
private fun checkClassLiteral(c: ExpressionTypingContext, expression: KtClassLiteralExpression, result: DoubleColonLHS) {
val type = result.type
if (result is DoubleColonLHS.Expression) {
if (!result.isObject) reportUnsupportedIfNeeded(expression, c)
if (!result.isObject) {
if (!type.isSubtypeOf(type.builtIns.anyType)) {
c.trace.report(EXPRESSION_OF_NULLABLE_TYPE_IN_CLASS_LITERAL_LHS.on(expression.receiverExpression!!, type))
}
reportUnsupportedIfNeeded(expression, c)
}
return
}
val type = (result as DoubleColonLHS.Type).type
result as DoubleColonLHS.Type
val descriptor = type.constructor.declarationDescriptor
if (result.possiblyBareType.isBare) {
if (descriptor is ClassDescriptor && KotlinBuiltIns.isNonPrimitiveArray(descriptor)) {