Skip to content

Commit c7daf18

Browse files
Remove pointer comparison from slice equality
1 parent 1f5bc17 commit c7daf18

File tree

2 files changed

+12
-27
lines changed

2 files changed

+12
-27
lines changed

library/core/src/slice/cmp.rs

-27
Original file line numberDiff line numberDiff line change
@@ -75,28 +75,6 @@ where
7575
}
7676
}
7777

78-
// Use an equal-pointer optimization when types are `Eq`
79-
// We can't make `A` and `B` the same type because `min_specialization` won't
80-
// allow it.
81-
impl<A, B> SlicePartialEq<B> for [A]
82-
where
83-
A: MarkerEq<B>,
84-
{
85-
default fn equal(&self, other: &[B]) -> bool {
86-
if self.len() != other.len() {
87-
return false;
88-
}
89-
90-
// While performance would suffer if `guaranteed_eq` just returned `false`
91-
// for all arguments, correctness and return value of this function are not affected.
92-
if self.as_ptr().guaranteed_eq(other.as_ptr() as *const A) {
93-
return true;
94-
}
95-
96-
self.iter().zip(other.iter()).all(|(x, y)| x == y)
97-
}
98-
}
99-
10078
// Use memcmp for bytewise equality when the types allow
10179
impl<A, B> SlicePartialEq<B> for [A]
10280
where
@@ -107,11 +85,6 @@ where
10785
return false;
10886
}
10987

110-
// While performance would suffer if `guaranteed_eq` just returned `false`
111-
// for all arguments, correctness and return value of this function are not affected.
112-
if self.as_ptr().guaranteed_eq(other.as_ptr() as *const A) {
113-
return true;
114-
}
11588
// SAFETY: `self` and `other` are references and are thus guaranteed to be valid.
11689
// The two slices have been checked to have the same size above.
11790
unsafe {
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// compile-flags: -C opt-level=3
2+
3+
#![crate_type = "lib"]
4+
5+
// #71602: check that there is no pointer comparison generated for slice equality
6+
7+
// CHECK-LABEL: @is_zero_slice
8+
#[no_mangle]
9+
pub fn is_zero_slice(data: &[u8; 4]) -> bool {
10+
// CHECK-NOT: %{{.+}} = icmp eq [4 x i8]* {{.+}}
11+
*data == [0; 4]
12+
}

0 commit comments

Comments
 (0)