From: Yap Zhi Heng <[email protected]>

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 <[email protected]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: 
https://github.com/rust-GCC/gccrs/commit/c01290e234ba1018aaa88394cb1e095f6a5d99e0
 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(-)
 create mode 100644 gcc/testsuite/rust/compile/e0579-neg-float-fail.rs
 create mode 100644 gcc/testsuite/rust/compile/e0579-neg-float.rs

diff --git a/gcc/rust/backend/rust-compile-expr.cc 
b/gcc/rust/backend/rust-compile-expr.cc
index 6404825b0..9a9c31590 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 708a824ad..af5f4538c 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 000000000..fefe2213c
--- /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 000000000..cc60e80c3
--- /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

base-commit: 660620af945b5c6750c0ee5bbe39e66020806978
-- 
2.51.2

Reply via email to