Reserve secondary constructors with bodies inside inline classes

#KT-26575 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2018-09-04 14:21:43 +03:00
parent 002a66fec1
commit b4674a172e
6 changed files with 38 additions and 5 deletions
@@ -331,6 +331,7 @@ public interface Errors {
DiagnosticFactory0<KtTypeReference> INLINE_CLASS_CANNOT_EXTEND_CLASSES = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtTypeReference> INLINE_CLASS_CANNOT_BE_RECURSIVE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<PsiElement, String> RESERVED_MEMBER_INSIDE_INLINE_CLASS = DiagnosticFactory1.create(ERROR);
DiagnosticFactory0<PsiElement> SECONDARY_CONSTRUCTOR_WITH_BODY_INSIDE_INLINE_CLASS = DiagnosticFactory0.create(ERROR);
// Secondary constructors
@@ -662,6 +662,7 @@ public class DefaultErrorMessages {
MAP.put(INLINE_CLASS_CANNOT_EXTEND_CLASSES, "Inline class cannot extend classes");
MAP.put(INLINE_CLASS_CANNOT_BE_RECURSIVE, "Inline class cannot be recursive");
MAP.put(RESERVED_MEMBER_INSIDE_INLINE_CLASS, "Member with the name ''{0}'' is reserved for future releases", STRING);
MAP.put(SECONDARY_CONSTRUCTOR_WITH_BODY_INSIDE_INLINE_CLASS, "Secondary constructors with bodies are reserved for for future releases");
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");
@@ -151,11 +151,25 @@ class ReservedMembersAndConstructsForInlineClass : DeclarationChecker {
val containingDeclaration = descriptor.containingDeclaration ?: return
if (!containingDeclaration.isInlineClass()) return
if (declaration is KtFunction && descriptor is FunctionDescriptor) {
val functionName = descriptor.name.asString()
if (functionName in reservedFunctions) {
val nameIdentifier = declaration.nameIdentifier ?: return
context.trace.report(Errors.RESERVED_MEMBER_INSIDE_INLINE_CLASS.on(nameIdentifier, functionName))
if (descriptor !is FunctionDescriptor) return
when (descriptor) {
is SimpleFunctionDescriptor -> {
val ktFunction = declaration as? KtFunction ?: return
val functionName = descriptor.name.asString()
if (functionName in reservedFunctions) {
val nameIdentifier = ktFunction.nameIdentifier ?: return
context.trace.report(Errors.RESERVED_MEMBER_INSIDE_INLINE_CLASS.on(nameIdentifier, functionName))
}
}
is ConstructorDescriptor -> {
val secondaryConstructor = declaration as? KtSecondaryConstructor ?: return
val bodyExpression = secondaryConstructor.bodyExpression
if (secondaryConstructor.hasBlockBody() && bodyExpression is KtBlockExpression) {
val lBrace = bodyExpression.lBrace ?: return
context.trace.report(Errors.SECONDARY_CONSTRUCTOR_WITH_BODY_INSIDE_INLINE_CLASS.on(lBrace))
}
}
}
}
@@ -1,6 +1,8 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
@file:Suppress("SECONDARY_CONSTRUCTOR_WITH_BODY_INSIDE_INLINE_CLASS")
var global = "wrong"
inline class Foo(val x: String) {
@@ -36,4 +36,10 @@ interface WithBox {
inline class IC4(val s: String) : WithBox {
override fun <!RESERVED_MEMBER_INSIDE_INLINE_CLASS!>box<!>(): String = ""
}
inline class IC5(val a: String) {
constructor(i: Int) : this(i.toString()) <!SECONDARY_CONSTRUCTOR_WITH_BODY_INSIDE_INLINE_CLASS!>{<!>
TODO("something")
}
}
@@ -46,6 +46,15 @@ public final inline class IC4 : WithBox {
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
}
public final inline class IC5 {
public constructor IC5(/*0*/ i: kotlin.Int)
public constructor IC5(/*0*/ a: kotlin.String)
public final val a: kotlin.String
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 interface WithBox {
public abstract fun box(): kotlin.String
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean