union requires first member's default value
Default constructor of union type should initialize with the first
member. Java backend was failing with union types when their first member
is of enum type because enum type in Java backend is primitive but with
no default value. (To be specific, the generated default constructor was
trying to init primitive type(enum) with "null").
Now union's first member should have a useful default value.
- set explicit default value (e.g. int a = 3;)
- @nullable annotated.
So, when the first member of a union is an enum, it should have a
default value. Or, it will fail with an error.
Bug: 175834770
Test: aidl_unittests / aidl_integration_test
Change-Id: I6a0acc25331927826a8e747e6ea703f34dccae94
diff --git a/aidl_checkapi.cpp b/aidl_checkapi.cpp
index c3eff08..b54ddbb 100644
--- a/aidl_checkapi.cpp
+++ b/aidl_checkapi.cpp
@@ -191,19 +191,6 @@
return compatible;
}
-// returns whether the given type when defaulted will be accepted by
-// unmarshalling code
-static bool has_usable_nil_type(const AidlTypeSpecifier& specifier) {
- // TODO(b/155238508): fix for primitives
-
- // This technically only applies in C++, but even if both the client and the
- // server of an interface are in Java at a particular point in time, where
- // null is currently always acceptable, we want to make sure that versions
- // of this service can work in native and future backends without a problem.
- // Also, in that case, adding nullable does not hurt.
- return specifier.IsNullable();
-}
-
static bool HasZeroEnumerator(const AidlEnumDeclaration& enum_decl) {
return std::any_of(enum_decl.GetEnumerators().begin(), enum_decl.GetEnumerators().end(),
[&](const unique_ptr<AidlEnumerator>& enumerator) {
@@ -261,12 +248,7 @@
for (size_t i = old_fields.size(); i < new_fields.size(); i++) {
const auto& new_field = new_fields.at(i);
- if (new_field->GetDefaultValue()) {
- continue;
- }
-
- // null is accepted as a valid default value
- if (has_usable_nil_type(new_field->GetType())) {
+ if (new_field->HasUsefulDefaultValue()) {
continue;
}