Prohibit to use enum entry as a type

#KT-14179 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2017-01-09 13:47:22 +03:00
parent 316da8331f
commit 76b0b785c5
14 changed files with 82 additions and 83 deletions
@@ -786,7 +786,7 @@ public interface Errors {
DiagnosticFactory0<KtSimpleNameExpression> CAST_NEVER_SUCCEEDS = DiagnosticFactory0.create(WARNING);
DiagnosticFactory0<KtTypeReference> DYNAMIC_NOT_ALLOWED = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtTypeReference> IS_ENUM_ENTRY = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtTypeReference> ENUM_ENTRY_AS_TYPE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtSimpleNameExpression> ENUM_ENTRY_AS_TYPE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory2<KtExpression, KotlinType, KotlinType> IMPLICIT_CAST_TO_ANY = DiagnosticFactory2.create(WARNING);
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
* 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.
@@ -40,35 +40,6 @@ import org.jetbrains.kotlin.types.typeUtil.*
import org.jetbrains.kotlin.utils.addToStdlib.check
import java.util.*
fun KtDeclaration.checkTypeReferences(trace: BindingTrace) {
if (this is KtCallableDeclaration) {
typeReference?.checkNotEnumEntry(trace)
receiverTypeReference?.checkNotEnumEntry(trace)
}
if (this is KtDeclarationWithBody) {
for (parameter in valueParameters) {
parameter.typeReference?.checkNotEnumEntry(trace)
}
}
}
fun KtTypeReference.checkNotEnumEntry(trace: BindingTrace): Boolean {
var result = false
trace.bindingContext.get(TYPE, this)?.let {
val targetDescriptor = TypeUtils.getClassDescriptor(it)
if (targetDescriptor != null && DescriptorUtils.isEnumEntry(targetDescriptor)) {
trace.report(ENUM_ENTRY_AS_TYPE.on(this))
result = true
}
}
typeElement?.let {
for (typeArgument in it.typeArgumentsAsTypes) {
typeArgument?.checkNotEnumEntry(trace)
}
}
return result
}
internal class DeclarationsCheckerBuilder(
private val descriptorResolver: DescriptorResolver,
private val originalModifiersChecker: ModifiersChecker,
@@ -93,8 +64,6 @@ class DeclarationsChecker(
private val exposedChecker = ExposedVisibilityChecker(trace)
fun KtDeclaration.checkTypeReferences() = checkTypeReferences(trace)
fun process(bodiesResolveContext: BodiesResolveContext) {
for (file in bodiesResolveContext.files) {
checkModifiersAndAnnotationsInPackageDirective(file)
@@ -118,7 +87,6 @@ class DeclarationsChecker(
checkPrimaryConstructor(classOrObject, classDescriptor)
classOrObject.checkTypeReferences()
modifiersChecker.checkModifiersForDeclaration(classOrObject, classDescriptor)
identifierChecker.checkDeclaration(classOrObject, trace)
exposedChecker.checkClassHeader(classOrObject, classDescriptor)
@@ -126,14 +94,12 @@ class DeclarationsChecker(
for ((function, functionDescriptor) in bodiesResolveContext.functions.entries) {
checkFunction(function, functionDescriptor)
function.checkTypeReferences()
modifiersChecker.checkModifiersForDeclaration(function, functionDescriptor)
identifierChecker.checkDeclaration(function, trace)
}
for ((property, propertyDescriptor) in bodiesResolveContext.properties.entries) {
checkProperty(property, propertyDescriptor)
property.checkTypeReferences()
modifiersChecker.checkModifiersForDeclaration(property, propertyDescriptor)
identifierChecker.checkDeclaration(property, trace)
}
@@ -259,7 +225,6 @@ class DeclarationsChecker(
}
private fun checkConstructorDeclaration(constructorDescriptor: ClassConstructorDescriptor, declaration: KtConstructor<*>) {
declaration.checkTypeReferences()
modifiersChecker.checkModifiersForDeclaration(declaration, constructorDescriptor)
identifierChecker.checkDeclaration(declaration, trace)
checkVarargParameters(trace, constructorDescriptor)
@@ -327,7 +292,6 @@ class DeclarationsChecker(
for (delegationSpecifier in classOrObject.getSuperTypeListEntries()) {
val typeReference = delegationSpecifier.typeReference ?: continue
typeReference.type()?.let { DescriptorResolver.checkBounds(typeReference, it, trace) }
typeReference.checkNotEnumEntry(trace)
}
if (classOrObject !is KtClass) return
@@ -841,7 +805,6 @@ class DeclarationsChecker(
for (accessorDescriptor in propertyDescriptor.accessors) {
val accessor = if (accessorDescriptor is PropertyGetterDescriptor) property.getter else property.setter
if (accessor != null) {
accessor.checkTypeReferences()
modifiersChecker.checkModifiersForDeclaration(accessor, accessorDescriptor)
identifierChecker.checkDeclaration(accessor, trace)
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
* 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.
@@ -622,7 +622,6 @@ public class DescriptorResolver {
@NotNull KotlinType upperBoundType,
BindingTrace trace
) {
if (DeclarationsCheckerKt.checkNotEnumEntry(upperBound, trace)) return;
if (!TypeUtils.canHaveSubtypes(KotlinTypeChecker.DEFAULT, upperBoundType)) {
trace.report(FINAL_UPPER_BOUND.on(upperBound, upperBoundType));
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
* 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.
@@ -120,7 +120,6 @@ class LocalVariableResolver(
ExpressionTypingUtils.checkVariableShadowing(context.scope, context.trace, propertyDescriptor)
property.checkTypeReferences(context.trace)
modifiersChecker.withTrace(context.trace).checkModifiersForLocalDeclaration(property, propertyDescriptor)
identifierChecker.checkDeclaration(property, context.trace)
return Pair(typeInfo.replaceType(dataFlowAnalyzer.checkStatementType(property, context)), propertyDescriptor)
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
* 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.
@@ -68,9 +68,10 @@ class QualifiedExpressionResolver {
): TypeQualifierResolutionResult {
val ownerDescriptor = if (!isDebuggerContext) scope.ownerDescriptor else null
if (userType.qualifier == null) {
val descriptor = userType.referenceExpression?.let {
val classifier = scope.findClassifier(it.getReferencedNameAsName(), KotlinLookupLocation(it))
storeResult(trace, it, classifier, ownerDescriptor, position = QualifierPosition.TYPE, isQualifier = false)
val descriptor = userType.referenceExpression?.let { expression ->
val classifier = scope.findClassifier(expression.getReferencedNameAsName(), KotlinLookupLocation(expression))
checkNotEnumEntry(classifier, trace, expression)
storeResult(trace, expression, classifier, ownerDescriptor, position = QualifierPosition.TYPE, isQualifier = false)
classifier
}
@@ -106,13 +107,23 @@ class QualifiedExpressionResolver {
val lastPart = qualifierPartList.last()
val classifier = when (qualifier) {
is PackageViewDescriptor -> qualifier.memberScope.getContributedClassifier(lastPart.name, lastPart.location)
is ClassDescriptor -> qualifier.unsubstitutedInnerClassesScope.getContributedClassifier(lastPart.name, lastPart.location)
is ClassDescriptor -> {
val descriptor = qualifier.unsubstitutedInnerClassesScope.getContributedClassifier(lastPart.name, lastPart.location)
checkNotEnumEntry(descriptor, trace, lastPart.expression)
descriptor
}
else -> null
}
storeResult(trace, lastPart.expression, classifier, ownerDescriptor, position = QualifierPosition.TYPE, isQualifier = isQualifier)
return TypeQualifierResolutionResult(qualifierPartList, classifier)
}
private fun checkNotEnumEntry(descriptor: DeclarationDescriptor?, trace: BindingTrace, expression: KtSimpleNameExpression) {
if (descriptor != null && DescriptorUtils.isEnumEntry(descriptor)) {
trace.report(Errors.ENUM_ENTRY_AS_TYPE.on(expression))
}
}
fun resolveDescriptorForDoubleColonLHS(
expression: KtExpression,
scope: LexicalScope,
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
* 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.
@@ -353,8 +353,6 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
) {
if (actualType == null || noExpectedType(targetType) || targetType.isError()) return;
DeclarationsCheckerKt.checkNotEnumEntry(expression.getRight(), context.trace);
if (DynamicTypesKt.isDynamic(targetType)) {
KtTypeReference right = expression.getRight();
assert right != null : "We know target is dynamic, but RHS is missing";
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
* 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.
@@ -28,8 +28,11 @@ import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.diagnostics.Errors.*
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getAnnotationEntries
import org.jetbrains.kotlin.resolve.*
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.BindingContext.EXPECTED_RETURN_TYPE
import org.jetbrains.kotlin.resolve.BindingContextUtils
import org.jetbrains.kotlin.resolve.BindingTrace
import org.jetbrains.kotlin.resolve.FunctionDescriptorUtil
import org.jetbrains.kotlin.resolve.checkers.UnderscoreChecker
import org.jetbrains.kotlin.resolve.lazy.ForceResolveUtil
import org.jetbrains.kotlin.resolve.scopes.LexicalWritableScope
@@ -109,7 +112,6 @@ internal class FunctionsTypingVisitor(facade: ExpressionTypingInternals) : Expre
function.getValueParameters(), functionDescriptor.valueParameters, context.scope, context.dataFlowInfo, context.trace
)
function.checkTypeReferences(context.trace)
components.modifiersChecker.withTrace(context.trace).checkModifiersForLocalDeclaration(function, functionDescriptor)
components.identifierChecker.checkDeclaration(function, context.trace)
components.declarationsCheckerBuilder.withTrace(context.trace).checkFunction(function, functionDescriptor)
+41 -21
View File
@@ -1,34 +1,54 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
enum class Color { RED }
enum class Color {
RED {
fun <T : <!ENUM_ENTRY_AS_TYPE, FINAL_UPPER_BOUND!>RED<!>> simpleName(): <!ENUM_ENTRY_AS_TYPE!>RED<!> = null!!
}
}
class MyColor(val x: <!ENUM_ENTRY_AS_TYPE!>Color.RED<!>, y: <!ENUM_ENTRY_AS_TYPE!>Color.RED<!>) : <!ENUM_ENTRY_AS_TYPE!>Color.RED<!> {
class MyColor(val x: Color.<!ENUM_ENTRY_AS_TYPE!>RED<!>, y: Color.<!ENUM_ENTRY_AS_TYPE!>RED<!>) : Color.<!ENUM_ENTRY_AS_TYPE!>RED<!> {
var z: <!ENUM_ENTRY_AS_TYPE!>Color.RED<!> = <!TYPE_MISMATCH!>Color.RED<!>
set(arg: <!ENUM_ENTRY_AS_TYPE!>Color.RED<!>) { z = arg }
var z: Color.<!ENUM_ENTRY_AS_TYPE!>RED<!> = <!TYPE_MISMATCH!>Color.RED<!>
set(arg: Color.<!ENUM_ENTRY_AS_TYPE!>RED<!>) { z = arg }
fun foo(arg: <!ENUM_ENTRY_AS_TYPE!>Color.RED<!>): <!ENUM_ENTRY_AS_TYPE!>Color.RED<!> = arg
fun foo(arg: Color.<!ENUM_ENTRY_AS_TYPE!>RED<!>): Color.<!ENUM_ENTRY_AS_TYPE!>RED<!> = arg
fun bar(): <!ENUM_ENTRY_AS_TYPE!>Color.RED<!> {
class Local : <!ENUM_ENTRY_AS_TYPE!>Color.RED<!>
fun local(arg: <!ENUM_ENTRY_AS_TYPE!>Color.RED<!>): <!ENUM_ENTRY_AS_TYPE!>Color.RED<!> = arg
val temp: <!ENUM_ENTRY_AS_TYPE!>Color.RED<!> = <!TYPE_MISMATCH!>Color.RED<!>
temp <!USELESS_CAST!>as? <!ENUM_ENTRY_AS_TYPE!>Color.RED<!><!>
if (temp is <!IS_ENUM_ENTRY!>Color.RED<!>) {
return temp <!USELESS_CAST!>as <!ENUM_ENTRY_AS_TYPE!>Color.RED<!><!>
}
val obj = object : <!ENUM_ENTRY_AS_TYPE!>Color.RED<!> {}
if (obj is <!IS_ENUM_ENTRY!>Color.RED<!>) {
return obj
}
fun bar(): Color.<!ENUM_ENTRY_AS_TYPE!>RED<!> {
class Local : Color.<!ENUM_ENTRY_AS_TYPE!>RED<!>
fun local(arg: Color.<!ENUM_ENTRY_AS_TYPE!>RED<!>): Color.<!ENUM_ENTRY_AS_TYPE!>RED<!> = arg
val temp: Color.<!ENUM_ENTRY_AS_TYPE!>RED<!> = <!TYPE_MISMATCH!>Color.RED<!>
temp <!USELESS_CAST!>as? Color.<!ENUM_ENTRY_AS_TYPE!>RED<!><!>
if (temp is <!IS_ENUM_ENTRY!>Color.<!ENUM_ENTRY_AS_TYPE!>RED<!><!>) {
return temp <!USELESS_CAST!>as Color.<!ENUM_ENTRY_AS_TYPE!>RED<!><!>
}
val obj = object : Color.<!ENUM_ENTRY_AS_TYPE!>RED<!> {}
if (obj is <!IS_ENUM_ENTRY!>Color.<!ENUM_ENTRY_AS_TYPE!>RED<!><!>) {
return obj
}
return <!TYPE_MISMATCH!>Color.RED<!>
}
}
fun create(): Array<<!ENUM_ENTRY_AS_TYPE!>Color.RED<!>>? = null
fun create(): Array<Color.<!ENUM_ENTRY_AS_TYPE!>RED<!>>? = null
interface Your<T : <!ENUM_ENTRY_AS_TYPE!>Color.RED<!>>
interface Your<T : <!FINAL_UPPER_BOUND!>Color.<!ENUM_ENTRY_AS_TYPE!>RED<!><!>>
class His : Your<<!ENUM_ENTRY_AS_TYPE!>Color.RED<!>>
class His : Your<Color.<!ENUM_ENTRY_AS_TYPE!>RED<!>>
fun <T : <!ENUM_ENTRY_AS_TYPE!>Color.RED<!>> otherCreate(): Array<T>? = null
fun <T : <!FINAL_UPPER_BOUND!>Color.<!ENUM_ENTRY_AS_TYPE!>RED<!><!>> otherCreate(): Array<T>? = null
typealias RedAlias = Color.<!ENUM_ENTRY_AS_TYPE!>RED<!>
typealias ArrayOfEnumEntry = Array<Color.<!ENUM_ENTRY_AS_TYPE!>RED<!>>
typealias ArrayOfEnumEntryAlias = Array<RedAlias>
fun <T> bar(a: Any): T = <!UNCHECKED_CAST!>a as T<!>
fun <T> foo() {
foo<Color.<!ENUM_ENTRY_AS_TYPE!>RED<!>>()
foo<RedAlias>()
bar<Color.<!ENUM_ENTRY_AS_TYPE!>RED<!>>(Color.RED)
}
fun Array<Color.<!ENUM_ENTRY_AS_TYPE!>RED<!>>.foo(entries: Array<Color.<!ENUM_ENTRY_AS_TYPE!>RED<!>>): Array<Color.<!ENUM_ENTRY_AS_TYPE!>RED<!>> = null!!
@@ -1,7 +1,10 @@
package
public fun </*0*/ T> bar(/*0*/ a: kotlin.Any): T
public fun create(): kotlin.Array<Color.RED>?
public fun </*0*/ T> foo(): kotlin.Unit
public fun </*0*/ T : Color.RED> otherCreate(): kotlin.Array<T>?
public fun kotlin.Array<Color.RED>.foo(/*0*/ entries: kotlin.Array<Color.RED>): kotlin.Array<Color.RED>
public final enum class Color : kotlin.Enum<Color> {
enum entry RED
@@ -43,6 +46,7 @@ public final class MyColor : Color.RED {
public final fun foo(/*0*/ arg: Color.RED): Color.RED
public final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun getDeclaringClass(): java.lang.Class<Color!>!
public final override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final override /*1*/ /*fake_override*/ fun </*0*/ T : Color.RED> simpleName(): Color.RED
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -51,3 +55,6 @@ public interface Your</*0*/ T : Color.RED> {
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public typealias ArrayOfEnumEntry = kotlin.Array<Color.RED>
public typealias ArrayOfEnumEntryAlias = kotlin.Array<RedAlias>
public typealias RedAlias = Color.RED
+1 -1
View File
@@ -3,4 +3,4 @@ enum class MyEnum {
SECOND
}
fun foo(me: MyEnum): Boolean = if (me is <!IS_ENUM_ENTRY!>MyEnum.FIRST<!>) true else false
fun foo(me: MyEnum): Boolean = if (me is <!IS_ENUM_ENTRY!>MyEnum.<!ENUM_ENTRY_AS_TYPE!>FIRST<!><!>) true else false
@@ -2,4 +2,4 @@ enum class E {
ENTRY
}
class A : <!ENUM_ENTRY_AS_TYPE!>E.ENTRY<!>
class A : E.<!ENUM_ENTRY_AS_TYPE!>ENTRY<!>
+1 -1
View File
@@ -3,4 +3,4 @@ enum class MyEnum {
SECOND
}
fun foo(me: MyEnum): Boolean = me is <!IS_ENUM_ENTRY!>MyEnum.FIRST<!>
fun foo(me: MyEnum): Boolean = me is <!IS_ENUM_ENTRY!>MyEnum.<!ENUM_ENTRY_AS_TYPE!>FIRST<!><!>
@@ -4,8 +4,8 @@ enum class MyEnum {
fun foo(x: MyEnum): Int {
return when (x) {
is <!IS_ENUM_ENTRY!>MyEnum.A<!> -> 1
is <!IS_ENUM_ENTRY!>MyEnum.B<!> -> 2
is <!IS_ENUM_ENTRY!>MyEnum.C<!> -> 3
is <!IS_ENUM_ENTRY!>MyEnum.<!ENUM_ENTRY_AS_TYPE!>A<!><!> -> 1
is <!IS_ENUM_ENTRY!>MyEnum.<!ENUM_ENTRY_AS_TYPE!>B<!><!> -> 2
is <!IS_ENUM_ENTRY!>MyEnum.<!ENUM_ENTRY_AS_TYPE!>C<!><!> -> 3
}
}
@@ -5,7 +5,7 @@ enum class MyEnum {
fun foo(x: MyEnum): Int {
return when (x) {
MyEnum.A -> 1
is <!IS_ENUM_ENTRY!>MyEnum.B<!> -> 2
is <!IS_ENUM_ENTRY!>MyEnum.C<!> -> 3
is <!IS_ENUM_ENTRY!>MyEnum.<!ENUM_ENTRY_AS_TYPE!>B<!><!> -> 2
is <!IS_ENUM_ENTRY!>MyEnum.<!ENUM_ENTRY_AS_TYPE!>C<!><!> -> 3
}
}