Report error on nullable type alias in class literal

#KT-15736 Fixed
This commit is contained in:
Alexander Udalov
2017-01-16 12:50:45 +03:00
parent 284b9d2ba6
commit 6a352a1da7
9 changed files with 53 additions and 21 deletions
@@ -630,6 +630,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);
// Destructuring-declarations
@@ -879,6 +879,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");
//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);
@@ -50,6 +50,7 @@ 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.makeNullable
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import java.lang.UnsupportedOperationException
import java.util.*
@@ -110,24 +111,21 @@ class DoubleColonExpressionResolver(
}
val type = (result as DoubleColonLHS.Type).type
val reportError: Boolean
val descriptor = type.constructor.declarationDescriptor
if (result.possiblyBareType.isBare) {
val descriptor = type.constructor.declarationDescriptor
if (descriptor is ClassDescriptor && KotlinBuiltIns.isNonPrimitiveArray(descriptor)) {
c.trace.report(ARRAY_CLASS_LITERAL_REQUIRES_ARGUMENT.on(expression))
}
reportError = false
}
else {
reportError = !isAllowedInClassLiteral(type)
}
val typeParameterDescriptor = TypeUtils.getTypeParameterDescriptorOrNull(type)
if (type is SimpleType && !type.isMarkedNullable && typeParameterDescriptor != null && !typeParameterDescriptor.isReified) {
c.trace.report(TYPE_PARAMETER_AS_REIFIED.on(expression, typeParameterDescriptor))
if (type is SimpleType && !type.isMarkedNullable && descriptor is TypeParameterDescriptor && !descriptor.isReified) {
c.trace.report(TYPE_PARAMETER_AS_REIFIED.on(expression, descriptor))
}
else if (type.isMarkedNullable || reportError) {
// Note that "T::class" is allowed for type parameter T without a non-null upper bound
else if ((TypeUtils.isNullableType(type) && descriptor !is TypeParameterDescriptor) || expression.hasQuestionMarks) {
c.trace.report(NULLABLE_TYPE_IN_CLASS_LITERAL_LHS.on(expression))
}
else if (!result.possiblyBareType.isBare && !isAllowedInClassLiteral(type)) {
c.trace.report(CLASS_LITERAL_LHS_NOT_A_CLASS.on(expression))
}
}
@@ -438,7 +436,8 @@ class DoubleColonExpressionResolver(
)
}
else {
TypeUtils.makeNullableAsSpecified(possiblyBareType.actualType, doubleColonExpression.hasQuestionMarks)
val actualType = possiblyBareType.actualType
if (doubleColonExpression.hasQuestionMarks) actualType.makeNullable() else actualType
}
return DoubleColonLHS.Type(type, possiblyBareType)