Forbid private 'expect' declarations

#KT-19170 Fixed
This commit is contained in:
Alexander Udalov
2017-09-20 14:24:55 +03:00
parent 87b85ce978
commit ecfea9e340
15 changed files with 188 additions and 58 deletions
@@ -571,6 +571,7 @@ public interface Errors {
DiagnosticFactory0<KtPropertyDelegate> EXPECTED_DELEGATED_PROPERTY = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> EXPECTED_LATEINIT_PROPERTY = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> SUPERTYPE_INITIALIZED_IN_EXPECTED_CLASS = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> EXPECTED_PRIVATE_DECLARATION = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtDelegatedSuperTypeEntry> IMPLEMENTATION_BY_DELEGATION_IN_EXPECT_CLASS = DiagnosticFactory0.create(ERROR);
@@ -273,6 +273,7 @@ public class DefaultErrorMessages {
MAP.put(EXPECTED_DELEGATED_PROPERTY, "Expected property cannot be delegated");
MAP.put(EXPECTED_LATEINIT_PROPERTY, "Expected property cannot be lateinit");
MAP.put(SUPERTYPE_INITIALIZED_IN_EXPECTED_CLASS, "Expected classes cannot initialize supertypes");
MAP.put(EXPECTED_PRIVATE_DECLARATION, "Expected declaration cannot be private");
MAP.put(IMPLEMENTATION_BY_DELEGATION_IN_EXPECT_CLASS, "Implementation by delegation in expected classes is prohibited");
@@ -78,22 +78,7 @@ class DeclarationsChecker(
}
for ((classOrObject, classDescriptor) in bodiesResolveContext.declaredClasses.entries) {
checkSupertypesForConsistency(classDescriptor, classOrObject)
checkTypesInClassHeader(classOrObject)
when (classOrObject) {
is KtClass -> {
checkClassButNotObject(classOrObject, classDescriptor)
descriptorResolver.checkNamesInConstraints(
classOrObject, classDescriptor, classDescriptor.scopeForClassHeaderResolution, trace)
}
is KtObjectDeclaration -> {
checkObject(classOrObject, classDescriptor)
}
}
checkPrimaryConstructor(classOrObject, classDescriptor)
checkClass(classDescriptor, classOrObject)
modifiersChecker.checkModifiersForDeclaration(classOrObject, classDescriptor)
identifierChecker.checkDeclaration(classOrObject, trace)
exposedChecker.checkClassHeader(classOrObject, classDescriptor)
@@ -303,6 +288,26 @@ class DeclarationsChecker(
ModifierCheckerCore.check(packageDirective, trace, descriptor = null, languageVersionSettings = languageVersionSettings)
}
private fun checkClass(classDescriptor: ClassDescriptorWithResolutionScopes, classOrObject: KtClassOrObject) {
checkSupertypesForConsistency(classDescriptor, classOrObject)
checkTypesInClassHeader(classOrObject)
when (classOrObject) {
is KtClass -> {
checkClassButNotObject(classOrObject, classDescriptor)
descriptorResolver.checkNamesInConstraints(
classOrObject, classDescriptor, classDescriptor.scopeForClassHeaderResolution, trace)
}
is KtObjectDeclaration -> {
checkObject(classOrObject, classDescriptor)
}
}
checkPrimaryConstructor(classOrObject, classDescriptor)
checkPrivateExpectedDeclaration(classOrObject, classDescriptor)
}
private fun checkTypesInClassHeader(classOrObject: KtClassOrObject) {
fun KtTypeReference.type(): KotlinType? = trace.bindingContext.get(TYPE, this)
@@ -548,6 +553,13 @@ class DeclarationsChecker(
shadowedExtensionChecker.checkDeclaration(property, propertyDescriptor)
checkPropertyTypeParametersAreUsedInReceiverType(propertyDescriptor)
checkImplicitCallableType(property, propertyDescriptor)
checkPrivateExpectedDeclaration(property, propertyDescriptor)
}
private fun checkPrivateExpectedDeclaration(declaration: KtDeclaration, descriptor: MemberDescriptor) {
if (descriptor.isExpect && Visibilities.isPrivate(descriptor.visibility)) {
trace.report(EXPECTED_PRIVATE_DECLARATION.on(declaration.modifierList?.getModifier(KtTokens.PRIVATE_KEYWORD) ?: declaration))
}
}
private fun checkPropertyTypeParametersAreUsedInReceiverType(descriptor: PropertyDescriptor) {
@@ -579,7 +591,8 @@ class DeclarationsChecker(
private fun checkMemberProperty(
property: KtProperty,
propertyDescriptor: PropertyDescriptor,
classDescriptor: ClassDescriptor) {
classDescriptor: ClassDescriptor
) {
val modifierList = property.modifierList
if (modifierList != null) {
@@ -731,13 +744,13 @@ class DeclarationsChecker(
}
if (functionDescriptor.isExpect) {
checkExpectedFunction(function)
checkExpectedFunction(function, functionDescriptor)
}
shadowedExtensionChecker.checkDeclaration(function, functionDescriptor)
}
private fun checkExpectedFunction(function: KtNamedFunction) {
private fun checkExpectedFunction(function: KtNamedFunction, functionDescriptor: FunctionDescriptor) {
if (function.hasBody()) {
trace.report(EXPECTED_DECLARATION_WITH_BODY.on(function))
}
@@ -747,6 +760,8 @@ class DeclarationsChecker(
trace.report(EXPECTED_DECLARATION_WITH_DEFAULT_PARAMETER.on(parameter))
}
}
checkPrivateExpectedDeclaration(function, functionDescriptor)
}
private fun checkImplicitCallableType(declaration: KtCallableDeclaration, descriptor: CallableDescriptor) {