Skip to content

Commit b7435cf

Browse files
committed
implement raw pointer comparisons in librustc
This is mostly for consistency, as you can now compare raw pointers in constant expressions or without the standard library. It also reduces the number of `ptrtoint` instructions in the IR, making tracking down culprits of what's usually an anti-pattern easier.
1 parent 6216661 commit b7435cf

File tree

2 files changed

+68
-5
lines changed

2 files changed

+68
-5
lines changed

src/librustc/middle/ty.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -4266,6 +4266,7 @@ pub fn is_binopable(cx: ctxt, ty: t, op: ast::BinOp) -> bool {
42664266
static tycat_int: int = 3;
42674267
static tycat_float: int = 4;
42684268
static tycat_bot: int = 5;
4269+
static tycat_raw_ptr: int = 6;
42694270

42704271
static opcat_add: int = 0;
42714272
static opcat_sub: int = 1;
@@ -4309,6 +4310,7 @@ pub fn is_binopable(cx: ctxt, ty: t, op: ast::BinOp) -> bool {
43094310
ty_int(_) | ty_uint(_) | ty_infer(IntVar(_)) => tycat_int,
43104311
ty_float(_) | ty_infer(FloatVar(_)) => tycat_float,
43114312
ty_bot => tycat_bot,
4313+
ty_ptr(_) => tycat_raw_ptr,
43124314
_ => tycat_other
43134315
}
43144316
}
@@ -4323,7 +4325,8 @@ pub fn is_binopable(cx: ctxt, ty: t, op: ast::BinOp) -> bool {
43234325
/*char*/ [f, f, f, f, t, t, f, f],
43244326
/*int*/ [t, t, t, t, t, t, t, f],
43254327
/*float*/ [t, t, t, f, t, t, f, f],
4326-
/*bot*/ [t, t, t, t, f, f, t, t]];
4328+
/*bot*/ [t, t, t, t, f, f, t, t],
4329+
/*raw ptr*/ [f, f, f, f, t, t, f, f]];
43274330

43284331
return tbl[tycat(cx, ty)][opcat(op)];
43294332
}

src/libstd/ptr.rs

+64-4
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ impl<T> RawPtr<T> for *mut T {
383383
}
384384

385385
// Equality for pointers
386-
#[cfg(not(test))]
386+
#[cfg(stage0, not(test))]
387387
impl<T> Eq for *T {
388388
#[inline]
389389
fn eq(&self, other: &*T) -> bool {
@@ -393,7 +393,17 @@ impl<T> Eq for *T {
393393
fn ne(&self, other: &*T) -> bool { !self.eq(other) }
394394
}
395395

396-
#[cfg(not(test))]
396+
#[cfg(not(stage0), not(test))]
397+
impl<T> Eq for *T {
398+
#[inline]
399+
fn eq(&self, other: &*T) -> bool {
400+
*self == *other
401+
}
402+
#[inline]
403+
fn ne(&self, other: &*T) -> bool { !self.eq(other) }
404+
}
405+
406+
#[cfg(stage0, not(test))]
397407
impl<T> Eq for *mut T {
398408
#[inline]
399409
fn eq(&self, other: &*mut T) -> bool {
@@ -403,6 +413,16 @@ impl<T> Eq for *mut T {
403413
fn ne(&self, other: &*mut T) -> bool { !self.eq(other) }
404414
}
405415

416+
#[cfg(not(stage0), not(test))]
417+
impl<T> Eq for *mut T {
418+
#[inline]
419+
fn eq(&self, other: &*mut T) -> bool {
420+
*self == *other
421+
}
422+
#[inline]
423+
fn ne(&self, other: &*mut T) -> bool { !self.eq(other) }
424+
}
425+
406426
// Equivalence for pointers
407427
#[cfg(not(test))]
408428
impl<T> Equiv<*mut T> for *T {
@@ -460,7 +480,7 @@ mod externfnpointers {
460480
}
461481

462482
// Comparison for pointers
463-
#[cfg(not(test))]
483+
#[cfg(stage0, not(test))]
464484
impl<T> Ord for *T {
465485
#[inline]
466486
fn lt(&self, other: &*T) -> bool {
@@ -480,7 +500,27 @@ impl<T> Ord for *T {
480500
}
481501
}
482502

483-
#[cfg(not(test))]
503+
#[cfg(not(stage0), not(test))]
504+
impl<T> Ord for *T {
505+
#[inline]
506+
fn lt(&self, other: &*T) -> bool {
507+
*self < *other
508+
}
509+
#[inline]
510+
fn le(&self, other: &*T) -> bool {
511+
*self <= *other
512+
}
513+
#[inline]
514+
fn ge(&self, other: &*T) -> bool {
515+
*self >= *other
516+
}
517+
#[inline]
518+
fn gt(&self, other: &*T) -> bool {
519+
*self > *other
520+
}
521+
}
522+
523+
#[cfg(stage0, not(test))]
484524
impl<T> Ord for *mut T {
485525
#[inline]
486526
fn lt(&self, other: &*mut T) -> bool {
@@ -500,6 +540,26 @@ impl<T> Ord for *mut T {
500540
}
501541
}
502542

543+
#[cfg(not(stage0), not(test))]
544+
impl<T> Ord for *mut T {
545+
#[inline]
546+
fn lt(&self, other: &*mut T) -> bool {
547+
*self < *other
548+
}
549+
#[inline]
550+
fn le(&self, other: &*mut T) -> bool {
551+
*self <= *other
552+
}
553+
#[inline]
554+
fn ge(&self, other: &*mut T) -> bool {
555+
*self >= *other
556+
}
557+
#[inline]
558+
fn gt(&self, other: &*mut T) -> bool {
559+
*self > *other
560+
}
561+
}
562+
503563
#[cfg(test)]
504564
pub mod ptr_tests {
505565
use super::*;

0 commit comments

Comments
 (0)