Skip to content

Fix varargs on arm #1281

@kkysen

Description

@kkysen

In trying to run CI on arm64 macOS (#1280, CI), the varargs implementation isn't working correctly. So this is a prerequisite to fixing #1279.

Errors from CI
pointers:

 [ RUNNING ] libtest.a: creating a static C library...
 [ RUNNING ] function_pointers.c: translating the C file into Rust...
 [ RUNNING ] ref_decay.c: translating the C file into Rust...
 [ RUNNING ] pointer_init.c: translating the C file into Rust...
 [ RUNNING ] pointer_arith.c: translating the C file into Rust...
 [ FAILED ] compile main.rs
    Updating crates.io index
   Compiling libc v0.2.174
   Compiling pointer-tests v0.1.0 (/Users/runner/work/c2rust/c2rust/tests/pointers)
warning: unused imports: `rust_address_cast`, `rust_bar`, `rust_bitcast`, `rust_calls_all`, `rust_f`, `rust_foobar`
  --> src/test_pointers.rs:7:5
   |
7  |     rust_address_cast, rust_bar, rust_bitcast, rust_calls_all, rust_f, rust_foobar,
   |     ^^^^^^^^^^^^^^^^^  ^^^^^^^^  ^^^^^^^^^^^^  ^^^^^^^^^^^^^^  ^^^^^^  ^^^^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default
help: consider adding a `#[cfg(test)]` to the containing module
  --> src/main.rs:12:1
   |
12 | pub mod test_pointers;
   | ^^^^^^^^^^^^^^^^^^^^^^

warning: crate-level attribute should be in the root module
  --> src/function_pointers.rs:10:1
   |
10 | #![feature(c_variadic)]
   | ^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(unused_attributes)]` on by default

warning: the feature `nll` has been stable since 1.63.0 and no longer requires an attribute to enable
 --> src/main.rs:3:12
  |
3 | #![feature(nll)]
  |            ^^^
  |
  = note: `#[warn(stable_features)]` on by default

error[E0[308](https://github.com/immunant/c2rust/actions/runs/16262809558/job/45911744827#step:12:309)]: mismatched types
  --> src/function_pointers.rs:50:11
   |
49 |     let mut arg: va_list = 0 as *mut std::ffi::c_char;
   |                  ------- expected due to this type
50 |     arg = args.clone();
   |           ^^^^^^^^^^^^ expected *-ptr, found struct `VaListImpl`
   |
   = note: expected raw pointer `*mut i8`
                   found struct `VaListImpl<'_>`

error[E0599]: no method named `arg` found for raw pointer `*mut i8` in the current scope
  --> src/function_pointers.rs:52:13
   |
52 |         arg.arg::<*mut unsafe extern "C" fn(std::ffi::c_char) -> std::ffi::c_int>(),
   |             ^^^ method not found in `*mut i8`
   |
   = note: try using `<*const T>::as_ref()` to get a reference to the type behind the pointer: https://doc.rust-lang.org/std/primitive.pointer.html#method.as_ref
   = note: using `<*const T>::as_ref()` on a pointer which is unaligned or points to invalid or uninitialized memory is undefined behavior

Some errors have detailed explanations: E0308, E0599.
For more information about an error, try `rustc --explain E0308`.
warning: `pointer-tests` (bin "pointer-tests") generated 3 warnings
error: could not compile `pointer-tests` due to 2 previous errors; 3 warnings emitted

items:

 [ FAILED ] compile main.rs
   Compiling items-tests v0.1.0 (/Users/runner/work/c2rust/c2rust/tests/items)
error[E0432]: unresolved imports `crate::varargs::rust_valist_struct_member`, `crate::varargs::rust_valist_struct_pointer_member`
 --> src/test_varargs.rs:5:25
  |
5 |     rust_simple_vacopy, rust_valist_struct_member, rust_valist_struct_pointer_member,
  |                         ^^^^^^^^^^^^^^^^^^^^^^^^^  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no `rust_valist_struct_pointer_member` in `varargs`
  |                         |
  |                         no `rust_valist_struct_member` in `varargs`

warning: unused imports: `rust_ensure_use`, `rust_inline_extern`, `rust_noinline_nonstatic`
  --> src/test_fn_attrs.rs:1:23
   |
1  | use crate::fn_attrs::{rust_ensure_use, rust_inline_extern, rust_noinline_nonstatic};
    = note: expected raw pointer `*mut i8`
                    found struct `VaListImpl<'_>`

error[E0308]: mismatched types
   --> src/varargs.rs:112:10
    |
109 |     let mut ap: va_list = 0 as *mut std::ffi::c_char;
    |                 ------- expected due to this type
...
112 |     ap = args.clone();
    |          ^^^^^^^^^^^^ expected *-ptr, found struct `VaListImpl`
    |
    = note: expected raw pointer `*mut i8`
                    found struct `VaListImpl<'_>`

error[E0308]: mismatched types
   --> src/varargs.rs:122:13
    |
121 |     let mut args1: va_list = 0 as *mut std::ffi::c_char;
    |                    ------- expected due to this type
122 |     args1 = args.clone();
    |             ^^^^^^^^^^^^ expected *-ptr, found struct `VaListImpl`
    |
    = note: expected raw pointer `*mut i8`
                    found struct `VaListImpl<'_>`

error[E0599]: no method named `arg` found for raw pointer `*mut i8` in the current scope
   --> src/varargs.rs:127:49
    |
127 |         let mut num: std::ffi::c_double = args1.arg::<std::ffi::c_double>();
    |                                                 ^^^ method not found in `*mut i8`
    |
    = note: try using `<*const T>::as_ref()` to get a reference to the type behind the pointer: https://doc.rust-lang.org/std/primitive.pointer.html#method.as_ref
    = note: using `<*const T>::as_ref()` on a pointer which is unaligned or points to invalid or uninitialized memory is undefined behavior

error[E0599]: no method named `arg` found for raw pointer `*mut i8` in the current scope
   --> src/varargs.rs:136:51
    |
136 |         let mut num_0: std::ffi::c_double = args2.arg::<std::ffi::c_double>();
    |                                                   ^^^ method not found in `*mut i8`
    |
    = note: try using `<*const T>::as_ref()` to get a reference to the type behind the pointer: https://doc.rust-lang.org/std/primitive.pointer.html#method.as_ref
    = note: using `<*const T>::as_ref()` on a pointer which is unaligned or points to invalid or uninitialized memory is undefined behavior

Some errors have detailed explanations: E0308, E0432, E0599.
For more information about an error, try `rustc --explain E0308`.
warning: `items-tests` (bin "items-tests") generated 4 warnings
error: could not compile `items-tests` due to 12 previous errors; 4 warnings emitted

Metadata

Metadata

Assignees

No one assigned

    Labels

    mistranslationTranslating a well-behaved program changes its behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions