Skip to content

Commit

Permalink
fix: allow compound literals with address-of operator
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeaseen authored and fw-immunant committed Jan 28, 2025
1 parent adcc29c commit f106b9b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
6 changes: 0 additions & 6 deletions c2rust-transpile/src/translator/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,12 +544,6 @@ impl<'a> Translation<'a> {
Both(field_id, (field_name, _, bitfield_width, use_inner_type)) => {
let mut expr = self.convert_expr(ctx.used(), *field_id)?;

if !expr.is_pure() {
return Err(TranslationError::generic(
"Expected no statements in field expression",
));
}

if use_inner_type {
// See comment above
expr = expr.map(|fi| mk().anon_field_expr(fi, 0));
Expand Down
14 changes: 14 additions & 0 deletions tests/structs/src/struct_with_exp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

struct s {
int i;
};

void struct_with_exp(const unsigned int buffer_size, int buffer[const]){
if (buffer_size < 1) return;

struct s *p;
int j = 42;
p = &((struct s){j++}); // Compound literal with address-of operator and post-increment operator

buffer[0] = p->i;
}
23 changes: 23 additions & 0 deletions tests/structs/src/test_struct_with_exp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::struct_with_exp::rust_struct_with_exp;
use libc::{c_int, c_uint, size_t};

#[link(name = "test")]
extern "C" {
fn struct_with_exp(_: c_uint, _: *mut c_int);
}

const BUFFER_SIZE: usize = 1;

pub fn test_struct_with_exp() {
let mut buffer = [0; BUFFER_SIZE];
let mut rust_buffer = [0; BUFFER_SIZE];
let expected_buffer = [42];

unsafe {
struct_with_exp(BUFFER_SIZE as u32, buffer.as_mut_ptr());
rust_struct_with_exp(BUFFER_SIZE as u32, rust_buffer.as_mut_ptr());
}

assert_eq!(buffer, rust_buffer);
assert_eq!(buffer, expected_buffer);
}

0 comments on commit f106b9b

Please sign in to comment.