Skip to content

transpile: hoist function-local statics to the root of the current module - #1983

Open
ahomescu wants to merge 2 commits into
masterfrom
ahomescu/hoist_static_initializers
Open

transpile: hoist function-local statics to the root of the current module#1983
ahomescu wants to merge 2 commits into
masterfrom
ahomescu/hoist_static_initializers

Conversation

@ahomescu

Copy link
Copy Markdown
Contributor

Function-local statics with "uncompilable" initializers are referenced from c2rust_run_static_initializers but previously emitted inside the owner function. Hoist them to the top of the current module so c2rust_run_static_initializers can reference them by name. Example from issue #1981:

int main() {
  static const int local_static_arr[] = { 1, 2, 3 };
  static const struct foo local_static_s[] = { local_static_arr, ARR_SIZE(local_static_arr) };
  return 0;
}

Here local_static_s and local_static_arr (because the former references it) need to be moved out of main.

Fixes issue #1981.

@ahomescu

ahomescu commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

Here is the new Rust output for the test at the top level of sections.rs:

static mut rust_local_static_arr: [::core::ffi::c_int; 3] = [
    1 as ::core::ffi::c_int,
    2 as ::core::ffi::c_int,
    3 as ::core::ffi::c_int,
];
static mut rust_local_static_s: [slice_ref; 1] = [slice_ref {
    p: ::core::ptr::null::<::core::ffi::c_int>(),
    len: 0,
}; 1];
static mut rust_chain_a: [::core::ffi::c_int; 3] = [
    5 as ::core::ffi::c_int,
    6 as ::core::ffi::c_int,
    7 as ::core::ffi::c_int,
];
static mut rust_chain_b: *mut ::core::ffi::c_int =
    unsafe { &raw const rust_chain_a as *mut ::core::ffi::c_int };
static mut rust_chain_c: *mut *mut ::core::ffi::c_int =
    ::core::ptr::null_mut::<*mut ::core::ffi::c_int>();

@ahomescu
ahomescu force-pushed the ahomescu/hoist_static_initializers branch from f954554 to b975698 Compare August 25, 2026 03:00
@fw-immunant

Copy link
Copy Markdown
Contributor

Could the test here also exercise the renamer by including multiple functions with distinct, identically-named statics?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants