Skip to content

Commit cf35563

Browse files
committed
only allow side effect in unary expr when argument expr is not pure
1 parent 834db06 commit cf35563

File tree

7 files changed

+63
-12
lines changed

7 files changed

+63
-12
lines changed

c2rust-transpile/src/translator/operators.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,9 +1034,9 @@ impl<'c> Translation<'c> {
10341034
}
10351035
}?;
10361036

1037-
// Unused unary operators (`-foo()`) may have side effects, so we need
1038-
// to add them to stmts.
1039-
if ctx.is_unused() {
1037+
// Some unused unary operators (`-foo()`) may have side effects, so we need
1038+
// to add them to stmts when arg expr is not pure
1039+
if ctx.is_unused() && !self.ast_context.is_expr_pure(arg) {
10401040
let v = unary.clone().into_value();
10411041
unary
10421042
.stmts_mut()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <stdio.h>
2+
3+
void increment(){
4+
int i = 5;
5+
i++;
6+
i--;
7+
--i;
8+
++i;
9+
}
10+
11+
int side_effect(){
12+
printf("tuturu.. i am side effect");
13+
return 10;
14+
}
15+
16+
void unary(){
17+
-side_effect();
18+
+side_effect();
19+
~side_effect();
20+
!side_effect();
21+
&side_effect;
22+
*side_effect;
23+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
source: c2rust-transpile/tests/snapshots.rs
3+
expression: cat tests/snapshots/exprs.rs
4+
input_file: c2rust-transpile/tests/snapshots/exprs.c
5+
---
6+
#![allow(
7+
dead_code,
8+
mutable_transmutes,
9+
non_camel_case_types,
10+
non_snake_case,
11+
non_upper_case_globals,
12+
unused_assignments,
13+
unused_mut
14+
)]
15+
extern "C" {
16+
fn printf(__format: *const std::ffi::c_char, ...) -> std::ffi::c_int;
17+
}
18+
#[no_mangle]
19+
pub unsafe extern "C" fn increment() {
20+
let mut i: std::ffi::c_int = 5 as std::ffi::c_int;
21+
i += 1;
22+
i -= 1;
23+
i -= 1;
24+
i += 1;
25+
}
26+
#[no_mangle]
27+
pub unsafe extern "C" fn side_effect() -> std::ffi::c_int {
28+
printf(b"tuturu.. i am side effect\0" as *const u8 as *const std::ffi::c_char);
29+
return 10 as std::ffi::c_int;
30+
}
31+
#[no_mangle]
32+
pub unsafe extern "C" fn unary() {
33+
-side_effect();
34+
side_effect();
35+
!side_effect();
36+
(side_effect() == 0) as std::ffi::c_int;
37+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ pub unsafe extern "C" fn factorial(mut n: std::ffi::c_ushort) -> std::ffi::c_ush
1919
while (i as std::ffi::c_int) < n as std::ffi::c_int {
2020
result = (result as std::ffi::c_int * i as std::ffi::c_int) as std::ffi::c_ushort;
2121
i = i.wrapping_add(1);
22-
i;
2322
}
2423
return result;
2524
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
source: c2rust-transpile/tests/snapshots.rs
3-
assertion_line: 67
43
expression: cat tests/snapshots/gotos.rs
54
input_file: c2rust-transpile/tests/snapshots/gotos.c
65
---
@@ -19,8 +18,6 @@ pub unsafe extern "C" fn sum(mut count: std::ffi::c_int) -> std::ffi::c_int {
1918
while !(count <= 0 as std::ffi::c_int) {
2019
x += count;
2120
count -= 1;
22-
count;
2321
}
2422
return x;
2523
}
26-

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ pub unsafe extern "C" fn insertion_sort(n: std::ffi::c_int, p: *mut std::ffi::c_
2121
while j > 0 as std::ffi::c_int && *p.offset((j - 1 as std::ffi::c_int) as isize) > tmp {
2222
*p.offset(j as isize) = *p.offset((j - 1 as std::ffi::c_int) as isize);
2323
j -= 1;
24-
j;
2524
}
2625
*p.offset(j as isize) = tmp;
2726
i += 1;
28-
i;
2927
}
3028
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,13 +406,10 @@ pub unsafe extern "C" fn stmt_expr_inc() -> std::ffi::c_int {
406406
let mut b: *mut std::ffi::c_int = &mut a;
407407
({
408408
*b += 1;
409-
*b;
410-
*b;
411409
*b
412410
});
413411
return ({
414412
*b += 1;
415-
*b;
416413
*b
417414
});
418415
}

0 commit comments

Comments
 (0)