-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Suggest trait bounds for used associated type on type param #116257
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1061,6 +1061,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { | |
) | ||
}, | ||
param_name, | ||
Some(ty_param_def_id), | ||
assoc_name, | ||
span, | ||
None, | ||
|
@@ -1074,6 +1075,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { | |
&self, | ||
all_candidates: impl Fn() -> I, | ||
ty_param_name: impl Display, | ||
ty_param_def_id: Option<LocalDefId>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and use that new type here too? |
||
assoc_name: Ident, | ||
span: Span, | ||
is_equality: Option<ty::Term<'tcx>>, | ||
|
@@ -1095,6 +1097,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { | |
let reported = self.complain_about_assoc_type_not_found( | ||
all_candidates, | ||
&ty_param_name.to_string(), | ||
ty_param_def_id, | ||
assoc_name, | ||
span, | ||
); | ||
|
@@ -1142,39 +1145,34 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { | |
err.span_label( | ||
bound_span, | ||
format!( | ||
"ambiguous `{}` from `{}`", | ||
assoc_name, | ||
"ambiguous `{assoc_name}` from `{}`", | ||
bound.print_only_trait_path(), | ||
), | ||
); | ||
if let Some(constraint) = &is_equality { | ||
where_bounds.push(format!( | ||
" T: {trait}::{assoc} = {constraint}", | ||
" T: {trait}::{assoc_name} = {constraint}", | ||
trait=bound.print_only_trait_path(), | ||
assoc=assoc_name, | ||
constraint=constraint, | ||
)); | ||
} else { | ||
err.span_suggestion_verbose( | ||
span.with_hi(assoc_name.span.lo()), | ||
"use fully qualified syntax to disambiguate", | ||
format!("<{} as {}>::", ty_param_name, bound.print_only_trait_path()), | ||
format!("<{ty_param_name} as {}>::", bound.print_only_trait_path()), | ||
Applicability::MaybeIncorrect, | ||
); | ||
} | ||
} else { | ||
err.note(format!( | ||
"associated type `{}` could derive from `{}`", | ||
ty_param_name, | ||
"associated type `{ty_param_name}` could derive from `{}`", | ||
bound.print_only_trait_path(), | ||
)); | ||
} | ||
} | ||
if !where_bounds.is_empty() { | ||
err.help(format!( | ||
"consider introducing a new type parameter `T` and adding `where` constraints:\ | ||
\n where\n T: {},\n{}", | ||
ty_param_name, | ||
\n where\n T: {ty_param_name},\n{}", | ||
where_bounds.join(",\n"), | ||
)); | ||
} | ||
|
@@ -1396,6 +1394,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { | |
) | ||
}, | ||
kw::SelfUpper, | ||
None, | ||
assoc_ident, | ||
span, | ||
None, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// run-rustfix | ||
#![allow(dead_code)] | ||
trait Foo { | ||
type Bar; | ||
} | ||
|
||
fn foo<T: Foo>() | ||
where | ||
T::Bar: std::fmt::Debug, | ||
//~^ ERROR associated type `Baa` not found for `T` | ||
{ | ||
} | ||
|
||
fn bar<T>() | ||
where | ||
T::Bar: std::fmt::Debug, T: Foo | ||
//~^ ERROR associated type `Baa` not found for `T` | ||
{ | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,29 @@ | ||
error[E0220]: associated type `Baa` not found for `T` | ||
--> $DIR/issue-55673.rs:7:8 | ||
--> $DIR/issue-55673.rs:9:8 | ||
| | ||
LL | T::Baa: std::fmt::Debug, | ||
| ^^^ there is a similarly named associated type `Bar` in the trait `Foo` | ||
| | ||
help: change the associated type name to use `Bar` from `Foo` | ||
| | ||
LL | T::Bar: std::fmt::Debug, | ||
| ~~~ | ||
|
||
error[E0220]: associated type `Baa` not found for `T` | ||
--> $DIR/issue-55673.rs:16:8 | ||
| | ||
LL | T::Baa: std::fmt::Debug, | ||
| ^^^ there is a similarly named associated type `Bar` in the trait `Foo` | ||
| | ||
help: consider further restricting type parameter `T` | ||
| | ||
LL | T::Baa: std::fmt::Debug, T: Foo | ||
| ~~~~~~~~ | ||
help: and also change the associated type name | ||
| | ||
LL | T::Bar: std::fmt::Debug, | ||
| ~~~ | ||
|
||
error: aborting due to previous error | ||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0220`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// run-rustfix | ||
#![feature(type_alias_impl_trait)] | ||
#![allow(dead_code)] | ||
|
||
fn main() {} | ||
|
||
trait TraitWithAssoc { | ||
type Assoc; | ||
} | ||
|
||
type Foo<V: TraitWithAssoc> = impl Trait<V::Assoc>; //~ associated type `Assoc` not found for `V` | ||
|
||
trait Trait<U> {} | ||
|
||
impl<W> Trait<W> for () {} | ||
|
||
fn foo_desugared<T: TraitWithAssoc>(_: T) -> Foo<T> { | ||
() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
// run-rustfix | ||
#![feature(type_alias_impl_trait)] | ||
#![allow(dead_code)] | ||
|
||
fn main() {} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can you maybe combine
ty_param_name
andty_param_def_id
into a new type instead of passing them separately?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done