Skip to content

Commit 1778c58

Browse files
authored
Rollup merge of rust-lang#114322 - Urgau:fix-issue-110063, r=compiler-errors
Fix invalid slice coercion suggestion reported in turbofish This PR fixes the invalid slice coercion suggestion reported in turbofish and inferred generics by not emitting them. Fixes rust-lang#110063
2 parents 3d29ce7 + a408294 commit 1778c58

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

Diff for: compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3030,8 +3030,9 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
30303030
self.report_similar_impl_candidates_for_root_obligation(&obligation, *trait_predicate, body_def_id, err);
30313031
}
30323032

3033-
self.maybe_suggest_convert_to_slice(
3033+
self.suggest_convert_to_slice(
30343034
err,
3035+
obligation,
30353036
trait_ref,
30363037
impl_candidates.as_slice(),
30373038
span,

Diff for: compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,10 @@ pub trait TypeErrCtxtExt<'tcx> {
398398
param_env: ty::ParamEnv<'tcx>,
399399
) -> Vec<Option<(Span, (DefId, Ty<'tcx>))>>;
400400

401-
fn maybe_suggest_convert_to_slice(
401+
fn suggest_convert_to_slice(
402402
&self,
403403
err: &mut Diagnostic,
404+
obligation: &PredicateObligation<'tcx>,
404405
trait_ref: ty::PolyTraitRef<'tcx>,
405406
candidate_impls: &[ImplCandidate<'tcx>],
406407
span: Span,
@@ -3944,13 +3945,20 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
39443945
/// If the type that failed selection is an array or a reference to an array,
39453946
/// but the trait is implemented for slices, suggest that the user converts
39463947
/// the array into a slice.
3947-
fn maybe_suggest_convert_to_slice(
3948+
fn suggest_convert_to_slice(
39483949
&self,
39493950
err: &mut Diagnostic,
3951+
obligation: &PredicateObligation<'tcx>,
39503952
trait_ref: ty::PolyTraitRef<'tcx>,
39513953
candidate_impls: &[ImplCandidate<'tcx>],
39523954
span: Span,
39533955
) {
3956+
// We can only suggest the slice coersion for function arguments since the suggestion
3957+
// would make no sense in turbofish or call
3958+
let ObligationCauseCode::FunctionArgumentObligation { .. } = obligation.cause.code() else {
3959+
return;
3960+
};
3961+
39543962
// Three cases where we can make a suggestion:
39553963
// 1. `[T; _]` (array of T)
39563964
// 2. `&[T; _]` (reference to array of T)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
trait Test {}
2+
impl Test for &[u8] {}
3+
4+
fn needs_test<T: Test>() -> T {
5+
panic!()
6+
}
7+
8+
fn main() {
9+
needs_test::<[u8; 1]>();
10+
//~^ ERROR the trait bound
11+
let x: [u8; 1] = needs_test();
12+
//~^ ERROR the trait bound
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error[E0277]: the trait bound `[u8; 1]: Test` is not satisfied
2+
--> $DIR/issue-90528-unsizing-not-suggestion-110063.rs:9:18
3+
|
4+
LL | needs_test::<[u8; 1]>();
5+
| ^^^^^^^ the trait `Test` is not implemented for `[u8; 1]`
6+
|
7+
= help: the trait `Test` is implemented for `&[u8]`
8+
note: required by a bound in `needs_test`
9+
--> $DIR/issue-90528-unsizing-not-suggestion-110063.rs:4:18
10+
|
11+
LL | fn needs_test<T: Test>() -> T {
12+
| ^^^^ required by this bound in `needs_test`
13+
14+
error[E0277]: the trait bound `[u8; 1]: Test` is not satisfied
15+
--> $DIR/issue-90528-unsizing-not-suggestion-110063.rs:11:22
16+
|
17+
LL | let x: [u8; 1] = needs_test();
18+
| ^^^^^^^^^^ the trait `Test` is not implemented for `[u8; 1]`
19+
|
20+
= help: the trait `Test` is implemented for `&[u8]`
21+
note: required by a bound in `needs_test`
22+
--> $DIR/issue-90528-unsizing-not-suggestion-110063.rs:4:18
23+
|
24+
LL | fn needs_test<T: Test>() -> T {
25+
| ^^^^ required by this bound in `needs_test`
26+
27+
error: aborting due to 2 previous errors
28+
29+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)