Skip to content

Commit df5d535

Browse files
committed
Auto merge of rust-lang#119343 - matthiaskrgr:rollup-vkt8lst, r=matthiaskrgr
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
2 parents 9da4432 + f05e16a commit df5d535

File tree

7 files changed

+47
-49
lines changed

7 files changed

+47
-49
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() => {

compiler/rustc_infer/src/infer/relate/combine.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ impl<'tcx> InferCtxt<'tcx> {
335335
// constants and generic expressions are not yet handled correctly.
336336
let Generalization { value_may_be_infer: value, needs_wf: _ } = generalize::generalize(
337337
self,
338-
&mut CombineDelegate { infcx: self, span, param_env },
338+
&mut CombineDelegate { infcx: self, span },
339339
ct,
340340
target_vid,
341341
ty::Variance::Invariant,
@@ -454,11 +454,7 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
454454
// adding constraints like `'x: '?2` and `?1 <: ?3`.)
455455
let Generalization { value_may_be_infer: b_ty, needs_wf } = generalize::generalize(
456456
self.infcx,
457-
&mut CombineDelegate {
458-
infcx: self.infcx,
459-
param_env: self.param_env,
460-
span: self.trace.span(),
461-
},
457+
&mut CombineDelegate { infcx: self.infcx, span: self.trace.span() },
462458
a_ty,
463459
b_vid,
464460
ambient_variance,

compiler/rustc_infer/src/infer/relate/generalize.rs

-11
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ pub fn generalize<'tcx, D: GeneralizerDelegate<'tcx>, T: Into<Term<'tcx>> + Rela
5555
/// Abstracts the handling of region vars between HIR and MIR/NLL typechecking
5656
/// in the generalizer code.
5757
pub trait GeneralizerDelegate<'tcx> {
58-
fn param_env(&self) -> ty::ParamEnv<'tcx>;
59-
6058
fn forbid_inference_vars() -> bool;
6159

6260
fn span(&self) -> Span;
@@ -66,15 +64,10 @@ pub trait GeneralizerDelegate<'tcx> {
6664

6765
pub struct CombineDelegate<'cx, 'tcx> {
6866
pub infcx: &'cx InferCtxt<'tcx>,
69-
pub param_env: ty::ParamEnv<'tcx>,
7067
pub span: Span,
7168
}
7269

7370
impl<'tcx> GeneralizerDelegate<'tcx> for CombineDelegate<'_, 'tcx> {
74-
fn param_env(&self) -> ty::ParamEnv<'tcx> {
75-
self.param_env
76-
}
77-
7871
fn forbid_inference_vars() -> bool {
7972
false
8073
}
@@ -95,10 +88,6 @@ impl<'tcx, T> GeneralizerDelegate<'tcx> for T
9588
where
9689
T: TypeRelatingDelegate<'tcx>,
9790
{
98-
fn param_env(&self) -> ty::ParamEnv<'tcx> {
99-
<Self as TypeRelatingDelegate<'tcx>>::param_env(self)
100-
}
101-
10291
fn forbid_inference_vars() -> bool {
10392
<Self as TypeRelatingDelegate<'tcx>>::forbid_inference_vars()
10493
}

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)