https://gcc.gnu.org/g:ce39846e3117c6fd65f929220148f6e5a7287f77

commit r16-5343-gce39846e3117c6fd65f929220148f6e5a7287f77
Author: Yap Zhi Heng <[email protected]>
Date:   Sun Oct 26 11:13:25 2025 +0800

    gccrs: Fix `RangePattern` negative literal bounds being treated as positive
    
    GIMPLE output for compile/issue-4242.rs:
    
    ...
    
      x = 1;
      RUSTTMP.2 = x;
      _1 = RUSTTMP.2 >= -55;
      _2 = RUSTTMP.2 < 0;
      _3 = _1 & _2;
      if (_3 != 0) goto <D.112>; else goto <D.113>;
      <D.112>:
      {
        RUSTTMP.1 = 2;
        goto <D.105>;
      }
      <D.113>:
      _4 = RUSTTMP.2 >= -99;
      _5 = RUSTTMP.2 < -55;
      _6 = _4 & _5;
      if (_6 != 0) goto <D.114>; else goto <D.115>;
      <D.114>:
      {
        RUSTTMP.1 = 3;
        goto <D.105>;
      }
    ...
    
    gcc/rust/ChangeLog:
    
            * backend/rust-compile-pattern.cc (compile_range_pattern_bound): 
Set litexpr
            to negative if has_minus is present in the RangePatternBoundLiteral 
param.
    
    Signed-off-by: Yap Zhi Heng <[email protected]>

Diff:
---
 gcc/rust/backend/rust-compile-pattern.cc         |  2 ++
 gcc/testsuite/rust/compile/issue-4242.rs         | 10 ++++++++++
 gcc/testsuite/rust/execute/torture/issue-4242.rs | 11 +++++++++++
 3 files changed, 23 insertions(+)

diff --git a/gcc/rust/backend/rust-compile-pattern.cc 
b/gcc/rust/backend/rust-compile-pattern.cc
index 3a983e9bc887..c29359aebe96 100644
--- a/gcc/rust/backend/rust-compile-pattern.cc
+++ b/gcc/rust/backend/rust-compile-pattern.cc
@@ -121,6 +121,8 @@ compile_range_pattern_bound (HIR::RangePatternBound &bound,
 
        HIR::LiteralExpr litexpr (mappings, ref.get_literal (), locus,
                                  std::vector<AST::Attribute> ());
+       if (ref.get_has_minus())
+               litexpr.set_negative();
 
        result = CompileExpr::Compile (litexpr, ctx);
       }
diff --git a/gcc/testsuite/rust/compile/issue-4242.rs 
b/gcc/testsuite/rust/compile/issue-4242.rs
new file mode 100644
index 000000000000..ecbe258cec66
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-4242.rs
@@ -0,0 +1,10 @@
+#![feature(exclusive_range_pattern)]
+
+fn main() {
+    let x = 1;
+
+    match x {
+        -55..0 => 2,
+        -99..-55 => 3,
+    };
+}
\ No newline at end of file
diff --git a/gcc/testsuite/rust/execute/torture/issue-4242.rs 
b/gcc/testsuite/rust/execute/torture/issue-4242.rs
new file mode 100644
index 000000000000..867adc98e309
--- /dev/null
+++ b/gcc/testsuite/rust/execute/torture/issue-4242.rs
@@ -0,0 +1,11 @@
+#![feature(exclusive_range_pattern)]
+
+fn main() -> i32 {
+    let x = -77;
+
+    match x {
+        -55..99 => 1,
+        -99..-55 => 0, // the correct case
+        _ => 1,
+    }
+}
\ No newline at end of file

Reply via email to