parent
5ac4c8a63e
commit
e6e195e27e
@ -576,25 +576,19 @@ fn report_cast_to_unsized_type(&self, fcx: &FnCtxt<'a, 'tcx>) -> ErrorGuaranteed
|
|||||||
match self.expr_ty.kind() {
|
match self.expr_ty.kind() {
|
||||||
ty::Ref(_, _, mt) => {
|
ty::Ref(_, _, mt) => {
|
||||||
let mtstr = mt.prefix_str();
|
let mtstr = mt.prefix_str();
|
||||||
if self.cast_ty.is_trait() {
|
match fcx.tcx.sess.source_map().span_to_snippet(self.cast_span) {
|
||||||
match fcx.tcx.sess.source_map().span_to_snippet(self.cast_span) {
|
Ok(s) => {
|
||||||
Ok(s) => {
|
err.span_suggestion(
|
||||||
err.span_suggestion(
|
self.cast_span,
|
||||||
self.cast_span,
|
"try casting to a reference instead",
|
||||||
"try casting to a reference instead",
|
format!("&{mtstr}{s}"),
|
||||||
format!("&{mtstr}{s}"),
|
Applicability::MachineApplicable,
|
||||||
Applicability::MachineApplicable,
|
);
|
||||||
);
|
}
|
||||||
}
|
Err(_) => {
|
||||||
Err(_) => {
|
let msg = format!("did you mean `&{mtstr}{tstr}`?");
|
||||||
let msg = format!("did you mean `&{mtstr}{tstr}`?");
|
err.span_help(self.cast_span, msg);
|
||||||
err.span_help(self.cast_span, msg);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
let msg =
|
|
||||||
format!("consider using an implicit coercion to `&{mtstr}{tstr}` instead");
|
|
||||||
err.span_help(self.span, msg);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ty::Adt(def, ..) if def.is_box() => {
|
ty::Adt(def, ..) if def.is_box() => {
|
||||||
|
8
tests/ui/cast/cast-to-slice.rs
Normal file
8
tests/ui/cast/cast-to-slice.rs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fn main() {
|
||||||
|
"example".as_bytes() as [char];
|
||||||
|
//~^ ERROR cast to unsized type
|
||||||
|
|
||||||
|
let arr: &[u8] = &[0, 2, 3];
|
||||||
|
arr as [char];
|
||||||
|
//~^ ERROR cast to unsized type
|
||||||
|
}
|
19
tests/ui/cast/cast-to-slice.stderr
Normal file
19
tests/ui/cast/cast-to-slice.stderr
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
error[E0620]: cast to unsized type: `&[u8]` as `[char]`
|
||||||
|
--> $DIR/cast-to-slice.rs:2:5
|
||||||
|
|
|
||||||
|
LL | "example".as_bytes() as [char];
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^------
|
||||||
|
| |
|
||||||
|
| help: try casting to a reference instead: `&[char]`
|
||||||
|
|
||||||
|
error[E0620]: cast to unsized type: `&[u8]` as `[char]`
|
||||||
|
--> $DIR/cast-to-slice.rs:6:5
|
||||||
|
|
|
||||||
|
LL | arr as [char];
|
||||||
|
| ^^^^^^^------
|
||||||
|
| |
|
||||||
|
| help: try casting to a reference instead: `&[char]`
|
||||||
|
|
||||||
|
error: aborting due to 2 previous errors
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0620`.
|
Loading…
Reference in New Issue
Block a user