diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2023-12-27 09:11:35 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-27 09:11:35 +0100 |
| commit | d6382c785fbf0cdc0722decc2fb405e4713acb28 (patch) | |
| tree | 74b02712a948f08949cbc50813595865110b0ed4 | |
| parent | 625c2c401fae15dc3484cb25313ddff39b7e016c (diff) | |
| parent | 286f2d819eec03525992b8e4aeb201c3fb987a88 (diff) | |
| download | rust-d6382c785fbf0cdc0722decc2fb405e4713acb28.tar.gz rust-d6382c785fbf0cdc0722decc2fb405e4713acb28.zip | |
Rollup merge of #119175 - veera-sivarajan:fix-cast-to-slice, r=WaffleLapkin
fix: diagnostic for casting reference to slice fixes: #118790 Removes `if self.cast_ty.is_trait()` to produce the same diagnostic for cast to slice and trait.
| -rw-r--r-- | compiler/rustc_hir_typeck/src/cast.rs | 30 | ||||
| -rw-r--r-- | tests/ui/cast/cast-to-slice.rs | 8 | ||||
| -rw-r--r-- | tests/ui/cast/cast-to-slice.stderr | 19 | ||||
| -rw-r--r-- | tests/ui/error-codes/E0620.stderr | 10 | ||||
| -rw-r--r-- | tests/ui/issues/issue-17441.stderr | 10 |
5 files changed, 45 insertions, 32 deletions
diff --git a/compiler/rustc_hir_typeck/src/cast.rs b/compiler/rustc_hir_typeck/src/cast.rs index 0d21c013d67..63185444479 100644 --- a/compiler/rustc_hir_typeck/src/cast.rs +++ b/compiler/rustc_hir_typeck/src/cast.rs @@ -539,25 +539,19 @@ impl<'a, 'tcx> CastCheck<'tcx> { match self.expr_ty.kind() { ty::Ref(_, _, mt) => { let mtstr = mt.prefix_str(); - if self.cast_ty.is_trait() { - match fcx.tcx.sess.source_map().span_to_snippet(self.cast_span) { - Ok(s) => { - err.span_suggestion( - self.cast_span, - "try casting to a reference instead", - format!("&{mtstr}{s}"), - Applicability::MachineApplicable, - ); - } - Err(_) => { - let msg = format!("did you mean `&{mtstr}{tstr}`?"); - err.span_help(self.cast_span, msg); - } + match fcx.tcx.sess.source_map().span_to_snippet(self.cast_span) { + Ok(s) => { + err.span_suggestion( + self.cast_span, + "try casting to a reference instead", + format!("&{mtstr}{s}"), + Applicability::MachineApplicable, + ); + } + Err(_) => { + let msg = format!("did you mean `&{mtstr}{tstr}`?"); + 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() => { diff --git a/tests/ui/cast/cast-to-slice.rs b/tests/ui/cast/cast-to-slice.rs new file mode 100644 index 00000000000..a6c784a3d47 --- /dev/null +++ b/tests/ui/cast/cast-to-slice.rs @@ -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 +} diff --git a/tests/ui/cast/cast-to-slice.stderr b/tests/ui/cast/cast-to-slice.stderr new file mode 100644 index 00000000000..8f862c00014 --- /dev/null +++ b/tests/ui/cast/cast-to-slice.stderr @@ -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`. diff --git a/tests/ui/error-codes/E0620.stderr b/tests/ui/error-codes/E0620.stderr index 5bc8903624c..644ba813c96 100644 --- a/tests/ui/error-codes/E0620.stderr +++ b/tests/ui/error-codes/E0620.stderr @@ -2,13 +2,9 @@ error[E0620]: cast to unsized type: `&[usize; 2]` as `[usize]` --> $DIR/E0620.rs:2:16 | LL | let _foo = &[1_usize, 2] as [usize]; - | ^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: consider using an implicit coercion to `&[usize]` instead - --> $DIR/E0620.rs:2:16 - | -LL | let _foo = &[1_usize, 2] as [usize]; - | ^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^------- + | | + | help: try casting to a reference instead: `&[usize]` error: aborting due to 1 previous error diff --git a/tests/ui/issues/issue-17441.stderr b/tests/ui/issues/issue-17441.stderr index 4dbe50178cf..29e50b91c7c 100644 --- a/tests/ui/issues/issue-17441.stderr +++ b/tests/ui/issues/issue-17441.stderr @@ -2,13 +2,9 @@ error[E0620]: cast to unsized type: `&[usize; 2]` as `[usize]` --> $DIR/issue-17441.rs:2:16 | LL | let _foo = &[1_usize, 2] as [usize]; - | ^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: consider using an implicit coercion to `&[usize]` instead - --> $DIR/issue-17441.rs:2:16 - | -LL | let _foo = &[1_usize, 2] as [usize]; - | ^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^------- + | | + | help: try casting to a reference instead: `&[usize]` error[E0620]: cast to unsized type: `Box<usize>` as `dyn Debug` --> $DIR/issue-17441.rs:5:16 |
