Skip to content

Commit cabba6b

Browse files
committed
auto merge of #9136 : thestinger/rust/ptr, r=alexcrichton
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.
2 parents a6be8d3 + b7435cf commit cabba6b

File tree

2 files changed

+68
-5
lines changed

2 files changed

+68
-5
lines changed

src/librustc/middle/ty.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4267,6 +4267,7 @@ pub fn is_binopable(cx: ctxt, ty: t, op: ast::BinOp) -> bool {
42674267
static tycat_int: int = 3;
42684268
static tycat_float: int = 4;
42694269
static tycat_bot: int = 5;
4270+
static tycat_raw_ptr: int = 6;
42704271

42714272
static opcat_add: int = 0;
42724273
static opcat_sub: int = 1;
@@ -4310,6 +4311,7 @@ pub fn is_binopable(cx: ctxt, ty: t, op: ast::BinOp) -> bool {
43104311
ty_int(_) | ty_uint(_) | ty_infer(IntVar(_)) => tycat_int,
43114312
ty_float(_) | ty_infer(FloatVar(_)) => tycat_float,
43124313
ty_bot => tycat_bot,
4314+
ty_ptr(_) => tycat_raw_ptr,
43134315
_ => tycat_other
43144316
}
43154317
}
@@ -4324,7 +4326,8 @@ pub fn is_binopable(cx: ctxt, ty: t, op: ast::BinOp) -> bool {
43244326
/*char*/ [f, f, f, f, t, t, f, f],
43254327
/*int*/ [t, t, t, t, t, t, t, f],
43264328
/*float*/ [t, t, t, f, t, t, f, f],
4327-
/*bot*/ [t, t, t, t, f, f, t, t]];
4329+
/*bot*/ [t, t, t, t, f, f, t, t],
4330+
/*raw ptr*/ [f, f, f, f, t, t, f, f]];
43284331

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

src/libstd/ptr.rs

Lines changed: 64 additions & 4 deletions
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)