]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Fix compile_float_literal not compiling negatives properly
authorYap Zhi Heng <yapzhhg@gmail.com>
Sat, 15 Nov 2025 07:08:36 +0000 (15:08 +0800)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 25 Nov 2025 22:00:51 +0000 (23:00 +0100)
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>
gcc/rust/backend/rust-compile-expr.cc
gcc/rust/backend/rust-compile-pattern.cc
gcc/testsuite/rust/compile/e0579-neg-float-fail.rs [new file with mode: 0644]
gcc/testsuite/rust/compile/e0579-neg-float.rs [new file with mode: 0644]

index 6404825b02f7f5150ab159ed2603dadeff2e873f..9a9c315904839cd9385b1b7ba2aa27a87715c621 100644 (file)
@@ -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
index 708a824ad4d582630b9e906bdea17a82a2abe11c..af5f4538c4d07bec8fafdf5fd06b2a73eacdabba 100644 (file)
@@ -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 (file)
index 0000000..fefe221
--- /dev/null
@@ -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 (file)
index 0000000..cc60e80
--- /dev/null
@@ -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