C2Rust generates invalid Rust code when translating C wide string literals (L"...").
The generated code takes the address of a temporary value produced by core::mem::transmute, which fails to compile with:
error[E0745]: cannot take address of a temporary
In C, wide string literals have static storage duration, but the generated Rust code does not preserve this property.
Original C Program
#include <stdio.h>
#include <wchar.h>
int main() {
const wchar_t* wstr = L"Wide String";
printf("Wide string, First char code=%d\n", (int)wstr[0]);
return 0;
}
C2Rust-Generated Rust
#![allow(
dead_code,
non_camel_case_types,
non_snake_case,
non_upper_case_globals,
unused_assignments,
unused_mut
)]
#![feature(raw_ref_op)]
extern "C" {
fn printf(__format: *const ::core::ffi::c_char, ...) -> ::core::ffi::c_int;
}
pub type wchar_t = ::libc::wchar_t;
unsafe fn main_0() -> ::core::ffi::c_int {
let mut wstr: *const wchar_t = &raw const ::core::mem::transmute::<
[u8; 48],
[::core::ffi::c_int; 12],
>(
*b"W\0\0\0i\0\0\0d\0\0\0e\0\0\0 \0\0\0S\0\0\0t\0\0\0r\0\0\0i\0\0\0n\0\0\0g\0\0\0\0\0\0\0",
) as *const wchar_t;
printf(
b"Wide string, First char code=%d\n\0" as *const u8
as *const ::core::ffi::c_char,
*wstr.offset(0 as ::core::ffi::c_int as isize) as ::core::ffi::c_int,
);
return 0 as ::core::ffi::c_int;
}
pub fn main() {
unsafe { ::std::process::exit(main_0() as i32) }
}
Observed Behavior
Compilation fails with:
error[E0745]: cannot take address of a temporary
because &raw const transmute(...) takes the address of a temporary value, whose lifetime ends immediately.
Environment
- c2rust version: v0.21.0
- rustc version: 1.91.1
C2Rust generates invalid Rust code when translating C wide string literals (L"...").
The generated code takes the address of a temporary value produced by
core::mem::transmute, which fails to compile with:In C, wide string literals have static storage duration, but the generated Rust code does not preserve this property.
Original C Program
C2Rust-Generated Rust
Observed Behavior
Compilation fails with:
because
&raw const transmute(...)takes the address of a temporary value, whose lifetime ends immediately.Environment