From e6e195e27e6d5d12a135e4a40472c253bd63a77f Mon Sep 17 00:00:00 2001 From: Veera Date: Wed, 20 Dec 2023 20:35:53 -0500 Subject: [PATCH 1/3] fix: diagnostic for casting reference to slice fixes: #118790 --- compiler/rustc_hir_typeck/src/cast.rs | 30 +++++++++++---------------- tests/ui/cast/cast-to-slice.rs | 8 +++++++ tests/ui/cast/cast-to-slice.stderr | 19 +++++++++++++++++ 3 files changed, 39 insertions(+), 18 deletions(-) create mode 100644 tests/ui/cast/cast-to-slice.rs create mode 100644 tests/ui/cast/cast-to-slice.stderr diff --git a/compiler/rustc_hir_typeck/src/cast.rs b/compiler/rustc_hir_typeck/src/cast.rs index 0de0365364c6a..a6c22a536d739 100644 --- a/compiler/rustc_hir_typeck/src/cast.rs +++ b/compiler/rustc_hir_typeck/src/cast.rs @@ -576,25 +576,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 0000000000000..a6c784a3d4777 --- /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 0000000000000..340c9c48859bf --- /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`. From 286f2d819eec03525992b8e4aeb201c3fb987a88 Mon Sep 17 00:00:00 2001 From: Veera Date: Wed, 20 Dec 2023 22:53:56 -0500 Subject: [PATCH 2/3] Update existing tests --- tests/ui/cast/cast-to-slice.stderr | 2 +- tests/ui/error-codes/E0620.stderr | 10 +++------- tests/ui/issues/issue-17441.stderr | 10 +++------- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/tests/ui/cast/cast-to-slice.stderr b/tests/ui/cast/cast-to-slice.stderr index 340c9c48859bf..8f862c0001401 100644 --- a/tests/ui/cast/cast-to-slice.stderr +++ b/tests/ui/cast/cast-to-slice.stderr @@ -1,7 +1,7 @@ error[E0620]: cast to unsized type: `&[u8]` as `[char]` --> $DIR/cast-to-slice.rs:2:5 | -LL | "example".as_bytes() as [char]; +LL | "example".as_bytes() as [char]; | ^^^^^^^^^^^^^^^^^^^^^^^^------ | | | help: try casting to a reference instead: `&[char]` diff --git a/tests/ui/error-codes/E0620.stderr b/tests/ui/error-codes/E0620.stderr index 5bc8903624cef..644ba813c9608 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 4dbe50178cf3b..29e50b91c7c6f 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` as `dyn Debug` --> $DIR/issue-17441.rs:5:16 From 33867834bc98de003683d0b7632bbb004c2aacff Mon Sep 17 00:00:00 2001 From: r01and Date: Wed, 27 Dec 2023 07:33:43 +0000 Subject: [PATCH 3/3] Remove dead codes --- compiler/rustc_infer/src/infer/relate/combine.rs | 8 ++------ compiler/rustc_infer/src/infer/relate/generalize.rs | 11 ----------- 2 files changed, 2 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_infer/src/infer/relate/combine.rs b/compiler/rustc_infer/src/infer/relate/combine.rs index 6d8384e7a4791..8b31a1118cb75 100644 --- a/compiler/rustc_infer/src/infer/relate/combine.rs +++ b/compiler/rustc_infer/src/infer/relate/combine.rs @@ -335,7 +335,7 @@ impl<'tcx> InferCtxt<'tcx> { // constants and generic expressions are not yet handled correctly. let Generalization { value_may_be_infer: value, needs_wf: _ } = generalize::generalize( self, - &mut CombineDelegate { infcx: self, span, param_env }, + &mut CombineDelegate { infcx: self, span }, ct, target_vid, ty::Variance::Invariant, @@ -454,11 +454,7 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> { // adding constraints like `'x: '?2` and `?1 <: ?3`.) let Generalization { value_may_be_infer: b_ty, needs_wf } = generalize::generalize( self.infcx, - &mut CombineDelegate { - infcx: self.infcx, - param_env: self.param_env, - span: self.trace.span(), - }, + &mut CombineDelegate { infcx: self.infcx, span: self.trace.span() }, a_ty, b_vid, ambient_variance, diff --git a/compiler/rustc_infer/src/infer/relate/generalize.rs b/compiler/rustc_infer/src/infer/relate/generalize.rs index 665af7381dc75..4d8c691ea3db6 100644 --- a/compiler/rustc_infer/src/infer/relate/generalize.rs +++ b/compiler/rustc_infer/src/infer/relate/generalize.rs @@ -55,8 +55,6 @@ pub fn generalize<'tcx, D: GeneralizerDelegate<'tcx>, T: Into> + Rela /// Abstracts the handling of region vars between HIR and MIR/NLL typechecking /// in the generalizer code. pub trait GeneralizerDelegate<'tcx> { - fn param_env(&self) -> ty::ParamEnv<'tcx>; - fn forbid_inference_vars() -> bool; fn span(&self) -> Span; @@ -66,15 +64,10 @@ pub trait GeneralizerDelegate<'tcx> { pub struct CombineDelegate<'cx, 'tcx> { pub infcx: &'cx InferCtxt<'tcx>, - pub param_env: ty::ParamEnv<'tcx>, pub span: Span, } impl<'tcx> GeneralizerDelegate<'tcx> for CombineDelegate<'_, 'tcx> { - fn param_env(&self) -> ty::ParamEnv<'tcx> { - self.param_env - } - fn forbid_inference_vars() -> bool { false } @@ -95,10 +88,6 @@ impl<'tcx, T> GeneralizerDelegate<'tcx> for T where T: TypeRelatingDelegate<'tcx>, { - fn param_env(&self) -> ty::ParamEnv<'tcx> { - >::param_env(self) - } - fn forbid_inference_vars() -> bool { >::forbid_inference_vars() }