From f9c51950732bb47dfae5c35223e9955d203ba846 Mon Sep 17 00:00:00 2001 From: fw Date: Mon, 24 Apr 2023 15:40:19 -0400 Subject: [PATCH] c2rust-transpile: emit unary operator arguments as statements if unused in the presence of side-effecting argument expressions, it is not safe to simply drop these if unused. --- c2rust-transpile/src/translator/operators.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/c2rust-transpile/src/translator/operators.rs b/c2rust-transpile/src/translator/operators.rs index d697107cf1..da006d07c6 100644 --- a/c2rust-transpile/src/translator/operators.rs +++ b/c2rust-transpile/src/translator/operators.rs @@ -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; @@ -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) } }