Skip to content

Add a note when suggesting to use Impl Trait #112088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
3 changes: 3 additions & 0 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
format!("impl {}", all_bounds_str),
Applicability::MaybeIncorrect,
);

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 from `{}`", expected_ty_as_param.name, found_ty_as_param.name));
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@compiler-errors Here found_ty_as_param should return the parameter found right, or am I understanding something wrong

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need found_ty_as_param -- the found ty might be any type, and you can just render that to string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    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.to_string()));

Converting found to string still does not return the type expected.


pub(in super::super) fn suggest_missing_break_or_return_expr(
Expand Down
1 change: 1 addition & 0 deletions tests/ui/return/return-impl-trait-bad.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 and may be a type that is different than `T`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be a type that is different than T

Hmm... typo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed it

Copy link
Member

@compiler-errors compiler-errors May 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I was not clear with what I meant here.

the type of T is chosen by the caller and may be a type that is different than T

This sentence mentions T twice, which does not make sense. The other type in this error is &'static str, which should be mentioned.


error: aborting due to 4 previous errors

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/return/return-impl-trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ LL | ()
|
= note: expected type parameter `T`
found unit type `()`
= 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
Expand All @@ -28,6 +29,7 @@ LL | ()
|
= note: expected type parameter `T`
found unit type `()`
= 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

Expand Down