Add E0620
This commit is contained in:
parent
09f42fb9cc
commit
05ac25affa
3 changed files with 36 additions and 6 deletions
|
@ -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 {
|
||||
|
|
|
@ -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! {
|
||||
|
|
13
src/test/compile-fail/E0620.rs
Normal file
13
src/test/compile-fail/E0620.rs
Normal file
|
@ -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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue