]> git.lizzy.rs Git - rust.git/commitdiff
Add E0620
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Sat, 24 Jun 2017 21:23:47 +0000 (23:23 +0200)
committerGuillaume Gomez <guillaume1.gomez@gmail.com>
Thu, 29 Jun 2017 07:48:17 +0000 (09:48 +0200)
src/librustc_typeck/check/cast.rs
src/librustc_typeck/diagnostics.rs
src/test/compile-fail/E0620.rs [new file with mode: 0644]

index 46d304976dc635b11585a36ba061e301b90b9cba..7bd24c939caf0beef8e405216da7da4decfc25cb 100644 (file)
@@ -239,12 +239,10 @@ fn report_cast_to_unsized_type(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) {
         }
 
         let tstr = fcx.ty_to_string(self.cast_ty);
-        let mut err =
-            fcx.type_error_struct(self.span,
-                                  |actual| {
-                                      format!("cast to unsized type: `{}` as `{}`", actual, tstr)
-                                  },
-                                  self.expr_ty);
+        let mut err = type_error_struct!(fcx.tcx.sess, self.span, self.expr_ty, E0620,
+                                         "cast to unsized type: `{}` as `{}`",
+                                         fcx.resolve_type_vars_if_possible(&self.expr_ty),
+                                         tstr);
         match self.expr_ty.sty {
             ty::TyRef(_, ty::TypeAndMut { mutbl: mt, .. }) => {
                 let mtstr = match mt {
index 09c3445672a3148c0cd77f6bcff8160ceb72f17d..112249463f67b81f238ea8a76a5dfb9aa87d6029 100644 (file)
@@ -4692,6 +4692,25 @@ fn i_am_a_function() {}
 ```
 "##,
 
+E0620: r##"
+A cast to an unsized type was attempted.
+
+Erroneous code example:
+
+```compile_fail,E0620
+let x = &[1_usize, 2] as [usize]; // error: cast to unsized type: `&[usize; 2]`
+                                  //        as `[usize]`
+```
+
+In Rust, some types don't have a size at compile-time (like slices and traits
+for example). Therefore, you can't cast into them directly. Try casting to a
+reference instead:
+
+```
+let x = &[1_usize, 2] as &[usize]; // ok!
+```
+"##,
+
 }
 
 register_diagnostics! {
diff --git a/src/test/compile-fail/E0620.rs b/src/test/compile-fail/E0620.rs
new file mode 100644 (file)
index 0000000..5e945df
--- /dev/null
@@ -0,0 +1,13 @@
+// Copyright 2017 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.
+
+fn main() {
+    let _foo = &[1_usize, 2] as [usize]; //~ ERROR E0620
+}