Skip to content

Commit 0358f31

Browse files
committed
rollup merge of rust-lang#17598 : bkoropoff/issue-17441
2 parents 7784a8d + 69d570f commit 0358f31

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/librustc/middle/typeck/check/mod.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,37 @@ fn check_cast(fcx: &FnCtxt,
14051405
return
14061406
}
14071407

1408+
if !ty::type_is_sized(fcx.tcx(), t_1) {
1409+
let tstr = fcx.infcx().ty_to_string(t_1);
1410+
fcx.type_error_message(span, |actual| {
1411+
format!("cast to unsized type: `{}` as `{}`", actual, tstr)
1412+
}, t_e, None);
1413+
match ty::get(t_e).sty {
1414+
ty::ty_rptr(_, ty::mt { mutbl: mt, .. }) => {
1415+
let mtstr = match mt {
1416+
ast::MutMutable => "mut ",
1417+
ast::MutImmutable => ""
1418+
};
1419+
if ty::type_is_trait(t_1) {
1420+
span_note!(fcx.tcx().sess, t.span, "did you mean `&{}{}`?", mtstr, tstr);
1421+
} else {
1422+
span_note!(fcx.tcx().sess, span,
1423+
"consider using an implicit coercion to `&{}{}` instead",
1424+
mtstr, tstr);
1425+
}
1426+
}
1427+
ty::ty_uniq(..) => {
1428+
span_note!(fcx.tcx().sess, t.span, "did you mean `Box<{}>`?", tstr);
1429+
}
1430+
_ => {
1431+
span_note!(fcx.tcx().sess, e.span,
1432+
"consider using a box or reference as appropriate");
1433+
}
1434+
}
1435+
fcx.write_error(id);
1436+
return
1437+
}
1438+
14081439
if ty::type_is_trait(t_1) {
14091440
// This will be looked up later on.
14101441
vtable2::check_object_cast(fcx, cast_expr, e, t_1);

src/test/compile-fail/issue-17441.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2014 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 = &[1u, 2] as [uint];
13+
//~^ ERROR cast to unsized type: `&[uint, .. 2]` as `[uint]`
14+
//~^^ NOTE consider using an implicit coercion to `&[uint]` instead
15+
let _bar = box 1u as std::fmt::Show;
16+
//~^ ERROR cast to unsized type: `Box<uint>` as `core::fmt::Show`
17+
//~^^ NOTE did you mean `Box<core::fmt::Show>`?
18+
let _baz = 1u as std::fmt::Show;
19+
//~^ ERROR cast to unsized type: `uint` as `core::fmt::Show`
20+
//~^^ NOTE consider using a box or reference as appropriate
21+
let _quux = [1u, 2] as [uint];
22+
//~^ ERROR cast to unsized type: `[uint, .. 2]` as `[uint]`
23+
//~^^ NOTE consider using a box or reference as appropriate
24+
}

0 commit comments

Comments
 (0)