Implement additional declaration checks for inline classes

- Implementation by delegation is prohibited
- Delegated properties are prohibited
This commit is contained in:
Dmitry Petrov
2018-08-10 16:58:34 +03:00
parent 0af33462ef
commit b7df36643b
11 changed files with 202 additions and 3 deletions
@@ -325,7 +325,9 @@ public interface Errors {
DiagnosticFactory0<KtParameter> INLINE_CLASS_CONSTRUCTOR_NOT_FINAL_READ_ONLY_PARAMETER = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> INLINE_CLASS_WITH_INITIALIZER = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtProperty> PROPERTY_WITH_BACKING_FIELD_INSIDE_INLINE_CLASS = DiagnosticFactory0.create(ERROR, DECLARATION_SIGNATURE);
DiagnosticFactory0<PsiElement> DELEGATED_PROPERTY_INSIDE_INLINE_CLASS = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<KtTypeReference, KotlinType> INLINE_CLASS_HAS_INAPPLICABLE_PARAMETER_TYPE = DiagnosticFactory1.create(ERROR);
DiagnosticFactory0<PsiElement> INLINE_CLASS_CANNOT_IMPLEMENT_INTERFACE_BY_DELEGATION = DiagnosticFactory0.create(ERROR);
// Secondary constructors
@@ -651,7 +651,9 @@ public class DefaultErrorMessages {
MAP.put(INLINE_CLASS_CONSTRUCTOR_NOT_FINAL_READ_ONLY_PARAMETER, "Inline class primary constructor must have only final read-only (val) property parameter");
MAP.put(INLINE_CLASS_WITH_INITIALIZER, "Inline class cannot have an initializer block");
MAP.put(PROPERTY_WITH_BACKING_FIELD_INSIDE_INLINE_CLASS, "Inline class cannot have properties with backing fields");
MAP.put(DELEGATED_PROPERTY_INSIDE_INLINE_CLASS, "Inline class cannot have delegated properties");
MAP.put(INLINE_CLASS_HAS_INAPPLICABLE_PARAMETER_TYPE, "Inline class cannot have value parameter of type ''{0}''", RENDER_TYPE);
MAP.put(INLINE_CLASS_CANNOT_IMPLEMENT_INTERFACE_BY_DELEGATION, "Inline class cannot implement an interface by delegation");
MAP.put(VARIANCE_ON_TYPE_PARAMETER_NOT_ALLOWED, "Variance annotations are only allowed for type parameters of classes and interfaces");
MAP.put(BOUND_ON_TYPE_ALIAS_PARAMETER_NOT_ALLOWED, "Bounds are not allowed on type alias parameters");
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.resolve.checkers
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.lexer.KtTokens
@@ -15,6 +16,7 @@ import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.isInlineClass
import org.jetbrains.kotlin.resolve.substitutedUnderlyingType
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.typeUtil.isNothing
import org.jetbrains.kotlin.types.typeUtil.isTypeParameter
import org.jetbrains.kotlin.types.typeUtil.isUnit
@@ -78,15 +80,32 @@ object InlineClassDeclarationChecker : DeclarationChecker {
}
val baseParameterType = descriptor.safeAs<ClassDescriptor>()?.defaultType?.substitutedUnderlyingType()
if (baseParameterType != null &&
(baseParameterType.isUnit() || baseParameterType.isNothing() || baseParameterType.isTypeParameter())
) {
if (baseParameterType != null && baseParameterType.isInapplicableParameterType()) {
val typeReference = baseParameter.typeReference
if (typeReference != null) {
trace.report(Errors.INLINE_CLASS_HAS_INAPPLICABLE_PARAMETER_TYPE.on(typeReference, baseParameterType))
return
}
}
for (supertypeEntry in declaration.superTypeListEntries) {
if (supertypeEntry is KtDelegatedSuperTypeEntry) {
trace.report(Errors.INLINE_CLASS_CANNOT_IMPLEMENT_INTERFACE_BY_DELEGATION.on(supertypeEntry))
return
}
}
}
private fun KotlinType.isInapplicableParameterType() =
isUnit() || isNothing() || isTypeParameter() || isGenericArrayOfTypeParameter()
private fun KotlinType.isGenericArrayOfTypeParameter(): Boolean {
if (!KotlinBuiltIns.isArray(this)) return false
val argument0 = arguments[0]
if (argument0.isStarProjection) return false
val argument0type = argument0.type
return argument0type.isTypeParameter() ||
argument0type.isGenericArrayOfTypeParameter()
}
private fun isParameterAcceptableForInlineClass(parameter: KtParameter): Boolean {
@@ -105,5 +124,9 @@ class PropertiesWithBackingFieldsInsideInlineClass : DeclarationChecker {
if (context.trace.get(BindingContext.BACKING_FIELD_REQUIRED, descriptor) == true) {
context.trace.report(Errors.PROPERTY_WITH_BACKING_FIELD_INSIDE_INLINE_CLASS.on(declaration))
}
declaration.delegate?.let {
context.trace.report(Errors.DELEGATED_PROPERTY_INSIDE_INLINE_CLASS.on(it))
}
}
}
@@ -0,0 +1,28 @@
// !LANGUAGE: +InlineClasses
class Val {
operator fun getValue(thisRef: Any?, kProp: Any?) = 1
}
class Var {
operator fun getValue(thisRef: Any?, kProp: Any?) = 2
operator fun setValue(thisRef: Any?, kProp: Any?, value: Int) {}
}
object ValObject {
operator fun getValue(thisRef: Any?, kProp: Any?) = 1
}
object VarObject {
operator fun getValue(thisRef: Any?, kProp: Any?) = 2
operator fun setValue(thisRef: Any?, kProp: Any?, value: Int) {}
}
inline class Z(val data: Int) {
val testVal <!DELEGATED_PROPERTY_INSIDE_INLINE_CLASS!>by Val()<!>
var testVar <!DELEGATED_PROPERTY_INSIDE_INLINE_CLASS!>by Var()<!>
val testValBySingleton <!DELEGATED_PROPERTY_INSIDE_INLINE_CLASS!>by ValObject<!>
var testVarBySingleton <!DELEGATED_PROPERTY_INSIDE_INLINE_CLASS!>by VarObject<!>
}
@@ -0,0 +1,47 @@
package
public final class Val {
public constructor Val()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final operator fun getValue(/*0*/ thisRef: kotlin.Any?, /*1*/ kProp: kotlin.Any?): kotlin.Int
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public object ValObject {
private constructor ValObject()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final operator fun getValue(/*0*/ thisRef: kotlin.Any?, /*1*/ kProp: kotlin.Any?): kotlin.Int
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Var {
public constructor Var()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final operator fun getValue(/*0*/ thisRef: kotlin.Any?, /*1*/ kProp: kotlin.Any?): kotlin.Int
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final operator fun setValue(/*0*/ thisRef: kotlin.Any?, /*1*/ kProp: kotlin.Any?, /*2*/ value: kotlin.Int): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public object VarObject {
private constructor VarObject()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final operator fun getValue(/*0*/ thisRef: kotlin.Any?, /*1*/ kProp: kotlin.Any?): kotlin.Int
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final operator fun setValue(/*0*/ thisRef: kotlin.Any?, /*1*/ kProp: kotlin.Any?, /*2*/ value: kotlin.Int): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final inline class Z {
public constructor Z(/*0*/ data: kotlin.Int)
public final val data: kotlin.Int
public final val testVal: kotlin.Int
public final val testValBySingleton: kotlin.Int
public final var testVar: kotlin.Int
public final var testVarBySingleton: kotlin.Int
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
}
@@ -0,0 +1,9 @@
// !LANGUAGE: +InlineClasses
interface IFoo
object FooImpl : IFoo
inline class Test1(val x: Any) : <!INLINE_CLASS_CANNOT_IMPLEMENT_INTERFACE_BY_DELEGATION!>IFoo by FooImpl<!>
inline class Test2(val x: IFoo) : <!INLINE_CLASS_CANNOT_IMPLEMENT_INTERFACE_BY_DELEGATION!>IFoo by x<!>
@@ -0,0 +1,30 @@
package
public object FooImpl : IFoo {
private constructor FooImpl()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface IFoo {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final inline class Test1 : IFoo {
public constructor Test1(/*0*/ x: kotlin.Any)
public final val x: kotlin.Any
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
}
public final inline class Test2 : IFoo {
public constructor Test2(/*0*/ x: IFoo)
public final val x: IFoo
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
}
@@ -3,6 +3,12 @@
inline class Foo<T>(val x: <!INLINE_CLASS_HAS_INAPPLICABLE_PARAMETER_TYPE!>T<!>)
inline class FooNullable<T>(val x: <!INLINE_CLASS_HAS_INAPPLICABLE_PARAMETER_TYPE!>T?<!>)
inline class FooGenericArray<T>(val x: <!INLINE_CLASS_HAS_INAPPLICABLE_PARAMETER_TYPE!>Array<T><!>)
inline class FooGenericArray2<T>(val x: <!INLINE_CLASS_HAS_INAPPLICABLE_PARAMETER_TYPE!>Array<Array<T>><!>)
inline class FooStarProjectedArray(val x: Array<*>)
inline class FooStarProjectedArray2(val x: Array<Array<*>>)
inline class Bar(val u: <!INLINE_CLASS_HAS_INAPPLICABLE_PARAMETER_TYPE!>Unit<!>)
inline class BarNullable(val u: Unit?)
@@ -40,6 +40,22 @@ public final inline class Foo</*0*/ T> {
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
}
public final inline class FooGenericArray</*0*/ T> {
public constructor FooGenericArray</*0*/ T>(/*0*/ x: kotlin.Array<T>)
public final val x: kotlin.Array<T>
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
}
public final inline class FooGenericArray2</*0*/ T> {
public constructor FooGenericArray2</*0*/ T>(/*0*/ x: kotlin.Array<kotlin.Array<T>>)
public final val x: kotlin.Array<kotlin.Array<T>>
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
}
public final inline class FooNullable</*0*/ T> {
public constructor FooNullable</*0*/ T>(/*0*/ x: T?)
public final val x: T?
@@ -47,3 +63,19 @@ public final inline class FooNullable</*0*/ T> {
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
}
public final inline class FooStarProjectedArray {
public constructor FooStarProjectedArray(/*0*/ x: kotlin.Array<*>)
public final val x: kotlin.Array<*>
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
}
public final inline class FooStarProjectedArray2 {
public constructor FooStarProjectedArray2(/*0*/ x: kotlin.Array<kotlin.Array<*>>)
public final val x: kotlin.Array<kotlin.Array<*>>
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
}
@@ -10857,11 +10857,21 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
runTest("compiler/testData/diagnostics/tests/inlineClasses/basicInlineClassDeclarationDisabled.kt");
}
@TestMetadata("delegatedPropertyInInlineClass.kt")
public void testDelegatedPropertyInInlineClass() throws Exception {
runTest("compiler/testData/diagnostics/tests/inlineClasses/delegatedPropertyInInlineClass.kt");
}
@TestMetadata("identityComparisonWithInlineClasses.kt")
public void testIdentityComparisonWithInlineClasses() throws Exception {
runTest("compiler/testData/diagnostics/tests/inlineClasses/identityComparisonWithInlineClasses.kt");
}
@TestMetadata("inlineClassCannotImplementInterfaceByDelegation.kt")
public void testInlineClassCannotImplementInterfaceByDelegation() throws Exception {
runTest("compiler/testData/diagnostics/tests/inlineClasses/inlineClassCannotImplementInterfaceByDelegation.kt");
}
@TestMetadata("inlineClassDeclarationCheck.kt")
public void testInlineClassDeclarationCheck() throws Exception {
runTest("compiler/testData/diagnostics/tests/inlineClasses/inlineClassDeclarationCheck.kt");
@@ -10857,11 +10857,21 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/inlineClasses/basicInlineClassDeclarationDisabled.kt");
}
@TestMetadata("delegatedPropertyInInlineClass.kt")
public void testDelegatedPropertyInInlineClass() throws Exception {
runTest("compiler/testData/diagnostics/tests/inlineClasses/delegatedPropertyInInlineClass.kt");
}
@TestMetadata("identityComparisonWithInlineClasses.kt")
public void testIdentityComparisonWithInlineClasses() throws Exception {
runTest("compiler/testData/diagnostics/tests/inlineClasses/identityComparisonWithInlineClasses.kt");
}
@TestMetadata("inlineClassCannotImplementInterfaceByDelegation.kt")
public void testInlineClassCannotImplementInterfaceByDelegation() throws Exception {
runTest("compiler/testData/diagnostics/tests/inlineClasses/inlineClassCannotImplementInterfaceByDelegation.kt");
}
@TestMetadata("inlineClassDeclarationCheck.kt")
public void testInlineClassDeclarationCheck() throws Exception {
runTest("compiler/testData/diagnostics/tests/inlineClasses/inlineClassDeclarationCheck.kt");