File tree 4 files changed +46
-4
lines changed
4 files changed +46
-4
lines changed Original file line number Diff line number Diff line change @@ -1002,7 +1002,9 @@ impl<T: ?Sized> Rc<T> {
1002
1002
///
1003
1003
/// [`ptr::eq`]: core::ptr::eq
1004
1004
pub fn ptr_eq ( this : & Self , other : & Self ) -> bool {
1005
- this. ptr . as_ptr ( ) == other. ptr . as_ptr ( )
1005
+ // The *const u8 cast discards the vtable to work around
1006
+ // https://github.com/rust-lang/rust/issues/46139
1007
+ this. ptr . as_ptr ( ) as * const u8 == other. ptr . as_ptr ( ) as * const u8
1006
1008
}
1007
1009
}
1008
1010
@@ -2136,7 +2138,9 @@ impl<T: ?Sized> Weak<T> {
2136
2138
#[ inline]
2137
2139
#[ stable( feature = "weak_ptr_eq" , since = "1.39.0" ) ]
2138
2140
pub fn ptr_eq ( & self , other : & Self ) -> bool {
2139
- self . ptr . as_ptr ( ) == other. ptr . as_ptr ( )
2141
+ // The *const u8 cast discards the vtable to work around
2142
+ // https://github.com/rust-lang/rust/issues/46139
2143
+ self . ptr . as_ptr ( ) as * const u8 == other. ptr . as_ptr ( ) as * const u8
2140
2144
}
2141
2145
}
2142
2146
Original file line number Diff line number Diff line change @@ -1066,7 +1066,9 @@ impl<T: ?Sized> Arc<T> {
1066
1066
///
1067
1067
/// [`ptr::eq`]: core::ptr::eq
1068
1068
pub fn ptr_eq ( this : & Self , other : & Self ) -> bool {
1069
- this. ptr . as_ptr ( ) == other. ptr . as_ptr ( )
1069
+ // The *const u8 cast discards the vtable to work around
1070
+ // https://github.com/rust-lang/rust/issues/46139
1071
+ this. ptr . as_ptr ( ) as * const u8 == other. ptr . as_ptr ( ) as * const u8
1070
1072
}
1071
1073
}
1072
1074
@@ -1936,7 +1938,9 @@ impl<T: ?Sized> Weak<T> {
1936
1938
#[ inline]
1937
1939
#[ stable( feature = "weak_ptr_eq" , since = "1.39.0" ) ]
1938
1940
pub fn ptr_eq ( & self , other : & Self ) -> bool {
1939
- self . ptr . as_ptr ( ) == other. ptr . as_ptr ( )
1941
+ // The *const u8 cast discards the vtable to work around
1942
+ // https://github.com/rust-lang/rust/issues/46139
1943
+ self . ptr . as_ptr ( ) as * const u8 == other. ptr . as_ptr ( ) as * const u8
1940
1944
}
1941
1945
}
1942
1946
Original file line number Diff line number Diff line change @@ -195,3 +195,20 @@ fn shared_from_iter_trustedlen_no_fuse() {
195
195
assert_trusted_len ( & iter) ;
196
196
assert_eq ! ( & [ Box :: new( 42 ) , Box :: new( 24 ) ] , & * iter. collect:: <Rc <[ _] >>( ) ) ;
197
197
}
198
+
199
+ mod other_mod {
200
+ use std:: sync:: Arc ;
201
+
202
+ pub fn cast ( r : Arc < ( ) > ) -> Arc < dyn Send > {
203
+ r
204
+ }
205
+ }
206
+
207
+ #[ test]
208
+ fn ptr_eq_ignores_duplicated_vtable ( ) {
209
+ let a = Arc :: new ( ( ) ) ;
210
+ let b = other_mod:: cast ( Arc :: clone ( & a) ) ;
211
+ let c: Arc < dyn Send > = a;
212
+ assert ! ( Arc :: ptr_eq( & b, & c) ) ;
213
+ assert ! ( Weak :: ptr_eq( & Arc :: downgrade( & b) , & Arc :: downgrade( & c) ) ) ;
214
+ }
Original file line number Diff line number Diff line change @@ -191,3 +191,20 @@ fn shared_from_iter_trustedlen_no_fuse() {
191
191
assert_trusted_len ( & iter) ;
192
192
assert_eq ! ( & [ Box :: new( 42 ) , Box :: new( 24 ) ] , & * iter. collect:: <Rc <[ _] >>( ) ) ;
193
193
}
194
+
195
+ mod other_mod {
196
+ use std:: rc:: Rc ;
197
+
198
+ pub fn cast ( r : Rc < ( ) > ) -> Rc < dyn Send > {
199
+ r
200
+ }
201
+ }
202
+
203
+ #[ test]
204
+ fn ptr_eq_ignores_duplicated_vtable ( ) {
205
+ let a = Rc :: new ( ( ) ) ;
206
+ let b = other_mod:: cast ( Rc :: clone ( & a) ) ;
207
+ let c: Rc < dyn Send > = a;
208
+ assert ! ( Rc :: ptr_eq( & b, & c) ) ;
209
+ assert ! ( Weak :: ptr_eq( & Rc :: downgrade( & b) , & Rc :: downgrade( & c) ) ) ;
210
+ }
You can’t perform that action at this time.
0 commit comments