Assert that function pointers always have a `'static` lifetime.
PiperOrigin-RevId: 520351745
diff --git a/rs_bindings_from_cc/importer.cc b/rs_bindings_from_cc/importer.cc
index 229e8aa..4259e8f 100644
--- a/rs_bindings_from_cc/importer.cc
+++ b/rs_bindings_from_cc/importer.cc
@@ -773,6 +773,8 @@
}
if (const auto* func_type =
pointee_type->getAs<clang::FunctionProtoType>()) {
+ // TODO(b/275628345): Replace with `CHECK(!lifetime.has_value())` after
+ // function pointers are not allowed to have any lifetime annotations.
if (lifetime.has_value() &&
lifetime->value() !=
clang::tidy::lifetimes::Lifetime::Static().Id()) {
diff --git a/rs_bindings_from_cc/src_code_gen.rs b/rs_bindings_from_cc/src_code_gen.rs
index cb86cb9..548b5e2 100644
--- a/rs_bindings_from_cc/src_code_gen.rs
+++ b/rs_bindings_from_cc/src_code_gen.rs
@@ -3647,8 +3647,20 @@
match name.strip_prefix("#funcPtr ") {
None => RsTypeKind::Other { name: name.into(), type_args: Rc::from(type_args) },
Some(abi) => {
- // TODO(b/254858027): Consider enforcing `'static` lifetime.
- ensure!(!type_args.is_empty(), "No return type in fn type: {:?}", ty);
+ // Assert that function pointers in the IR either have static lifetime or
+ // no lifetime.
+ // TODO(b/275628345): Replace with `assert!(ty.lifetime_args.is_empty())`
+ // after function pointers are not allowed to have any lifetime
+ // annotations.
+ match get_lifetime() {
+ Err(_) => (), // No lifetime
+ Ok(lifetime) => assert_eq!(lifetime.0.as_ref(), "static"),
+ }
+
+ assert!(
+ !type_args.is_empty(),
+ "In well-formed IR function pointers include at least the return type",
+ );
RsTypeKind::FuncPtr {
abi: abi.into(),
return_type: Rc::new(type_args.remove(type_args.len() - 1)),