Skip to content

Commit 156c35f

Browse files
authored
Rollup merge of #57547 - Xanewok:ptr-eq, r=petrochenkov
Use `ptr::eq` where applicable Stumbled upon a few of `A as *const _ as usize == B as *const as usize`, so I decided to follow the programming boy scout rule (:smile:) and replaced the pattern with more widely used `ptr::eq`.
2 parents 11549df + 7948b18 commit 156c35f

File tree

4 files changed

+12
-13
lines changed

4 files changed

+12
-13
lines changed

Diff for: src/librustc/ty/context.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ use std::hash::{Hash, Hasher};
5959
use std::fmt;
6060
use std::mem;
6161
use std::ops::{Deref, Bound};
62+
use std::ptr;
6263
use std::iter;
6364
use std::sync::mpsc;
6465
use std::sync::Arc;
@@ -168,7 +169,7 @@ impl<'gcx: 'tcx, 'tcx> CtxtInterners<'tcx> {
168169

169170
// Make sure we don't end up with inference
170171
// types/regions in the global interner
171-
if local as *const _ as usize == global as *const _ as usize {
172+
if ptr::eq(local, global) {
172173
bug!("Attempted to intern `{:?}` which contains \
173174
inference types/regions in the global type context",
174175
&ty_struct);
@@ -1135,9 +1136,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
11351136

11361137
/// Returns true if self is the same as self.global_tcx().
11371138
fn is_global(self) -> bool {
1138-
let local = self.interners as *const _;
1139-
let global = &self.global_interners as *const _;
1140-
local as usize == global as usize
1139+
ptr::eq(self.interners, &self.global_interners)
11411140
}
11421141

11431142
/// Create a type context and call the closure with a `TyCtxt` reference
@@ -1787,6 +1786,7 @@ pub mod tls {
17871786
use std::fmt;
17881787
use std::mem;
17891788
use std::marker::PhantomData;
1789+
use std::ptr;
17901790
use syntax_pos;
17911791
use ty::query;
17921792
use errors::{Diagnostic, TRACK_DIAGNOSTICS};
@@ -2021,8 +2021,7 @@ pub mod tls {
20212021
{
20222022
with_context(|context| {
20232023
unsafe {
2024-
let gcx = tcx.gcx as *const _ as usize;
2025-
assert!(context.tcx.gcx as *const _ as usize == gcx);
2024+
assert!(ptr::eq(context.tcx.gcx, tcx.gcx));
20262025
let context: &ImplicitCtxt<'_, '_, '_> = mem::transmute(context);
20272026
f(context)
20282027
}
@@ -2040,10 +2039,8 @@ pub mod tls {
20402039
{
20412040
with_context(|context| {
20422041
unsafe {
2043-
let gcx = tcx.gcx as *const _ as usize;
2044-
let interners = tcx.interners as *const _ as usize;
2045-
assert!(context.tcx.gcx as *const _ as usize == gcx);
2046-
assert!(context.tcx.interners as *const _ as usize == interners);
2042+
assert!(ptr::eq(context.tcx.gcx, tcx.gcx));
2043+
assert!(ptr::eq(context.tcx.interners, tcx.interners));
20472044
let context: &ImplicitCtxt<'_, '_, '_> = mem::transmute(context);
20482045
f(context)
20492046
}

Diff for: src/librustc_codegen_llvm/debuginfo/metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use syntax_pos::{self, Span, FileName};
4747

4848
impl PartialEq for llvm::Metadata {
4949
fn eq(&self, other: &Self) -> bool {
50-
self as *const _ == other as *const _
50+
ptr::eq(self, other)
5151
}
5252
}
5353

Diff for: src/librustc_codegen_llvm/type_.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ use abi::{LlvmType, FnTypeExt};
2020

2121
use std::fmt;
2222
use std::cell::RefCell;
23+
use std::ptr;
2324

2425
use libc::c_uint;
2526

2627
impl PartialEq for Type {
2728
fn eq(&self, other: &Self) -> bool {
28-
self as *const _ == other as *const _
29+
ptr::eq(self, other)
2930
}
3031
}
3132

Diff for: src/librustc_codegen_llvm/value.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ use llvm;
44

55
use std::fmt;
66
use std::hash::{Hash, Hasher};
7+
use std::ptr;
78

89
impl PartialEq for Value {
910
fn eq(&self, other: &Self) -> bool {
10-
self as *const _ == other as *const _
11+
ptr::eq(self, other)
1112
}
1213
}
1314

0 commit comments

Comments
 (0)