Skip to content

Commit ba9d215

Browse files
committed
Fix tests
1 parent 3c5036a commit ba9d215

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

c2rust-transpile/src/translator/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4508,7 +4508,7 @@ impl<'c> Translation<'c> {
45084508
{
45094509
Ok(val)
45104510
} else {
4511-
let mutbl = if is_const {
4511+
let mutbl = if is_const || ctx.is_static {
45124512
Mutability::Immutable
45134513
} else {
45144514
Mutability::Mutable

c2rust-transpile/src/translator/operators.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,7 @@ impl<'c> Translation<'c> {
901901
lrvalue: LRValue,
902902
) -> TranslationResult<WithStmts<Box<Expr>>> {
903903
let CQualTypeId { ctype, .. } = cqual_type;
904+
let ty = self.convert_type(ctype)?;
904905
let resolved_ctype = self.ast_context.resolve_type(ctype);
905906

906907
let mut unary = match name {
@@ -941,8 +942,19 @@ impl<'c> Translation<'c> {
941942
Mutability::Mutable
942943
};
943944

944-
self.use_feature("raw_ref_op");
945-
Ok(arg.map(|a| mk().set_mutbl(mutbl).raw_borrow_expr(a)))
945+
Ok(arg.map(|a| {
946+
self.use_feature("raw_ref_op");
947+
948+
if ctx.is_static && matches!(mutbl, Mutability::Mutable) {
949+
// TODO: The currently used nightly doesn't allow `&raw mut` in static
950+
// initialisers, but the latest version does.
951+
// So we take a `&raw const` and then cast.
952+
// Remove this exemption when the version is updated.
953+
mk().cast_expr(mk().raw_borrow_expr(a), ty)
954+
} else {
955+
mk().set_mutbl(mutbl).raw_borrow_expr(a)
956+
}
957+
}))
946958
}
947959
}
948960
c_ast::UnOp::PreIncrement => self.convert_pre_increment(ctx, cqual_type, true, arg),

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ input_file: c2rust-transpile/tests/snapshots/arrays.c
1212
unused_assignments,
1313
unused_mut
1414
)]
15+
#![feature(raw_ref_op)]
1516
#[derive(Copy, Clone)]
1617
#[repr(C)]
1718
pub struct C2RustUnnamed {
@@ -79,11 +80,9 @@ pub unsafe extern "C" fn entry() {
7980
&[u8; 20],
8081
&mut [std::ffi::c_char; 20],
8182
>(b"abc\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
82-
let mut past_end: *mut std::ffi::c_char = &mut *simple
83-
.as_mut_ptr()
84-
.offset(::core::mem::size_of::<[std::ffi::c_char; 9]>() as isize)
85-
as *mut std::ffi::c_char;
86-
past_end = &mut *foo.offset(8 as std::ffi::c_int as isize) as *mut std::ffi::c_char;
83+
let mut past_end: *mut std::ffi::c_char = &raw mut *(&raw mut simple as *mut std::ffi::c_char)
84+
.offset(::core::mem::size_of::<[std::ffi::c_char; 9]>() as isize);
85+
past_end = &raw mut *foo.offset(8 as std::ffi::c_int as isize);
8786
}
8887
#[no_mangle]
8988
pub unsafe extern "C" fn short_initializer() {

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,26 @@ input_file: c2rust-transpile/tests/snapshots/atomics.c
1515
#![feature(core_intrinsics)]
1616
#[no_mangle]
1717
pub unsafe extern "C" fn c11_atomics(mut x: std::ffi::c_int) -> std::ffi::c_int {
18-
*&mut x = 0 as std::ffi::c_int;
19-
::core::intrinsics::atomic_store_seqcst(&mut x, 1 as std::ffi::c_int);
20-
::core::intrinsics::atomic_load_seqcst(&mut x);
21-
::core::intrinsics::atomic_xadd_seqcst(&mut x, 2 as std::ffi::c_int);
22-
::core::intrinsics::atomic_xsub_seqcst(&mut x, 1 as std::ffi::c_int);
23-
::core::intrinsics::atomic_and_seqcst(&mut x, 0xf as std::ffi::c_int);
24-
::core::intrinsics::atomic_or_seqcst(&mut x, 0x10 as std::ffi::c_int);
25-
::core::intrinsics::atomic_nand_seqcst(&mut x, 0xff as std::ffi::c_int);
26-
::core::intrinsics::atomic_xchg_seqcst(&mut x, 42 as std::ffi::c_int);
18+
*&raw mut x = 0 as std::ffi::c_int;
19+
::core::intrinsics::atomic_store_seqcst(&raw mut x, 1 as std::ffi::c_int);
20+
::core::intrinsics::atomic_load_seqcst(&raw mut x);
21+
::core::intrinsics::atomic_xadd_seqcst(&raw mut x, 2 as std::ffi::c_int);
22+
::core::intrinsics::atomic_xsub_seqcst(&raw mut x, 1 as std::ffi::c_int);
23+
::core::intrinsics::atomic_and_seqcst(&raw mut x, 0xf as std::ffi::c_int);
24+
::core::intrinsics::atomic_or_seqcst(&raw mut x, 0x10 as std::ffi::c_int);
25+
::core::intrinsics::atomic_nand_seqcst(&raw mut x, 0xff as std::ffi::c_int);
26+
::core::intrinsics::atomic_xchg_seqcst(&raw mut x, 42 as std::ffi::c_int);
2727
let mut expected: std::ffi::c_int = 42 as std::ffi::c_int;
2828
let mut desired: std::ffi::c_int = 100 as std::ffi::c_int;
29-
let fresh0 = ::core::intrinsics::atomic_cxchg_seqcst_seqcst(&mut x, *&mut expected, desired);
30-
*&mut expected = fresh0.0;
29+
let fresh0 =
30+
::core::intrinsics::atomic_cxchg_seqcst_seqcst(&raw mut x, *&raw mut expected, desired);
31+
*&raw mut expected = fresh0.0;
3132
fresh0.1;
3233
expected = 100 as std::ffi::c_int;
3334
desired = 200 as std::ffi::c_int;
3435
let fresh1 =
35-
::core::intrinsics::atomic_cxchgweak_seqcst_seqcst(&mut x, *&mut expected, desired);
36-
*&mut expected = fresh1.0;
36+
::core::intrinsics::atomic_cxchgweak_seqcst_seqcst(&raw mut x, *&raw mut expected, desired);
37+
*&raw mut expected = fresh1.0;
3738
fresh1.1;
3839
return x;
3940
}

0 commit comments

Comments
 (0)