}
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 {
```
"##,
+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! {
--- /dev/null
+// 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
+}