nested types: Reject unstructured parcelable
Having unstructured parcelables as nested types doesn't make sense
because they are defined somewhere else in native languages (e.g. C++,
Java...).
Bug: 182508839
Test: aidl_unittests
Change-Id: I4a6cd76ac58fc246db77c7e62d16403eabf90dda
diff --git a/aidl_language.cpp b/aidl_language.cpp
index b1c4a12..71c79fb 100644
--- a/aidl_language.cpp
+++ b/aidl_language.cpp
@@ -1035,6 +1035,13 @@
AIDL_ERROR(t) << "Nested type '" << GetName() << "' has the same name as its parent.";
success = false;
}
+ // Having unstructured parcelables as nested types doesn't make sense because they are defined
+ // somewhere else in native languages (e.g. C++, Java...).
+ if (AidlCast<AidlParcelable>(*t)) {
+ AIDL_ERROR(t) << "'" << t->GetName()
+ << "' is nested. Unstructured parcelables should be at the root scope.";
+ return false;
+ }
// For now we don't allow "interface" to be nested
if (AidlCast<AidlInterface>(*t)) {
AIDL_ERROR(t) << "'" << t->GetName()
diff --git a/aidl_unittest.cpp b/aidl_unittest.cpp
index c1a2137..e3a761b 100644
--- a/aidl_unittest.cpp
+++ b/aidl_unittest.cpp
@@ -1606,6 +1606,19 @@
EXPECT_THAT(GetCapturedStderr(), HasSubstr("Interfaces should be at the root scope"));
}
+TEST_F(AidlTest, RejectUnstructuredParcelableAsNestedTypes) {
+ const string input_path = "p/IFoo.aidl";
+ const string input =
+ "package p;\n"
+ "interface IFoo {\n"
+ " parcelable Bar cpp_header \"Bar.h\";\n"
+ "}";
+ CaptureStderr();
+ EXPECT_EQ(nullptr, Parse(input_path, input, typenames_, Options::Language::CPP));
+ EXPECT_THAT(GetCapturedStderr(),
+ HasSubstr("Unstructured parcelables should be at the root scope"));
+}
+
TEST_F(AidlTest, HandleSyntaxErrorsInNestedDecl) {
const string input_path = "p/IFoo.aidl";
const string input =