1
Fork 0

Auto merge of #119343 - matthiaskrgr:rollup-vkt8lst, r=matthiaskrgr

Rollup of 2 pull requests

Successful merges:

 - #119175 (fix: diagnostic for casting reference to slice)
 - #119337 (Remove dead codes)

r? `@ghost`
`@rustbot` modify labels: rollup
This commit is contained in:
bors 2023-12-27 09:46:51 +00:00
commit df5d53585a
7 changed files with 47 additions and 49 deletions

View file

@ -539,25 +539,19 @@ impl<'a, 'tcx> CastCheck<'tcx> {
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() => {

View file

@ -335,7 +335,7 @@ impl<'tcx> InferCtxt<'tcx> {
// constants and generic expressions are not yet handled correctly. // constants and generic expressions are not yet handled correctly.
let Generalization { value_may_be_infer: value, needs_wf: _ } = generalize::generalize( let Generalization { value_may_be_infer: value, needs_wf: _ } = generalize::generalize(
self, self,
&mut CombineDelegate { infcx: self, span, param_env }, &mut CombineDelegate { infcx: self, span },
ct, ct,
target_vid, target_vid,
ty::Variance::Invariant, ty::Variance::Invariant,
@ -454,11 +454,7 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
// adding constraints like `'x: '?2` and `?1 <: ?3`.) // adding constraints like `'x: '?2` and `?1 <: ?3`.)
let Generalization { value_may_be_infer: b_ty, needs_wf } = generalize::generalize( let Generalization { value_may_be_infer: b_ty, needs_wf } = generalize::generalize(
self.infcx, self.infcx,
&mut CombineDelegate { &mut CombineDelegate { infcx: self.infcx, span: self.trace.span() },
infcx: self.infcx,
param_env: self.param_env,
span: self.trace.span(),
},
a_ty, a_ty,
b_vid, b_vid,
ambient_variance, ambient_variance,

View file

@ -55,8 +55,6 @@ pub fn generalize<'tcx, D: GeneralizerDelegate<'tcx>, T: Into<Term<'tcx>> + Rela
/// Abstracts the handling of region vars between HIR and MIR/NLL typechecking /// Abstracts the handling of region vars between HIR and MIR/NLL typechecking
/// in the generalizer code. /// in the generalizer code.
pub trait GeneralizerDelegate<'tcx> { pub trait GeneralizerDelegate<'tcx> {
fn param_env(&self) -> ty::ParamEnv<'tcx>;
fn forbid_inference_vars() -> bool; fn forbid_inference_vars() -> bool;
fn span(&self) -> Span; fn span(&self) -> Span;
@ -66,15 +64,10 @@ pub trait GeneralizerDelegate<'tcx> {
pub struct CombineDelegate<'cx, 'tcx> { pub struct CombineDelegate<'cx, 'tcx> {
pub infcx: &'cx InferCtxt<'tcx>, pub infcx: &'cx InferCtxt<'tcx>,
pub param_env: ty::ParamEnv<'tcx>,
pub span: Span, pub span: Span,
} }
impl<'tcx> GeneralizerDelegate<'tcx> for CombineDelegate<'_, 'tcx> { impl<'tcx> GeneralizerDelegate<'tcx> for CombineDelegate<'_, 'tcx> {
fn param_env(&self) -> ty::ParamEnv<'tcx> {
self.param_env
}
fn forbid_inference_vars() -> bool { fn forbid_inference_vars() -> bool {
false false
} }
@ -95,10 +88,6 @@ impl<'tcx, T> GeneralizerDelegate<'tcx> for T
where where
T: TypeRelatingDelegate<'tcx>, T: TypeRelatingDelegate<'tcx>,
{ {
fn param_env(&self) -> ty::ParamEnv<'tcx> {
<Self as TypeRelatingDelegate<'tcx>>::param_env(self)
}
fn forbid_inference_vars() -> bool { fn forbid_inference_vars() -> bool {
<Self as TypeRelatingDelegate<'tcx>>::forbid_inference_vars() <Self as TypeRelatingDelegate<'tcx>>::forbid_inference_vars()
} }

View 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
}

View 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`.

View file

@ -2,13 +2,9 @@ error[E0620]: cast to unsized type: `&[usize; 2]` as `[usize]`
--> $DIR/E0620.rs:2:16 --> $DIR/E0620.rs:2:16
| |
LL | let _foo = &[1_usize, 2] as [usize]; LL | let _foo = &[1_usize, 2] as [usize];
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^-------
| | |
help: consider using an implicit coercion to `&[usize]` instead | help: try casting to a reference instead: `&[usize]`
--> $DIR/E0620.rs:2:16
|
LL | let _foo = &[1_usize, 2] as [usize];
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error error: aborting due to 1 previous error

View file

@ -2,13 +2,9 @@ error[E0620]: cast to unsized type: `&[usize; 2]` as `[usize]`
--> $DIR/issue-17441.rs:2:16 --> $DIR/issue-17441.rs:2:16
| |
LL | let _foo = &[1_usize, 2] as [usize]; LL | let _foo = &[1_usize, 2] as [usize];
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^-------
| | |
help: consider using an implicit coercion to `&[usize]` instead | help: try casting to a reference instead: `&[usize]`
--> $DIR/issue-17441.rs:2:16
|
LL | let _foo = &[1_usize, 2] as [usize];
| ^^^^^^^^^^^^^^^^^^^^^^^^
error[E0620]: cast to unsized type: `Box<usize>` as `dyn Debug` error[E0620]: cast to unsized type: `Box<usize>` as `dyn Debug`
--> $DIR/issue-17441.rs:5:16 --> $DIR/issue-17441.rs:5:16