Fix invalid slice coercion suggestion reported in turbofish
This commit is contained in:
parent
04411507be
commit
87e8feaf50
@ -3034,6 +3034,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
|
|||||||
|
|
||||||
self.maybe_suggest_convert_to_slice(
|
self.maybe_suggest_convert_to_slice(
|
||||||
err,
|
err,
|
||||||
|
obligation,
|
||||||
trait_ref,
|
trait_ref,
|
||||||
impl_candidates.as_slice(),
|
impl_candidates.as_slice(),
|
||||||
span,
|
span,
|
||||||
|
@ -401,6 +401,7 @@ pub trait TypeErrCtxtExt<'tcx> {
|
|||||||
fn maybe_suggest_convert_to_slice(
|
fn maybe_suggest_convert_to_slice(
|
||||||
&self,
|
&self,
|
||||||
err: &mut Diagnostic,
|
err: &mut Diagnostic,
|
||||||
|
obligation: &PredicateObligation<'tcx>,
|
||||||
trait_ref: ty::PolyTraitRef<'tcx>,
|
trait_ref: ty::PolyTraitRef<'tcx>,
|
||||||
candidate_impls: &[ImplCandidate<'tcx>],
|
candidate_impls: &[ImplCandidate<'tcx>],
|
||||||
span: Span,
|
span: Span,
|
||||||
@ -3957,10 +3958,17 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
|
|||||||
fn maybe_suggest_convert_to_slice(
|
fn maybe_suggest_convert_to_slice(
|
||||||
&self,
|
&self,
|
||||||
err: &mut Diagnostic,
|
err: &mut Diagnostic,
|
||||||
|
obligation: &PredicateObligation<'tcx>,
|
||||||
trait_ref: ty::PolyTraitRef<'tcx>,
|
trait_ref: ty::PolyTraitRef<'tcx>,
|
||||||
candidate_impls: &[ImplCandidate<'tcx>],
|
candidate_impls: &[ImplCandidate<'tcx>],
|
||||||
span: Span,
|
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:
|
// Three cases where we can make a suggestion:
|
||||||
// 1. `[T; _]` (array of T)
|
// 1. `[T; _]` (array of T)
|
||||||
// 2. `&[T; _]` (reference to array of T)
|
// 2. `&[T; _]` (reference to array of T)
|
||||||
|
13
tests/ui/dst/issue-90528-unsizing-not-suggestion-110063.rs
Normal file
13
tests/ui/dst/issue-90528-unsizing-not-suggestion-110063.rs
Normal file
@ -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
|
||||||
|
}
|
@ -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`.
|
Loading…
x
Reference in New Issue
Block a user