Skip to content

Commit f04da22

Browse files
Make {Arc,Rc,Weak}::ptr_eq ignore pointer metadata
1 parent ae3ab14 commit f04da22

File tree

6 files changed

+18
-38
lines changed

6 files changed

+18
-38
lines changed

library/alloc/src/rc.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ impl<T: ?Sized> Rc<T> {
11691169
#[inline]
11701170
#[stable(feature = "ptr_eq", since = "1.17.0")]
11711171
/// Returns `true` if the two `Rc`s point to the same allocation in a vein similar to
1172-
/// [`ptr::eq`]. See [that function][`ptr::eq`] for caveats when comparing `dyn Trait` pointers.
1172+
/// [`ptr::eq`]. This function ignores the metadata of `dyn Trait` pointers.
11731173
///
11741174
/// # Examples
11751175
///
@@ -1184,7 +1184,7 @@ impl<T: ?Sized> Rc<T> {
11841184
/// assert!(!Rc::ptr_eq(&five, &other_five));
11851185
/// ```
11861186
pub fn ptr_eq(this: &Self, other: &Self) -> bool {
1187-
this.ptr.as_ptr() == other.ptr.as_ptr()
1187+
this.ptr.as_ptr() as *const () == other.ptr.as_ptr() as *const ()
11881188
}
11891189
}
11901190

@@ -2468,8 +2468,8 @@ impl<T: ?Sized> Weak<T> {
24682468
}
24692469

24702470
/// Returns `true` if the two `Weak`s point to the same allocation similar to [`ptr::eq`], or if
2471-
/// both don't point to any allocation (because they were created with `Weak::new()`). See [that
2472-
/// function][`ptr::eq`] for caveats when comparing `dyn Trait` pointers.
2471+
/// both don't point to any allocation (because they were created with `Weak::new()`). However,
2472+
/// this function ignores the metadata of `dyn Trait` pointers.
24732473
///
24742474
/// # Notes
24752475
///
@@ -2510,7 +2510,7 @@ impl<T: ?Sized> Weak<T> {
25102510
#[must_use]
25112511
#[stable(feature = "weak_ptr_eq", since = "1.39.0")]
25122512
pub fn ptr_eq(&self, other: &Self) -> bool {
2513-
self.ptr.as_ptr() == other.ptr.as_ptr()
2513+
ptr::eq(self.ptr.as_ptr() as *const (), other.ptr.as_ptr() as *const ())
25142514
}
25152515
}
25162516

library/alloc/src/sync.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,7 @@ impl<T: ?Sized> Arc<T> {
12631263
}
12641264

12651265
/// Returns `true` if the two `Arc`s point to the same allocation in a vein similar to
1266-
/// [`ptr::eq`]. See [that function][`ptr::eq`] for caveats when comparing `dyn Trait` pointers.
1266+
/// [`ptr::eq`]. This function ignores the metadata of `dyn Trait` pointers.
12671267
///
12681268
/// # Examples
12691269
///
@@ -1283,7 +1283,7 @@ impl<T: ?Sized> Arc<T> {
12831283
#[must_use]
12841284
#[stable(feature = "ptr_eq", since = "1.17.0")]
12851285
pub fn ptr_eq(this: &Self, other: &Self) -> bool {
1286-
this.ptr.as_ptr() == other.ptr.as_ptr()
1286+
this.ptr.as_ptr() as *const () == other.ptr.as_ptr() as *const ()
12871287
}
12881288
}
12891289

@@ -2243,8 +2243,8 @@ impl<T: ?Sized> Weak<T> {
22432243
}
22442244

22452245
/// Returns `true` if the two `Weak`s point to the same allocation similar to [`ptr::eq`], or if
2246-
/// both don't point to any allocation (because they were created with `Weak::new()`). See [that
2247-
/// function][`ptr::eq`] for caveats when comparing `dyn Trait` pointers.
2246+
/// both don't point to any allocation (because they were created with `Weak::new()`). However,
2247+
/// this function ignores the metadata of `dyn Trait` pointers.
22482248
///
22492249
/// # Notes
22502250
///
@@ -2287,7 +2287,7 @@ impl<T: ?Sized> Weak<T> {
22872287
#[must_use]
22882288
#[stable(feature = "weak_ptr_eq", since = "1.39.0")]
22892289
pub fn ptr_eq(&self, other: &Self) -> bool {
2290-
self.ptr.as_ptr() == other.ptr.as_ptr()
2290+
ptr::eq(self.ptr.as_ptr() as *const (), other.ptr.as_ptr() as *const ())
22912291
}
22922292
}
22932293

src/tools/clippy/clippy_lints/src/unnamed_address.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ impl LateLintPass<'_> for UnnamedAddress {
9696
if let ExprKind::Call(func, [ref _left, ref _right]) = expr.kind;
9797
if let ExprKind::Path(ref func_qpath) = func.kind;
9898
if let Some(def_id) = cx.qpath_res(func_qpath, func.hir_id).opt_def_id();
99-
if match_def_path(cx, def_id, &paths::PTR_EQ) ||
100-
match_def_path(cx, def_id, &paths::RC_PTR_EQ) ||
101-
match_def_path(cx, def_id, &paths::ARC_PTR_EQ);
99+
if match_def_path(cx, def_id, &paths::PTR_EQ);
102100
let ty_param = cx.typeck_results().node_substs(func.hir_id).type_at(0);
103101
if ty_param.is_trait();
104102
then {

src/tools/clippy/clippy_utils/src/paths.rs

-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub const APPLICABILITY_VALUES: [[&str; 3]; 4] = [
1515
];
1616
#[cfg(feature = "internal")]
1717
pub const DIAGNOSTIC_BUILDER: [&str; 3] = ["rustc_errors", "diagnostic_builder", "DiagnosticBuilder"];
18-
pub const ARC_PTR_EQ: [&str; 4] = ["alloc", "sync", "Arc", "ptr_eq"];
1918
pub const BTREEMAP_CONTAINS_KEY: [&str; 6] = ["alloc", "collections", "btree", "map", "BTreeMap", "contains_key"];
2019
pub const BTREEMAP_INSERT: [&str; 6] = ["alloc", "collections", "btree", "map", "BTreeMap", "insert"];
2120
pub const BTREESET_ITER: [&str; 6] = ["alloc", "collections", "btree", "set", "BTreeSet", "iter"];
@@ -93,7 +92,6 @@ pub const PTR_WRITE_UNALIGNED: [&str; 3] = ["core", "ptr", "write_unaligned"];
9392
pub const PTR_WRITE_VOLATILE: [&str; 3] = ["core", "ptr", "write_volatile"];
9493
pub const PUSH_STR: [&str; 4] = ["alloc", "string", "String", "push_str"];
9594
pub const RANGE_ARGUMENT_TRAIT: [&str; 3] = ["core", "ops", "RangeBounds"];
96-
pub const RC_PTR_EQ: [&str; 4] = ["alloc", "rc", "Rc", "ptr_eq"];
9795
pub const REFCELL_REF: [&str; 3] = ["core", "cell", "Ref"];
9896
pub const REFCELL_REFMUT: [&str; 3] = ["core", "cell", "RefMut"];
9997
pub const REGEX_BUILDER_NEW: [&str; 5] = ["regex", "re_builder", "unicode", "RegexBuilder", "new"];

src/tools/clippy/tests/ui/vtable_address_comparisons.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ fn main() {
2323
let b = &1 as &dyn Debug;
2424
ptr::eq(a, b);
2525

26-
let a: Rc<dyn Debug> = Rc::new(1);
27-
Rc::ptr_eq(&a, &a);
28-
29-
let a: Arc<dyn Debug> = Arc::new(1);
30-
Arc::ptr_eq(&a, &a);
31-
3226
// These should be fine:
3327
let a = &1;
3428
ptr::eq(a, a);
@@ -39,6 +33,12 @@ fn main() {
3933
let a = Arc::new(1);
4034
Arc::ptr_eq(&a, &a);
4135

36+
let a: Rc<dyn Debug> = Rc::new(1);
37+
Rc::ptr_eq(&a, &a);
38+
39+
let a: Arc<dyn Debug> = Arc::new(1);
40+
Arc::ptr_eq(&a, &a);
41+
4242
let a: &[u8] = b"";
4343
ptr::eq(a, a);
4444
}

src/tools/clippy/tests/ui/vtable_address_comparisons.stderr

+1-17
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,5 @@ LL | ptr::eq(a, b);
6363
|
6464
= help: consider extracting and comparing data pointers only
6565

66-
error: comparing trait object pointers compares a non-unique vtable address
67-
--> $DIR/vtable_address_comparisons.rs:27:5
68-
|
69-
LL | Rc::ptr_eq(&a, &a);
70-
| ^^^^^^^^^^^^^^^^^^
71-
|
72-
= help: consider extracting and comparing data pointers only
73-
74-
error: comparing trait object pointers compares a non-unique vtable address
75-
--> $DIR/vtable_address_comparisons.rs:30:5
76-
|
77-
LL | Arc::ptr_eq(&a, &a);
78-
| ^^^^^^^^^^^^^^^^^^^
79-
|
80-
= help: consider extracting and comparing data pointers only
81-
82-
error: aborting due to 10 previous errors
66+
error: aborting due to 8 previous errors
8367

0 commit comments

Comments
 (0)