Skip to content

Conversation

bungcip
Copy link
Contributor

@bungcip bungcip commented Aug 24, 2025

currently, c2rust transpiled this code

void increment(){
    int i = 5;
    i++;
    i--;
    --i;
    ++i;
}

to this

#[no_mangle]
pub unsafe extern "C" fn increment() {
    let mut i: std::ffi::c_int = 5 as std::ffi::c_int;
    i += 1;
    i;
    i -= 1;
    i;
    i -= 1;
    i;
    i += 1;
    i;
}

which contain useless i expression. this is because UnaryExpr can contain side effect expression which cannot be ignored, however not all unary expr can contain side effects. this pull request limit to only Negate , Plus, Complement, and Not . However we can limit it to only expr which is not pure.

@bungcip bungcip changed the title only allow side effect in unary expr for Negate, Plus, Complement, and Not operator [wip] only allow side effect in unary expr for Negate, Plus, Complement, and Not operator Aug 24, 2025
Copy link
Contributor

@kkysen kkysen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the CI failure, if you're only developing on Linux, the macOS-specific snapshots won't be automatically updated so you have to manually update them based on what CI (or a macOS machine) says.

Copy link
Contributor

@kkysen kkysen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, actually, this test is OS specific since the printf declarations are slightly different. It's an annoying difference, so you could just try to rewrite it without using printf for the side effect (or just declaring it yourself).

@bungcip
Copy link
Contributor Author

bungcip commented Aug 25, 2025

ok, now testcase is not using printf. let see the this PR pass the testsuite.

@bungcip bungcip changed the title [wip] only allow side effect in unary expr for Negate, Plus, Complement, and Not operator [wip] Limit to write unused unary expr only when expr is not pure Aug 25, 2025
@bungcip bungcip changed the title [wip] Limit to write unused unary expr only when expr is not pure Limit to write unused unary expr only when expr is not pure Aug 25, 2025
Copy link
Contributor

@kkysen kkysen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the help on this!

// to add them to stmts.
if ctx.is_unused() {
// Some unused unary operators (`-foo()`) may have side effects, so we need
// to add them to stmts when arg expr is not pure
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// to add them to stmts when arg expr is not pure
// to add them to stmts when `arg` is not pure.

Comment on lines +8 to +13
-i;
+i;
~i;
!i;
&i;
*&i;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So these unary ops now emit nothing, and while that's generally correct (I haven't thought through the exact semantics of the *& one yet), it may also delete code that was intentionally written in C, similar to #1326. For things like i++, removing the i; and only keeping the i += 1; makes perfect sense, but I'm not sure for these other unary operators.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, i thought that c2rust have policy to removing unused things, i saw some other code that skip generating rust ouput when ctx.is_unused() is true, if this is wrong I will the change the code to remove in increment expression only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants