Skip to content

Commit

Permalink
c2rust-transpile: emit unary operator arguments as statements if unused
Browse files Browse the repository at this point in the history
in the presence of side-effecting argument expressions, it is not safe to simply drop these if unused.
  • Loading branch information
fw-immunant committed Apr 24, 2023
1 parent 4da2a04 commit f9c5195
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion c2rust-transpile/src/translator/operators.rs
Expand Up @@ -816,7 +816,7 @@ impl<'c> Translation<'c> {
let ty = self.convert_type(ctype)?;
let resolved_ctype = self.ast_context.resolve_type(ctype);

match name {
let mut unary = match name {
c_ast::UnOp::AddressOf => {
let arg_kind = &self.ast_context[arg].kind;

Expand Down Expand Up @@ -944,6 +944,14 @@ impl<'c> Translation<'c> {
c_ast::UnOp::Real | c_ast::UnOp::Imag | c_ast::UnOp::Coawait => {
panic!("Unsupported extension operator")
}
}?;

// Unused unary operators (`-foo()`) may have side effects, so we need
// to add them to stmts.
if ctx.is_unused() {
let v = unary.clone().into_value();
unary.stmts_mut().push(Stmt::Semi(*v, Default::default()));
}
Ok(unary)
}
}

2 comments on commit f9c5195

@mewmew
Copy link

@mewmew mewmew commented on f9c5195 Aug 13, 2023

Choose a reason for hiding this comment

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

Does this commit fix the issue outlined in figure 6 ("Sample code illustrating the functional incorrectness about side effects") of https://csslab-ustc.github.io/publications/2023/rust-transpiler.pdf?

@kkysen
Copy link
Contributor

@kkysen kkysen commented on f9c5195 Aug 13, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.