Skip to content

c2rust_run_static_initializers fails with function-scoped statics #1981

Description

@brk

Small reproducer:

struct foo { int* x; int nelts; };

#define ARR_SIZE(a) (sizeof(a)/sizeof(a[0]))

/* 
globals are supported
     const int global_arr[] = { 1, 2, 3 };
     const struct foo global_s[] = { global_arr, ARR_SIZE(global_arr) };
*/

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;
}

Relevant parts of translation:

static mut local_static_s: [foo; 1] = [foo { x: ::core::ptr::null_mut::<::core::ffi::c_int>(), nelts: 0, }; 1];

unsafe fn main_0() -> ::core::ffi::c_int {
    static mut local_static_arr: [::core::ffi::c_int; 3] = [ 1 as ::core::ffi::c_int, 2, 3 ];
    return 0 as ::core::ffi::c_int;
}

unsafe extern "C" fn c2rust_run_static_initializers() {
    local_static_s = [foo {
        x: &raw const local_static_arr as *const ::core::ffi::c_int as *mut ::core::ffi::c_int,
        nelts: ::core::mem::size_of::<[::core::ffi::c_int; 3]>()
            .wrapping_div(::core::mem::size_of::<::core::ffi::c_int>())
            as ::core::ffi::c_int,
    }];
}

which fails because local_static_arr is not in scope within fn c2rust_run_static_initializers().

This bug affects https://github.com/mity/md4c

Activity

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions