Skip to content

Commit 25cf53e

Browse files
committed
fix: allow compound literals with address-of operator
1 parent 78c32c5 commit 25cf53e

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

c2rust-transpile/src/translator/structs.rs

-6
Original file line numberDiff line numberDiff line change
@@ -544,12 +544,6 @@ impl<'a> Translation<'a> {
544544
Both(field_id, (field_name, _, bitfield_width, use_inner_type)) => {
545545
let mut expr = self.convert_expr(ctx.used(), *field_id)?;
546546

547-
if !expr.is_pure() {
548-
return Err(TranslationError::generic(
549-
"Expected no statements in field expression",
550-
));
551-
}
552-
553547
if use_inner_type {
554548
// See comment above
555549
expr = expr.map(|fi| mk().anon_field_expr(fi, 0));

tests/structs/src/struct_with_exp.c

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
struct s {
3+
int i;
4+
};
5+
6+
void struct_with_exp(const unsigned int buffer_size, int buffer[const]){
7+
if (buffer_size < 1) return;
8+
9+
struct s *p;
10+
int j = 42;
11+
p = &((struct s){j++}); // Compound literal with address-of operator and post-increment operator
12+
13+
buffer[0] = p->i;
14+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use crate::struct_with_exp::rust_struct_with_exp;
2+
use libc::{c_int, c_uint, size_t};
3+
4+
#[link(name = "test")]
5+
extern "C" {
6+
fn struct_with_exp(_: c_uint, _: *mut c_int);
7+
}
8+
9+
const BUFFER_SIZE: usize = 1;
10+
11+
pub fn test_struct_with_exp() {
12+
let mut buffer = [0; BUFFER_SIZE];
13+
let mut rust_buffer = [0; BUFFER_SIZE];
14+
let expected_buffer = [42];
15+
16+
unsafe {
17+
struct_with_exp(BUFFER_SIZE as u32, buffer.as_mut_ptr());
18+
rust_struct_with_exp(BUFFER_SIZE as u32, rust_buffer.as_mut_ptr());
19+
}
20+
21+
assert_eq!(buffer, rust_buffer);
22+
assert_eq!(buffer, expected_buffer);
23+
}

0 commit comments

Comments
 (0)