Skip to content

Commit 2c9087a

Browse files
committed
Auto merge of rust-lang#117758 - Urgau:lint_pointer_trait_comparisons, r=davidtwco
Add lint against ambiguous wide pointer comparisons This PR is the resolution of rust-lang#106447 decided in rust-lang#117717 by T-lang. ## `ambiguous_wide_pointer_comparisons` *warn-by-default* The `ambiguous_wide_pointer_comparisons` lint checks comparison of `*const/*mut ?Sized` as the operands. ### Example ```rust let ab = (A, B); let a = &ab.0 as *const dyn T; let b = &ab.1 as *const dyn T; let _ = a == b; ``` ### Explanation The comparison includes metadata which may not be expected. ------- This PR also drops `clippy::vtable_address_comparisons` which is superseded by this one. ~~One thing: is the current naming right? `invalid` seems a bit too much.~~ Fixes rust-lang#117717
2 parents e5e5bc6 + 81f1fa6 commit 2c9087a

File tree

6 files changed

+17
-1
lines changed

6 files changed

+17
-1
lines changed

alloc/tests/vec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1980,7 +1980,7 @@ fn vec_macro_repeating_null_raw_fat_pointer() {
19801980

19811981
let vec = vec![null_raw_dyn; 1];
19821982
dbg!(ptr_metadata(vec[0]));
1983-
assert!(vec[0] == null_raw_dyn);
1983+
assert!(std::ptr::eq(vec[0], null_raw_dyn));
19841984

19851985
// Polyfill for https://github.com/rust-lang/rfcs/pull/2580
19861986

core/src/cmp.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,15 @@ pub trait PartialEq<Rhs: ?Sized = Self> {
230230
/// by `==`.
231231
#[must_use]
232232
#[stable(feature = "rust1", since = "1.0.0")]
233+
#[cfg_attr(not(bootstrap), rustc_diagnostic_item = "cmp_partialeq_eq")]
233234
fn eq(&self, other: &Rhs) -> bool;
234235

235236
/// This method tests for `!=`. The default implementation is almost always
236237
/// sufficient, and should not be overridden without very good reason.
237238
#[inline]
238239
#[must_use]
239240
#[stable(feature = "rust1", since = "1.0.0")]
241+
#[cfg_attr(not(bootstrap), rustc_diagnostic_item = "cmp_partialeq_ne")]
240242
fn ne(&self, other: &Rhs) -> bool {
241243
!self.eq(other)
242244
}

core/src/ptr/const_ptr.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,6 +1772,7 @@ impl<T> *const [T] {
17721772
#[stable(feature = "rust1", since = "1.0.0")]
17731773
impl<T: ?Sized> PartialEq for *const T {
17741774
#[inline]
1775+
#[cfg_attr(not(bootstrap), allow(ambiguous_wide_pointer_comparisons))]
17751776
fn eq(&self, other: &*const T) -> bool {
17761777
*self == *other
17771778
}
@@ -1784,6 +1785,7 @@ impl<T: ?Sized> Eq for *const T {}
17841785
#[stable(feature = "rust1", since = "1.0.0")]
17851786
impl<T: ?Sized> Ord for *const T {
17861787
#[inline]
1788+
#[cfg_attr(not(bootstrap), allow(ambiguous_wide_pointer_comparisons))]
17871789
fn cmp(&self, other: &*const T) -> Ordering {
17881790
if self < other {
17891791
Less
@@ -1803,21 +1805,25 @@ impl<T: ?Sized> PartialOrd for *const T {
18031805
}
18041806

18051807
#[inline]
1808+
#[cfg_attr(not(bootstrap), allow(ambiguous_wide_pointer_comparisons))]
18061809
fn lt(&self, other: &*const T) -> bool {
18071810
*self < *other
18081811
}
18091812

18101813
#[inline]
1814+
#[cfg_attr(not(bootstrap), allow(ambiguous_wide_pointer_comparisons))]
18111815
fn le(&self, other: &*const T) -> bool {
18121816
*self <= *other
18131817
}
18141818

18151819
#[inline]
1820+
#[cfg_attr(not(bootstrap), allow(ambiguous_wide_pointer_comparisons))]
18161821
fn gt(&self, other: &*const T) -> bool {
18171822
*self > *other
18181823
}
18191824

18201825
#[inline]
1826+
#[cfg_attr(not(bootstrap), allow(ambiguous_wide_pointer_comparisons))]
18211827
fn ge(&self, other: &*const T) -> bool {
18221828
*self >= *other
18231829
}

core/src/ptr/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,6 +1897,7 @@ pub(crate) const unsafe fn align_offset<T: Sized>(p: *const T, a: usize) -> usiz
18971897
#[inline(always)]
18981898
#[must_use = "pointer comparison produces a value"]
18991899
#[rustc_diagnostic_item = "ptr_eq"]
1900+
#[cfg_attr(not(bootstrap), allow(ambiguous_wide_pointer_comparisons))] // it's actually clear here
19001901
pub fn eq<T: ?Sized>(a: *const T, b: *const T) -> bool {
19011902
a == b
19021903
}

core/src/ptr/mut_ptr.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2199,6 +2199,7 @@ impl<T> *mut [T] {
21992199
#[stable(feature = "rust1", since = "1.0.0")]
22002200
impl<T: ?Sized> PartialEq for *mut T {
22012201
#[inline(always)]
2202+
#[cfg_attr(not(bootstrap), allow(ambiguous_wide_pointer_comparisons))]
22022203
fn eq(&self, other: &*mut T) -> bool {
22032204
*self == *other
22042205
}
@@ -2210,6 +2211,7 @@ impl<T: ?Sized> Eq for *mut T {}
22102211
#[stable(feature = "rust1", since = "1.0.0")]
22112212
impl<T: ?Sized> Ord for *mut T {
22122213
#[inline]
2214+
#[cfg_attr(not(bootstrap), allow(ambiguous_wide_pointer_comparisons))]
22132215
fn cmp(&self, other: &*mut T) -> Ordering {
22142216
if self < other {
22152217
Less
@@ -2229,21 +2231,25 @@ impl<T: ?Sized> PartialOrd for *mut T {
22292231
}
22302232

22312233
#[inline(always)]
2234+
#[cfg_attr(not(bootstrap), allow(ambiguous_wide_pointer_comparisons))]
22322235
fn lt(&self, other: &*mut T) -> bool {
22332236
*self < *other
22342237
}
22352238

22362239
#[inline(always)]
2240+
#[cfg_attr(not(bootstrap), allow(ambiguous_wide_pointer_comparisons))]
22372241
fn le(&self, other: &*mut T) -> bool {
22382242
*self <= *other
22392243
}
22402244

22412245
#[inline(always)]
2246+
#[cfg_attr(not(bootstrap), allow(ambiguous_wide_pointer_comparisons))]
22422247
fn gt(&self, other: &*mut T) -> bool {
22432248
*self > *other
22442249
}
22452250

22462251
#[inline(always)]
2252+
#[cfg_attr(not(bootstrap), allow(ambiguous_wide_pointer_comparisons))]
22472253
fn ge(&self, other: &*mut T) -> bool {
22482254
*self >= *other
22492255
}

core/src/ptr/non_null.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,7 @@ impl<T: ?Sized> Eq for NonNull<T> {}
17911791
#[stable(feature = "nonnull", since = "1.25.0")]
17921792
impl<T: ?Sized> PartialEq for NonNull<T> {
17931793
#[inline]
1794+
#[cfg_attr(not(bootstrap), allow(ambiguous_wide_pointer_comparisons))]
17941795
fn eq(&self, other: &Self) -> bool {
17951796
self.as_ptr() == other.as_ptr()
17961797
}

0 commit comments

Comments
 (0)