Skip to content

Commit 2d96d13

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 94032ee commit 2d96d13

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

c2rust-transpile/src/translator/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2192,7 +2192,7 @@ impl<'c> Translation<'c> {
21922192
ctx: ExprContext,
21932193
expansions: &[CExprId],
21942194
) -> TranslationResult<(Box<Expr>, CTypeId)> {
2195-
let (val, ty) = expansions
2195+
let (mut val, ty) = expansions
21962196
.iter()
21972197
.try_fold::<Option<(WithStmts<Box<Expr>>, CTypeId)>, _, _>(None, |canonical, &id| {
21982198
self.can_convert_const_macro_expansion(id)?;
@@ -2229,6 +2229,7 @@ impl<'c> Translation<'c> {
22292229
})?
22302230
.ok_or_else(|| format_err!("Could not find a valid type for macro"))?;
22312231

2232+
val.set_unsafe();
22322233
val.to_unsafe_pure_expr()
22332234
.map(|val| (val, ty))
22342235
.ok_or_else(|| TranslationError::generic("Macro expansion is not a pure expression"))

c2rust-transpile/tests/snapshots/[email protected]

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/[email protected]

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub struct fn_ptrs {
2929
pub fn2: Option<unsafe extern "C" fn(std::ffi::c_int) -> std::ffi::c_int>,
3030
}
3131
pub type zstd_platform_dependent_type = std::ffi::c_long;
32-
pub const NESTED_INT: std::ffi::c_int = 0xffff as std::ffi::c_int;
32+
pub const NESTED_INT: std::ffi::c_int = unsafe { 0xffff as std::ffi::c_int };
3333
#[no_mangle]
3434
pub unsafe extern "C" fn local_muts() {
3535
let mut literal_int: std::ffi::c_int = 0xffff as std::ffi::c_int;
@@ -323,7 +323,7 @@ pub static mut global_const_str_concatenation: [std::ffi::c_char; 18] = unsafe {
323323
pub unsafe extern "C" fn test_fn_macro(mut x: std::ffi::c_int) -> std::ffi::c_int {
324324
return x * x;
325325
}
326-
pub const TEST_CONST2: std::ffi::c_int = 2 as std::ffi::c_int;
326+
pub const TEST_CONST2: std::ffi::c_int = unsafe { 2 as std::ffi::c_int };
327327
#[no_mangle]
328328
pub unsafe extern "C" fn reference_define() -> std::ffi::c_int {
329329
let mut x: std::ffi::c_int = 1 as std::ffi::c_int;
@@ -344,8 +344,8 @@ pub static mut fns: fn_ptrs = {
344344
};
345345
#[no_mangle]
346346
pub static mut p: *const fn_ptrs = unsafe { &fns as *const fn_ptrs };
347-
pub const ZSTD_WINDOWLOG_MAX_32: std::ffi::c_int = 30 as std::ffi::c_int;
348-
pub const ZSTD_WINDOWLOG_MAX_64: std::ffi::c_int = 31 as std::ffi::c_int;
347+
pub const ZSTD_WINDOWLOG_MAX_32: std::ffi::c_int = unsafe { 30 as std::ffi::c_int };
348+
pub const ZSTD_WINDOWLOG_MAX_64: std::ffi::c_int = unsafe { 31 as std::ffi::c_int };
349349
#[no_mangle]
350350
pub unsafe extern "C" fn test_zstd() -> U64 {
351351
return (if ::core::mem::size_of::<zstd_platform_dependent_type>() as std::ffi::c_ulong

0 commit comments

Comments
 (0)