Add trivial cast lints.

This permits all coercions to be performed in casts, but adds lints to warn in those cases.

Part of this patch moves cast checking to a later stage of type checking. We acquire obligations to check casts as part of type checking where we previously checked them. Once we have type checked a function or module, then we check any cast obligations which have been acquired. That means we have more type information available to check casts (this was crucial to making coercions work properly in place of some casts), but it means that casts cannot feed input into type inference.

[breaking change]

* Adds two new lints for trivial casts and trivial numeric casts, these are warn by default, but can cause errors if you build with warnings as errors. Previously, trivial numeric casts and casts to trait objects were allowed.
* The unused casts lint has gone.
* Interactions between casting and type inference have changed in subtle ways. Two ways this might manifest are:
- You may need to 'direct' casts more with extra type information, for example, in some cases where `foo as _ as T` succeeded, you may now need to specify the type for `_`
- Casts do not influence inference of integer types. E.g., the following used to type check:

```
let x = 42;
let y = &x as *const u32;
```

Because the cast would inform inference that `x` must have type `u32`. This no longer applies and the compiler will fallback to `i32` for `x` and thus there will be a type error in the cast. The solution is to add more type information:

```
let x: u32 = 42;
let y = &x as *const u32;
```
This commit is contained in:
Nick Cameron
2015-03-20 17:15:27 +13:00
parent ed81038504
commit 95602a759d
90 changed files with 287 additions and 449 deletions

View File

@@ -713,7 +713,11 @@ impl<T> UnsafeCell<T> {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get(&self) -> *mut T { &self.value as *const T as *mut T }
pub fn get(&self) -> *mut T {
// FIXME(#23542) Replace with type ascription.
#![allow(trivial_cast)]
&self.value as *const T as *mut T
}
/// Unwraps the value
///

View File

@@ -833,6 +833,8 @@ impl<T> Pointer for *const T {
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Pointer for *mut T {
fn fmt(&self, f: &mut Formatter) -> Result {
// FIXME(#23542) Replace with type ascription.
#![allow(trivial_cast)]
Pointer::fmt(&(*self as *const T), f)
}
}
@@ -840,6 +842,8 @@ impl<T> Pointer for *mut T {
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> Pointer for &'a T {
fn fmt(&self, f: &mut Formatter) -> Result {
// FIXME(#23542) Replace with type ascription.
#![allow(trivial_cast)]
Pointer::fmt(&(*self as *const T), f)
}
}
@@ -847,6 +851,8 @@ impl<'a, T> Pointer for &'a T {
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> Pointer for &'a mut T {
fn fmt(&self, f: &mut Formatter) -> Result {
// FIXME(#23542) Replace with type ascription.
#![allow(trivial_cast)]
Pointer::fmt(&(&**self as *const T), f)
}
}

View File

@@ -13,6 +13,7 @@
// FIXME: #6220 Implement floating point formatting
#![allow(unsigned_negation)]
#![allow(trivial_numeric_cast)]
use fmt;
use iter::IteratorExt;

View File

@@ -182,6 +182,8 @@ mod impls {
}
fn hash_slice<H: Hasher>(data: &[$ty], state: &mut H) {
// FIXME(#23542) Replace with type ascription.
#![allow(trivial_cast)]
let newlen = data.len() * ::$ty::BYTES as usize;
let ptr = data.as_ptr() as *const u8;
state.write(unsafe { slice::from_raw_parts(ptr, newlen) })

View File

@@ -313,6 +313,8 @@ pub fn drop<T>(_x: T) { }
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
// FIXME(#23542) Replace with type ascription.
#![allow(trivial_cast)]
ptr::read(src as *const T as *const U)
}

View File

@@ -12,5 +12,6 @@
#![stable(feature = "rust1", since = "1.0.0")]
#![doc(primitive = "i16")]
#![allow(trivial_numeric_cast)]
int_module! { i16, 16 }

View File

@@ -12,5 +12,6 @@
#![stable(feature = "rust1", since = "1.0.0")]
#![doc(primitive = "i32")]
#![allow(trivial_numeric_cast)]
int_module! { i32, 32 }

View File

@@ -12,5 +12,6 @@
#![stable(feature = "rust1", since = "1.0.0")]
#![doc(primitive = "i64")]
#![allow(trivial_numeric_cast)]
int_module! { i64, 64 }

View File

@@ -12,5 +12,6 @@
#![stable(feature = "rust1", since = "1.0.0")]
#![doc(primitive = "i8")]
#![allow(trivial_numeric_cast)]
int_module! { i8, 8 }

View File

@@ -9,6 +9,7 @@
// except according to those terms.
#![doc(hidden)]
#![allow(trivial_numeric_cast)]
macro_rules! int_module { ($T:ty, $bits:expr) => (

View File

@@ -16,6 +16,7 @@
#![stable(feature = "rust1", since = "1.0.0")]
#![doc(primitive = "isize")]
#![allow(trivial_numeric_cast)]
#[cfg(target_pointer_width = "32")]
int_module! { isize, 32 }

View File

@@ -14,6 +14,7 @@
#![stable(feature = "rust1", since = "1.0.0")]
#![allow(missing_docs)]
#![allow(trivial_numeric_cast)]
use self::wrapping::{OverflowingOps, WrappingOps};

View File

@@ -12,5 +12,6 @@
#![stable(feature = "rust1", since = "1.0.0")]
#![doc(primitive = "u16")]
#![allow(trivial_numeric_cast)]
uint_module! { u16, i16, 16 }

View File

@@ -12,5 +12,6 @@
#![stable(feature = "rust1", since = "1.0.0")]
#![doc(primitive = "u32")]
#![allow(trivial_numeric_cast)]
uint_module! { u32, i32, 32 }

View File

@@ -12,5 +12,6 @@
#![stable(feature = "rust1", since = "1.0.0")]
#![doc(primitive = "u64")]
#![allow(trivial_numeric_cast)]
uint_module! { u64, i64, 64 }

View File

@@ -12,5 +12,6 @@
#![stable(feature = "rust1", since = "1.0.0")]
#![doc(primitive = "u8")]
#![allow(trivial_numeric_cast)]
uint_module! { u8, i8, 8 }

View File

@@ -9,6 +9,7 @@
// except according to those terms.
#![doc(hidden)]
#![allow(trivial_numeric_cast)]
macro_rules! uint_module { ($T:ty, $T_SIGNED:ty, $bits:expr) => (

View File

@@ -16,5 +16,6 @@
#![stable(feature = "rust1", since = "1.0.0")]
#![doc(primitive = "usize")]
#![allow(trivial_numeric_cast)]
uint_module! { usize, isize, ::isize::BITS }

View File

@@ -529,7 +529,7 @@ impl<T: ?Sized> Unique<T> {
/// Create a new `Unique`.
#[unstable(feature = "unique")]
pub unsafe fn new(ptr: *mut T) -> Unique<T> {
Unique { pointer: NonZero::new(ptr as *const T), _marker: PhantomData }
Unique { pointer: NonZero::new(ptr), _marker: PhantomData }
}
/// Dereference the content.

View File

@@ -261,7 +261,7 @@ pub unsafe fn from_utf8_unchecked<'a>(v: &'a [u8]) -> &'a str {
reason = "use std::ffi::c_str_to_bytes + str::from_utf8")]
pub unsafe fn from_c_str(s: *const i8) -> &'static str {
let s = s as *const u8;
let mut len = 0;
let mut len: usize = 0;
while *s.offset(len as isize) != 0 {
len += 1;
}