union: default constructor inits with first member

CheckValid() checks if union is not-empty.

Bug: 150948558
Test: aidl_unittests / aidl_integration_test
Change-Id: I493b27c84977c3984d599ff7a7a32b0acdd5457f
diff --git a/aidl_unittest.cpp b/aidl_unittest.cpp
index 0471570..8848c82 100644
--- a/aidl_unittest.cpp
+++ b/aidl_unittest.cpp
@@ -2692,6 +2692,23 @@
   EXPECT_EQ(expected_stderr, GetCapturedStderr());
 }
 
+TEST_P(AidlTest, UnionRejectsEmptyDecl) {
+  const string method = "package a; union Foo {}";
+  const string expected_stderr = "ERROR: a/Foo.aidl:1.17-21: The union 'Foo' has no fields.\n";
+  CaptureStderr();
+  EXPECT_EQ(nullptr, Parse("a/Foo.aidl", method, typenames_, GetLanguage()));
+  EXPECT_THAT(GetCapturedStderr(), testing::HasSubstr(expected_stderr));
+}
+
+TEST_P(AidlTest, UnionRejectsParcelableHolder) {
+  const string method = "package a; union Foo { ParcelableHolder x; }";
+  const string expected_stderr =
+      "ERROR: a/Foo.aidl:1.40-42: A union can't have a member of ParcelableHolder 'x'\n";
+  CaptureStderr();
+  EXPECT_EQ(nullptr, Parse("a/Foo.aidl", method, typenames_, GetLanguage()));
+  EXPECT_THAT(GetCapturedStderr(), testing::HasSubstr(expected_stderr));
+}
+
 TEST_P(AidlTest, GenericStructuredParcelable) {
   io_delegate_.SetFileContents("Foo.aidl", "parcelable Foo<T, U> { int a; int A; }");
   Options options =