Require presence of public primary constructor for inline class

This commit is contained in:
Mikhail Zarechenskiy
2018-05-06 03:31:16 +03:00
parent 096fe1c411
commit 6a120d2f85
10 changed files with 68 additions and 20 deletions
@@ -313,6 +313,7 @@ public interface Errors {
DiagnosticFactory0<PsiElement> INLINE_CLASS_NOT_TOP_LEVEL = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> INLINE_CLASS_NOT_FINAL = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> ABSENCE_OF_PRIMARY_CONSTRUCTOR_FOR_INLINE_CLASS = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtElement> INLINE_CLASS_CONSTRUCTOR_WRONG_PARAMETERS_SIZE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtParameter> INLINE_CLASS_CONSTRUCTOR_NOT_FINAL_READ_ONLY_PARAMETER = DiagnosticFactory0.create(ERROR);
@@ -627,6 +627,7 @@ public class DefaultErrorMessages {
MAP.put(INLINE_CLASS_NOT_TOP_LEVEL, "Inline classes are only allowed on top level");
MAP.put(INLINE_CLASS_NOT_FINAL, "Inline classes can be only final");
MAP.put(ABSENCE_OF_PRIMARY_CONSTRUCTOR_FOR_INLINE_CLASS, "Primary constructor is required for inline class");
MAP.put(NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS, "Primary constructor of inline class must be public");
MAP.put(INLINE_CLASS_CONSTRUCTOR_WRONG_PARAMETERS_SIZE, "Inline class must have exactly one primary constructor parameter");
MAP.put(INLINE_CLASS_CONSTRUCTOR_NOT_FINAL_READ_ONLY_PARAMETER, "Inline class primary constructor must have only final read-only (val) property parameter");
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.lexer.KtModifierKeywordToken
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtDeclaration
@@ -17,6 +18,7 @@ import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.psi.getModificationStamp
import org.jetbrains.kotlin.psi.psiUtil.hasActualModifier
import org.jetbrains.kotlin.psi.psiUtil.modalityModifier
import org.jetbrains.kotlin.psi.psiUtil.visibilityModifier
import org.jetbrains.kotlin.resolve.DescriptorUtils
object InlineClassDeclarationChecker : DeclarationChecker {
@@ -46,6 +48,12 @@ object InlineClassDeclarationChecker : DeclarationChecker {
return
}
val primaryConstructorVisibility = descriptor.unsubstitutedPrimaryConstructor?.visibility
if (primaryConstructorVisibility != null && primaryConstructorVisibility != Visibilities.PUBLIC) {
trace.report(Errors.NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS.on(primaryConstructor.visibilityModifier() ?: inlineKeyword))
return
}
val baseParameter = primaryConstructor.valueParameters.singleOrNull()
if (baseParameter == null) {
(primaryConstructor.valueParameterList ?: declaration).let {
@@ -1,3 +0,0 @@
// !LANGUAGE: +InlineClasses
inline class UInt private constructor(val value: Int)
@@ -1,12 +0,0 @@
@kotlin.Metadata
public final static class UInt$Erased {
public final static @org.jetbrains.annotations.NotNull method box(p0: int): UInt
}
@kotlin.Metadata
public final class UInt {
private final field value: int
private method <init>(p0: int): void
public final method getValue(): int
public final method unbox(): int
}
@@ -0,0 +1,7 @@
// !LANGUAGE: +InlineClasses
inline class ConstructorWithDefaultVisibility(val x: Int)
inline class PublicConstructor public constructor(val x: Int)
inline class InternalConstructor <!NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS!>internal<!> constructor(val x: Int)
inline class ProtectedConstructor <!NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS!>protected<!> constructor(val x: Int)
inline class PrivateConstructor <!NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS!>private<!> constructor(val x: Int)
@@ -0,0 +1,41 @@
package
public final inline class ConstructorWithDefaultVisibility {
public constructor ConstructorWithDefaultVisibility(/*0*/ x: kotlin.Int)
public final val x: kotlin.Int
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 InternalConstructor {
internal constructor InternalConstructor(/*0*/ x: kotlin.Int)
public final val x: kotlin.Int
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 PrivateConstructor {
private constructor PrivateConstructor(/*0*/ x: kotlin.Int)
public final val x: kotlin.Int
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 ProtectedConstructor {
protected constructor ProtectedConstructor(/*0*/ x: kotlin.Int)
public final val x: kotlin.Int
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 PublicConstructor {
public constructor PublicConstructor(/*0*/ x: kotlin.Int)
public final val x: kotlin.Int
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
}
@@ -10682,6 +10682,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
public void testInlineClassDeclarationCheck() throws Exception {
runTest("compiler/testData/diagnostics/tests/inlineClasses/inlineClassDeclarationCheck.kt");
}
@TestMetadata("presenceOfPublicPrimaryConstructorForInlineClass.kt")
public void testPresenceOfPublicPrimaryConstructorForInlineClass() throws Exception {
runTest("compiler/testData/diagnostics/tests/inlineClasses/presenceOfPublicPrimaryConstructorForInlineClass.kt");
}
}
@TestMetadata("compiler/testData/diagnostics/tests/inner")
@@ -10682,6 +10682,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
public void testInlineClassDeclarationCheck() throws Exception {
runTest("compiler/testData/diagnostics/tests/inlineClasses/inlineClassDeclarationCheck.kt");
}
@TestMetadata("presenceOfPublicPrimaryConstructorForInlineClass.kt")
public void testPresenceOfPublicPrimaryConstructorForInlineClass() throws Exception {
runTest("compiler/testData/diagnostics/tests/inlineClasses/presenceOfPublicPrimaryConstructorForInlineClass.kt");
}
}
@TestMetadata("compiler/testData/diagnostics/tests/inner")
@@ -284,11 +284,6 @@ public class BytecodeListingTestGenerated extends AbstractBytecodeListingTest {
public void testShapeOfInlineClassWithPrimitive() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/inlineClasses/shapeOfInlineClassWithPrimitive.kt");
}
@TestMetadata("shapeOfInlineClassWithPrivateConstructor.kt")
public void testShapeOfInlineClassWithPrivateConstructor() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/inlineClasses/shapeOfInlineClassWithPrivateConstructor.kt");
}
}
@TestMetadata("compiler/testData/codegen/bytecodeListing/specialBridges")