From 7b82bd7e57419c226f878cd69a5aab3980968764 Mon Sep 17 00:00:00 2001 From: Per Larsen Date: Thu, 18 Jun 2026 03:20:44 -0700 Subject: [PATCH] transpile: fix zero initializer imports --- c2rust-transpile/src/translator/mod.rs | 3 +-- c2rust-transpile/tests/snapshots.rs | 15 +++++++++++++++ .../tests/snapshots/zero_init_typedef_reorg.c | 17 +++++++++++++++++ .../tests/snapshots/zero_init_typedef_reorg.h | 4 ++++ 4 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 c2rust-transpile/tests/snapshots/zero_init_typedef_reorg.c create mode 100644 c2rust-transpile/tests/snapshots/zero_init_typedef_reorg.h diff --git a/c2rust-transpile/src/translator/mod.rs b/c2rust-transpile/src/translator/mod.rs index 81a859995c..98ce1dab0a 100644 --- a/c2rust-transpile/src/translator/mod.rs +++ b/c2rust-transpile/src/translator/mod.rs @@ -3918,8 +3918,6 @@ impl<'c> Translation<'c> { decl_id: CDeclId, type_id: CTypeId, ) -> TranslationResult>> { - self.import_type(type_id); - let name = self.type_converter.borrow().resolve_decl_name(decl_id); let name = name.as_deref().unwrap_or("???"); @@ -3934,6 +3932,7 @@ impl<'c> Translation<'c> { let func_name = func_name.as_deref().unwrap_or(""); log::debug!("deferring imports to save them for {name} in {func_name}"); self.defer_imports(); + self.import_type(type_id); let name_decl_id = match self.ast_context.resolve_type_no_typedef(type_id).kind { CTypeKind::Typedef(decl_id) => decl_id, diff --git a/c2rust-transpile/tests/snapshots.rs b/c2rust-transpile/tests/snapshots.rs index ca9727c3eb..62cd70b1f9 100644 --- a/c2rust-transpile/tests/snapshots.rs +++ b/c2rust-transpile/tests/snapshots.rs @@ -570,6 +570,21 @@ fn test_wide_strings() { .run(); } +#[test] +fn test_zero_init_typedef_reorg_imports() { + let c_path = Path::new("tests/snapshots/zero_init_typedef_reorg.c"); + let mut cfg = config(Edition2021); + cfg.reorganize_definitions = true; + cfg.disable_refactoring = true; + compile_and_transpile_file(c_path, cfg); + + let rs_path = c_path.with_extension("rs"); + rustc(&rs_path) + .edition(Edition2021) + .crate_name("zero_init_typedef_reorg") + .run(); +} + // arch-os-specific #[test] diff --git a/c2rust-transpile/tests/snapshots/zero_init_typedef_reorg.c b/c2rust-transpile/tests/snapshots/zero_init_typedef_reorg.c new file mode 100644 index 0000000000..707ee4c255 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/zero_init_typedef_reorg.c @@ -0,0 +1,17 @@ +typedef struct Stat { + int mode; + long size; +} STAT_T; + +static void before_include(void) { + STAT_T st = {0}; + (void)st; +} + +#include "zero_init_typedef_reorg.h" + +int main(void) { + before_include(); + after_include(); + return 0; +} diff --git a/c2rust-transpile/tests/snapshots/zero_init_typedef_reorg.h b/c2rust-transpile/tests/snapshots/zero_init_typedef_reorg.h new file mode 100644 index 0000000000..8e8178c2db --- /dev/null +++ b/c2rust-transpile/tests/snapshots/zero_init_typedef_reorg.h @@ -0,0 +1,4 @@ +static void after_include(void) { + STAT_T st = {0}; + (void)st; +}