Skip to content

Commit 67da586

Browse files
committed
Auto merge of #106450 - albertlarsan68:fix-arc-ptr-eq, r=Amanieu
Make `{Arc,Rc,Weak}::ptr_eq` ignore pointer metadata FCP completed in #103763 (comment) Closes #103763
2 parents c55d1ee + f04da22 commit 67da586

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

@@ -2466,8 +2466,8 @@ impl<T: ?Sized> Weak<T> {
24662466
}
24672467

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

library/alloc/src/sync.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,7 @@ impl<T: ?Sized> Arc<T> {
12671267
}
12681268

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

@@ -2254,8 +2254,8 @@ impl<T: ?Sized> Weak<T> {
22542254
}
22552255

22562256
/// Returns `true` if the two `Weak`s point to the same allocation similar to [`ptr::eq`], or if
2257-
/// both don't point to any allocation (because they were created with `Weak::new()`). See [that
2258-
/// function][`ptr::eq`] for caveats when comparing `dyn Trait` pointers.
2257+
/// both don't point to any allocation (because they were created with `Weak::new()`). However,
2258+
/// this function ignores the metadata of `dyn Trait` pointers.
22592259
///
22602260
/// # Notes
22612261
///
@@ -2298,7 +2298,7 @@ impl<T: ?Sized> Weak<T> {
22982298
#[must_use]
22992299
#[stable(feature = "weak_ptr_eq", since = "1.39.0")]
23002300
pub fn ptr_eq(&self, other: &Self) -> bool {
2301-
self.ptr.as_ptr() == other.ptr.as_ptr()
2301+
ptr::eq(self.ptr.as_ptr() as *const (), other.ptr.as_ptr() as *const ())
23022302
}
23032303
}
23042304

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)