]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Fix `RangePattern` negative literal bounds being treated as positive
authorYap Zhi Heng <yapzhhg@gmail.com>
Sun, 26 Oct 2025 03:13:25 +0000 (11:13 +0800)
committerArthur Cohen <arthur.cohen@embecosm.com>
Mon, 17 Nov 2025 14:58:17 +0000 (15:58 +0100)
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 <yapzhhg@gmail.com>
gcc/rust/backend/rust-compile-pattern.cc
gcc/testsuite/rust/compile/issue-4242.rs [new file with mode: 0644]
gcc/testsuite/rust/execute/torture/issue-4242.rs [new file with mode: 0644]

index 3a983e9bc887f16772b741986bbcbb0ca2fc8871..c29359aebe96458656ee741364c2c844ad770b88 100644 (file)
@@ -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 (file)
index 0000000..ecbe258
--- /dev/null
@@ -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 (file)
index 0000000..867adc9
--- /dev/null
@@ -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