about summary refs log tree commit diff
path: root/tests/codegen
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-04-07 12:49:15 +0000
committerbors <bors@rust-lang.org>2024-04-07 12:49:15 +0000
commit4e431fad67b46c480f1833119cd368fa33df95f7 (patch)
tree9d793a570f5ce2b0c6aedd34c2d5aeaac4df2a16 /tests/codegen
parentfc1a4c5cc9308c4b5980c64a73fd344a59c10601 (diff)
parent712aab72df2a9c511e49a51974294d3d2712056e (diff)
downloadrust-4e431fad67b46c480f1833119cd368fa33df95f7.tar.gz
rust-4e431fad67b46c480f1833119cd368fa33df95f7.zip
Auto merge of #123561 - saethlin:str-unchecked-sub-index, r=scottmcm
Use unchecked_sub in str indexing

https://github.com/rust-lang/rust/pull/108763 applied this logic to indexing for slices, but of course `str` has its own separate impl.

Found this by skimming over the codegen for https://github.com/oxidecomputer/hubris/; their dist builds enable overflow checks so the lack of `unchecked_sub` was producing an impossible-to-hit overflow check and also inhibiting some inlining.

r? scottmcm
Diffstat (limited to 'tests/codegen')
-rw-r--r--tests/codegen/slice-indexing.rs28
1 files changed, 28 insertions, 0 deletions
diff --git a/tests/codegen/slice-indexing.rs b/tests/codegen/slice-indexing.rs
index ecce9201071..3d284148db2 100644
--- a/tests/codegen/slice-indexing.rs
+++ b/tests/codegen/slice-indexing.rs
@@ -32,3 +32,31 @@ pub unsafe fn get_unchecked_mut_by_range(x: &mut [i32], r: Range<usize>) -> &mut
     // CHECK: sub nuw i64
     x.get_unchecked_mut(r)
 }
+
+// CHECK-LABEL: @str_index_by_range(
+#[no_mangle]
+pub fn str_index_by_range(x: &str, r: Range<usize>) -> &str {
+    // CHECK: sub nuw i64
+    &x[r]
+}
+
+// CHECK-LABEL: @str_get_unchecked_by_range(
+#[no_mangle]
+pub unsafe fn str_get_unchecked_by_range(x: &str, r: Range<usize>) -> &str {
+    // CHECK: sub nuw i64
+    x.get_unchecked(r)
+}
+
+// CHECK-LABEL: @str_index_mut_by_range(
+#[no_mangle]
+pub fn str_index_mut_by_range(x: &mut str, r: Range<usize>) -> &mut str {
+    // CHECK: sub nuw i64
+    &mut x[r]
+}
+
+// CHECK-LABEL: @str_get_unchecked_mut_by_range(
+#[no_mangle]
+pub unsafe fn str_get_unchecked_mut_by_range(x: &mut str, r: Range<usize>) -> &mut str {
+    // CHECK: sub nuw i64
+    x.get_unchecked_mut(r)
+}