Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3032,8 +3032,9 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
self.report_similar_impl_candidates_for_root_obligation(&obligation, *trait_predicate, body_def_id, err);
}

self.maybe_suggest_convert_to_slice(
self.suggest_convert_to_slice(
err,
obligation,
trait_ref,
impl_candidates.as_slice(),
span,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,10 @@ pub trait TypeErrCtxtExt<'tcx> {
param_env: ty::ParamEnv<'tcx>,
) -> Vec<Option<(Span, (DefId, Ty<'tcx>))>>;

fn maybe_suggest_convert_to_slice(
fn suggest_convert_to_slice(
&self,
err: &mut Diagnostic,
obligation: &PredicateObligation<'tcx>,
trait_ref: ty::PolyTraitRef<'tcx>,
candidate_impls: &[ImplCandidate<'tcx>],
span: Span,
Expand Down Expand Up @@ -3954,13 +3955,20 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
/// If the type that failed selection is an array or a reference to an array,
/// but the trait is implemented for slices, suggest that the user converts
/// the array into a slice.
fn maybe_suggest_convert_to_slice(
fn suggest_convert_to_slice(
&self,
err: &mut Diagnostic,
obligation: &PredicateObligation<'tcx>,
trait_ref: ty::PolyTraitRef<'tcx>,
candidate_impls: &[ImplCandidate<'tcx>],
span: Span,
) {
// We can only suggest the slice coersion for function arguments since the suggestion
// would make no sense in turbofish or call
let ObligationCauseCode::FunctionArgumentObligation { .. } = obligation.cause.code() else {
return;
};

// Three cases where we can make a suggestion:
// 1. `[T; _]` (array of T)
// 2. `&[T; _]` (reference to array of T)
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/dst/issue-90528-unsizing-not-suggestion-110063.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
trait Test {}
impl Test for &[u8] {}

fn needs_test<T: Test>() -> T {
panic!()
}

fn main() {
needs_test::<[u8; 1]>();
//~^ ERROR the trait bound
let x: [u8; 1] = needs_test();
//~^ ERROR the trait bound
}
29 changes: 29 additions & 0 deletions tests/ui/dst/issue-90528-unsizing-not-suggestion-110063.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
error[E0277]: the trait bound `[u8; 1]: Test` is not satisfied
--> $DIR/issue-90528-unsizing-not-suggestion-110063.rs:9:18
|
LL | needs_test::<[u8; 1]>();
| ^^^^^^^ the trait `Test` is not implemented for `[u8; 1]`
|
= help: the trait `Test` is implemented for `&[u8]`
note: required by a bound in `needs_test`
--> $DIR/issue-90528-unsizing-not-suggestion-110063.rs:4:18
|
LL | fn needs_test<T: Test>() -> T {
| ^^^^ required by this bound in `needs_test`

error[E0277]: the trait bound `[u8; 1]: Test` is not satisfied
--> $DIR/issue-90528-unsizing-not-suggestion-110063.rs:11:22
|
LL | let x: [u8; 1] = needs_test();
| ^^^^^^^^^^ the trait `Test` is not implemented for `[u8; 1]`
|
= help: the trait `Test` is implemented for `&[u8]`
note: required by a bound in `needs_test`
--> $DIR/issue-90528-unsizing-not-suggestion-110063.rs:4:18
|
LL | fn needs_test<T: Test>() -> T {
| ^^^^ required by this bound in `needs_test`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.