Skip to content
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

More information for fully-qualified suggestion when there are multiple impls #128527

Merged
merged 1 commit into from
Aug 7, 2024
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 @@ -388,39 +388,67 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
trait_impls.non_blanket_impls().values().flatten().count();
// If there is only one implementation of the trait, suggest using it.
// Otherwise, use a placeholder comment for the implementation.
let (message, self_type) = if non_blanket_impl_count == 1 {
let (message, self_types) = if non_blanket_impl_count == 1 {
(
"use the fully-qualified path to the only available \
implementation",
format!(
vec![format!(
"{}",
self.tcx.type_of(impl_def_id).instantiate_identity()
),
)],
)
} else if non_blanket_impl_count < 20 {
(
"use a fully-qualified path to one of the available \
implementations",
trait_impls
.non_blanket_impls()
.values()
.flatten()
.map(|id| {
format!(
"{}",
self.tcx.type_of(id).instantiate_identity()
)
})
.collect::<Vec<String>>(),
)
} else {
(
"use a fully-qualified path to a specific available \
implementation",
"/* self type */".to_string(),
vec!["/* self type */".to_string()],
)
};
let mut suggestions =
vec![(path.span.shrink_to_lo(), format!("<{self_type} as "))];
if let Some(generic_arg) = trait_path_segment.args {
let between_span =
trait_path_segment.ident.span.between(generic_arg.span_ext);
// get rid of :: between Trait and <type>
// must be '::' between them, otherwise the parser won't accept the code
suggestions.push((between_span, "".to_string()));
suggestions
.push((generic_arg.span_ext.shrink_to_hi(), ">".to_string()));
} else {
suggestions.push((
trait_path_segment.ident.span.shrink_to_hi(),
">".to_string(),
));
}
err.multipart_suggestion(
let suggestions: Vec<_> = self_types
.into_iter()
.map(|self_type| {
let mut suggestions = vec![(
path.span.shrink_to_lo(),
format!("<{self_type} as "),
)];
if let Some(generic_arg) = trait_path_segment.args {
let between_span = trait_path_segment
.ident
.span
.between(generic_arg.span_ext);
// get rid of :: between Trait and <type>
// must be '::' between them, otherwise the parser won't accept the code
suggestions.push((between_span, "".to_string()));
suggestions.push((
generic_arg.span_ext.shrink_to_hi(),
">".to_string(),
));
} else {
suggestions.push((
trait_path_segment.ident.span.shrink_to_hi(),
">".to_string(),
));
}
suggestions
})
.collect();
err.multipart_suggestions(
message,
suggestions,
Applicability::MaybeIncorrect,
Expand Down
8 changes: 5 additions & 3 deletions tests/ui/error-codes/E0283.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ LL | fn create() -> u32;
LL | let cont: u32 = Coroutine::create();
| ^^^^^^^^^^^^^^^^^^^ cannot call associated function of trait
|
help: use a fully-qualified path to a specific available implementation
help: use a fully-qualified path to one of the available implementations
|
LL | let cont: u32 = </* self type */ as Coroutine>::create();
| +++++++++++++++++++ +
LL | let cont: u32 = <Impl as Coroutine>::create();
| ++++++++ +
LL | let cont: u32 = <AnotherImpl as Coroutine>::create();
| +++++++++++++++ +

error[E0283]: type annotations needed
--> $DIR/E0283.rs:35:24
Expand Down
8 changes: 5 additions & 3 deletions tests/ui/error-codes/E0790.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ LL | fn my_fn();
LL | MyTrait2::my_fn();
| ^^^^^^^^^^^^^^^^^ cannot call associated function of trait
|
help: use a fully-qualified path to a specific available implementation
help: use a fully-qualified path to one of the available implementations
|
LL | </* self type */ as MyTrait2>::my_fn();
| +++++++++++++++++++ +
LL | <Impl1 as MyTrait2>::my_fn();
| +++++++++ +
LL | <Impl2 as MyTrait2>::my_fn();
| +++++++++ +

error: aborting due to 5 previous errors

Expand Down
Loading