Skip to content

Commit 4c40153

Browse files
authored
Rollup merge of #121556 - GrigorenkoPV:addr_of, r=Nilstrieb
Use `addr_of!` As per #121303 (comment)
2 parents 9975e84 + 613cb32 commit 4c40153

File tree

42 files changed

+92
-83
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+92
-83
lines changed

compiler/rustc_codegen_llvm/src/back/archive.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ fn get_llvm_object_symbols(
313313
llvm::LLVMRustGetSymbols(
314314
buf.as_ptr(),
315315
buf.len(),
316-
&mut *state as *mut &mut _ as *mut c_void,
316+
std::ptr::addr_of_mut!(*state) as *mut c_void,
317317
callback,
318318
error_callback,
319319
)

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl WriteBackendMethods for LlvmCodegenBackend {
169169
fn print_pass_timings(&self) {
170170
unsafe {
171171
let mut size = 0;
172-
let cstr = llvm::LLVMRustPrintPassTimings(&mut size as *mut usize);
172+
let cstr = llvm::LLVMRustPrintPassTimings(std::ptr::addr_of_mut!(size));
173173
if cstr.is_null() {
174174
println!("failed to get pass timings");
175175
} else {
@@ -182,7 +182,7 @@ impl WriteBackendMethods for LlvmCodegenBackend {
182182
fn print_statistics(&self) {
183183
unsafe {
184184
let mut size = 0;
185-
let cstr = llvm::LLVMRustPrintStatistics(&mut size as *mut usize);
185+
let cstr = llvm::LLVMRustPrintStatistics(std::ptr::addr_of_mut!(size));
186186
if cstr.is_null() {
187187
println!("failed to get pass stats");
188188
} else {

compiler/rustc_codegen_llvm/src/llvm_util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ pub(crate) fn print(req: &PrintRequest, mut out: &mut dyn PrintBackendInfo, sess
435435
&tm,
436436
cpu_cstring.as_ptr(),
437437
callback,
438-
&mut out as *mut &mut dyn PrintBackendInfo as *mut c_void,
438+
std::ptr::addr_of_mut!(out) as *mut c_void,
439439
);
440440
}
441441
}

compiler/rustc_data_structures/src/sync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ impl<T> RwLock<T> {
429429
#[inline(always)]
430430
pub fn leak(&self) -> &T {
431431
let guard = self.read();
432-
let ret = unsafe { &*(&*guard as *const T) };
432+
let ret = unsafe { &*std::ptr::addr_of!(*guard) };
433433
std::mem::forget(guard);
434434
ret
435435
}

compiler/rustc_middle/src/query/on_disk_cache.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ impl<'sess> OnDiskCache<'sess> {
233233

234234
for (index, file) in files.iter().enumerate() {
235235
let index = SourceFileIndex(index as u32);
236-
let file_ptr: *const SourceFile = &**file as *const _;
236+
let file_ptr: *const SourceFile = std::ptr::addr_of!(**file);
237237
file_to_file_index.insert(file_ptr, index);
238238
let source_file_id = EncodedSourceFileId::new(tcx, file);
239239
file_index_to_stable_id.insert(index, source_file_id);
@@ -835,7 +835,7 @@ pub struct CacheEncoder<'a, 'tcx> {
835835
impl<'a, 'tcx> CacheEncoder<'a, 'tcx> {
836836
#[inline]
837837
fn source_file_index(&mut self, source_file: Lrc<SourceFile>) -> SourceFileIndex {
838-
self.file_to_file_index[&(&*source_file as *const SourceFile)]
838+
self.file_to_file_index[&std::ptr::addr_of!(*source_file)]
839839
}
840840

841841
/// Encode something with additional information that allows to do some

compiler/rustc_middle/src/ty/list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<T> List<T> {
6161
// length) that is 64-byte aligned, thus featuring the necessary
6262
// trailing padding for elements with up to 64-byte alignment.
6363
static EMPTY_SLICE: InOrder<usize, MaxAlign> = InOrder(0, MaxAlign);
64-
unsafe { &*(&EMPTY_SLICE as *const _ as *const List<T>) }
64+
unsafe { &*(std::ptr::addr_of!(EMPTY_SLICE) as *const List<T>) }
6565
}
6666

6767
pub fn len(&self) -> usize {

compiler/stable_mir/src/compiler_interface.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ where
208208
if TLV.is_set() {
209209
Err(Error::from("StableMIR already running"))
210210
} else {
211-
let ptr: *const () = &context as *const &_ as _;
211+
let ptr: *const () = std::ptr::addr_of!(context) as _;
212212
TLV.set(&Cell::new(ptr), || Ok(f()))
213213
}
214214
}

library/alloc/src/boxed/thin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl<T: ?Sized> ThinBox<T> {
176176

177177
fn with_header(&self) -> &WithHeader<<T as Pointee>::Metadata> {
178178
// SAFETY: both types are transparent to `NonNull<u8>`
179-
unsafe { &*((&self.ptr) as *const WithOpaqueHeader as *const WithHeader<_>) }
179+
unsafe { &*(core::ptr::addr_of!(self.ptr) as *const WithHeader<_>) }
180180
}
181181
}
182182

library/alloc/src/rc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,7 +1969,7 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
19691969

19701970
// Copy value as bytes
19711971
ptr::copy_nonoverlapping(
1972-
&*src as *const T as *const u8,
1972+
core::ptr::addr_of!(*src) as *const u8,
19731973
ptr::addr_of_mut!((*ptr).value) as *mut u8,
19741974
value_size,
19751975
);
@@ -2440,7 +2440,7 @@ impl<T: ?Sized + fmt::Debug, A: Allocator> fmt::Debug for Rc<T, A> {
24402440
#[stable(feature = "rust1", since = "1.0.0")]
24412441
impl<T: ?Sized, A: Allocator> fmt::Pointer for Rc<T, A> {
24422442
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2443-
fmt::Pointer::fmt(&(&**self as *const T), f)
2443+
fmt::Pointer::fmt(&core::ptr::addr_of!(**self), f)
24442444
}
24452445
}
24462446

library/alloc/src/sync.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,7 +1914,7 @@ impl<T: ?Sized, A: Allocator> Arc<T, A> {
19141914

19151915
// Copy value as bytes
19161916
ptr::copy_nonoverlapping(
1917-
&*src as *const T as *const u8,
1917+
core::ptr::addr_of!(*src) as *const u8,
19181918
ptr::addr_of_mut!((*ptr).data) as *mut u8,
19191919
value_size,
19201920
);
@@ -3265,7 +3265,7 @@ impl<T: ?Sized + fmt::Debug, A: Allocator> fmt::Debug for Arc<T, A> {
32653265
#[stable(feature = "rust1", since = "1.0.0")]
32663266
impl<T: ?Sized, A: Allocator> fmt::Pointer for Arc<T, A> {
32673267
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3268-
fmt::Pointer::fmt(&(&**self as *const T), f)
3268+
fmt::Pointer::fmt(&core::ptr::addr_of!(**self), f)
32693269
}
32703270
}
32713271

0 commit comments

Comments
 (0)