Skip to content

Commit 22b8757

Browse files
committed
rustc: Make shape-based compare glue never called for comparison operators.
Only called for string patterns.
1 parent 9a15c50 commit 22b8757

File tree

22 files changed

+322
-82
lines changed

22 files changed

+322
-82
lines changed

src/libcore/iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ pure fn min<A:Copy Ord,IA:BaseIter<A>>(self: IA) -> A {
179179
}
180180
}
181181

182-
pure fn max<A:Copy,IA:BaseIter<A>>(self: IA) -> A {
182+
pure fn max<A:Copy Ord,IA:BaseIter<A>>(self: IA) -> A {
183183
match do foldl::<A,Option<A>,IA>(self, None) |a, b| {
184184
match a {
185185
Some(a_) if a_ > b => {

src/libcore/str.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -769,28 +769,17 @@ pure fn lt(a: &str, b: &str) -> bool {
769769

770770
/// Bytewise less than or equal
771771
pure fn le(a: &str, b: &str) -> bool {
772-
let (a_len, b_len) = (a.len(), b.len());
773-
let mut end = uint::min(a_len, b_len);
774-
775-
let mut i = 0;
776-
while i < end {
777-
let (c_a, c_b) = (a[i], b[i]);
778-
if c_a < c_b { return true; }
779-
if c_a > c_b { return false; }
780-
i += 1;
781-
}
782-
783-
return a_len <= b_len;
772+
!lt(b, a)
784773
}
785774

786775
/// Bytewise greater than or equal
787776
pure fn ge(a: &str, b: &str) -> bool {
788-
!lt(b, a)
777+
!lt(a, b)
789778
}
790779

791780
/// Bytewise greater than
792781
pure fn gt(a: &str, b: &str) -> bool {
793-
!le(b, a)
782+
!le(a, b)
794783
}
795784

796785
impl &str: Eq {

src/libcore/task.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,46 @@ enum SchedMode {
158158
PlatformThread
159159
}
160160

161+
impl SchedMode : cmp::Eq {
162+
pure fn eq(&&other: SchedMode) -> bool {
163+
match self {
164+
SingleThreaded => {
165+
match other {
166+
SingleThreaded => true,
167+
_ => false
168+
}
169+
}
170+
ThreadPerCore => {
171+
match other {
172+
ThreadPerCore => true,
173+
_ => false
174+
}
175+
}
176+
ThreadPerTask => {
177+
match other {
178+
ThreadPerTask => true,
179+
_ => false
180+
}
181+
}
182+
ManualThreads(e0a) => {
183+
match other {
184+
ManualThreads(e0b) => e0a == e0b,
185+
_ => false
186+
}
187+
}
188+
PlatformThread => {
189+
match other {
190+
PlatformThread => true,
191+
_ => false
192+
}
193+
}
194+
}
195+
}
196+
pure fn ne(&&other: SchedMode) -> bool {
197+
!self.eq(other)
198+
}
199+
}
200+
161201
/**
162202
* Scheduler configuration options
163203
*

src/libstd/json.rs

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
//! json serialization
55
6-
use core::cmp::Eq;
6+
use core::cmp::{Eq, Ord};
77
use result::{Result, Ok, Err};
88
use io::WriterUtil;
99
use map::hashmap;
1010
use map::map;
11+
use sort::Sort;
1112

1213
export Json;
1314
export Error;
@@ -603,6 +604,75 @@ pure fn eq(value0: Json, value1: Json) -> bool {
603604
}
604605
}
605606

607+
/// Test if two json values are less than one another
608+
pure fn lt(value0: Json, value1: Json) -> bool {
609+
match value0 {
610+
Num(f0) => {
611+
match value1 {
612+
Num(f1) => f0 < f1,
613+
String(_) | Boolean(_) | List(_) | Dict(_) | Null => true
614+
}
615+
}
616+
617+
String(s0) => {
618+
match value1 {
619+
Num(_) => false,
620+
String(s1) => s0 < s1,
621+
Boolean(_) | List(_) | Dict(_) | Null => true
622+
}
623+
}
624+
625+
Boolean(b0) => {
626+
match value1 {
627+
Num(_) | String(_) => false,
628+
Boolean(b1) => b0 < b1,
629+
List(_) | Dict(_) | Null => true
630+
}
631+
}
632+
633+
List(l0) => {
634+
match value1 {
635+
Num(_) | String(_) | Boolean(_) => false,
636+
List(l1) => l0 < l1,
637+
Dict(_) | Null => true
638+
}
639+
}
640+
641+
Dict(d0) => {
642+
match value1 {
643+
Num(_) | String(_) | Boolean(_) | List(_) => false,
644+
Dict(d1) => {
645+
unchecked {
646+
let (d0_flat, d1_flat) = {
647+
let d0_flat = dvec::DVec();
648+
for d0.each |k, v| { d0_flat.push((k, v)); }
649+
let d0_flat = dvec::unwrap(d0_flat);
650+
d0_flat.qsort();
651+
652+
let mut d1_flat = dvec::DVec();
653+
for d1.each |k, v| { d1_flat.push((k, v)); }
654+
let d1_flat = dvec::unwrap(d1_flat);
655+
d1_flat.qsort();
656+
657+
(d0_flat, d1_flat)
658+
};
659+
660+
d0_flat < d1_flat
661+
}
662+
}
663+
Null => true
664+
}
665+
}
666+
667+
Null => {
668+
match value1 {
669+
Num(_) | String(_) | Boolean(_) | List(_) | Dict(_) => false,
670+
Null => true
671+
}
672+
}
673+
}
674+
}
675+
606676
impl Error : Eq {
607677
pure fn eq(&&other: Error) -> bool {
608678
self.line == other.line &&
@@ -617,6 +687,13 @@ impl Json : Eq {
617687
pure fn ne(&&other: Json) -> bool { !self.eq(other) }
618688
}
619689

690+
impl Json : Ord {
691+
pure fn lt(&&other: Json) -> bool { lt(self, other) }
692+
pure fn le(&&other: Json) -> bool { !other.lt(self) }
693+
pure fn ge(&&other: Json) -> bool { !self.lt(other) }
694+
pure fn gt(&&other: Json) -> bool { other.lt(self) }
695+
}
696+
620697
trait ToJson { fn to_json() -> Json; }
621698

622699
impl Json: ToJson {

src/libsyntax/ast.rs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,13 @@ enum proto {
405405
proto_block, // fn&
406406
}
407407

408+
impl proto : cmp::Eq {
409+
pure fn eq(&&other: proto) -> bool {
410+
(self as uint) == (other as uint)
411+
}
412+
pure fn ne(&&other: proto) -> bool { !self.eq(other) }
413+
}
414+
408415
#[auto_serialize]
409416
enum vstore {
410417
// FIXME (#2112): Change uint to @expr (actually only constant exprs)
@@ -454,7 +461,49 @@ impl binop : cmp::Eq {
454461
enum unop {
455462
box(mutability),
456463
uniq(mutability),
457-
deref, not, neg
464+
deref,
465+
not,
466+
neg
467+
}
468+
469+
impl unop : cmp::Eq {
470+
pure fn eq(&&other: unop) -> bool {
471+
match self {
472+
box(e0a) => {
473+
match other {
474+
box(e0b) => e0a == e0b,
475+
_ => false
476+
}
477+
}
478+
uniq(e0a) => {
479+
match other {
480+
uniq(e0b) => e0a == e0b,
481+
_ => false
482+
}
483+
}
484+
deref => {
485+
match other {
486+
deref => true,
487+
_ => false
488+
}
489+
}
490+
not => {
491+
match other {
492+
not => true,
493+
_ => false
494+
}
495+
}
496+
neg => {
497+
match other {
498+
neg => true,
499+
_ => false
500+
}
501+
}
502+
}
503+
}
504+
pure fn ne(&&other: unop) -> bool {
505+
!self.eq(other)
506+
}
458507
}
459508

460509
// Generally, after typeck you can get the inferred value

src/libsyntax/attr.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,13 @@ enum inline_attr {
337337
ia_never,
338338
}
339339
340+
impl inline_attr : cmp::Eq {
341+
pure fn eq(&&other: inline_attr) -> bool {
342+
(self as uint) == (other as uint)
343+
}
344+
pure fn ne(&&other: inline_attr) -> bool { !self.eq(other) }
345+
}
346+
340347
/// True if something like #[inline] is found in the list of attrs.
341348
fn find_inline_attr(attrs: ~[ast::attribute]) -> inline_attr {
342349
// FIXME (#2809)---validate the usage of #[inline] and #[inline(always)]

src/libsyntax/ext/pipes/proto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ struct protocol_ {
143143
fn get_state_by_id(id: uint) -> state { self.states[id] }
144144

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

149149
fn filename() -> ~str {

src/libsyntax/ext/simplext.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn elts_to_ell(cx: ext_ctxt, elts: ~[@expr]) ->
6868
match elt.node {
6969
expr_mac(m) => match m.node {
7070
ast::mac_ellipsis => {
71-
if res != None {
71+
if res.is_some() {
7272
cx.span_fatal(m.span, ~"only one ellipsis allowed");
7373
}
7474
res =
@@ -449,7 +449,7 @@ fn p_t_s_rec(cx: ext_ctxt, m: matchable, s: selector, b: binders) {
449449
}
450450
}
451451
{pre: pre, rep: None, post: post} => {
452-
if post != ~[] {
452+
if post.len() > 0 {
453453
cx.bug(~"elts_to_ell provided an invalid result");
454454
}
455455
p_t_s_r_length(cx, vec::len(pre), false, s, b);

src/libsyntax/parse/comments.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ enum cmnt_style {
1717
blank_line, // Just a manual blank line "\n\n", for layout
1818
}
1919

20+
impl cmnt_style : cmp::Eq {
21+
pure fn eq(&&other: cmnt_style) -> bool {
22+
(self as uint) == (other as uint)
23+
}
24+
pure fn ne(&&other: cmnt_style) -> bool {
25+
(self as uint) != (other as uint)
26+
}
27+
}
28+
2029
type cmnt = {style: cmnt_style, lines: ~[~str], pos: uint};
2130

2231
fn is_doc_comment(s: ~str) -> bool {

src/libsyntax/print/pprust.rs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,10 +1616,13 @@ fn print_fn_args_and_ret(s: ps, decl: ast::fn_decl,
16161616
pclose(s);
16171617

16181618
maybe_print_comment(s, decl.output.span.lo);
1619-
if decl.output.node != ast::ty_nil {
1620-
space_if_not_bol(s);
1621-
word_space(s, ~"->");
1622-
print_type(s, decl.output);
1619+
match decl.output.node {
1620+
ast::ty_nil => {}
1621+
_ => {
1622+
space_if_not_bol(s);
1623+
word_space(s, ~"->");
1624+
print_type(s, decl.output);
1625+
}
16231626
}
16241627
}
16251628

@@ -1628,11 +1631,16 @@ fn print_fn_block_args(s: ps, decl: ast::fn_decl,
16281631
word(s.s, ~"|");
16291632
print_fn_args(s, decl, cap_items, None);
16301633
word(s.s, ~"|");
1631-
if decl.output.node != ast::ty_infer {
1632-
space_if_not_bol(s);
1633-
word_space(s, ~"->");
1634-
print_type(s, decl.output);
1634+
1635+
match decl.output.node {
1636+
ast::ty_infer => {}
1637+
_ => {
1638+
space_if_not_bol(s);
1639+
word_space(s, ~"->");
1640+
print_type(s, decl.output);
1641+
}
16351642
}
1643+
16361644
maybe_print_comment(s, decl.output.span.lo);
16371645
}
16381646
@@ -1829,14 +1837,19 @@ fn print_ty_fn(s: ps, opt_proto: Option<ast::proto>, purity: ast::purity,
18291837
pclose(s);
18301838
18311839
maybe_print_comment(s, decl.output.span.lo);
1832-
if decl.output.node != ast::ty_nil {
1833-
space_if_not_bol(s);
1834-
ibox(s, indent_unit);
1835-
word_space(s, ~"->");
1836-
if decl.cf == ast::noreturn { word_nbsp(s, ~"!"); }
1837-
else { print_type(s, decl.output); }
1838-
end(s);
1840+
1841+
match decl.output.node {
1842+
ast::ty_nil => {}
1843+
_ => {
1844+
space_if_not_bol(s);
1845+
ibox(s, indent_unit);
1846+
word_space(s, ~"->");
1847+
if decl.cf == ast::noreturn { word_nbsp(s, ~"!"); }
1848+
else { print_type(s, decl.output); }
1849+
end(s);
1850+
}
18391851
}
1852+
18401853
end(s);
18411854
}
18421855

src/rustc/middle/borrowck/check_loans.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ enum assignment_type {
7979
at_mutbl_ref,
8080
}
8181

82+
impl assignment_type : cmp::Eq {
83+
pure fn eq(&&other: assignment_type) -> bool {
84+
(self as uint) == (other as uint)
85+
}
86+
pure fn ne(&&other: assignment_type) -> bool { !self.eq(other) }
87+
}
88+
8289
impl assignment_type {
8390
fn checked_by_liveness() -> bool {
8491
// the liveness pass guarantees that immutable local variables

0 commit comments

Comments
 (0)