Skip to content

Commit

Permalink
librustc: Ensure that type parameters are in the right positions in p…
Browse files Browse the repository at this point in the history
…aths.

This removes the stacking of type parameters that occurs when invoking
trait methods, and fixes all places in the standard library that were
relying on it. It is somewhat awkward in places; I think we'll probably
want something like the `Foo::<for T>::new()` syntax.
  • Loading branch information
pcwalton committed Aug 28, 2013
1 parent 3b6314c commit 8693943
Show file tree
Hide file tree
Showing 70 changed files with 1,128 additions and 526 deletions.
18 changes: 9 additions & 9 deletions src/libextra/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ mod tests {

#[test]
fn test_basic() {
let mut m = DList::new::<~int>();
let mut m: DList<~int> = DList::new();
assert_eq!(m.pop_front(), None);
assert_eq!(m.pop_back(), None);
assert_eq!(m.pop_front(), None);
Expand Down Expand Up @@ -768,7 +768,7 @@ mod tests {

#[test]
fn test_rotate() {
let mut n = DList::new::<int>();
let mut n: DList<int> = DList::new();
n.rotate_backward(); check_links(&n);
assert_eq!(n.len(), 0);
n.rotate_forward(); check_links(&n);
Expand Down Expand Up @@ -1033,7 +1033,7 @@ mod tests {

#[cfg(test)]
fn fuzz_test(sz: int) {
let mut m = DList::new::<int>();
let mut m: DList<int> = DList::new();
let mut v = ~[];
for i in range(0, sz) {
check_links(&m);
Expand Down Expand Up @@ -1078,23 +1078,23 @@ mod tests {

#[bench]
fn bench_push_front(b: &mut test::BenchHarness) {
let mut m = DList::new::<int>();
let mut m: DList<int> = DList::new();
do b.iter {
m.push_front(0);
}
}

#[bench]
fn bench_push_back(b: &mut test::BenchHarness) {
let mut m = DList::new::<int>();
let mut m: DList<int> = DList::new();
do b.iter {
m.push_back(0);
}
}

#[bench]
fn bench_push_back_pop_back(b: &mut test::BenchHarness) {
let mut m = DList::new::<int>();
let mut m: DList<int> = DList::new();
do b.iter {
m.push_back(0);
m.pop_back();
Expand All @@ -1103,7 +1103,7 @@ mod tests {

#[bench]
fn bench_push_front_pop_front(b: &mut test::BenchHarness) {
let mut m = DList::new::<int>();
let mut m: DList<int> = DList::new();
do b.iter {
m.push_front(0);
m.pop_front();
Expand All @@ -1112,7 +1112,7 @@ mod tests {

#[bench]
fn bench_rotate_forward(b: &mut test::BenchHarness) {
let mut m = DList::new::<int>();
let mut m: DList<int> = DList::new();
m.push_front(0);
m.push_front(1);
do b.iter {
Expand All @@ -1122,7 +1122,7 @@ mod tests {

#[bench]
fn bench_rotate_backward(b: &mut test::BenchHarness) {
let mut m = DList::new::<int>();
let mut m: DList<int> = DList::new();
m.push_front(0);
m.push_front(1);
do b.iter {
Expand Down
61 changes: 42 additions & 19 deletions src/libextra/num/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ impl Integer for BigUint {

fn div_mod_floor_inner(a: BigUint, b: BigUint) -> (BigUint, BigUint) {
let mut m = a;
let mut d = Zero::zero::<BigUint>();
let mut d: BigUint = Zero::zero();
let mut n = 1;
while m >= b {
let (d0, d_unit, b_unit) = div_estimate(&m, &b, n);
Expand Down Expand Up @@ -411,8 +411,9 @@ impl Integer for BigUint {
if shift == 0 {
return (BigUint::new(d), One::one(), (*b).clone());
}
let one: BigUint = One::one();
return (BigUint::from_slice(d).shl_unit(shift),
One::one::<BigUint>().shl_unit(shift),
one.shl_unit(shift),
b.shl_unit(shift));
}
}
Expand Down Expand Up @@ -1445,11 +1446,18 @@ mod biguint_tests {

#[test]
fn test_is_even() {
assert!(FromStr::from_str::<BigUint>("1").unwrap().is_odd());
assert!(FromStr::from_str::<BigUint>("2").unwrap().is_even());
assert!(FromStr::from_str::<BigUint>("1000").unwrap().is_even());
assert!(FromStr::from_str::<BigUint>("1000000000000000000000").unwrap().is_even());
assert!(FromStr::from_str::<BigUint>("1000000000000000000001").unwrap().is_odd());
let one: Option<BigUint> = FromStr::from_str("1");
let two: Option<BigUint> = FromStr::from_str("2");
let thousand: Option<BigUint> = FromStr::from_str("1000");
let big: Option<BigUint> =
FromStr::from_str("1000000000000000000000");
let bigger: Option<BigUint> =
FromStr::from_str("1000000000000000000001");
assert!(one.unwrap().is_odd());
assert!(two.unwrap().is_even());
assert!(thousand.unwrap().is_even());
assert!(big.unwrap().is_even());
assert!(bigger.unwrap().is_odd());
assert!((BigUint::from_uint(1) << 64).is_even());
assert!(((BigUint::from_uint(1) << 64) + BigUint::from_uint(1)).is_odd());
}
Expand Down Expand Up @@ -1534,15 +1542,19 @@ mod biguint_tests {
}
}

assert_eq!(FromStrRadix::from_str_radix::<BigUint>("Z", 10), None);
assert_eq!(FromStrRadix::from_str_radix::<BigUint>("_", 2), None);
assert_eq!(FromStrRadix::from_str_radix::<BigUint>("-1", 10), None);
let zed: Option<BigUint> = FromStrRadix::from_str_radix("Z", 10);
assert_eq!(zed, None);
let blank: Option<BigUint> = FromStrRadix::from_str_radix("_", 2);
assert_eq!(blank, None);
let minus_one: Option<BigUint> = FromStrRadix::from_str_radix("-1",
10);
assert_eq!(minus_one, None);
}

#[test]
fn test_factor() {
fn factor(n: uint) -> BigUint {
let mut f= One::one::<BigUint>();
let mut f: BigUint = One::one();
for i in range(2, n + 1) {
// FIXME(#6102): Assignment operator for BigInt causes ICE
// f *= BigUint::from_uint(i);
Expand Down Expand Up @@ -1939,17 +1951,24 @@ mod bigint_tests {

#[test]
fn test_abs_sub() {
assert_eq!((-One::one::<BigInt>()).abs_sub(&One::one()), Zero::zero());
assert_eq!(One::one::<BigInt>().abs_sub(&One::one()), Zero::zero());
assert_eq!(One::one::<BigInt>().abs_sub(&Zero::zero()), One::one());
assert_eq!(One::one::<BigInt>().abs_sub(&-One::one::<BigInt>()),
IntConvertible::from_int(2));
let zero: BigInt = Zero::zero();
let one: BigInt = One::one();
assert_eq!((-one).abs_sub(&one), zero);
let one: BigInt = One::one();
let zero: BigInt = Zero::zero();
assert_eq!(one.abs_sub(&one), zero);
let one: BigInt = One::one();
let zero: BigInt = Zero::zero();
assert_eq!(one.abs_sub(&zero), one);
let one: BigInt = One::one();
assert_eq!(one.abs_sub(&-one), IntConvertible::from_int(2));
}

#[test]
fn test_to_str_radix() {
fn check(n: int, ans: &str) {
assert!(ans == IntConvertible::from_int::<BigInt>(n).to_str_radix(10));
let n: BigInt = IntConvertible::from_int(n);
assert!(ans == n.to_str_radix(10));
}
check(10, "10");
check(1, "1");
Expand All @@ -1962,7 +1981,10 @@ mod bigint_tests {
#[test]
fn test_from_str_radix() {
fn check(s: &str, ans: Option<int>) {
let ans = ans.map_move(|n| IntConvertible::from_int::<BigInt>(n));
let ans = ans.map_move(|n| {
let x: BigInt = IntConvertible::from_int(n);
x
});
assert_eq!(FromStrRadix::from_str_radix(s, 10), ans);
}
check("10", Some(10));
Expand All @@ -1980,7 +2002,8 @@ mod bigint_tests {
BigInt::new(Minus, ~[1, 1, 1]));
assert!(-BigInt::new(Minus, ~[1, 1, 1]) ==
BigInt::new(Plus, ~[1, 1, 1]));
assert_eq!(-Zero::zero::<BigInt>(), Zero::zero::<BigInt>());
let zero: BigInt = Zero::zero();
assert_eq!(-zero, zero);
}
}

Expand Down
29 changes: 20 additions & 9 deletions src/libextra/num/rational.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,13 @@ impl<T: FromStr + Clone + Integer + Ord>
/// Parses `numer/denom`.
fn from_str(s: &str) -> Option<Ratio<T>> {
let split: ~[&str] = s.splitn_iter('/', 1).collect();
if split.len() < 2 { return None; }
do FromStr::from_str::<T>(split[0]).chain |a| {
do FromStr::from_str::<T>(split[1]).chain |b| {
if split.len() < 2 {
return None
}
let a_option: Option<T> = FromStr::from_str(split[0]);
do a_option.chain |a| {
let b_option: Option<T> = FromStr::from_str(split[1]);
do b_option.chain |b| {
Some(Ratio::new(a.clone(), b.clone()))
}
}
Expand All @@ -282,10 +286,15 @@ impl<T: FromStrRadix + Clone + Integer + Ord>
/// Parses `numer/denom` where the numbers are in base `radix`.
fn from_str_radix(s: &str, radix: uint) -> Option<Ratio<T>> {
let split: ~[&str] = s.splitn_iter('/', 1).collect();
if split.len() < 2 { None }
else {
do FromStrRadix::from_str_radix::<T>(split[0], radix).chain |a| {
do FromStrRadix::from_str_radix::<T>(split[1], radix).chain |b| {
if split.len() < 2 {
None
} else {
let a_option: Option<T> = FromStrRadix::from_str_radix(split[0],
radix);
do a_option.chain |a| {
let b_option: Option<T> =
FromStrRadix::from_str_radix(split[1], radix);
do b_option.chain |b| {
Some(Ratio::new(a.clone(), b.clone()))
}
}
Expand Down Expand Up @@ -496,7 +505,8 @@ mod test {
#[test]
fn test_from_str_fail() {
fn test(s: &str) {
assert_eq!(FromStr::from_str::<Rational>(s), None);
let rational: Option<Rational> = FromStr::from_str(s);
assert_eq!(rational, None);
}

let xs = ["0 /1", "abc", "", "1/", "--1/2","3/2/1"];
Expand Down Expand Up @@ -536,7 +546,8 @@ mod test {
#[test]
fn test_from_str_radix_fail() {
fn test(s: &str) {
assert_eq!(FromStrRadix::from_str_radix::<Rational>(s, 3), None);
let radix: Option<Rational> = FromStrRadix::from_str_radix(s, 3);
assert_eq!(radix, None);
}

let xs = ["0 /1", "abc", "", "1/", "--1/2","3/2/1", "3/2"];
Expand Down
19 changes: 14 additions & 5 deletions src/libextra/priority_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,27 +338,36 @@ mod tests {

#[test]
#[should_fail]
fn test_empty_pop() { let mut heap = PriorityQueue::new::<int>(); heap.pop(); }
fn test_empty_pop() {
let mut heap: PriorityQueue<int> = PriorityQueue::new();
heap.pop();
}

#[test]
fn test_empty_maybe_pop() {
let mut heap = PriorityQueue::new::<int>();
let mut heap: PriorityQueue<int> = PriorityQueue::new();
assert!(heap.maybe_pop().is_none());
}

#[test]
#[should_fail]
fn test_empty_top() { let empty = PriorityQueue::new::<int>(); empty.top(); }
fn test_empty_top() {
let empty: PriorityQueue<int> = PriorityQueue::new();
empty.top();
}

#[test]
fn test_empty_maybe_top() {
let empty = PriorityQueue::new::<int>();
let empty: PriorityQueue<int> = PriorityQueue::new();
assert!(empty.maybe_top().is_none());
}

#[test]
#[should_fail]
fn test_empty_replace() { let mut heap = PriorityQueue::new(); heap.replace(5); }
fn test_empty_replace() {
let mut heap: PriorityQueue<int> = PriorityQueue::new();
heap.replace(5);
}

#[test]
fn test_from_iter() {
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/ringbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ mod tests {
#[bench]
fn bench_new(b: &mut test::BenchHarness) {
do b.iter {
let _ = RingBuf::new::<u64>();
let _: RingBuf<u64> = RingBuf::new();
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/libextra/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,8 @@ mod test_treemap {

#[test]
fn find_empty() {
let m = TreeMap::new::<int, int>(); assert!(m.find(&5) == None);
let m: TreeMap<int,int> = TreeMap::new();
assert!(m.find(&5) == None);
}

#[test]
Expand Down Expand Up @@ -1006,7 +1007,7 @@ mod test_treemap {

#[test]
fn test_rand_int() {
let mut map = TreeMap::new::<int, int>();
let mut map: TreeMap<int,int> = TreeMap::new();
let mut ctrl = ~[];

check_equal(ctrl, &map);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/front/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ fn path_node(ids: ~[ast::ident]) -> ast::Path {
ast::Path {
span: dummy_sp(),
global: false,
segments: ids.consume_iter().transform(|identifier| ast::PathSegment {
segments: ids.move_iter().map(|identifier| ast::PathSegment {
identifier: identifier,
lifetime: None,
types: opt_vec::Empty,
Expand All @@ -399,7 +399,7 @@ fn path_node_global(ids: ~[ast::ident]) -> ast::Path {
ast::Path {
span: dummy_sp(),
global: true,
segments: ids.consume_iter().transform(|identifier| ast::PathSegment {
segments: ids.move_iter().map(|identifier| ast::PathSegment {
identifier: identifier,
lifetime: None,
types: opt_vec::Empty,
Expand Down
20 changes: 12 additions & 8 deletions src/librustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,15 +335,19 @@ fn item_to_def_like(item: ebml::Doc, did: ast::def_id, cnum: ast::CrateNum)
let purity = if fam == UnsafeStaticMethod { ast::unsafe_fn } else
{ ast::impure_fn };
// def_static_method carries an optional field of its enclosing
// *trait*, but not an inclosing Impl (if this is an inherent
// static method). So we need to detect whether this is in
// a trait or not, which we do through the mildly hacky
// way of checking whether there is a trait_method_sort.
let trait_did_opt = if reader::maybe_get_doc(
// trait or enclosing impl (if this is an inherent static method).
// So we need to detect whether this is in a trait or not, which
// we do through the mildly hacky way of checking whether there is
// a trait_method_sort.
let provenance = if reader::maybe_get_doc(
item, tag_item_trait_method_sort).is_some() {
Some(item_reqd_and_translated_parent_item(cnum, item))
} else { None };
dl_def(ast::def_static_method(did, trait_did_opt, purity))
ast::FromTrait(item_reqd_and_translated_parent_item(cnum,
item))
} else {
ast::FromImpl(item_reqd_and_translated_parent_item(cnum,
item))
};
dl_def(ast::def_static_method(did, provenance, purity))
}
Type | ForeignType => dl_def(ast::def_ty(did)),
Mod => dl_def(ast::def_mod(did)),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/metadata/tydecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ fn parse_path(st: &mut PState) -> @ast::Path {
return @ast::Path {
span: dummy_sp(),
global: false,
segments: idents.consume_iter().transform(|identifier| {
segments: idents.move_iter().map(|identifier| {
ast::PathSegment {
identifier: identifier,
lifetime: None,
Expand Down
Loading

0 comments on commit 8693943

Please sign in to comment.