Skip to content

Commit

Permalink
rustc: Make shape-based compare glue never called for comparison oper…
Browse files Browse the repository at this point in the history
…ators.

Only called for string patterns.
  • Loading branch information
pcwalton committed Sep 10, 2012
1 parent 9a15c50 commit 22b8757
Show file tree
Hide file tree
Showing 22 changed files with 322 additions and 82 deletions.
2 changes: 1 addition & 1 deletion src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ pure fn min<A:Copy Ord,IA:BaseIter<A>>(self: IA) -> A {
}
}

pure fn max<A:Copy,IA:BaseIter<A>>(self: IA) -> A {
pure fn max<A:Copy Ord,IA:BaseIter<A>>(self: IA) -> A {
match do foldl::<A,Option<A>,IA>(self, None) |a, b| {
match a {
Some(a_) if a_ > b => {
Expand Down
17 changes: 3 additions & 14 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,28 +769,17 @@ pure fn lt(a: &str, b: &str) -> bool {

/// Bytewise less than or equal
pure fn le(a: &str, b: &str) -> bool {
let (a_len, b_len) = (a.len(), b.len());
let mut end = uint::min(a_len, b_len);

let mut i = 0;
while i < end {
let (c_a, c_b) = (a[i], b[i]);
if c_a < c_b { return true; }
if c_a > c_b { return false; }
i += 1;
}

return a_len <= b_len;
!lt(b, a)
}

/// Bytewise greater than or equal
pure fn ge(a: &str, b: &str) -> bool {
!lt(b, a)
!lt(a, b)
}

/// Bytewise greater than
pure fn gt(a: &str, b: &str) -> bool {
!le(b, a)
!le(a, b)
}

impl &str: Eq {
Expand Down
40 changes: 40 additions & 0 deletions src/libcore/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,46 @@ enum SchedMode {
PlatformThread
}

impl SchedMode : cmp::Eq {
pure fn eq(&&other: SchedMode) -> bool {
match self {
SingleThreaded => {
match other {
SingleThreaded => true,
_ => false
}
}
ThreadPerCore => {
match other {
ThreadPerCore => true,
_ => false
}
}
ThreadPerTask => {
match other {
ThreadPerTask => true,
_ => false
}
}
ManualThreads(e0a) => {
match other {
ManualThreads(e0b) => e0a == e0b,
_ => false
}
}
PlatformThread => {
match other {
PlatformThread => true,
_ => false
}
}
}
}
pure fn ne(&&other: SchedMode) -> bool {
!self.eq(other)
}
}

/**
* Scheduler configuration options
*
Expand Down
79 changes: 78 additions & 1 deletion src/libstd/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

//! json serialization
use core::cmp::Eq;
use core::cmp::{Eq, Ord};
use result::{Result, Ok, Err};
use io::WriterUtil;
use map::hashmap;
use map::map;
use sort::Sort;

export Json;
export Error;
Expand Down Expand Up @@ -603,6 +604,75 @@ pure fn eq(value0: Json, value1: Json) -> bool {
}
}

/// Test if two json values are less than one another
pure fn lt(value0: Json, value1: Json) -> bool {
match value0 {
Num(f0) => {
match value1 {
Num(f1) => f0 < f1,
String(_) | Boolean(_) | List(_) | Dict(_) | Null => true
}
}

String(s0) => {
match value1 {
Num(_) => false,
String(s1) => s0 < s1,
Boolean(_) | List(_) | Dict(_) | Null => true
}
}

Boolean(b0) => {
match value1 {
Num(_) | String(_) => false,
Boolean(b1) => b0 < b1,
List(_) | Dict(_) | Null => true
}
}

List(l0) => {
match value1 {
Num(_) | String(_) | Boolean(_) => false,
List(l1) => l0 < l1,
Dict(_) | Null => true
}
}

Dict(d0) => {
match value1 {
Num(_) | String(_) | Boolean(_) | List(_) => false,
Dict(d1) => {
unchecked {
let (d0_flat, d1_flat) = {
let d0_flat = dvec::DVec();
for d0.each |k, v| { d0_flat.push((k, v)); }
let d0_flat = dvec::unwrap(d0_flat);
d0_flat.qsort();

let mut d1_flat = dvec::DVec();
for d1.each |k, v| { d1_flat.push((k, v)); }
let d1_flat = dvec::unwrap(d1_flat);
d1_flat.qsort();

(d0_flat, d1_flat)
};

d0_flat < d1_flat
}
}
Null => true
}
}

Null => {
match value1 {
Num(_) | String(_) | Boolean(_) | List(_) | Dict(_) => false,
Null => true
}
}
}
}

impl Error : Eq {
pure fn eq(&&other: Error) -> bool {
self.line == other.line &&
Expand All @@ -617,6 +687,13 @@ impl Json : Eq {
pure fn ne(&&other: Json) -> bool { !self.eq(other) }
}

impl Json : Ord {
pure fn lt(&&other: Json) -> bool { lt(self, other) }
pure fn le(&&other: Json) -> bool { !other.lt(self) }
pure fn ge(&&other: Json) -> bool { !self.lt(other) }
pure fn gt(&&other: Json) -> bool { other.lt(self) }
}

trait ToJson { fn to_json() -> Json; }

impl Json: ToJson {
Expand Down
51 changes: 50 additions & 1 deletion src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,13 @@ enum proto {
proto_block, // fn&
}

impl proto : cmp::Eq {
pure fn eq(&&other: proto) -> bool {
(self as uint) == (other as uint)
}
pure fn ne(&&other: proto) -> bool { !self.eq(other) }
}

#[auto_serialize]
enum vstore {
// FIXME (#2112): Change uint to @expr (actually only constant exprs)
Expand Down Expand Up @@ -454,7 +461,49 @@ impl binop : cmp::Eq {
enum unop {
box(mutability),
uniq(mutability),
deref, not, neg
deref,
not,
neg
}

impl unop : cmp::Eq {
pure fn eq(&&other: unop) -> bool {
match self {
box(e0a) => {
match other {
box(e0b) => e0a == e0b,
_ => false
}
}
uniq(e0a) => {
match other {
uniq(e0b) => e0a == e0b,
_ => false
}
}
deref => {
match other {
deref => true,
_ => false
}
}
not => {
match other {
not => true,
_ => false
}
}
neg => {
match other {
neg => true,
_ => false
}
}
}
}
pure fn ne(&&other: unop) -> bool {
!self.eq(other)
}
}

// Generally, after typeck you can get the inferred value
Expand Down
7 changes: 7 additions & 0 deletions src/libsyntax/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,13 @@ enum inline_attr {
ia_never,
}
impl inline_attr : cmp::Eq {
pure fn eq(&&other: inline_attr) -> bool {
(self as uint) == (other as uint)
}
pure fn ne(&&other: inline_attr) -> bool { !self.eq(other) }
}
/// True if something like #[inline] is found in the list of attrs.
fn find_inline_attr(attrs: ~[ast::attribute]) -> inline_attr {
// FIXME (#2809)---validate the usage of #[inline] and #[inline(always)]
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ext/pipes/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ struct protocol_ {
fn get_state_by_id(id: uint) -> state { self.states[id] }

fn has_state(name: ~str) -> bool {
self.states.find(|i| i.name == name) != None
self.states.find(|i| i.name == name).is_some()
}

fn filename() -> ~str {
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/ext/simplext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn elts_to_ell(cx: ext_ctxt, elts: ~[@expr]) ->
match elt.node {
expr_mac(m) => match m.node {
ast::mac_ellipsis => {
if res != None {
if res.is_some() {
cx.span_fatal(m.span, ~"only one ellipsis allowed");
}
res =
Expand Down Expand Up @@ -449,7 +449,7 @@ fn p_t_s_rec(cx: ext_ctxt, m: matchable, s: selector, b: binders) {
}
}
{pre: pre, rep: None, post: post} => {
if post != ~[] {
if post.len() > 0 {
cx.bug(~"elts_to_ell provided an invalid result");
}
p_t_s_r_length(cx, vec::len(pre), false, s, b);
Expand Down
9 changes: 9 additions & 0 deletions src/libsyntax/parse/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ enum cmnt_style {
blank_line, // Just a manual blank line "\n\n", for layout
}

impl cmnt_style : cmp::Eq {
pure fn eq(&&other: cmnt_style) -> bool {
(self as uint) == (other as uint)
}
pure fn ne(&&other: cmnt_style) -> bool {
(self as uint) != (other as uint)
}
}

type cmnt = {style: cmnt_style, lines: ~[~str], pos: uint};

fn is_doc_comment(s: ~str) -> bool {
Expand Down
43 changes: 28 additions & 15 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1616,10 +1616,13 @@ fn print_fn_args_and_ret(s: ps, decl: ast::fn_decl,
pclose(s);

maybe_print_comment(s, decl.output.span.lo);
if decl.output.node != ast::ty_nil {
space_if_not_bol(s);
word_space(s, ~"->");
print_type(s, decl.output);
match decl.output.node {
ast::ty_nil => {}
_ => {
space_if_not_bol(s);
word_space(s, ~"->");
print_type(s, decl.output);
}
}
}

Expand All @@ -1628,11 +1631,16 @@ fn print_fn_block_args(s: ps, decl: ast::fn_decl,
word(s.s, ~"|");
print_fn_args(s, decl, cap_items, None);
word(s.s, ~"|");
if decl.output.node != ast::ty_infer {
space_if_not_bol(s);
word_space(s, ~"->");
print_type(s, decl.output);
match decl.output.node {
ast::ty_infer => {}
_ => {
space_if_not_bol(s);
word_space(s, ~"->");
print_type(s, decl.output);
}
}
maybe_print_comment(s, decl.output.span.lo);
}
Expand Down Expand Up @@ -1829,14 +1837,19 @@ fn print_ty_fn(s: ps, opt_proto: Option<ast::proto>, purity: ast::purity,
pclose(s);
maybe_print_comment(s, decl.output.span.lo);
if decl.output.node != ast::ty_nil {
space_if_not_bol(s);
ibox(s, indent_unit);
word_space(s, ~"->");
if decl.cf == ast::noreturn { word_nbsp(s, ~"!"); }
else { print_type(s, decl.output); }
end(s);
match decl.output.node {
ast::ty_nil => {}
_ => {
space_if_not_bol(s);
ibox(s, indent_unit);
word_space(s, ~"->");
if decl.cf == ast::noreturn { word_nbsp(s, ~"!"); }
else { print_type(s, decl.output); }
end(s);
}
}
end(s);
}
Expand Down
7 changes: 7 additions & 0 deletions src/rustc/middle/borrowck/check_loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ enum assignment_type {
at_mutbl_ref,
}

impl assignment_type : cmp::Eq {
pure fn eq(&&other: assignment_type) -> bool {
(self as uint) == (other as uint)
}
pure fn ne(&&other: assignment_type) -> bool { !self.eq(other) }
}

impl assignment_type {
fn checked_by_liveness() -> bool {
// the liveness pass guarantees that immutable local variables
Expand Down
Loading

0 comments on commit 22b8757

Please sign in to comment.