Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transpile: fix previously ignored enum usage in compound literals #1185

Merged
merged 3 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions c2rust-transpile/src/translator/literals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ impl<'c> Translation<'c> {
let id = ids.first().unwrap();
self.convert_expr(ctx.used(), *id)
}
CTypeKind::Enum(_) => {
let id = ids.first().unwrap();
self.convert_expr(ctx.used(), *id)
}
CTypeKind::Vector(CQualTypeId { ctype, .. }, len) => {
self.vector_list_initializer(ctx, ids, ctype, len)
}
Expand Down
13 changes: 13 additions & 0 deletions tests/enums/src/enum_compound.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

typedef enum {
B = 2
} a;

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

// Using a compound literal with enum to assign the value
buffer[0] = (a) { B };
}
18 changes: 18 additions & 0 deletions tests/enums/src/test_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::non_canonical_enum_def::{
hrtimer_restart, rust_abc, HRTIMER_NORESTART, HRTIMER_RESTART,
};
use crate::top_enum::{rust_entry4, E as otherE};
use crate::enum_compound::rust_entry6;

use libc::{c_int, c_uint};

Expand All @@ -21,13 +22,16 @@ extern "C" {
fn entry4(_: c_uint, _: *mut c_int);

fn entry5(_: c_uint, _: *mut c_int);

fn entry6(_: c_uint, _: *mut c_int);
}

const BUFFER_SIZE: usize = 10;
const BUFFER_SIZE2: usize = 7;
const BUFFER_SIZE3: usize = 4;
const BUFFER_SIZE4: usize = 1;
const BUFFER_SIZE5: usize = 6;
const BUFFER_SIZE6: usize = 1;

pub fn test_variants() {
assert_eq!(A as u32, 0);
Expand Down Expand Up @@ -103,3 +107,17 @@ pub fn test_buffer5() {
assert_eq!(buffer, rust_buffer);
assert_eq!(buffer, expected_buffer);
}

pub fn test_buffer6() {
let mut buffer = [0; BUFFER_SIZE6];
let mut rust_buffer = [0; BUFFER_SIZE6];
let expected_buffer = [2];

unsafe {
entry6(BUFFER_SIZE6 as u32, buffer.as_mut_ptr());
rust_entry6(BUFFER_SIZE6 as u32, rust_buffer.as_mut_ptr());
}

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