Skip to content

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

Merged
ahomescu merged 2 commits into
masterfrom
ahomescu/hoist_static_initializers
Aug 27, 2026
Merged

transpile: hoist function-local statics to the root of the current module#1983
ahomescu merged 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?

@ahomescu

Copy link
Copy Markdown
Contributor Author

Done, the test now produces:

static mut rust_sectioned_ptr_0: uintptr_t = 0;
static mut rust_sectioned_ptr_1: uintptr_t = 0;
pub static mut rust_sectioned_ptr: uintptr_t = 0;
// ...
    rust_sectioned_ptr = NULL.expose_addr() as uintptr_t;
    rust_sectioned_ptr_0 = (&raw mut rust_section_me).expose_addr() as uintptr_t;
    rust_sectioned_ptr_1 = (&raw mut rust_section_me3).expose_addr() as uintptr_t;

@ahomescu
ahomescu force-pushed the ahomescu/hoist_static_initializers branch from b975698 to f7a5a0c Compare August 25, 2026 21:38
@ahomescu
ahomescu force-pushed the ahomescu/hoist_static_initializers branch from f7a5a0c to f4edefd Compare August 25, 2026 21:40

@fw-immunant fw-immunant left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@ahomescu
ahomescu merged commit 0049b39 into master Aug 27, 2026
11 checks passed
@ahomescu
ahomescu deleted the ahomescu/hoist_static_initializers branch August 27, 2026 01:23
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