From 70ee73211fc00752b85eb5cd569f04da1f745130 Mon Sep 17 00:00:00 2001 From: "Gordon P. Hemsley" Date: Sun, 22 Sep 2024 09:22:59 -0400 Subject: [PATCH] transpile: Make volatile post-increment assignment compile Fixes #1049. Fixes #1064. --- c2rust-transpile/src/translator/operators.rs | 2 +- tests/ints/src/test_volatile.rs | 11 ++++++++++- tests/ints/src/volatile.c | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/c2rust-transpile/src/translator/operators.rs b/c2rust-transpile/src/translator/operators.rs index da006d07c6..fedff6db62 100644 --- a/c2rust-transpile/src/translator/operators.rs +++ b/c2rust-transpile/src/translator/operators.rs @@ -797,7 +797,7 @@ impl<'c> Translation<'c> { }; Ok(WithStmts::new( - vec![save_old_val, mk().expr_stmt(assign_stmt)], + vec![save_old_val, mk().semi_stmt(assign_stmt)], mk().ident_expr(val_name), )) }, diff --git a/tests/ints/src/test_volatile.rs b/tests/ints/src/test_volatile.rs index 3045d70f03..3cde5d75bf 100644 --- a/tests/ints/src/test_volatile.rs +++ b/tests/ints/src/test_volatile.rs @@ -1,13 +1,22 @@ -use crate::volatile::rust_entry3; +use crate::volatile::{rust_entry3, rust_volatile_stuff}; use libc::{c_int, c_uint}; #[link(name = "test")] extern "C" { + fn volatile_stuff(); + fn entry3(_: c_uint, _: *mut c_int); } const BUFFER_SIZE: usize = 9; +pub fn test_compiles() { + unsafe { + volatile_stuff(); + rust_volatile_stuff(); + } +} + pub fn test_buffer() { let mut buffer = [0; BUFFER_SIZE]; let mut rust_buffer = [0; BUFFER_SIZE]; diff --git a/tests/ints/src/volatile.c b/tests/ints/src/volatile.c index 2dd78c47d0..1d60e17dbe 100644 --- a/tests/ints/src/volatile.c +++ b/tests/ints/src/volatile.c @@ -44,4 +44,23 @@ void entry3(const unsigned buffer_size, int buffer[]) buffer[8] = s.buffer[3]; } +void volatile_stuff(void) +{ + // Non-volatile + int x1 = 0; + int x2 = x1++; + x2; + + // https://github.com/immunant/c2rust/issues/1049 + volatile int x3 = 0; + int x4 = x3++; + x4; + // https://github.com/immunant/c2rust/issues/1064 + volatile int x5 = 0; + while (x5 < 5) + { + int x6 = x5++; + x6; + } +}