Improve error messages for unsupported fields and types.
1. Preserve the original, field-specific error message when ImportRecord
propagates an error from ImportFields
2. When ImportFields repors problems with the type of a field, then the
error message should include the name of the field and should
propagate the information from ConvertType. (The type of the field
will be covered by the propagated information.)
PiperOrigin-RevId: 432430883
diff --git a/rs_bindings_from_cc/importer.cc b/rs_bindings_from_cc/importer.cc
index 6d99e70..0490ad7 100644
--- a/rs_bindings_from_cc/importer.cc
+++ b/rs_bindings_from_cc/importer.cc
@@ -773,7 +773,7 @@
// Importing a field failed, so note that we didn't import this RecordDecl
// after all.
known_type_decls_.erase(record_decl);
- return LookupResult("Importing field failed");
+ return LookupResult(fields.status().ToString());
}
for (const Field& field : *fields) {
@@ -1090,9 +1090,9 @@
std::optional<devtools_rust::TypeLifetimes> no_lifetimes;
auto type = ConvertType(field_decl->getType(), no_lifetimes);
if (!type.ok()) {
- return absl::UnimplementedError(
- absl::Substitute("Field type '$0' is not supported",
- field_decl->getType().getAsString()));
+ return absl::UnimplementedError(absl::Substitute(
+ "Type of field '$0' is not supported: $1",
+ field_decl->getNameAsString(), type.status().message()));
}
clang::AccessSpecifier access = field_decl->getAccess();
if (access == clang::AS_none) {
diff --git a/rs_bindings_from_cc/ir_from_cc_test.rs b/rs_bindings_from_cc/ir_from_cc_test.rs
index 5cc68d4..fbbb160 100644
--- a/rs_bindings_from_cc/ir_from_cc_test.rs
+++ b/rs_bindings_from_cc/ir_from_cc_test.rs
@@ -673,6 +673,35 @@
}}
);
}
+
+#[test]
+fn test_record_with_unsupported_field() -> Result<()> {
+ // Using `union` because (I assume) it won't be supported in the long-term.
+ // But... any other unsupported type would also work for this test.
+ let ir = ir_from_cc(
+ r#"
+ union MyUnion {
+ int i;
+ float f;
+ };
+ struct StructWithUnsupportedField {
+ MyUnion my_field;
+ };
+ "#,
+ )?;
+ assert_ir_matches!(
+ ir,
+ quote! {
+ UnsupportedItem(UnsupportedItem {
+ name: "StructWithUnsupportedField",
+ message: "UNIMPLEMENTED: Type of field 'my_field' is not supported: Unsupported type 'union MyUnion'",
+ ...
+ })
+ }
+ );
+ Ok(())
+}
+
#[test]
fn test_do_not_import_static_member_functions_when_record_not_supported_yet() {
// only using nested struct as an example of a record we cannot import yet.