about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorNick Cameron <ncameron@mozilla.com>2015-03-20 17:15:27 +1300
committerNick Cameron <ncameron@mozilla.com>2015-03-25 10:03:57 +1300
commit95602a759d9190cad92279aa5929d30166f2255c (patch)
treea3f7026e046eda5326c70ba47c3c91c5b1e54a35 /src/libcore
parented810385045ab0db90303574ba3ea47dfa2a36d5 (diff)
downloadrust-95602a759d9190cad92279aa5929d30166f2255c.tar.gz
rust-95602a759d9190cad92279aa5929d30166f2255c.zip
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;
```
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/cell.rs6
-rw-r--r--src/libcore/fmt/mod.rs6
-rw-r--r--src/libcore/fmt/num.rs1
-rw-r--r--src/libcore/hash/mod.rs2
-rw-r--r--src/libcore/mem.rs2
-rw-r--r--src/libcore/num/i16.rs1
-rw-r--r--src/libcore/num/i32.rs1
-rw-r--r--src/libcore/num/i64.rs1
-rw-r--r--src/libcore/num/i8.rs1
-rw-r--r--src/libcore/num/int_macros.rs1
-rw-r--r--src/libcore/num/isize.rs1
-rw-r--r--src/libcore/num/mod.rs1
-rw-r--r--src/libcore/num/u16.rs1
-rw-r--r--src/libcore/num/u32.rs1
-rw-r--r--src/libcore/num/u64.rs1
-rw-r--r--src/libcore/num/u8.rs1
-rw-r--r--src/libcore/num/uint_macros.rs1
-rw-r--r--src/libcore/num/usize.rs1
-rw-r--r--src/libcore/ptr.rs2
-rw-r--r--src/libcore/str/mod.rs2
20 files changed, 31 insertions, 3 deletions
diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs
index a9c5de23d94..f5db3e63c7a 100644
--- a/src/libcore/cell.rs
+++ b/src/libcore/cell.rs
@@ -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
     ///
diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs
index cf427c16588..e00cdc9de88 100644
--- a/src/libcore/fmt/mod.rs
+++ b/src/libcore/fmt/mod.rs
@@ -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)
     }
 }
diff --git a/src/libcore/fmt/num.rs b/src/libcore/fmt/num.rs
index 49da99b97cb..d01d240e5b5 100644
--- a/src/libcore/fmt/num.rs
+++ b/src/libcore/fmt/num.rs
@@ -13,6 +13,7 @@
 // FIXME: #6220 Implement floating point formatting
 
 #![allow(unsigned_negation)]
+#![allow(trivial_numeric_cast)]
 
 use fmt;
 use iter::IteratorExt;
diff --git a/src/libcore/hash/mod.rs b/src/libcore/hash/mod.rs
index 1d5e174a8dc..ccc1d32eab3 100644
--- a/src/libcore/hash/mod.rs
+++ b/src/libcore/hash/mod.rs
@@ -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) })
diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs
index 551f97ead12..0bc41eb2c6e 100644
--- a/src/libcore/mem.rs
+++ b/src/libcore/mem.rs
@@ -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)
 }
 
diff --git a/src/libcore/num/i16.rs b/src/libcore/num/i16.rs
index 5ea60d0d96d..1557434a28a 100644
--- a/src/libcore/num/i16.rs
+++ b/src/libcore/num/i16.rs
@@ -12,5 +12,6 @@
 
 #![stable(feature = "rust1", since = "1.0.0")]
 #![doc(primitive = "i16")]
+#![allow(trivial_numeric_cast)]
 
 int_module! { i16, 16 }
diff --git a/src/libcore/num/i32.rs b/src/libcore/num/i32.rs
index 7d9faa998c1..bdccab4cb9a 100644
--- a/src/libcore/num/i32.rs
+++ b/src/libcore/num/i32.rs
@@ -12,5 +12,6 @@
 
 #![stable(feature = "rust1", since = "1.0.0")]
 #![doc(primitive = "i32")]
+#![allow(trivial_numeric_cast)]
 
 int_module! { i32, 32 }
diff --git a/src/libcore/num/i64.rs b/src/libcore/num/i64.rs
index 5a70911387b..d8f5aa6e548 100644
--- a/src/libcore/num/i64.rs
+++ b/src/libcore/num/i64.rs
@@ -12,5 +12,6 @@
 
 #![stable(feature = "rust1", since = "1.0.0")]
 #![doc(primitive = "i64")]
+#![allow(trivial_numeric_cast)]
 
 int_module! { i64, 64 }
diff --git a/src/libcore/num/i8.rs b/src/libcore/num/i8.rs
index 1d7d78ffa6c..da2410a04cf 100644
--- a/src/libcore/num/i8.rs
+++ b/src/libcore/num/i8.rs
@@ -12,5 +12,6 @@
 
 #![stable(feature = "rust1", since = "1.0.0")]
 #![doc(primitive = "i8")]
+#![allow(trivial_numeric_cast)]
 
 int_module! { i8, 8 }
diff --git a/src/libcore/num/int_macros.rs b/src/libcore/num/int_macros.rs
index fe0d6d13c4c..5f697e02a99 100644
--- a/src/libcore/num/int_macros.rs
+++ b/src/libcore/num/int_macros.rs
@@ -9,6 +9,7 @@
 // except according to those terms.
 
 #![doc(hidden)]
+#![allow(trivial_numeric_cast)]
 
 macro_rules! int_module { ($T:ty, $bits:expr) => (
 
diff --git a/src/libcore/num/isize.rs b/src/libcore/num/isize.rs
index 0fd0d90b125..838caa59e47 100644
--- a/src/libcore/num/isize.rs
+++ b/src/libcore/num/isize.rs
@@ -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 }
diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs
index 9ca7b48fbe5..7686a990acc 100644
--- a/src/libcore/num/mod.rs
+++ b/src/libcore/num/mod.rs
@@ -14,6 +14,7 @@
 
 #![stable(feature = "rust1", since = "1.0.0")]
 #![allow(missing_docs)]
+#![allow(trivial_numeric_cast)]
 
 use self::wrapping::{OverflowingOps, WrappingOps};
 
diff --git a/src/libcore/num/u16.rs b/src/libcore/num/u16.rs
index 21635799a77..9d91bbc7f90 100644
--- a/src/libcore/num/u16.rs
+++ b/src/libcore/num/u16.rs
@@ -12,5 +12,6 @@
 
 #![stable(feature = "rust1", since = "1.0.0")]
 #![doc(primitive = "u16")]
+#![allow(trivial_numeric_cast)]
 
 uint_module! { u16, i16, 16 }
diff --git a/src/libcore/num/u32.rs b/src/libcore/num/u32.rs
index 7d520770503..2d5f163e093 100644
--- a/src/libcore/num/u32.rs
+++ b/src/libcore/num/u32.rs
@@ -12,5 +12,6 @@
 
 #![stable(feature = "rust1", since = "1.0.0")]
 #![doc(primitive = "u32")]
+#![allow(trivial_numeric_cast)]
 
 uint_module! { u32, i32, 32 }
diff --git a/src/libcore/num/u64.rs b/src/libcore/num/u64.rs
index f10822077dc..26813aa281c 100644
--- a/src/libcore/num/u64.rs
+++ b/src/libcore/num/u64.rs
@@ -12,5 +12,6 @@
 
 #![stable(feature = "rust1", since = "1.0.0")]
 #![doc(primitive = "u64")]
+#![allow(trivial_numeric_cast)]
 
 uint_module! { u64, i64, 64 }
diff --git a/src/libcore/num/u8.rs b/src/libcore/num/u8.rs
index 3d6922b07b1..7fb28f27d62 100644
--- a/src/libcore/num/u8.rs
+++ b/src/libcore/num/u8.rs
@@ -12,5 +12,6 @@
 
 #![stable(feature = "rust1", since = "1.0.0")]
 #![doc(primitive = "u8")]
+#![allow(trivial_numeric_cast)]
 
 uint_module! { u8, i8, 8 }
diff --git a/src/libcore/num/uint_macros.rs b/src/libcore/num/uint_macros.rs
index d0c4885ad00..9f502e3ea43 100644
--- a/src/libcore/num/uint_macros.rs
+++ b/src/libcore/num/uint_macros.rs
@@ -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) => (
 
diff --git a/src/libcore/num/usize.rs b/src/libcore/num/usize.rs
index 602ef4fe45e..be5acaac92a 100644
--- a/src/libcore/num/usize.rs
+++ b/src/libcore/num/usize.rs
@@ -16,5 +16,6 @@
 
 #![stable(feature = "rust1", since = "1.0.0")]
 #![doc(primitive = "usize")]
+#![allow(trivial_numeric_cast)]
 
 uint_module! { usize, isize, ::isize::BITS }
diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs
index d92622eeb70..9b3ee3ef5e0 100644
--- a/src/libcore/ptr.rs
+++ b/src/libcore/ptr.rs
@@ -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.
diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs
index b7285d30a73..035d070cd6d 100644
--- a/src/libcore/str/mod.rs
+++ b/src/libcore/str/mod.rs
@@ -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;
     }