Skip to content

Commit 87e8fea

Browse files
committed
Fix invalid slice coercion suggestion reported in turbofish
1 parent 0441150 commit 87e8fea

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -3034,6 +3034,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
30343034

30353035
self.maybe_suggest_convert_to_slice(
30363036
err,
3037+
obligation,
30373038
trait_ref,
30383039
impl_candidates.as_slice(),
30393040
span,

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

+8
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ pub trait TypeErrCtxtExt<'tcx> {
401401
fn maybe_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,
@@ -3957,10 +3958,17 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
39573958
fn maybe_suggest_convert_to_slice(
39583959
&self,
39593960
err: &mut Diagnostic,
3961+
obligation: &PredicateObligation<'tcx>,
39603962
trait_ref: ty::PolyTraitRef<'tcx>,
39613963
candidate_impls: &[ImplCandidate<'tcx>],
39623964
span: Span,
39633965
) {
3966+
// We can only suggest the slice coersion for function arguments since the suggestion
3967+
// would make no sense in turbofish or call
3968+
let ObligationCauseCode::FunctionArgumentObligation { .. } = obligation.cause.code() else {
3969+
return;
3970+
};
3971+
39643972
// Three cases where we can make a suggestion:
39653973
// 1. `[T; _]` (array of T)
39663974
// 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)