[gcc r16-5599] gccrs: Fix compile_float_literal not compiling negatives properly
Arthur Cohen
cohenarthur@gcc.gnu.org
Tue Nov 25 21:59:02 GMT 2025
https://gcc.gnu.org/g:86d1fc783097c820e78290c64a89985cac792549
commit r16-5599-g86d1fc783097c820e78290c64a89985cac792549
Author: Yap Zhi Heng <yapzhhg@gmail.com>
Date: Sat Nov 15 15:08:36 2025 +0800
gccrs: Fix compile_float_literal not compiling negatives properly
gcc/rust/ChangeLog:
* backend/rust-compile-expr.cc (compile_float_literal): Add is_negative
check to compile negative float literals properly.
* backend/rust-compile-pattern.cc (CompilePatternCheckExpr::visit(RangePattern)):
Minor optimization to E0579 checks to reduce memory copy.
Signed-off-by: Yap Zhi Heng <yapzhhg@gmail.com>
Diff:
---
gcc/rust/backend/rust-compile-expr.cc | 2 ++
gcc/rust/backend/rust-compile-pattern.cc | 6 +++---
gcc/testsuite/rust/compile/e0579-neg-float-fail.rs | 9 +++++++++
gcc/testsuite/rust/compile/e0579-neg-float.rs | 9 +++++++++
4 files changed, 23 insertions(+), 3 deletions(-)
diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc
index 6404825b02f7..9a9c31590483 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -1712,6 +1712,8 @@ CompileExpr::compile_float_literal (const HIR::LiteralExpr &expr,
rust_error_at (expr.get_locus (), "bad number in literal");
return error_mark_node;
}
+ if (expr.is_negative ())
+ mpfr_neg (fval, fval, MPFR_RNDN);
// taken from:
// see go/gofrontend/expressions.cc:check_float_type
diff --git a/gcc/rust/backend/rust-compile-pattern.cc b/gcc/rust/backend/rust-compile-pattern.cc
index 708a824ad4d5..af5f4538c4d0 100644
--- a/gcc/rust/backend/rust-compile-pattern.cc
+++ b/gcc/rust/backend/rust-compile-pattern.cc
@@ -170,9 +170,9 @@ CompilePatternCheckExpr::visit (HIR::RangePattern &pattern)
bool error_E0579 = false;
if (TREE_CODE (upper) == REAL_CST)
{
- REAL_VALUE_TYPE upper_r = TREE_REAL_CST (upper);
- REAL_VALUE_TYPE lower_r = TREE_REAL_CST (lower);
- if (real_compare (GE_EXPR, &lower_r, &upper_r))
+ const REAL_VALUE_TYPE *upper_r = TREE_REAL_CST_PTR (upper);
+ const REAL_VALUE_TYPE *lower_r = TREE_REAL_CST_PTR (lower);
+ if (real_compare (GE_EXPR, lower_r, upper_r))
error_E0579 = true;
}
else if (TREE_CODE (upper) == INTEGER_CST)
diff --git a/gcc/testsuite/rust/compile/e0579-neg-float-fail.rs b/gcc/testsuite/rust/compile/e0579-neg-float-fail.rs
new file mode 100644
index 000000000000..fefe2213c57c
--- /dev/null
+++ b/gcc/testsuite/rust/compile/e0579-neg-float-fail.rs
@@ -0,0 +1,9 @@
+#![feature(exclusive_range_pattern)]
+
+fn main() {
+ let x = 1.0;
+
+ match x { // { dg-message "sorry, unimplemented: match on floating-point types is not yet supported" }
+ -1.0f32..-1.2f32 => 2, // { dg-error "lower range bound must be less than upper .E0579." }
+ };
+}
\ No newline at end of file
diff --git a/gcc/testsuite/rust/compile/e0579-neg-float.rs b/gcc/testsuite/rust/compile/e0579-neg-float.rs
new file mode 100644
index 000000000000..cc60e80c3a58
--- /dev/null
+++ b/gcc/testsuite/rust/compile/e0579-neg-float.rs
@@ -0,0 +1,9 @@
+#![feature(exclusive_range_pattern)]
+
+fn main() {
+ let x = 1.0;
+
+ match x { // { dg-message "sorry, unimplemented: match on floating-point types is not yet supported" }
+ -1.2f32..-1.0f32 => 2,
+ };
+}
\ No newline at end of file
More information about the Gcc-cvs
mailing list