Skip to content

Commit 05ac25a

Browse files
Add E0620
1 parent 09f42fb commit 05ac25a

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

src/librustc_typeck/check/cast.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,10 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
239239
}
240240

241241
let tstr = fcx.ty_to_string(self.cast_ty);
242-
let mut err =
243-
fcx.type_error_struct(self.span,
244-
|actual| {
245-
format!("cast to unsized type: `{}` as `{}`", actual, tstr)
246-
},
247-
self.expr_ty);
242+
let mut err = type_error_struct!(fcx.tcx.sess, self.span, self.expr_ty, E0620,
243+
"cast to unsized type: `{}` as `{}`",
244+
fcx.resolve_type_vars_if_possible(&self.expr_ty),
245+
tstr);
248246
match self.expr_ty.sty {
249247
ty::TyRef(_, ty::TypeAndMut { mutbl: mt, .. }) => {
250248
let mtstr = match mt {

src/librustc_typeck/diagnostics.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4692,6 +4692,25 @@ match x {
46924692
```
46934693
"##,
46944694

4695+
E0620: r##"
4696+
A cast to an unsized type was attempted.
4697+
4698+
Erroneous code example:
4699+
4700+
```compile_fail,E0620
4701+
let x = &[1_usize, 2] as [usize]; // error: cast to unsized type: `&[usize; 2]`
4702+
// as `[usize]`
4703+
```
4704+
4705+
In Rust, some types don't have a size at compile-time (like slices and traits
4706+
for example). Therefore, you can't cast into them directly. Try casting to a
4707+
reference instead:
4708+
4709+
```
4710+
let x = &[1_usize, 2] as &[usize]; // ok!
4711+
```
4712+
"##,
4713+
46954714
}
46964715

46974716
register_diagnostics! {

src/test/compile-fail/E0620.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let _foo = &[1_usize, 2] as [usize]; //~ ERROR E0620
13+
}

0 commit comments

Comments
 (0)