From b42078879d758ff25daba406b7eaf9cc5812d0a3 Mon Sep 17 00:00:00 2001 From: Zhi Heng Date: Sun, 29 Jun 2025 10:44:31 +0800 Subject: gccrs: Implement compilation support for TuplePatternItems::RANGED Example GIMPLE output of the match statement for match-restpattern-tuple-1.rs: ... RUSTTMP.2 = x; _1 = RUSTTMP.2.__0; _2 = _1 == 1; _3 = RUSTTMP.2.__3; _4 = _3 == 4; _5 = _2 & _4; if (_5 != 0) goto ; else goto ; : { { } goto ; } : if (1 != 0) goto ; else goto ; : { { } goto ; } : : ... gcc/rust/ChangeLog: * backend/rust-compile-pattern.cc (CompilePatternCheckExpr::visit(TuplePattern)): Implement check expression compilation for TuplePatternItems::RANGED. Signed-off-by: Yap Zhi Heng --- .../rust/compile/match-restpattern-tuple-1.rs | 8 +++++++ .../rust/compile/match-restpattern-tuple-2.rs | 8 +++++++ .../execute/torture/match-restpattern-tuple.rs | 27 ++++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 gcc/testsuite/rust/compile/match-restpattern-tuple-1.rs create mode 100644 gcc/testsuite/rust/compile/match-restpattern-tuple-2.rs create mode 100644 gcc/testsuite/rust/execute/torture/match-restpattern-tuple.rs (limited to 'gcc/testsuite/rust') diff --git a/gcc/testsuite/rust/compile/match-restpattern-tuple-1.rs b/gcc/testsuite/rust/compile/match-restpattern-tuple-1.rs new file mode 100644 index 0000000..5cce3c4 --- /dev/null +++ b/gcc/testsuite/rust/compile/match-restpattern-tuple-1.rs @@ -0,0 +1,8 @@ +fn main() { + let x = (1, 2, 3, 4); + + match x { + (1, .., 4) => {}, + _ => {} + } +} \ No newline at end of file diff --git a/gcc/testsuite/rust/compile/match-restpattern-tuple-2.rs b/gcc/testsuite/rust/compile/match-restpattern-tuple-2.rs new file mode 100644 index 0000000..40900a3 --- /dev/null +++ b/gcc/testsuite/rust/compile/match-restpattern-tuple-2.rs @@ -0,0 +1,8 @@ +fn main() { + let x = (1, 2, 3, 4); + + match x { + (1, .., 2, 3, 4, 5) => {}, // { dg-error "expected a tuple with 4 elements, found one with 5 elements" } + _ => {} + } +} \ No newline at end of file diff --git a/gcc/testsuite/rust/execute/torture/match-restpattern-tuple.rs b/gcc/testsuite/rust/execute/torture/match-restpattern-tuple.rs new file mode 100644 index 0000000..2c1418c --- /dev/null +++ b/gcc/testsuite/rust/execute/torture/match-restpattern-tuple.rs @@ -0,0 +1,27 @@ +// { dg-output "correct\r*" } +extern "C" { + fn puts(s: *const i8); +} + +fn main() -> i32 { + let x = (1, 2, 3, 4); + let mut ret = 1; + + match x { + (1, .., 2, 4) => { + /* should not take this path */ + unsafe { puts("wrong\0" as *const str as *const i8) } + }, + (2, ..) => { + /* should not take this path */ + unsafe { puts("wrong\0" as *const str as *const i8) } + }, + (b, .., 4) => { + ret -= b; + unsafe { puts("correct\0" as *const str as *const i8) } + }, + _ => {} + } + + ret +} \ No newline at end of file -- cgit v1.1