Repair various cases where values of distinct types were being operated

upon (e.g., `&int` added to `int`).
This commit is contained in:
Niko Matsakis 2014-10-31 05:40:15 -04:00
parent 98958bcaf4
commit 4af52eee59
10 changed files with 56 additions and 15 deletions

View File

@ -132,7 +132,7 @@ impl Drop for Arena {
#[inline]
fn round_up(base: uint, align: uint) -> uint {
(base.checked_add(&(align - 1))).unwrap() & !(&(align - 1))
(base.checked_add(&(align - 1))).unwrap() & !(align - 1)
}
// Walk down a chunk, running the destructors for any objects stored

View File

@ -919,7 +919,7 @@ impl<T> Vec<T> {
///
/// ```
/// let mut vec = vec![1i, 2, 3, 4];
/// vec.retain(|x| x%2 == 0);
/// vec.retain(|&x| x%2 == 0);
/// assert_eq!(vec, vec![2, 4]);
/// ```
#[unstable = "the closure argument may become an unboxed closure"]

View File

@ -787,8 +787,8 @@ impl<A, V: FromIterator<A>> FromIterator<Option<A>> for Option<V> {
/// use std::uint;
///
/// let v = vec!(1u, 2u);
/// let res: Option<Vec<uint>> = v.iter().map(|x: &uint|
/// if *x == uint::MAX { None }
/// let res: Option<Vec<uint>> = v.iter().map(|&x: &uint|
/// if x == uint::MAX { None }
/// else { Some(x + 1) }
/// ).collect();
/// assert!(res == Some(vec!(2u, 3u)));

View File

@ -894,8 +894,8 @@ impl<A, E, V: FromIterator<A>> FromIterator<Result<A, E>> for Result<V, E> {
/// use std::uint;
///
/// let v = vec!(1u, 2u);
/// let res: Result<Vec<uint>, &'static str> = v.iter().map(|x: &uint|
/// if *x == uint::MAX { Err("Overflow!") }
/// let res: Result<Vec<uint>, &'static str> = v.iter().map(|&x: &uint|
/// if x == uint::MAX { Err("Overflow!") }
/// else { Ok(x + 1) }
/// ).collect();
/// assert!(res == Ok(vec!(2u, 3u)));

View File

@ -356,7 +356,7 @@ fn test_iterator_size_hint() {
assert_eq!(vi.zip(v2.iter()).size_hint(), (3, Some(3)));
assert_eq!(vi.scan(0i, |_,_| Some(0i)).size_hint(), (0, Some(10)));
assert_eq!(vi.filter(|_| false).size_hint(), (0, Some(10)));
assert_eq!(vi.map(|i| i+1).size_hint(), (10, Some(10)));
assert_eq!(vi.map(|&i| i+1).size_hint(), (10, Some(10)));
assert_eq!(vi.filter_map(|_| Some(0i)).size_hint(), (0, Some(10)));
}
@ -388,9 +388,9 @@ fn test_any() {
#[test]
fn test_find() {
let v: &[int] = &[1i, 3, 9, 27, 103, 14, 11];
assert_eq!(*v.iter().find(|x| *x & 1 == 0).unwrap(), 14);
assert_eq!(*v.iter().find(|x| *x % 3 == 0).unwrap(), 3);
assert!(v.iter().find(|x| *x % 12 == 0).is_none());
assert_eq!(*v.iter().find(|&&x| x & 1 == 0).unwrap(), 14);
assert_eq!(*v.iter().find(|&&x| x % 3 == 0).unwrap(), 3);
assert!(v.iter().find(|&&x| x % 12 == 0).is_none());
}
#[test]

View File

@ -177,13 +177,13 @@ macro_rules! bitflags {
/// Returns `true` if there are flags common to both `self` and `other`.
#[inline]
pub fn intersects(&self, other: $BitFlags) -> bool {
!(self & other).is_empty()
!(*self & other).is_empty()
}
/// Returns `true` all of the flags in `other` are contained within `self`.
#[inline]
pub fn contains(&self, other: $BitFlags) -> bool {
(self & other) == other
(*self & other) == other
}
/// Inserts the specified flags in-place.

View File

@ -319,7 +319,7 @@ impl fmt::Show for Duration {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// technically speaking, negative duration is not valid ISO 8601,
// but we need to print it anyway.
let (abs, sign) = if self.secs < 0 { (-self, "-") } else { (*self, "") };
let (abs, sign) = if self.secs < 0 { (-*self, "-") } else { (*self, "") };
let days = abs.secs / SECS_PER_DAY;
let secs = abs.secs - days * SECS_PER_DAY;

View File

@ -96,7 +96,7 @@ impl Add<Duration, Timespec> for Timespec {
let d_sec = other.num_seconds();
// It is safe to unwrap the nanoseconds, because there cannot be
// more than one second left, which fits in i64 and in i32.
let d_nsec = (other - Duration::seconds(d_sec))
let d_nsec = (*other - Duration::seconds(d_sec))
.num_nanoseconds().unwrap() as i32;
let mut sec = self.sec + d_sec;
let mut nsec = self.nsec + d_nsec;

View File

@ -290,7 +290,7 @@ fn search(
let masks_at = &masks[i];
// for every unused piece
for id in range(0u, 10).filter(|id| board & (1 << (id + 50)) == 0) {
for id in range(0u, 10).filter(|&id| board & (1 << (id + 50)) == 0) {
// for each mask that fits on the board
for m in masks_at[id].iter().filter(|&m| board & *m == 0) {
// This check is too costly.

View File

@ -0,0 +1,41 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Test that we can overload the `+` operator for points so that two
// points can be added, and a point can be added to an integer.
use std::ops;
#[deriving(Show,PartialEq,Eq)]
struct Point {
x: int,
y: int
}
impl ops::Add<Point,Point> for Point {
fn add(&self, other: &Point) -> Point {
Point {x: self.x + (*other).x, y: self.y + (*other).y}
}
}
impl ops::Add<int,Point> for Point {
fn add(&self, &other: &int) -> Point {
Point {x: self.x + other,
y: self.y + other}
}
}
pub fn main() {
let mut p = Point {x: 10, y: 20};
p = p + Point {x: 101, y: 102};
assert_eq!(p, Point {x: 111, y: 122});
p = p + 1;
assert_eq!(p, Point {x: 112, y: 123});
}