Prohibit @JvmRecord for non-data classes

^KT-43677 In Progress
This commit is contained in:
Denis.Zharkov
2020-11-30 20:20:14 +03:00
parent cc0b584445
commit c8851c4f75
11 changed files with 92 additions and 53 deletions
@@ -83,28 +83,6 @@ object JvmRecordApplicabilityChecker : DeclarationChecker {
return
}
val primaryConstructor = declaration.primaryConstructor
val parameters = primaryConstructor?.valueParameters ?: emptyList()
if (parameters.isEmpty()) {
(primaryConstructor?.valueParameterList ?: declaration.nameIdentifier)?.let {
context.trace.report(ErrorsJvm.JVM_RECORD_WITHOUT_PRIMARY_CONSTRUCTOR_PARAMETERS.on(it))
return
}
}
for (parameter in parameters) {
if (!parameter.hasValOrVar() || parameter.isMutable) {
context.trace.report(ErrorsJvm.JVM_RECORD_NOT_VAL_PARAMETER.on(parameter))
return
}
}
for (parameter in parameters.dropLast(1)) {
if (parameter.isVarArg) {
context.trace.report(ErrorsJvm.JVM_RECORD_NOT_LAST_VARARG_PARAMETER.on(parameter))
return
}
}
for (member in declaration.declarations) {
if (member !is KtProperty) continue
@@ -131,6 +109,34 @@ object JvmRecordApplicabilityChecker : DeclarationChecker {
context.trace.report(ErrorsJvm.JVM_RECORD_EXTENDS_CLASS.on(reportSupertypeOn, supertype))
return
}
if (!descriptor.isData) {
context.trace.report(ErrorsJvm.NON_DATA_CLASS_JVM_RECORD.on(reportOn))
return
}
val primaryConstructor = declaration.primaryConstructor
val parameters = primaryConstructor?.valueParameters ?: emptyList()
if (parameters.isEmpty()) {
(primaryConstructor?.valueParameterList ?: declaration.nameIdentifier)?.let {
context.trace.report(ErrorsJvm.JVM_RECORD_WITHOUT_PRIMARY_CONSTRUCTOR_PARAMETERS.on(it))
return
}
}
for (parameter in parameters) {
if (!parameter.hasValOrVar() || parameter.isMutable) {
context.trace.report(ErrorsJvm.JVM_RECORD_NOT_VAL_PARAMETER.on(parameter))
return
}
}
for (parameter in parameters.dropLast(1)) {
if (parameter.isVarArg) {
context.trace.report(ErrorsJvm.JVM_RECORD_NOT_LAST_VARARG_PARAMETER.on(parameter))
return
}
}
}
}
@@ -163,6 +163,7 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension {
MAP.put(INNER_JVM_RECORD, "@JvmRecord class should not be inner");
MAP.put(FIELD_IN_JVM_RECORD, "It's not allowed to have non-constructor properties with backing filed in @JvmRecord class");
MAP.put(DELEGATION_BY_IN_JVM_RECORD, "Delegation is not allowed for @JvmRecord classes");
MAP.put(NON_DATA_CLASS_JVM_RECORD, "Only data classes are allowed to be marked as @JvmRecord");
String MESSAGE_FOR_CONCURRENT_HASH_MAP_CONTAINS =
"Method 'contains' from ConcurrentHashMap may have unexpected semantics: it calls 'containsValue' instead of 'containsKey'. " +
@@ -126,6 +126,7 @@ public interface ErrorsJvm {
DiagnosticFactory0<PsiElement> INNER_JVM_RECORD = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> FIELD_IN_JVM_RECORD = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> DELEGATION_BY_IN_JVM_RECORD = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> NON_DATA_CLASS_JVM_RECORD = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtDeclaration> NON_JVM_DEFAULT_OVERRIDES_JAVA_DEFAULT = DiagnosticFactory0.create(WARNING, DECLARATION_SIGNATURE);