Skip to content

Commit

Permalink
[#3141] Remove double negation by returning CompileExpr::Compile early
Browse files Browse the repository at this point in the history
Fixes #3141.

gcc/rust/ChangeLog:

	* backend/rust-compile-expr.cc

Signed-off-by: Joan Vilardaga <[email protected]>
  • Loading branch information
JoanVC100 authored and philberty committed Sep 16, 2024
1 parent ba7b5a4 commit b0c8579
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions gcc/rust/backend/rust-compile-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ CompileExpr::visit (HIR::NegationExpr &expr)
auto op = expr.get_expr_type ();

const auto literal_expr = expr.get_expr ().get ();

// If it's a negated integer/float literal, we can return early
if (op == NegationOperator::NEGATE
&& literal_expr->get_expression_type () == HIR::Expr::ExprType::Lit)
{
Expand All @@ -246,10 +248,12 @@ CompileExpr::visit (HIR::NegationExpr &expr)
|| lit_type == HIR::Literal::LitType::FLOAT)
{
new_literal_expr->set_negative ();
translated = CompileExpr::Compile (literal_expr, ctx);
return;
}
}
auto negated_expr = CompileExpr::Compile (literal_expr, ctx);

auto negated_expr = CompileExpr::Compile (literal_expr, ctx);
auto location = expr.get_locus ();

// this might be an operator overload situation lets check
Expand Down Expand Up @@ -1531,11 +1535,6 @@ CompileExpr::compile_integer_literal (const HIR::LiteralExpr &expr,
tyty->get_name ().c_str ());
return error_mark_node;
}
// Other tests break if we don't reverse the negation
if (expr.is_negative ())
{
mpz_neg (ival, ival);
}

tree result = wide_int_to_tree (type, wi::from_mpz (type, ival, true));

Expand Down

0 comments on commit b0c8579

Please sign in to comment.