rs_bindings_from_cc: Add `#[allow(unused_imports)]` to generated re-export statements.
[A recent rustc commit](https://github.com/rust-lang/rust/pull/116033/commits/482275b19422b871b986ec0400257a9b80080218) began to flag re-exports of empty modules as a case of unused_imports, which is a warning that cause Crubit-generated code to fail to compile: Crubit may fail to generate bindings for symbols in C++ namespaces but we'd still generate `mod __helper_mod_index` for it and `pub mod canonical_mod`.
Add `#[allow(unused_import)]` to disable it for now to avoid CI failure. However, in the long term, we should check if module to be reexported is empty (transitively) before emitting the re-export, to make generated bindings more ergonomic and avoid hiding other kinds of unused_imports errors.
PiperOrigin-RevId: 579007904
Change-Id: I34488cad3a2b68030e5bdf58f9c1543467b26803
diff --git a/rs_bindings_from_cc/src_code_gen.rs b/rs_bindings_from_cc/src_code_gen.rs
index 4a73a3f..36860be 100644
--- a/rs_bindings_from_cc/src_code_gen.rs
+++ b/rs_bindings_from_cc/src_code_gen.rs
@@ -2651,11 +2651,22 @@
&namespace.name.identifier,
reopened_namespace_idx - 1
));
- quote! { pub use super::#previous_namespace_ident::*; __NEWLINE__ __NEWLINE__ }
+ // unused_imports warns a re-export of an empty module. Currently, there is no
+ // infra in Crubit to tell if the (generated) module is empty, so we
+ // emit `allow(unused_imports)`. TODO(b/308949532): Skip re-export if
+ // previous module is empty (transitively).
+ quote! {
+ __HASH_TOKEN__ [allow(unused_imports)]
+ pub use super::#previous_namespace_ident::*; __NEWLINE__ __NEWLINE__
+ }
};
-
let use_stmt_for_inline_namespace = if namespace.is_inline && is_canonical_namespace_module {
- quote! {pub use #name::*; __NEWLINE__}
+ // TODO(b/308949532): Skip re-export if the canonical module is empty
+ // (transitively).
+ quote! {
+ __HASH_TOKEN__ [allow(unused_imports)]
+ pub use #name::*; __NEWLINE__
+ }
} else {
quote! {}
};
@@ -2795,9 +2806,12 @@
Item::UseMod(use_mod) => {
let UseMod { path, mod_name, .. } = &**use_mod;
let mod_name = make_rs_ident(&mod_name.identifier);
+ // TODO(b/308949532): Skip re-export if the module being used is empty
+ // (transitively).
quote! {
#[path = #path]
mod #mod_name;
+ __HASH_TOKEN__ [allow(unused_imports)]
pub use #mod_name::*;
}
.into()
@@ -8825,9 +8839,11 @@
}
...
pub mod test_namespace_bindings {
+ __HASH_TOKEN__[allow(unused_imports)]
pub use super::test_namespace_bindings_0::*;
...
pub mod inner {
+ __HASH_TOKEN__[allow(unused_imports)]
pub use super::inner_0::*;
...
}
@@ -8893,6 +8909,7 @@
...
pub struct MyStruct {...} ...
}
+ __HASH_TOKEN__[allow(unused_imports)]
pub use inner::*;
...
pub fn processMyStruct(
@@ -8931,11 +8948,13 @@
pub mod my_inline_0 {}
pub mod foo {}
pub mod my_inline {
+ __HASH_TOKEN__[allow(unused_imports)]
pub use super::my_inline_0::*;
...
pub struct MyStruct {...}
...
}
+ __HASH_TOKEN__[allow(unused_imports)]
pub use my_inline::*;
...
}
diff --git a/rs_bindings_from_cc/test/golden/namespace_rs_api.rs b/rs_bindings_from_cc/test/golden/namespace_rs_api.rs
index d0f2427..0a88799 100644
--- a/rs_bindings_from_cc/test/golden/namespace_rs_api.rs
+++ b/rs_bindings_from_cc/test/golden/namespace_rs_api.rs
@@ -176,6 +176,7 @@
// namespace test_namespace_bindings_reopened
pub mod test_namespace_bindings_reopened {
+ #[allow(unused_imports)]
pub use super::test_namespace_bindings_reopened_0::*;
#[inline(always)]
@@ -184,6 +185,7 @@
}
pub mod inner {
+ #[allow(unused_imports)]
pub use super::inner_0::*;
#[inline(always)]
@@ -255,6 +257,7 @@
}
}
}
+ #[allow(unused_imports)]
pub use inner::*;
// namespace inner