diff --git a/src/librustc_typeck/check/cast.rs b/src/librustc_typeck/check/cast.rs index 46d304976dc..7bd24c939ca 100644 --- a/src/librustc_typeck/check/cast.rs +++ b/src/librustc_typeck/check/cast.rs @@ -239,12 +239,10 @@ impl<'a, 'gcx, 'tcx> CastCheck<'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 { diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index 09c3445672a..112249463f6 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -4692,6 +4692,25 @@ match x { ``` "##, +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 index 00000000000..5e945dfa5c8 --- /dev/null +++ b/src/test/compile-fail/E0620.rs @@ -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 or the MIT license +// , 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 +}