/src/test/codegen/issue-73396-bounds-check-after-position.rs

https://gitlab.com/rust-lang/rust · Rust · 77 lines · 49 code · 6 blank · 22 comment · 18 complexity · 1f5d7e61d0c5aeedb542c591077b4b97 MD5 · raw file

  1. // compile-flags: -O
  2. // ignore-debug: the debug assertions get in the way
  3. #![crate_type = "lib"]
  4. // Make sure no bounds checks are emitted when slicing or indexing
  5. // with an index from `position()` or `rposition()`.
  6. // CHECK-LABEL: @position_slice_to_no_bounds_check
  7. #[no_mangle]
  8. pub fn position_slice_to_no_bounds_check(s: &[u8]) -> &[u8] {
  9. // CHECK-NOT: panic
  10. // CHECK-NOT: slice_index_len_fail
  11. if let Some(idx) = s.iter().position(|b| *b == b'\\') {
  12. &s[..idx]
  13. } else {
  14. s
  15. }
  16. }
  17. // CHECK-LABEL: @position_slice_from_no_bounds_check
  18. #[no_mangle]
  19. pub fn position_slice_from_no_bounds_check(s: &[u8]) -> &[u8] {
  20. // CHECK-NOT: panic
  21. // CHECK-NOT: slice_index_len_fail
  22. if let Some(idx) = s.iter().position(|b| *b == b'\\') {
  23. &s[idx..]
  24. } else {
  25. s
  26. }
  27. }
  28. // CHECK-LABEL: @position_index_no_bounds_check
  29. #[no_mangle]
  30. pub fn position_index_no_bounds_check(s: &[u8]) -> u8 {
  31. // CHECK-NOT: panic
  32. // CHECK-NOT: slice_index_len_fail
  33. if let Some(idx) = s.iter().position(|b| *b == b'\\') {
  34. s[idx]
  35. } else {
  36. 42
  37. }
  38. }
  39. // CHECK-LABEL: @rposition_slice_to_no_bounds_check
  40. #[no_mangle]
  41. pub fn rposition_slice_to_no_bounds_check(s: &[u8]) -> &[u8] {
  42. // CHECK-NOT: panic
  43. // CHECK-NOT: slice_index_len_fail
  44. if let Some(idx) = s.iter().rposition(|b| *b == b'\\') {
  45. &s[..idx]
  46. } else {
  47. s
  48. }
  49. }
  50. // CHECK-LABEL: @rposition_slice_from_no_bounds_check
  51. #[no_mangle]
  52. pub fn rposition_slice_from_no_bounds_check(s: &[u8]) -> &[u8] {
  53. // CHECK-NOT: panic
  54. // CHECK-NOT: slice_index_len_fail
  55. if let Some(idx) = s.iter().rposition(|b| *b == b'\\') {
  56. &s[idx..]
  57. } else {
  58. s
  59. }
  60. }
  61. // CHECK-LABEL: @rposition_index_no_bounds_check
  62. #[no_mangle]
  63. pub fn rposition_index_no_bounds_check(s: &[u8]) -> u8 {
  64. // CHECK-NOT: panic
  65. // CHECK-NOT: slice_index_len_fail
  66. if let Some(idx) = s.iter().rposition(|b| *b == b'\\') {
  67. s[idx]
  68. } else {
  69. 42
  70. }
  71. }