Skip to content

Commit 3635b4e

Browse files
committed
transpile: unconditionally make consts use unsafe blocks for --translate-const-macros conservative
Some operations, such as ptr arithmetic, are `unsafe` and can be done in const macros. So as an initially overly conservative implementation, just make all `const`s use `unsafe` blocks in case `unsafe` operations are used. This is what we already do for `fn`s, for example, even if all of the operations in a `fn` are safe.
1 parent 834db06 commit 3635b4e

3 files changed

Lines changed: 8 additions & 7 deletions

File tree

c2rust-transpile/src/translator/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2152,7 +2152,7 @@ impl<'c> Translation<'c> {
21522152
ctx: ExprContext,
21532153
expansions: &[CExprId],
21542154
) -> TranslationResult<(Box<Expr>, CTypeId)> {
2155-
let (val, ty) = expansions
2155+
let (mut val, ty) = expansions
21562156
.iter()
21572157
.try_fold::<Option<(WithStmts<Box<Expr>>, CTypeId)>, _, _>(None, |canonical, &id| {
21582158
self.can_convert_const_macro_expansion(id)?;
@@ -2189,6 +2189,7 @@ impl<'c> Translation<'c> {
21892189
})?
21902190
.ok_or_else(|| format_err!("Could not find a valid type for macro"))?;
21912191

2192+
val.set_unsafe();
21922193
val.to_unsafe_pure_expr()
21932194
.map(|val| (val, ty))
21942195
.ok_or_else(|| TranslationError::generic("Macro expansion is not a pure expression"))

c2rust-transpile/tests/snapshots/snapshots__transpile@atomics.c.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ pub unsafe extern "C" fn c11_atomics(mut x: std::ffi::c_int) -> std::ffi::c_int
3737
fresh1.1;
3838
return x;
3939
}
40-
pub const __ATOMIC_SEQ_CST: std::ffi::c_int = 5 as std::ffi::c_int;
40+
pub const __ATOMIC_SEQ_CST: std::ffi::c_int = unsafe { 5 as std::ffi::c_int };

c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ pub struct fn_ptrs {
3030
pub fn2: Option<unsafe extern "C" fn(std::ffi::c_int) -> std::ffi::c_int>,
3131
}
3232
pub type zstd_platform_dependent_type = std::ffi::c_long;
33-
pub const UINTPTR_MAX: std::ffi::c_ulong = 18446744073709551615 as std::ffi::c_ulong;
34-
pub const NESTED_INT: std::ffi::c_int = 0xffff as std::ffi::c_int;
33+
pub const UINTPTR_MAX: std::ffi::c_ulong = unsafe { 18446744073709551615 as std::ffi::c_ulong };
34+
pub const NESTED_INT: std::ffi::c_int = unsafe { 0xffff as std::ffi::c_int };
3535
#[no_mangle]
3636
pub unsafe extern "C" fn local_muts() {
3737
let mut literal_int: std::ffi::c_int = 0xffff as std::ffi::c_int;
@@ -367,7 +367,7 @@ pub static mut global_const_member: std::ffi::c_int = 0;
367367
pub unsafe extern "C" fn test_fn_macro(mut x: std::ffi::c_int) -> std::ffi::c_int {
368368
return x * x;
369369
}
370-
pub const TEST_CONST2: std::ffi::c_int = 2 as std::ffi::c_int;
370+
pub const TEST_CONST2: std::ffi::c_int = unsafe { 2 as std::ffi::c_int };
371371
#[no_mangle]
372372
pub unsafe extern "C" fn reference_define() -> std::ffi::c_int {
373373
let mut x: std::ffi::c_int = 1 as std::ffi::c_int;
@@ -388,8 +388,8 @@ pub static mut fns: fn_ptrs = {
388388
};
389389
#[no_mangle]
390390
pub static mut p: *const fn_ptrs = unsafe { &fns as *const fn_ptrs };
391-
pub const ZSTD_WINDOWLOG_MAX_32: std::ffi::c_int = 30 as std::ffi::c_int;
392-
pub const ZSTD_WINDOWLOG_MAX_64: std::ffi::c_int = 31 as std::ffi::c_int;
391+
pub const ZSTD_WINDOWLOG_MAX_32: std::ffi::c_int = unsafe { 30 as std::ffi::c_int };
392+
pub const ZSTD_WINDOWLOG_MAX_64: std::ffi::c_int = unsafe { 31 as std::ffi::c_int };
393393
#[no_mangle]
394394
pub unsafe extern "C" fn test_zstd() -> U64 {
395395
return (if ::core::mem::size_of::<zstd_platform_dependent_type>() as std::ffi::c_ulong

0 commit comments

Comments
 (0)