Do not import abstract classes
Right now Crubit generates C++ thunks for instantiating a type, for example:
```
extern "C" void __rust_thunk___Foo1234_(class Foo* __this,
const class Foo& __param_0) {
crubit::construct_at(std::forward<decltype(__this)>(__this),
std::forward<decltype(__param_0)>(__param_0));
}
```
Abstract classes cannot be instantiated (`error: allocating an object of abstract class type`). Instead of fixing that I decided (since it's currently higher priority for me to fix crashes and false positives than increasing coverage) to temporarily disable generating bindings for abstract classes (classes having a pure virtual method).
This CL does not touch non-abstract classes, with or without virtual methods.
PiperOrigin-RevId: 451336767
diff --git a/rs_bindings_from_cc/importers/cxx_record.cc b/rs_bindings_from_cc/importers/cxx_record.cc
index 9272931..b79b877 100644
--- a/rs_bindings_from_cc/importers/cxx_record.cc
+++ b/rs_bindings_from_cc/importers/cxx_record.cc
@@ -44,6 +44,10 @@
if (record_decl->isInjectedClassName()) {
return std::nullopt;
}
+ if (record_decl->hasDefinition() && record_decl->isAbstract()) {
+ return ictx_.ImportUnsupportedItem(
+ record_decl, "Abstract classes are not supported yet");
+ }
if (decl_context->isRecord()) {
return ictx_.ImportUnsupportedItem(record_decl,
"Nested classes are not supported yet");
diff --git a/rs_bindings_from_cc/ir_from_cc_test.rs b/rs_bindings_from_cc/ir_from_cc_test.rs
index 5755da4..e800748 100644
--- a/rs_bindings_from_cc/ir_from_cc_test.rs
+++ b/rs_bindings_from_cc/ir_from_cc_test.rs
@@ -2916,3 +2916,10 @@
}
);
}
+
+#[test]
+// TODO(b/234104583): Delete the test once we support abstract classes.
+fn test_abstract_classes_not_supported() {
+ let ir = ir_from_cc("struct MyStruct { virtual void run() = 0; };").unwrap();
+ assert_ir_matches!(ir, quote! { UnsupportedItem { name: "MyStruct" ... } });
+}
diff --git a/rs_bindings_from_cc/test/golden/inheritance.h b/rs_bindings_from_cc/test/golden/inheritance.h
index 569cfa1..7b7c70e 100644
--- a/rs_bindings_from_cc/test/golden/inheritance.h
+++ b/rs_bindings_from_cc/test/golden/inheritance.h
@@ -29,4 +29,9 @@
class VirtualDerived : public virtual VirtualBase1,
public virtual VirtualBase2 {};
+class MyAbstractClass {
+ virtual void PureVirtualMethod() = 0;
+ virtual ~MyAbstractClass();
+};
+
#endif // CRUBIT_RS_BINDINGS_FROM_CC_TEST_GOLDEN_INHERITANCE_H_
diff --git a/rs_bindings_from_cc/test/golden/inheritance_rs_api.rs b/rs_bindings_from_cc/test/golden/inheritance_rs_api.rs
index 9e4ba58..38f3818 100644
--- a/rs_bindings_from_cc/test/golden/inheritance_rs_api.rs
+++ b/rs_bindings_from_cc/test/golden/inheritance_rs_api.rs
@@ -257,6 +257,10 @@
}
}
+// rs_bindings_from_cc/test/golden/inheritance.h;l=32
+// Error while generating bindings for item 'MyAbstractClass':
+// Abstract classes are not supported yet
+
// CRUBIT_RS_BINDINGS_FROM_CC_TEST_GOLDEN_INHERITANCE_H_
mod detail {