about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNick Cameron <ncameron@mozilla.com>2015-03-23 17:28:57 +1300
committerNick Cameron <ncameron@mozilla.com>2015-03-25 10:06:13 +1300
commitdc206a91c8b641b4eccd51541f59d96fabea877d (patch)
treeebfae46d74b49dce77a8519a3f4f03e155915391
parent088cd566ea87c90a9c765a113a2310d60849e7c1 (diff)
downloadrust-dc206a91c8b641b4eccd51541f59d96fabea877d.tar.gz
rust-dc206a91c8b641b4eccd51541f59d96fabea877d.zip
Add tests
-rw-r--r--src/librustc_typeck/check/mod.rs4
-rw-r--r--src/test/compile-fail/trivial_casts.rs94
-rw-r--r--src/test/run-pass/trivial_casts.rs71
3 files changed, 167 insertions, 2 deletions
diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs
index 4eddf8ae97a..c8c7fb046b1 100644
--- a/src/librustc_typeck/check/mod.rs
+++ b/src/librustc_typeck/check/mod.rs
@@ -1091,14 +1091,14 @@ fn check_cast<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, cast: &CastCheck<'tcx>) {
                 fcx.tcx().sess.add_lint(lint::builtin::TRIVIAL_NUMERIC_CAST,
                                         e.id,
                                         span,
-                                        format!("trivial numeric cast: {} as {}",
+                                        format!("trivial numeric cast: `{}` as `{}`",
                                                 fcx.infcx().ty_to_string(t_e),
                                                 fcx.infcx().ty_to_string(t_1)));
             } else {
                 fcx.tcx().sess.add_lint(lint::builtin::TRIVIAL_CAST,
                                         e.id,
                                         span,
-                                        format!("trivial cast: {} as {}",
+                                        format!("trivial cast: `{}` as `{}`",
                                                 fcx.infcx().ty_to_string(t_e),
                                                 fcx.infcx().ty_to_string(t_1)));
             }
diff --git a/src/test/compile-fail/trivial_casts.rs b/src/test/compile-fail/trivial_casts.rs
new file mode 100644
index 00000000000..05c7747d5b9
--- /dev/null
+++ b/src/test/compile-fail/trivial_casts.rs
@@ -0,0 +1,94 @@
+// Copyright 2015 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 the trivial_cast and trivial_numeric_cast lints. For each error we also
+// check that the cast can be done using just coercion.
+
+#![deny(trivial_cast, trivial_numeric_cast)]
+
+trait Foo {
+    fn foo(&self) {}
+}
+
+pub struct Bar;
+
+impl Foo for Bar {}
+
+pub fn main() {
+    // Numeric
+    let _ = 42_i32 as i32; //~ ERROR trivial numeric cast: `i32` as `i32`
+    let _: i32 = 42_i32;
+
+    let _ = 42_u8 as u8; //~ ERROR trivial numeric cast: `u8` as `u8`
+    let _: u8 = 42_u8;
+
+    // & to * pointers
+    let x: &u32 = &42;
+    let _ = x as *const u32; //~ERROR trivial cast: `&u32` as `*const u32`
+    let _: *const u32 = x;
+
+    let x: &mut u32 = &mut 42;
+    let _ = x as *mut u32; //~ERROR trivial cast: `&mut u32` as `*mut u32`
+    let _: *mut u32 = x;
+
+    // unsize array
+    let x: &[u32; 3] = &[42, 43, 44];
+    let _ = x as &[u32]; //~ERROR trivial cast: `&[u32; 3]` as `&[u32]`
+    let _ = x as *const [u32]; //~ERROR trivial cast: `&[u32; 3]` as `*const [u32]`
+    let _: &[u32] = x;
+    let _: *const [u32] = x;
+
+    let x: &mut [u32; 3] = &mut [42, 43, 44];
+    let _ = x as &mut [u32]; //~ERROR trivial cast: `&mut [u32; 3]` as `&mut [u32]`
+    let _ = x as *mut [u32]; //~ERROR trivial cast: `&mut [u32; 3]` as `*mut [u32]`
+    let _: &mut [u32] = x;
+    let _: *mut [u32] = x;
+
+    let x: Box<[u32; 3]> = Box::new([42, 43, 44]);
+    let _ = x as Box<[u32]>; //~ERROR trivial cast: `Box<[u32; 3]>` as `Box<[u32]>`
+    let x: Box<[u32; 3]> = Box::new([42, 43, 44]);
+    let _: Box<[u32]> = x;
+
+    // unsize trait
+    let x: &Bar = &Bar;
+    let _ = x as &Foo; //~ERROR trivial cast: `&Bar` as `&Foo`
+    let _ = x as *const Foo; //~ERROR trivial cast: `&Bar` as `*const Foo`
+    let _: &Foo = x;
+    let _: *const Foo = x;
+
+    let x: &mut Bar = &mut Bar;
+    let _ = x as &mut Foo; //~ERROR trivial cast: `&mut Bar` as `&mut Foo`
+    let _ = x as *mut Foo; //~ERROR trivial cast: `&mut Bar` as `*mut Foo`
+    let _: &mut Foo = x;
+    let _: *mut Foo = x;
+
+    let x: Box<Bar> = Box::new(Bar);
+    let _ = x as Box<Foo>; //~ERROR trivial cast: `Box<Bar>` as `Box<Foo>`
+    let x: Box<Bar> = Box::new(Bar);
+    let _: Box<Foo> = x;
+
+    // functions
+    fn baz(_x: i32) {}
+    let _ = &baz as &Fn(i32); //~ERROR trivial cast: `&fn(i32) {main::baz}` as `&core::ops::Fn(i32)`
+    let _: &Fn(i32) = &baz;
+    let x = |_x: i32| {};
+    let _ = &x as &Fn(i32); //~ERROR trivial cast
+    let _: &Fn(i32) = &x;
+}
+
+// subtyping
+pub fn test_subtyping<'a, 'b: 'a>(a: &'a Bar, b: &'b Bar) {
+    let _ = a as &'a Bar; //~ERROR trivial cast
+    let _: &'a Bar = a;
+    let _ = b as &'a Bar; //~ERROR trivial cast
+    let _: &'a Bar = b;
+    let _ = b as &'b Bar; //~ERROR trivial cast
+    let _: &'b Bar = b;
+}
diff --git a/src/test/run-pass/trivial_casts.rs b/src/test/run-pass/trivial_casts.rs
new file mode 100644
index 00000000000..4b145f1079b
--- /dev/null
+++ b/src/test/run-pass/trivial_casts.rs
@@ -0,0 +1,71 @@
+// Copyright 2015 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 all coercions can actually be done using casts (modulo the lints).
+
+#![allow(trivial_cast, trivial_numeric_cast)]
+
+trait Foo {
+    fn foo(&self) {}
+}
+
+pub struct Bar;
+
+impl Foo for Bar {}
+
+pub fn main() {
+    // Numeric
+    let _ = 42_i32 as i32;
+    let _ = 42_u8 as u8;
+
+    // & to * pointers
+    let x: &u32 = &42;
+    let _ = x as *const u32;
+
+    let x: &mut u32 = &mut 42;
+    let _ = x as *mut u32;
+
+    // unsize array
+    let x: &[u32; 3] = &[42, 43, 44];
+    let _ = x as &[u32];
+    let _ = x as *const [u32];
+
+    let x: &mut [u32; 3] = &mut [42, 43, 44];
+    let _ = x as &mut [u32];
+    let _ = x as *mut [u32];
+
+    let x: Box<[u32; 3]> = Box::new([42, 43, 44]);
+    let _ = x as Box<[u32]>;
+
+    // unsize trait
+    let x: &Bar = &Bar;
+    let _ = x as &Foo;
+    let _ = x as *const Foo;
+
+    let x: &mut Bar = &mut Bar;
+    let _ = x as &mut Foo;
+    let _ = x as *mut Foo;
+
+    let x: Box<Bar> = Box::new(Bar);
+    let _ = x as Box<Foo>;
+
+    // functions
+    fn baz(_x: i32) {}
+    let _ = &baz as &Fn(i32);
+    let x = |_x: i32| {};
+    let _ = &x as &Fn(i32);
+}
+
+// subtyping
+pub fn test_subtyping<'a, 'b: 'a>(a: &'a Bar, b: &'b Bar) {
+    let _ = a as &'a Bar;
+    let _ = b as &'a Bar;
+    let _ = b as &'b Bar;
+}