Prohibit serializable inner classes

Fixes https://github.com/Kotlin/kotlinx.serialization/issues/1101
This commit is contained in:
Leonid Startsev
2021-03-18 18:43:17 +03:00
parent c66cddc442
commit 70fa6d50d3
4 changed files with 16 additions and 2 deletions
@@ -17,6 +17,7 @@ public interface SerializationErrors {
DiagnosticFactory2<PsiElement, String, String> INLINE_CLASSES_NOT_SUPPORTED = DiagnosticFactory2.create(ERROR);
DiagnosticFactory0<PsiElement> PLUGIN_IS_NOT_ENABLED = DiagnosticFactory0.create(WARNING);
DiagnosticFactory0<PsiElement> LOCAL_CLASSES_NOT_SUPPORTED = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> INNER_CLASSES_NOT_SUPPORTED = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> EXPLICIT_SERIALIZABLE_IS_REQUIRED = DiagnosticFactory0.create(WARNING);
@@ -123,6 +123,11 @@ open class SerializationPluginDeclarationChecker : DeclarationChecker {
return false
}
if (descriptor.isInner) {
trace.reportOnSerializableAnnotation(descriptor, SerializationErrors.INNER_CLASSES_NOT_SUPPORTED)
return false
}
if (descriptor.isInlineClass() && !canSupportInlineClasses(descriptor.module, trace)) {
descriptor.onSerializableAnnotation {
trace.report(
@@ -27,7 +27,11 @@ object SerializationPluginErrorsRendering : DefaultErrorMessages.Extension {
)
MAP.put(
SerializationErrors.LOCAL_CLASSES_NOT_SUPPORTED,
"Local class and anonymous objects can not be serializable"
"Local classes and anonymous objects can not be serializable."
)
MAP.put(
SerializationErrors.INNER_CLASSES_NOT_SUPPORTED,
"Inner (with reference to outer this) serializable classes are not supported. Remove @Serializable annotation or 'inner' keyword."
)
MAP.put(
SerializationErrors.EXPLICIT_SERIALIZABLE_IS_REQUIRED,
@@ -14,5 +14,9 @@ fun container() {
val topLevelAnon = <!LOCAL_CLASSES_NOT_SUPPORTED!>@Serializable<!> object {}
@Serializable class A {
@Serializable class B // nesting classes are allowed
@Serializable class B // nested classes are allowed
<!INNER_CLASSES_NOT_SUPPORTED!>@Serializable<!> inner class C // inner classes are not
@Serializable object F {} // regular named object, OK
}