Auto merge of #33494 - Manishearth:rollup, r=Manishearth

Rollup of 8 pull requests

- Successful merges: #33369, #33373, #33404, #33406, #33420, #33424, #33426, #33473
- Failed merges:
This commit is contained in:
bors 2016-05-08 07:04:17 -07:00
commit cae42a471c
19 changed files with 618 additions and 188 deletions

View File

@ -77,6 +77,7 @@ $(PKG_TAR): $(PKG_FILES)
$(Q)mkdir -p tmp/dist/$(PKG_NAME)
$(Q)tar \
-C $(S) \
-f - \
--exclude-vcs \
--exclude=*~ \
--exclude=*.pyc \
@ -86,7 +87,7 @@ $(PKG_TAR): $(PKG_FILES)
--exclude=*/llvm/test/*/*/*.ll \
--exclude=*/llvm/test/*/*/*.td \
--exclude=*/llvm/test/*/*/*.s \
-c $(UNROOTED_PKG_FILES) | tar -x -C tmp/dist/$(PKG_NAME)
-c $(UNROOTED_PKG_FILES) | tar -x -f - -C tmp/dist/$(PKG_NAME)
@$(call E, making $@)
$(Q)tar -czf $(PKG_TAR) -C tmp/dist $(PKG_NAME)
$(Q)rm -Rf tmp/dist/$(PKG_NAME)

View File

@ -49,6 +49,11 @@ use marker::Sized;
/// A common trait for cloning an object.
///
/// This trait can be used with `#[derive]`.
///
/// Types that are `Copy` should have a trivial implementation of `Clone`. More formally:
/// if `T: Copy`, `x: T`, and `y: &T`, then `let x = y.clone();` is equivalent to `let x = *y;`.
/// Manual implementations should be careful to uphold this invariant; however, unsafe code
/// must not rely on it to ensure memory safety.
#[stable(feature = "rust1", since = "1.0.0")]
pub trait Clone : Sized {
/// Returns a copy of the value.

View File

@ -20,18 +20,19 @@
//! - Impl the `As*` traits for reference-to-reference conversions
//! - Impl the `Into` trait when you want to consume the value in the conversion
//! - The `From` trait is the most flexible, useful for value _and_ reference conversions
//! - The `TryFrom` and `TryInto` traits behave like `From` and `Into`, but allow for the
//! conversion to fail
//!
//! As a library author, you should prefer implementing `From<T>` rather than
//! `Into<U>`, as `From` provides greater flexibility and offers an equivalent `Into`
//! implementation for free, thanks to a blanket implementation in the standard library.
//!
//! **Note: these traits must not fail**. If the conversion can fail, you must use a dedicated
//! method which returns an `Option<T>` or a `Result<T, E>`.
//! As a library author, you should prefer implementing `From<T>` or `TryFrom<T>` rather than
//! `Into<U>` or `TryInto<U>`, as `From` and `TryFrom` provide greater flexibility and offer
//! equivalent `Into` or `TryInto` implementations for free, thanks to a blanket implementation
//! in the standard library.
//!
//! # Generic impl
//!
//! - `AsRef` and `AsMut` auto-dereference if the inner type is a reference
//! - `From<U> for T` implies `Into<T> for U`
//! - `TryFrom<U> for T` implies `TryInto<T> for U`
//! - `From` and `Into` are reflexive, which means that all types can `into()`
//! themselves and `from()` themselves
//!
@ -40,6 +41,7 @@
#![stable(feature = "rust1", since = "1.0.0")]
use marker::Sized;
use result::Result;
/// A cheap, reference-to-reference conversion.
///
@ -98,8 +100,8 @@ pub trait AsMut<T: ?Sized> {
/// A conversion that consumes `self`, which may or may not be expensive.
///
/// **Note: this trait must not fail**. If the conversion can fail, use a dedicated method which
/// returns an `Option<T>` or a `Result<T, E>`.
/// **Note: this trait must not fail**. If the conversion can fail, use `TryInto` or a dedicated
/// method which returns an `Option<T>` or a `Result<T, E>`.
///
/// Library authors should not directly implement this trait, but should prefer implementing
/// the `From` trait, which offers greater flexibility and provides an equivalent `Into`
@ -133,8 +135,8 @@ pub trait Into<T>: Sized {
/// Construct `Self` via a conversion.
///
/// **Note: this trait must not fail**. If the conversion can fail, use a dedicated method which
/// returns an `Option<T>` or a `Result<T, E>`.
/// **Note: this trait must not fail**. If the conversion can fail, use `TryFrom` or a dedicated
/// method which returns an `Option<T>` or a `Result<T, E>`.
///
/// # Examples
///
@ -158,6 +160,30 @@ pub trait From<T>: Sized {
fn from(T) -> Self;
}
/// An attempted conversion that consumes `self`, which may or may not be expensive.
///
/// Library authors should not directly implement this trait, but should prefer implementing
/// the `TryFrom` trait, which offers greater flexibility and provides an equivalent `TryInto`
/// implementation for free, thanks to a blanket implementation in the standard library.
#[unstable(feature = "try_from", issue = "33417")]
pub trait TryInto<T>: Sized {
/// The type returned in the event of a conversion error.
type Err;
/// Performs the conversion.
fn try_into(self) -> Result<T, Self::Err>;
}
/// Attempt to construct `Self` via a conversion.
#[unstable(feature = "try_from", issue = "33417")]
pub trait TryFrom<T>: Sized {
/// The type returned in the event of a conversion error.
type Err;
/// Performs the conversion.
fn try_from(T) -> Result<Self, Self::Err>;
}
////////////////////////////////////////////////////////////////////////////////
// GENERIC IMPLS
////////////////////////////////////////////////////////////////////////////////
@ -216,6 +242,17 @@ impl<T> From<T> for T {
fn from(t: T) -> T { t }
}
// TryFrom implies TryInto
#[unstable(feature = "try_from", issue = "33417")]
impl<T, U> TryInto<U> for T where U: TryFrom<T> {
type Err = U::Err;
fn try_into(self) -> Result<U, U::Err> {
U::try_from(self)
}
}
////////////////////////////////////////////////////////////////////////////////
// CONCRETE IMPLS
////////////////////////////////////////////////////////////////////////////////

View File

@ -15,7 +15,7 @@
use char::CharExt;
use cmp::PartialOrd;
use convert::From;
use convert::{From, TryFrom};
use fmt;
use intrinsics;
use marker::{Copy, Sized};
@ -2352,9 +2352,101 @@ macro_rules! from_str_radix_int_impl {
}
from_str_radix_int_impl! { isize i8 i16 i32 i64 usize u8 u16 u32 u64 }
/// The error type returned when a checked integral type conversion fails.
#[unstable(feature = "try_from", issue = "33417")]
#[derive(Debug, Copy, Clone)]
pub struct TryFromIntError(());
impl TryFromIntError {
#[unstable(feature = "int_error_internals",
reason = "available through Error trait and this method should \
not be exposed publicly",
issue = "0")]
#[doc(hidden)]
pub fn __description(&self) -> &str {
"out of range integral type conversion attempted"
}
}
#[unstable(feature = "try_from", issue = "33417")]
impl fmt::Display for TryFromIntError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.__description().fmt(fmt)
}
}
macro_rules! same_sign_from_int_impl {
($storage:ty, $target:ty, $($source:ty),*) => {$(
#[stable(feature = "rust1", since = "1.0.0")]
impl TryFrom<$source> for $target {
type Err = TryFromIntError;
fn try_from(u: $source) -> Result<$target, TryFromIntError> {
let min = <$target as FromStrRadixHelper>::min_value() as $storage;
let max = <$target as FromStrRadixHelper>::max_value() as $storage;
if u as $storage < min || u as $storage > max {
Err(TryFromIntError(()))
} else {
Ok(u as $target)
}
}
}
)*}
}
same_sign_from_int_impl!(u64, u8, u8, u16, u32, u64, usize);
same_sign_from_int_impl!(i64, i8, i8, i16, i32, i64, isize);
same_sign_from_int_impl!(u64, u16, u8, u16, u32, u64, usize);
same_sign_from_int_impl!(i64, i16, i8, i16, i32, i64, isize);
same_sign_from_int_impl!(u64, u32, u8, u16, u32, u64, usize);
same_sign_from_int_impl!(i64, i32, i8, i16, i32, i64, isize);
same_sign_from_int_impl!(u64, u64, u8, u16, u32, u64, usize);
same_sign_from_int_impl!(i64, i64, i8, i16, i32, i64, isize);
same_sign_from_int_impl!(u64, usize, u8, u16, u32, u64, usize);
same_sign_from_int_impl!(i64, isize, i8, i16, i32, i64, isize);
macro_rules! cross_sign_from_int_impl {
($unsigned:ty, $($signed:ty),*) => {$(
#[stable(feature = "rust1", since = "1.0.0")]
impl TryFrom<$unsigned> for $signed {
type Err = TryFromIntError;
fn try_from(u: $unsigned) -> Result<$signed, TryFromIntError> {
let max = <$signed as FromStrRadixHelper>::max_value() as u64;
if u as u64 > max {
Err(TryFromIntError(()))
} else {
Ok(u as $signed)
}
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl TryFrom<$signed> for $unsigned {
type Err = TryFromIntError;
fn try_from(u: $signed) -> Result<$unsigned, TryFromIntError> {
let max = <$unsigned as FromStrRadixHelper>::max_value() as u64;
if u < 0 || u as u64 > max {
Err(TryFromIntError(()))
} else {
Ok(u as $unsigned)
}
}
}
)*}
}
cross_sign_from_int_impl!(u8, i8, i16, i32, i64, isize);
cross_sign_from_int_impl!(u16, i8, i16, i32, i64, isize);
cross_sign_from_int_impl!(u32, i8, i16, i32, i64, isize);
cross_sign_from_int_impl!(u64, i8, i16, i32, i64, isize);
cross_sign_from_int_impl!(usize, i8, i16, i32, i64, isize);
#[doc(hidden)]
trait FromStrRadixHelper: PartialOrd + Copy {
fn min_value() -> Self;
fn max_value() -> Self;
fn from_u32(u: u32) -> Self;
fn checked_mul(&self, other: u32) -> Option<Self>;
fn checked_sub(&self, other: u32) -> Option<Self>;
@ -2364,6 +2456,7 @@ trait FromStrRadixHelper: PartialOrd + Copy {
macro_rules! doit {
($($t:ty)*) => ($(impl FromStrRadixHelper for $t {
fn min_value() -> Self { Self::min_value() }
fn max_value() -> Self { Self::max_value() }
fn from_u32(u: u32) -> Self { u as Self }
fn checked_mul(&self, other: u32) -> Option<Self> {
Self::checked_mul(*self, other as Self)

View File

@ -33,6 +33,7 @@
#![feature(unboxed_closures)]
#![feature(unicode)]
#![feature(unique)]
#![feature(try_from)]
extern crate core;
extern crate test;

View File

@ -8,10 +8,13 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use core::convert::TryFrom;
use core::cmp::PartialEq;
use core::fmt::Debug;
use core::ops::{Add, Sub, Mul, Div, Rem};
use core::marker::Copy;
use core::ops::{Add, Sub, Mul, Div, Rem};
use core::option::Option;
use core::option::Option::{Some, None};
#[macro_use]
mod int_macros;
@ -48,168 +51,318 @@ pub fn test_num<T>(ten: T, two: T) where
assert_eq!(ten.rem(two), ten % two);
}
#[cfg(test)]
mod tests {
use core::option::Option;
use core::option::Option::{Some, None};
#[test]
fn from_str_issue7588() {
let u : Option<u8> = u8::from_str_radix("1000", 10).ok();
assert_eq!(u, None);
let s : Option<i16> = i16::from_str_radix("80000", 10).ok();
assert_eq!(s, None);
}
#[test]
fn from_str_issue7588() {
let u : Option<u8> = u8::from_str_radix("1000", 10).ok();
assert_eq!(u, None);
let s : Option<i16> = i16::from_str_radix("80000", 10).ok();
assert_eq!(s, None);
}
#[test]
fn test_int_from_str_overflow() {
let mut i8_val: i8 = 127;
assert_eq!("127".parse::<i8>().ok(), Some(i8_val));
assert_eq!("128".parse::<i8>().ok(), None);
#[test]
fn test_int_from_str_overflow() {
let mut i8_val: i8 = 127;
assert_eq!("127".parse::<i8>().ok(), Some(i8_val));
assert_eq!("128".parse::<i8>().ok(), None);
i8_val = i8_val.wrapping_add(1);
assert_eq!("-128".parse::<i8>().ok(), Some(i8_val));
assert_eq!("-129".parse::<i8>().ok(), None);
i8_val = i8_val.wrapping_add(1);
assert_eq!("-128".parse::<i8>().ok(), Some(i8_val));
assert_eq!("-129".parse::<i8>().ok(), None);
let mut i16_val: i16 = 32_767;
assert_eq!("32767".parse::<i16>().ok(), Some(i16_val));
assert_eq!("32768".parse::<i16>().ok(), None);
let mut i16_val: i16 = 32_767;
assert_eq!("32767".parse::<i16>().ok(), Some(i16_val));
assert_eq!("32768".parse::<i16>().ok(), None);
i16_val = i16_val.wrapping_add(1);
assert_eq!("-32768".parse::<i16>().ok(), Some(i16_val));
assert_eq!("-32769".parse::<i16>().ok(), None);
i16_val = i16_val.wrapping_add(1);
assert_eq!("-32768".parse::<i16>().ok(), Some(i16_val));
assert_eq!("-32769".parse::<i16>().ok(), None);
let mut i32_val: i32 = 2_147_483_647;
assert_eq!("2147483647".parse::<i32>().ok(), Some(i32_val));
assert_eq!("2147483648".parse::<i32>().ok(), None);
let mut i32_val: i32 = 2_147_483_647;
assert_eq!("2147483647".parse::<i32>().ok(), Some(i32_val));
assert_eq!("2147483648".parse::<i32>().ok(), None);
i32_val = i32_val.wrapping_add(1);
assert_eq!("-2147483648".parse::<i32>().ok(), Some(i32_val));
assert_eq!("-2147483649".parse::<i32>().ok(), None);
i32_val = i32_val.wrapping_add(1);
assert_eq!("-2147483648".parse::<i32>().ok(), Some(i32_val));
assert_eq!("-2147483649".parse::<i32>().ok(), None);
let mut i64_val: i64 = 9_223_372_036_854_775_807;
assert_eq!("9223372036854775807".parse::<i64>().ok(), Some(i64_val));
assert_eq!("9223372036854775808".parse::<i64>().ok(), None);
let mut i64_val: i64 = 9_223_372_036_854_775_807;
assert_eq!("9223372036854775807".parse::<i64>().ok(), Some(i64_val));
assert_eq!("9223372036854775808".parse::<i64>().ok(), None);
i64_val = i64_val.wrapping_add(1);
assert_eq!("-9223372036854775808".parse::<i64>().ok(), Some(i64_val));
assert_eq!("-9223372036854775809".parse::<i64>().ok(), None);
}
i64_val = i64_val.wrapping_add(1);
assert_eq!("-9223372036854775808".parse::<i64>().ok(), Some(i64_val));
assert_eq!("-9223372036854775809".parse::<i64>().ok(), None);
}
#[test]
fn test_leading_plus() {
assert_eq!("+127".parse::<u8>().ok(), Some(127));
assert_eq!("+9223372036854775807".parse::<i64>().ok(), Some(9223372036854775807));
}
#[test]
fn test_leading_plus() {
assert_eq!("+127".parse::<u8>().ok(), Some(127));
assert_eq!("+9223372036854775807".parse::<i64>().ok(), Some(9223372036854775807));
}
#[test]
fn test_invalid() {
assert_eq!("--129".parse::<i8>().ok(), None);
assert_eq!("++129".parse::<i8>().ok(), None);
assert_eq!("Съешь".parse::<u8>().ok(), None);
}
#[test]
fn test_invalid() {
assert_eq!("--129".parse::<i8>().ok(), None);
assert_eq!("++129".parse::<i8>().ok(), None);
assert_eq!("Съешь".parse::<u8>().ok(), None);
}
#[test]
fn test_empty() {
assert_eq!("-".parse::<i8>().ok(), None);
assert_eq!("+".parse::<i8>().ok(), None);
assert_eq!("".parse::<u8>().ok(), None);
}
#[test]
fn test_empty() {
assert_eq!("-".parse::<i8>().ok(), None);
assert_eq!("+".parse::<i8>().ok(), None);
assert_eq!("".parse::<u8>().ok(), None);
}
macro_rules! test_impl_from {
($fn_name: ident, $Small: ty, $Large: ty) => {
#[test]
fn $fn_name() {
let small_max = <$Small>::max_value();
let small_min = <$Small>::min_value();
let large_max: $Large = small_max.into();
let large_min: $Large = small_min.into();
assert_eq!(large_max as $Small, small_max);
assert_eq!(large_min as $Small, small_min);
}
macro_rules! test_impl_from {
($fn_name: ident, $Small: ty, $Large: ty) => {
#[test]
fn $fn_name() {
let small_max = <$Small>::max_value();
let small_min = <$Small>::min_value();
let large_max: $Large = small_max.into();
let large_min: $Large = small_min.into();
assert_eq!(large_max as $Small, small_max);
assert_eq!(large_min as $Small, small_min);
}
}
}
// Unsigned -> Unsigned
test_impl_from! { test_u8u16, u8, u16 }
test_impl_from! { test_u8u32, u8, u32 }
test_impl_from! { test_u8u64, u8, u64 }
test_impl_from! { test_u8usize, u8, usize }
test_impl_from! { test_u16u32, u16, u32 }
test_impl_from! { test_u16u64, u16, u64 }
test_impl_from! { test_u32u64, u32, u64 }
// Unsigned -> Unsigned
test_impl_from! { test_u8u16, u8, u16 }
test_impl_from! { test_u8u32, u8, u32 }
test_impl_from! { test_u8u64, u8, u64 }
test_impl_from! { test_u8usize, u8, usize }
test_impl_from! { test_u16u32, u16, u32 }
test_impl_from! { test_u16u64, u16, u64 }
test_impl_from! { test_u32u64, u32, u64 }
// Signed -> Signed
test_impl_from! { test_i8i16, i8, i16 }
test_impl_from! { test_i8i32, i8, i32 }
test_impl_from! { test_i8i64, i8, i64 }
test_impl_from! { test_i8isize, i8, isize }
test_impl_from! { test_i16i32, i16, i32 }
test_impl_from! { test_i16i64, i16, i64 }
test_impl_from! { test_i32i64, i32, i64 }
// Signed -> Signed
test_impl_from! { test_i8i16, i8, i16 }
test_impl_from! { test_i8i32, i8, i32 }
test_impl_from! { test_i8i64, i8, i64 }
test_impl_from! { test_i8isize, i8, isize }
test_impl_from! { test_i16i32, i16, i32 }
test_impl_from! { test_i16i64, i16, i64 }
test_impl_from! { test_i32i64, i32, i64 }
// Unsigned -> Signed
test_impl_from! { test_u8i16, u8, i16 }
test_impl_from! { test_u8i32, u8, i32 }
test_impl_from! { test_u8i64, u8, i64 }
test_impl_from! { test_u16i32, u16, i32 }
test_impl_from! { test_u16i64, u16, i64 }
test_impl_from! { test_u32i64, u32, i64 }
// Unsigned -> Signed
test_impl_from! { test_u8i16, u8, i16 }
test_impl_from! { test_u8i32, u8, i32 }
test_impl_from! { test_u8i64, u8, i64 }
test_impl_from! { test_u16i32, u16, i32 }
test_impl_from! { test_u16i64, u16, i64 }
test_impl_from! { test_u32i64, u32, i64 }
// Signed -> Float
test_impl_from! { test_i8f32, i8, f32 }
test_impl_from! { test_i8f64, i8, f64 }
test_impl_from! { test_i16f32, i16, f32 }
test_impl_from! { test_i16f64, i16, f64 }
test_impl_from! { test_i32f64, i32, f64 }
// Signed -> Float
test_impl_from! { test_i8f32, i8, f32 }
test_impl_from! { test_i8f64, i8, f64 }
test_impl_from! { test_i16f32, i16, f32 }
test_impl_from! { test_i16f64, i16, f64 }
test_impl_from! { test_i32f64, i32, f64 }
// Unsigned -> Float
test_impl_from! { test_u8f32, u8, f32 }
test_impl_from! { test_u8f64, u8, f64 }
test_impl_from! { test_u16f32, u16, f32 }
test_impl_from! { test_u16f64, u16, f64 }
test_impl_from! { test_u32f64, u32, f64 }
// Unsigned -> Float
test_impl_from! { test_u8f32, u8, f32 }
test_impl_from! { test_u8f64, u8, f64 }
test_impl_from! { test_u16f32, u16, f32 }
test_impl_from! { test_u16f64, u16, f64 }
test_impl_from! { test_u32f64, u32, f64 }
// Float -> Float
#[test]
fn test_f32f64() {
use core::f32;
// Float -> Float
#[test]
fn test_f32f64() {
use core::f32;
let max: f64 = f32::MAX.into();
assert_eq!(max as f32, f32::MAX);
assert!(max.is_normal());
let max: f64 = f32::MAX.into();
assert_eq!(max as f32, f32::MAX);
assert!(max.is_normal());
let min: f64 = f32::MIN.into();
assert_eq!(min as f32, f32::MIN);
assert!(min.is_normal());
let min: f64 = f32::MIN.into();
assert_eq!(min as f32, f32::MIN);
assert!(min.is_normal());
let min_positive: f64 = f32::MIN_POSITIVE.into();
assert_eq!(min_positive as f32, f32::MIN_POSITIVE);
assert!(min_positive.is_normal());
let min_positive: f64 = f32::MIN_POSITIVE.into();
assert_eq!(min_positive as f32, f32::MIN_POSITIVE);
assert!(min_positive.is_normal());
let epsilon: f64 = f32::EPSILON.into();
assert_eq!(epsilon as f32, f32::EPSILON);
assert!(epsilon.is_normal());
let epsilon: f64 = f32::EPSILON.into();
assert_eq!(epsilon as f32, f32::EPSILON);
assert!(epsilon.is_normal());
let zero: f64 = (0.0f32).into();
assert_eq!(zero as f32, 0.0f32);
assert!(zero.is_sign_positive());
let zero: f64 = (0.0f32).into();
assert_eq!(zero as f32, 0.0f32);
assert!(zero.is_sign_positive());
let neg_zero: f64 = (-0.0f32).into();
assert_eq!(neg_zero as f32, -0.0f32);
assert!(neg_zero.is_sign_negative());
let neg_zero: f64 = (-0.0f32).into();
assert_eq!(neg_zero as f32, -0.0f32);
assert!(neg_zero.is_sign_negative());
let infinity: f64 = f32::INFINITY.into();
assert_eq!(infinity as f32, f32::INFINITY);
assert!(infinity.is_infinite());
assert!(infinity.is_sign_positive());
let infinity: f64 = f32::INFINITY.into();
assert_eq!(infinity as f32, f32::INFINITY);
assert!(infinity.is_infinite());
assert!(infinity.is_sign_positive());
let neg_infinity: f64 = f32::NEG_INFINITY.into();
assert_eq!(neg_infinity as f32, f32::NEG_INFINITY);
assert!(neg_infinity.is_infinite());
assert!(neg_infinity.is_sign_negative());
let neg_infinity: f64 = f32::NEG_INFINITY.into();
assert_eq!(neg_infinity as f32, f32::NEG_INFINITY);
assert!(neg_infinity.is_infinite());
assert!(neg_infinity.is_sign_negative());
let nan: f64 = f32::NAN.into();
assert!(nan.is_nan());
let nan: f64 = f32::NAN.into();
assert!(nan.is_nan());
}
macro_rules! test_impl_try_from_always_ok {
($fn_name:ident, $source:ty, $target: ty) => {
#[test]
fn $fn_name() {
let max = <$source>::max_value();
let min = <$source>::min_value();
let zero: $source = 0;
assert_eq!(<$target as TryFrom<$source>>::try_from(max).unwrap(),
max as $target);
assert_eq!(<$target as TryFrom<$source>>::try_from(min).unwrap(),
min as $target);
assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(),
zero as $target);
}
}
}
test_impl_try_from_always_ok! { test_try_u8u8, u8, u8 }
test_impl_try_from_always_ok! { test_try_u8u16, u8, u16 }
test_impl_try_from_always_ok! { test_try_u8u32, u8, u32 }
test_impl_try_from_always_ok! { test_try_u8u64, u8, u64 }
test_impl_try_from_always_ok! { test_try_u8i16, u8, i16 }
test_impl_try_from_always_ok! { test_try_u8i32, u8, i32 }
test_impl_try_from_always_ok! { test_try_u8i64, u8, i64 }
test_impl_try_from_always_ok! { test_try_u16u16, u16, u16 }
test_impl_try_from_always_ok! { test_try_u16u32, u16, u32 }
test_impl_try_from_always_ok! { test_try_u16u64, u16, u64 }
test_impl_try_from_always_ok! { test_try_u16i32, u16, i32 }
test_impl_try_from_always_ok! { test_try_u16i64, u16, i64 }
test_impl_try_from_always_ok! { test_try_u32u32, u32, u32 }
test_impl_try_from_always_ok! { test_try_u32u64, u32, u64 }
test_impl_try_from_always_ok! { test_try_u32i64, u32, i64 }
test_impl_try_from_always_ok! { test_try_u64u64, u64, u64 }
test_impl_try_from_always_ok! { test_try_i8i8, i8, i8 }
test_impl_try_from_always_ok! { test_try_i8i16, i8, i16 }
test_impl_try_from_always_ok! { test_try_i8i32, i8, i32 }
test_impl_try_from_always_ok! { test_try_i8i64, i8, i64 }
test_impl_try_from_always_ok! { test_try_i16i16, i16, i16 }
test_impl_try_from_always_ok! { test_try_i16i32, i16, i32 }
test_impl_try_from_always_ok! { test_try_i16i64, i16, i64 }
test_impl_try_from_always_ok! { test_try_i32i32, i32, i32 }
test_impl_try_from_always_ok! { test_try_i32i64, i32, i64 }
test_impl_try_from_always_ok! { test_try_i64i64, i64, i64 }
macro_rules! test_impl_try_from_signed_to_unsigned_upper_ok {
($fn_name:ident, $source:ty, $target:ty) => {
#[test]
fn $fn_name() {
let max = <$source>::max_value();
let min = <$source>::min_value();
let zero: $source = 0;
let neg_one: $source = -1;
assert_eq!(<$target as TryFrom<$source>>::try_from(max).unwrap(),
max as $target);
assert!(<$target as TryFrom<$source>>::try_from(min).is_err());
assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(),
zero as $target);
assert!(<$target as TryFrom<$source>>::try_from(neg_one).is_err());
}
}
}
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u8, i8, u8 }
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u16, i8, u16 }
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u32, i8, u32 }
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i8u64, i8, u64 }
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16u16, i16, u16 }
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16u32, i16, u32 }
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i16u64, i16, u64 }
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32u32, i32, u32 }
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i32u64, i32, u64 }
test_impl_try_from_signed_to_unsigned_upper_ok! { test_try_i64u64, i64, u64 }
macro_rules! test_impl_try_from_unsigned_to_signed_upper_err {
($fn_name:ident, $source:ty, $target:ty) => {
#[test]
fn $fn_name() {
let max = <$source>::max_value();
let min = <$source>::min_value();
let zero: $source = 0;
assert!(<$target as TryFrom<$source>>::try_from(max).is_err());
assert_eq!(<$target as TryFrom<$source>>::try_from(min).unwrap(),
min as $target);
assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(),
zero as $target);
}
}
}
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u8i8, u8, i8 }
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u16i8, u16, i8 }
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u16i16, u16, i16 }
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32i8, u32, i8 }
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32i16, u32, i16 }
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u32i32, u32, i32 }
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64i8, u64, i8 }
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64i16, u64, i16 }
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64i32, u64, i32 }
test_impl_try_from_unsigned_to_signed_upper_err! { test_try_u64i64, u64, i64 }
macro_rules! test_impl_try_from_same_sign_err {
($fn_name:ident, $source:ty, $target:ty) => {
#[test]
fn $fn_name() {
let max = <$source>::max_value();
let min = <$source>::min_value();
let zero: $source = 0;
let t_max = <$target>::max_value();
let t_min = <$target>::min_value();
assert!(<$target as TryFrom<$source>>::try_from(max).is_err());
if min != 0 {
assert!(<$target as TryFrom<$source>>::try_from(min).is_err());
}
assert_eq!(<$target as TryFrom<$source>>::try_from(zero).unwrap(),
zero as $target);
assert_eq!(<$target as TryFrom<$source>>::try_from(t_max as $source)
.unwrap(),
t_max as $target);
assert_eq!(<$target as TryFrom<$source>>::try_from(t_min as $source)
.unwrap(),
t_min as $target);
}
}
}
test_impl_try_from_same_sign_err! { test_try_u16u8, u16, u8 }
test_impl_try_from_same_sign_err! { test_try_u32u8, u32, u8 }
test_impl_try_from_same_sign_err! { test_try_u32u16, u32, u16 }
test_impl_try_from_same_sign_err! { test_try_u64u8, u64, u8 }
test_impl_try_from_same_sign_err! { test_try_u64u16, u64, u16 }
test_impl_try_from_same_sign_err! { test_try_u64u32, u64, u32 }
test_impl_try_from_same_sign_err! { test_try_i16i8, i16, i8 }
test_impl_try_from_same_sign_err! { test_try_i32i8, i32, i8 }
test_impl_try_from_same_sign_err! { test_try_i32i16, i32, i16 }
test_impl_try_from_same_sign_err! { test_try_i64i8, i64, i8 }
test_impl_try_from_same_sign_err! { test_try_i64i16, i64, i16 }
test_impl_try_from_same_sign_err! { test_try_i64i32, i64, i32 }

View File

@ -623,6 +623,69 @@ let Foo = 12i32; // ok!
The goal here is to avoid a conflict of names.
"##,
E0414: r##"
A variable binding in an irrefutable pattern is shadowing the name of a
constant. Example of erroneous code:
```compile_fail
const FOO: u8 = 7;
let FOO = 5; // error: variable bindings cannot shadow constants
// or
fn bar(FOO: u8) { // error: variable bindings cannot shadow constants
}
// or
for FOO in bar {
}
```
Introducing a new variable in Rust is done through a pattern. Thus you can have
`let` bindings like `let (a, b) = ...`. However, patterns also allow constants
in them, e.g. if you want to match over a constant:
```ignore
const FOO: u8 = 1;
match (x,y) {
(3, 4) => { .. }, // it is (3,4)
(FOO, 1) => { .. }, // it is (1,1)
(foo, 1) => { .. }, // it is (anything, 1)
// call the value in the first slot "foo"
_ => { .. } // it is anything
}
```
Here, the second arm matches the value of `x` against the constant `FOO`,
whereas the third arm will accept any value of `x` and call it `foo`.
This works for `match`, however in cases where an irrefutable pattern is
required, constants can't be used. An irrefutable pattern is one which always
matches, whose purpose is only to bind variable names to values. These are
required by let, for, and function argument patterns.
Refutable patterns in such a situation do not make sense, for example:
```ignore
let Some(x) = foo; // what if foo is None, instead?
let (1, x) = foo; // what if foo.0 is not 1?
let (SOME_CONST, x) = foo; // what if foo.0 is not SOME_CONST?
let SOME_CONST = foo; // what if foo is not SOME_CONST?
```
Thus, an irrefutable variable binding can't contain a constant.
To fix this error, just give the marked variable a different name.
"##,
E0415: r##"
More than one function parameter have the same name. Example of erroneous code:
@ -1086,7 +1149,6 @@ register_diagnostics! {
E0409, // variable is bound with different mode in pattern # than in
// pattern #1
E0410, // variable from pattern is not bound in pattern 1
E0414, // only irrefutable patterns allowed here
E0418, // is not an enum variant, struct or const
E0420, // is not an associated const
E0421, // unresolved associated const

View File

@ -139,7 +139,7 @@ enum ResolutionError<'a> {
/// error E0413: declaration shadows an enum variant or unit-like struct in scope
DeclarationShadowsEnumVariantOrUnitLikeStruct(Name),
/// error E0414: only irrefutable patterns allowed here
OnlyIrrefutablePatternsAllowedHere(Name),
ConstantForIrrefutableBinding(Name),
/// error E0415: identifier is bound more than once in this parameter list
IdentifierBoundMoreThanOnceInParameterList(&'a str),
/// error E0416: identifier is bound more than once in the same pattern
@ -321,11 +321,11 @@ fn resolve_struct_error<'b, 'a: 'b, 'tcx: 'a>(resolver: &'b Resolver<'a, 'tcx>,
or unit-like struct in scope",
name)
}
ResolutionError::OnlyIrrefutablePatternsAllowedHere(name) => {
ResolutionError::ConstantForIrrefutableBinding(name) => {
let mut err = struct_span_err!(resolver.session,
span,
E0414,
"only irrefutable patterns allowed here");
"variable bindings cannot shadow constants");
err.span_note(span,
"there already is a constant in scope sharing the same \
name as this pattern");
@ -2248,7 +2248,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
resolve_error(
self,
pattern.span,
ResolutionError::OnlyIrrefutablePatternsAllowedHere(name)
ResolutionError::ConstantForIrrefutableBinding(name)
);
self.record_def(pattern.id, err_path_resolution());
}

View File

@ -27,7 +27,7 @@
//! bounds by special casing scenarios such as these. Fun!
use std::mem;
use std::collections::HashMap;
use std::collections::BTreeMap;
use rustc::hir::def_id::DefId;
use rustc::ty::subst;
@ -39,7 +39,7 @@ use core::DocContext;
pub fn where_clauses(cx: &DocContext, clauses: Vec<WP>) -> Vec<WP> {
// First, partition the where clause into its separate components
let mut params = HashMap::new();
let mut params = BTreeMap::new();
let mut lifetimes = Vec::new();
let mut equalities = Vec::new();
let mut tybounds = Vec::new();
@ -62,7 +62,7 @@ pub fn where_clauses(cx: &DocContext, clauses: Vec<WP>) -> Vec<WP> {
// Simplify the type parameter bounds on all the generics
let mut params = params.into_iter().map(|(k, v)| {
(k, ty_bounds(v))
}).collect::<HashMap<_, _>>();
}).collect::<BTreeMap<_, _>>();
// Look for equality predicates on associated types that can be merged into
// general bound predicates

View File

@ -159,6 +159,13 @@ impl Error for num::ParseIntError {
}
}
#[unstable(feature = "try_from", issue = "33417")]
impl Error for num::TryFromIntError {
fn description(&self) -> &str {
self.__description()
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl Error for num::ParseFloatError {
fn description(&self) -> &str {

View File

@ -270,6 +270,7 @@
#![feature(vec_push_all)]
#![feature(zero_one)]
#![feature(question_mark)]
#![feature(try_from)]
// Issue# 30592: Systematically use alloc_system during stage0 since jemalloc
// might be unavailable or disabled

View File

@ -19,7 +19,7 @@
#[stable(feature = "rust1", since = "1.0.0")]
pub use core::num::{Zero, One};
#[stable(feature = "rust1", since = "1.0.0")]
pub use core::num::{FpCategory, ParseIntError, ParseFloatError};
pub use core::num::{FpCategory, ParseIntError, ParseFloatError, TryFromIntError};
#[stable(feature = "rust1", since = "1.0.0")]
pub use core::num::Wrapping;

View File

@ -376,11 +376,21 @@ impl FileInfo {
// Basically, although this loses information, multi-line spans just
// never look good.
let (line, start_col, end_col) = if lines.len() == 1 {
let (line, start_col, mut end_col) = if lines.len() == 1 {
(lines[0].line_index, lines[0].start_col, lines[0].end_col)
} else {
(lines[0].line_index, lines[0].start_col, CharPos(lines[0].start_col.0 + 1))
};
// Watch out for "empty spans". If we get a span like 6..6, we
// want to just display a `^` at 6, so convert that to
// 6..7. This is degenerate input, but it's best to degrade
// gracefully -- and the parser likes to suply a span like
// that for EOF, in particular.
if start_col == end_col {
end_col.0 += 1;
}
let index = self.ensure_source_line(line);
self.lines[index].push_annotation(start_col,
end_col,

View File

@ -519,3 +519,41 @@ fn span_overlap_label3() {
|> ----- bar
"#[1..]);
}
#[test]
fn span_empty() {
// In one of the unit tests, we found that the parser sometimes
// gives empty spans, and in particular it supplied an EOF span
// like this one, which points at the very end. We want to
// fallback gracefully in this case.
let file_text = r#"
fn main() {
struct Foo;
impl !Sync for Foo {}
unsafe impl Send for &'static Foo {
// error: cross-crate traits with a default impl, like `core::marker::Send`,
// can only be implemented for a struct/enum type, not
// `&'static Foo`
}"#;
let cm = Rc::new(CodeMap::new());
let foo = cm.new_filemap_and_lines("foo.rs", file_text);
let mut rbrace_span = cm.span_substr(&foo, file_text, "}", 1);
rbrace_span.lo = rbrace_span.hi;
let mut snippet = SnippetData::new(cm.clone(), Some(rbrace_span));
snippet.push(rbrace_span, false, None);
let lines = snippet.render_lines();
let text: String = make_string(&lines);
println!("r#\"\n{}\"", text);
assert_eq!(text, &r#"
--> foo.rs:11:2
11 |> }
|> -
"#[1..]);
}

31
src/rustc/Cargo.lock generated
View File

@ -7,15 +7,6 @@ dependencies = [
"rustdoc 0.0.0",
]
[[package]]
name = "advapi32-sys"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "arena"
version = "0.0.0"
@ -29,7 +20,7 @@ name = "flate"
version = "0.0.0"
dependencies = [
"build_helper 0.1.0",
"gcc 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)",
"gcc 0.3.28 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -38,12 +29,8 @@ version = "0.0.0"
[[package]]
name = "gcc"
version = "0.3.17"
version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"advapi32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "graphviz"
@ -192,7 +179,7 @@ name = "rustc_llvm"
version = "0.0.0"
dependencies = [
"build_helper 0.1.0",
"gcc 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)",
"gcc 0.3.28 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc_bitflags 0.0.0",
]
@ -326,7 +313,7 @@ version = "0.0.0"
dependencies = [
"arena 0.0.0",
"build_helper 0.1.0",
"gcc 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)",
"gcc 0.3.28 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.0.0",
"rustc 0.0.0",
"rustc_back 0.0.0",
@ -365,13 +352,3 @@ dependencies = [
"syntax 0.0.0",
]
[[package]]
name = "winapi"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi-build"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"

View File

@ -19,10 +19,10 @@ use foo::d; //~ NOTE constant imported here
const a: u8 = 2; //~ NOTE constant defined here
fn main() {
let a = 4; //~ ERROR only irrefutable
let a = 4; //~ ERROR variable bindings cannot
//~^ NOTE there already is a constant in scope
let c = 4; //~ ERROR only irrefutable
let c = 4; //~ ERROR variable bindings cannot
//~^ NOTE there already is a constant in scope
let d = 4; //~ ERROR only irrefutable
let d = 4; //~ ERROR variable bindings cannot
//~^ NOTE there already is a constant in scope
}

View File

@ -14,7 +14,7 @@ fn main() {
};
const C: u8 = 1;
match 1 {
C @ 2 => { //~ ERROR only irrefutable patterns allowed here
C @ 2 => { //~ ERROR variable bindings cannot shadow constants
println!("{}", C);
}
_ => {}

View File

@ -0,0 +1,43 @@
// Copyright 2016 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.
use std::path::Path;
const CARGO_LOCK: &'static str = "Cargo.lock";
pub fn check(path: &Path, bad: &mut bool) {
use std::process::Command;
super::walk(path,
&mut |path| super::filter_dirs(path) || path.ends_with("src/test"),
&mut |file| {
let name = file.file_name().unwrap().to_string_lossy();
if name == CARGO_LOCK {
let rel_path = file.strip_prefix(path).unwrap();
let ret_code = Command::new("git")
.arg("diff-index")
.arg("--quiet")
.arg("HEAD")
.arg(rel_path)
.current_dir(path)
.status()
.unwrap_or_else(|e| {
panic!("could not run git diff-index: {}", e);
});
if !ret_code.success() {
let parent_path = file.parent().unwrap().join("Cargo.toml");
print!("dirty lock file found at {} ", rel_path.display());
println!("please commit your changes or update the lock file by running:");
println!("\n\tcargo update --manifest-path {}", parent_path.display());
*bad = true;
}
}
});
}

View File

@ -35,6 +35,7 @@ mod style;
mod errors;
mod features;
mod cargo;
mod cargo_lock;
fn main() {
let path = env::args_os().skip(1).next().expect("need an argument");
@ -46,6 +47,7 @@ fn main() {
errors::check(&path, &mut bad);
cargo::check(&path, &mut bad);
features::check(&path, &mut bad);
cargo_lock::check(&path, &mut bad);
if bad {
panic!("some tidy checks failed");