From e80e577df7ca410eac549da61f9a9fd702dc55ea Mon Sep 17 00:00:00 2001 From: Yap Zhi Heng Date: Wed, 23 Jul 2025 08:24:18 +0800 Subject: gccrs: Implement compilation for SlicePattern against SliceType scrutinee 006t.original output from compiling testsuite/rust/compile/match-slicepattern-slice.rs: ... RUSTTMP.3 = slice; if (RUSTTMP.3.len == 1 && *(RUSTTMP.3.data + 0 * 4) == 1) { { struct () RUSTTMP.4; { } goto ; } } if (RUSTTMP.3.len == 2 && *(RUSTTMP.3.data + 1 * 4) == 2) { { struct () RUSTTMP.5; { } goto ; } } if (1) { { struct () RUSTTMP.6; { } goto ; } } :; ... gcc/rust/ChangeLog: * rust-backend.h: New slice_index_expression function. * rust-gcc.cc: Implementation of slice_index_expression to generate tree node for accessing slice elements. * backend/rust-compile-pattern.cc: Implement SlicePattern check expression & binding compilation against SliceType scrutinee. Signed-off-by: Yap Zhi Heng --- .../rust/compile/match-slicepattern-slice.rs | 10 +++++++++ .../execute/torture/match-slicepattern-slice-1.rs | 24 ++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 gcc/testsuite/rust/compile/match-slicepattern-slice.rs create mode 100644 gcc/testsuite/rust/execute/torture/match-slicepattern-slice-1.rs (limited to 'gcc/testsuite/rust') diff --git a/gcc/testsuite/rust/compile/match-slicepattern-slice.rs b/gcc/testsuite/rust/compile/match-slicepattern-slice.rs new file mode 100644 index 0000000..cc33d93 --- /dev/null +++ b/gcc/testsuite/rust/compile/match-slicepattern-slice.rs @@ -0,0 +1,10 @@ +fn main() { + let arr = [1, 2]; + let slice: &[i32] = &arr; + + match slice { + [1] => {}, + [_, 2] => {}, + _ => {} + } +} \ No newline at end of file diff --git a/gcc/testsuite/rust/execute/torture/match-slicepattern-slice-1.rs b/gcc/testsuite/rust/execute/torture/match-slicepattern-slice-1.rs new file mode 100644 index 0000000..3ed0b644 --- /dev/null +++ b/gcc/testsuite/rust/execute/torture/match-slicepattern-slice-1.rs @@ -0,0 +1,24 @@ +// { dg-output "correct\r*" } +extern "C" { + fn puts(s: *const i8); +} + +fn main() -> i32 { + let arr = [0, 1]; + let a: &[i32] = &arr; + let mut ret = 1; + + match a { + [0, 0] => { + /* should not take this path */ + unsafe { puts("wrong\0" as *const str as *const i8) } + }, + [0, b] => { + ret -= b; + unsafe { puts("correct\0" as *const str as *const i8) } + }, + _ => {} + } + + ret +} -- cgit v1.1