From 0bba4e59572219e4845547737e52657df1471611 Mon Sep 17 00:00:00 2001 From: sladynnunes Date: Mon, 29 May 2023 14:20:06 -0700 Subject: [PATCH 1/3] Add a note when suggesting to use Impl Trait Add tests Update compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs Co-authored-by: Lukas Fixed tests --- compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs | 1 + tests/ui/return/return-impl-trait-bad.stderr | 1 + tests/ui/return/return-impl-trait.stderr | 2 ++ 3 files changed, 4 insertions(+) diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index c4add4dbdfb2f..f7ad914cae42e 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -856,6 +856,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { format!("impl {}", all_bounds_str), Applicability::MaybeIncorrect, ); + err.note(format!("the type of `{}` is chosen by the caller", expected_ty_as_param.name)); } pub(in super::super) fn suggest_missing_break_or_return_expr( diff --git a/tests/ui/return/return-impl-trait-bad.stderr b/tests/ui/return/return-impl-trait-bad.stderr index 237b85ee66a10..a6537f9db1c37 100644 --- a/tests/ui/return/return-impl-trait-bad.stderr +++ b/tests/ui/return/return-impl-trait-bad.stderr @@ -53,6 +53,7 @@ LL | "don't suggest this, because the generic param is used in the bound." | = note: expected type parameter `T` found reference `&'static str` + = note: the type of `T` is chosen by the caller error: aborting due to 4 previous errors diff --git a/tests/ui/return/return-impl-trait.stderr b/tests/ui/return/return-impl-trait.stderr index 43d40972fcac0..4c1fc2f05ec2d 100644 --- a/tests/ui/return/return-impl-trait.stderr +++ b/tests/ui/return/return-impl-trait.stderr @@ -12,6 +12,7 @@ LL | () | = note: expected type parameter `T` found unit type `()` + = note: the type of `T` is chosen by the caller error[E0308]: mismatched types --> $DIR/return-impl-trait.rs:23:5 @@ -28,6 +29,7 @@ LL | () | = note: expected type parameter `T` found unit type `()` + = note: the type of `T` is chosen by the caller error: aborting due to 2 previous errors From f86335345522ec51fcf6a08efa8e308f63b43560 Mon Sep 17 00:00:00 2001 From: sladynnunes Date: Mon, 29 May 2023 23:34:26 -0700 Subject: [PATCH 2/3] Updated tests --- compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs | 4 +++- tests/ui/return/return-impl-trait-bad.stderr | 2 +- tests/ui/return/return-impl-trait.stderr | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index f7ad914cae42e..83dcd8a1e1e02 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -856,7 +856,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { format!("impl {}", all_bounds_str), Applicability::MaybeIncorrect, ); - err.note(format!("the type of `{}` is chosen by the caller", expected_ty_as_param.name)); + + let ty::Param(found_ty_as_param) = found.kind() else { return }; + err.note(format!("the type of `{}` is chosen by the caller and may be a type that is different than `{}`", expected_ty_as_param.name, found_ty_as_param.name)); } pub(in super::super) fn suggest_missing_break_or_return_expr( diff --git a/tests/ui/return/return-impl-trait-bad.stderr b/tests/ui/return/return-impl-trait-bad.stderr index a6537f9db1c37..30d7dd4f9541d 100644 --- a/tests/ui/return/return-impl-trait-bad.stderr +++ b/tests/ui/return/return-impl-trait-bad.stderr @@ -53,7 +53,7 @@ LL | "don't suggest this, because the generic param is used in the bound." | = note: expected type parameter `T` found reference `&'static str` - = note: the type of `T` is chosen by the caller + = note: the type of `T` is chosen by the caller and may be a type that is different than `T` error: aborting due to 4 previous errors diff --git a/tests/ui/return/return-impl-trait.stderr b/tests/ui/return/return-impl-trait.stderr index 4c1fc2f05ec2d..55e52d323ff24 100644 --- a/tests/ui/return/return-impl-trait.stderr +++ b/tests/ui/return/return-impl-trait.stderr @@ -12,7 +12,7 @@ LL | () | = note: expected type parameter `T` found unit type `()` - = note: the type of `T` is chosen by the caller + = note: the type of `T` is chosen by the caller and may be a type that is different than `T` error[E0308]: mismatched types --> $DIR/return-impl-trait.rs:23:5 @@ -29,7 +29,7 @@ LL | () | = note: expected type parameter `T` found unit type `()` - = note: the type of `T` is chosen by the caller + = note: the type of `T` is chosen by the caller and may be a type that is different than `T` error: aborting due to 2 previous errors From c4d04e7fb51be6fe735f8ec87c1ec438070ef6c0 Mon Sep 17 00:00:00 2001 From: sladynnunes Date: Tue, 30 May 2023 11:24:54 -0700 Subject: [PATCH 3/3] Fix typo --- compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 83dcd8a1e1e02..aa1294d2c3acb 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -858,7 +858,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ); let ty::Param(found_ty_as_param) = found.kind() else { return }; - err.note(format!("the type of `{}` is chosen by the caller and may be a type that is different than `{}`", expected_ty_as_param.name, found_ty_as_param.name)); + err.note(format!("the type of `{}` is chosen by the caller and may be a type that is different from `{}`", expected_ty_as_param.name, found_ty_as_param.name)); } pub(in super::super) fn suggest_missing_break_or_return_expr(