Skip to content

Commit d6382c7

Browse files
authored
Rollup merge of rust-lang#119175 - veera-sivarajan:fix-cast-to-slice, r=WaffleLapkin
fix: diagnostic for casting reference to slice fixes: rust-lang#118790 Removes `if self.cast_ty.is_trait()` to produce the same diagnostic for cast to slice and trait.
2 parents 625c2c4 + 286f2d8 commit d6382c7

File tree

5 files changed

+45
-32
lines changed

5 files changed

+45
-32
lines changed

compiler/rustc_hir_typeck/src/cast.rs

+12-18
Original file line numberDiff line numberDiff line change
@@ -539,25 +539,19 @@ impl<'a, 'tcx> CastCheck<'tcx> {
539539
match self.expr_ty.kind() {
540540
ty::Ref(_, _, mt) => {
541541
let mtstr = mt.prefix_str();
542-
if self.cast_ty.is_trait() {
543-
match fcx.tcx.sess.source_map().span_to_snippet(self.cast_span) {
544-
Ok(s) => {
545-
err.span_suggestion(
546-
self.cast_span,
547-
"try casting to a reference instead",
548-
format!("&{mtstr}{s}"),
549-
Applicability::MachineApplicable,
550-
);
551-
}
552-
Err(_) => {
553-
let msg = format!("did you mean `&{mtstr}{tstr}`?");
554-
err.span_help(self.cast_span, msg);
555-
}
542+
match fcx.tcx.sess.source_map().span_to_snippet(self.cast_span) {
543+
Ok(s) => {
544+
err.span_suggestion(
545+
self.cast_span,
546+
"try casting to a reference instead",
547+
format!("&{mtstr}{s}"),
548+
Applicability::MachineApplicable,
549+
);
550+
}
551+
Err(_) => {
552+
let msg = format!("did you mean `&{mtstr}{tstr}`?");
553+
err.span_help(self.cast_span, msg);
556554
}
557-
} else {
558-
let msg =
559-
format!("consider using an implicit coercion to `&{mtstr}{tstr}` instead");
560-
err.span_help(self.span, msg);
561555
}
562556
}
563557
ty::Adt(def, ..) if def.is_box() => {

tests/ui/cast/cast-to-slice.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
"example".as_bytes() as [char];
3+
//~^ ERROR cast to unsized type
4+
5+
let arr: &[u8] = &[0, 2, 3];
6+
arr as [char];
7+
//~^ ERROR cast to unsized type
8+
}

tests/ui/cast/cast-to-slice.stderr

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0620]: cast to unsized type: `&[u8]` as `[char]`
2+
--> $DIR/cast-to-slice.rs:2:5
3+
|
4+
LL | "example".as_bytes() as [char];
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^------
6+
| |
7+
| help: try casting to a reference instead: `&[char]`
8+
9+
error[E0620]: cast to unsized type: `&[u8]` as `[char]`
10+
--> $DIR/cast-to-slice.rs:6:5
11+
|
12+
LL | arr as [char];
13+
| ^^^^^^^------
14+
| |
15+
| help: try casting to a reference instead: `&[char]`
16+
17+
error: aborting due to 2 previous errors
18+
19+
For more information about this error, try `rustc --explain E0620`.

tests/ui/error-codes/E0620.stderr

+3-7
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@ error[E0620]: cast to unsized type: `&[usize; 2]` as `[usize]`
22
--> $DIR/E0620.rs:2:16
33
|
44
LL | let _foo = &[1_usize, 2] as [usize];
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^
6-
|
7-
help: consider using an implicit coercion to `&[usize]` instead
8-
--> $DIR/E0620.rs:2:16
9-
|
10-
LL | let _foo = &[1_usize, 2] as [usize];
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^-------
6+
| |
7+
| help: try casting to a reference instead: `&[usize]`
128

139
error: aborting due to 1 previous error
1410

tests/ui/issues/issue-17441.stderr

+3-7
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@ error[E0620]: cast to unsized type: `&[usize; 2]` as `[usize]`
22
--> $DIR/issue-17441.rs:2:16
33
|
44
LL | let _foo = &[1_usize, 2] as [usize];
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^
6-
|
7-
help: consider using an implicit coercion to `&[usize]` instead
8-
--> $DIR/issue-17441.rs:2:16
9-
|
10-
LL | let _foo = &[1_usize, 2] as [usize];
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^-------
6+
| |
7+
| help: try casting to a reference instead: `&[usize]`
128

139
error[E0620]: cast to unsized type: `Box<usize>` as `dyn Debug`
1410
--> $DIR/issue-17441.rs:5:16

0 commit comments

Comments
 (0)