Skip to content

Commit

Permalink
Auto merge of rust-lang#119343 - matthiaskrgr:rollup-vkt8lst, r=matth…
Browse files Browse the repository at this point in the history
…iaskrgr

Rollup of 2 pull requests

Successful merges:

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

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Dec 27, 2023
2 parents 9da4432 + f05e16a commit df5d535
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 49 deletions.
30 changes: 12 additions & 18 deletions compiler/rustc_hir_typeck/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() => {
Expand Down
8 changes: 2 additions & 6 deletions compiler/rustc_infer/src/infer/relate/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 0 additions & 11 deletions compiler/rustc_infer/src/infer/relate/generalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
/// in the generalizer code.
pub trait GeneralizerDelegate<'tcx> {
fn param_env(&self) -> ty::ParamEnv<'tcx>;

fn forbid_inference_vars() -> bool;

fn span(&self) -> Span;
Expand All @@ -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
}
Expand All @@ -95,10 +88,6 @@ impl<'tcx, T> GeneralizerDelegate<'tcx> for T
where
T: TypeRelatingDelegate<'tcx>,
{
fn param_env(&self) -> ty::ParamEnv<'tcx> {
<Self as TypeRelatingDelegate<'tcx>>::param_env(self)
}

fn forbid_inference_vars() -> bool {
<Self as TypeRelatingDelegate<'tcx>>::forbid_inference_vars()
}
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/cast/cast-to-slice.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 19 additions & 0 deletions tests/ui/cast/cast-to-slice.stderr
Original file line number Diff line number Diff line change
@@ -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`.
10 changes: 3 additions & 7 deletions tests/ui/error-codes/E0620.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 3 additions & 7 deletions tests/ui/issues/issue-17441.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit df5d535

Please sign in to comment.